Skip to content

Commit 3341f9d

Browse files
whichquaHermanObstnotlesh
authored
feat: add support for declare v0 txns (#471)
* feat: add support for declare v0 txns * lint: cargo fmt * lint: clippy tests * fix: ensure the correct txn version * fix: update the toolchain to support latest udeps * doc: add comment on why nonce is default * chore: prevent bump in udeps and toolchain * fix: use nightly for udeps * fix: comment for declare_v0_v1 * fix: returned value for test * deps: lock the udeps version * Try +nightly with install * Specify exactl +nightly version * Use more recent nightly --------- Co-authored-by: Herman Obst Demaestri <[email protected]> Co-authored-by: Stephen Shelton <[email protected]>
1 parent 80bbdef commit 3341f9d

File tree

4 files changed

+88
-25
lines changed

4 files changed

+88
-25
lines changed

.github/workflows/check.yml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ jobs:
6767
- uses: actions/checkout@v3
6868
with:
6969
submodules: "true"
70-
- run: rustup install nightly
70+
- run: rustup install nightly-2024-09-05
7171
- run: rustup show
7272
- uses: Swatinem/rust-cache@v2
7373
with:
@@ -84,5 +84,5 @@ jobs:
8484
run: |
8585
mkdir -p build
8686
cairo-compile cairo-lang/src/starkware/starknet/core/os/os.cairo --output build/os_latest.json --cairo_path cairo-lang/src
87-
- run: cargo install cargo-udeps --locked
88-
- run: cargo +nightly udeps --all-targets
87+
- run: cargo +nightly-2024-09-05 install cargo-udeps@0.1.54 --locked
88+
- run: cargo +nightly-2024-09-05 udeps --all-targets

crates/rpc-replay/src/transactions.rs

Lines changed: 28 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,10 @@ use blockifier::transaction::account_transaction::AccountTransaction;
77
use blockifier::transaction::errors::TransactionExecutionError;
88
use rpc_client::RpcClient;
99
use starknet::core::types::{
10-
BlockId, DeclareTransaction, DeclareTransactionV1, DeclareTransactionV2, DeclareTransactionV3,
11-
DeployAccountTransaction, DeployAccountTransactionV1, DeployAccountTransactionV3, Felt, InvokeTransaction,
12-
InvokeTransactionV1, InvokeTransactionV3, L1HandlerTransaction, ResourceBoundsMapping, Transaction,
13-
TransactionTrace, TransactionTraceWithHash,
10+
BlockId, DeclareTransaction, DeclareTransactionV0, DeclareTransactionV1, DeclareTransactionV2,
11+
DeclareTransactionV3, DeployAccountTransaction, DeployAccountTransactionV1, DeployAccountTransactionV3, Felt,
12+
InvokeTransaction, InvokeTransactionV1, InvokeTransactionV3, L1HandlerTransaction, ResourceBoundsMapping,
13+
Transaction, TransactionTrace, TransactionTraceWithHash,
1414
};
1515
use starknet::providers::{Provider, ProviderError};
1616
use starknet_api::core::{calculate_contract_address, ContractAddress, PatriciaKey};
@@ -148,6 +148,29 @@ async fn create_class_info(
148148
Ok(ClassInfo::new(&blockifier_contract_class, program_length, abi_length)?)
149149
}
150150

151+
async fn declare_v0_to_blockifier(
152+
tx: &DeclareTransactionV0,
153+
client: &RpcClient,
154+
block_number: u64,
155+
) -> Result<blockifier::transaction::transaction_execution::Transaction, ToBlockifierError> {
156+
let tx_hash = TransactionHash(tx.transaction_hash);
157+
let api_tx = starknet_api::transaction::DeclareTransaction::V0(starknet_api::transaction::DeclareTransactionV0V1 {
158+
max_fee: starknet_api::transaction::Fee(felt_to_u128(&tx.max_fee)?),
159+
signature: starknet_api::transaction::TransactionSignature(tx.signature.clone()),
160+
// Declare v0 does not have a nonce
161+
// So we default to 0
162+
nonce: starknet_api::core::Nonce(Default::default()),
163+
class_hash: starknet_api::core::ClassHash(tx.class_hash),
164+
sender_address: starknet_api::core::ContractAddress(PatriciaKey::try_from(tx.sender_address)?),
165+
});
166+
let class_info = create_class_info(tx.class_hash, client, block_number).await?;
167+
let declare = blockifier::transaction::transactions::DeclareTransaction::new(api_tx, tx_hash, class_info)?;
168+
169+
Ok(blockifier::transaction::transaction_execution::Transaction::AccountTransaction(AccountTransaction::Declare(
170+
declare,
171+
)))
172+
}
173+
151174
async fn declare_v1_to_blockifier(
152175
tx: &DeclareTransactionV1,
153176
client: &RpcClient,
@@ -365,7 +388,7 @@ pub async fn starknet_rs_to_blockifier(
365388
InvokeTransaction::V3(tx) => invoke_v3_to_blockifier(tx)?,
366389
},
367390
Transaction::Declare(tx) => match tx {
368-
DeclareTransaction::V0(_) => unimplemented!("starknet_rs_to_blockifier with DeclareTransaction::V0"),
391+
DeclareTransaction::V0(tx) => declare_v0_to_blockifier(tx, client, block_number).await?,
369392
DeclareTransaction::V1(tx) => declare_v1_to_blockifier(tx, client, block_number).await?,
370393
DeclareTransaction::V2(tx) => declare_v2_to_blockifier(tx, client, block_number).await?,
371394
DeclareTransaction::V3(tx) => declare_v3_to_blockifier(tx, client, block_number).await?,

tests/integration/common/transaction_utils.rs

Lines changed: 15 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -389,21 +389,18 @@ pub fn to_internal_tx(tx: &Transaction, chain_id: &ChainId) -> InternalTransacti
389389
}
390390
fn account_tx_to_internal_tx(account_tx: &AccountTransaction, chain_id: &ChainId) -> InternalTransaction {
391391
match account_tx {
392-
Declare(declare_tx) => {
393-
match &declare_tx.tx() {
394-
starknet_api::transaction::DeclareTransaction::V0(_) => {
395-
// explicitly not supported
396-
panic!("Declare V0 is not supported");
397-
}
398-
starknet_api::transaction::DeclareTransaction::V1(tx) => {
399-
to_internal_declare_v1_tx(account_tx, tx, chain_id)
400-
}
401-
starknet_api::transaction::DeclareTransaction::V2(tx) => {
402-
to_internal_declare_v2_tx(account_tx, tx, chain_id)
403-
}
404-
starknet_api::transaction::DeclareTransaction::V3(tx) => to_internal_declare_v3_tx(tx, chain_id),
392+
Declare(declare_tx) => match &declare_tx.tx() {
393+
starknet_api::transaction::DeclareTransaction::V0(tx) => {
394+
to_internal_declare_v0_v1_tx(account_tx, tx, chain_id, Felt252::ZERO)
405395
}
406-
}
396+
starknet_api::transaction::DeclareTransaction::V1(tx) => {
397+
to_internal_declare_v0_v1_tx(account_tx, tx, chain_id, Felt252::ONE)
398+
}
399+
starknet_api::transaction::DeclareTransaction::V2(tx) => {
400+
to_internal_declare_v2_tx(account_tx, tx, chain_id)
401+
}
402+
starknet_api::transaction::DeclareTransaction::V3(tx) => to_internal_declare_v3_tx(tx, chain_id),
403+
},
407404
DeployAccount(deploy_tx) => match deploy_tx.tx() {
408405
starknet_api::transaction::DeployAccountTransaction::V1(tx) => {
409406
to_internal_deploy_v1_tx(account_tx, tx, chain_id)
@@ -449,11 +446,12 @@ fn to_internal_l1_handler_tx(l1_tx: &L1HandlerTransaction, chain_id: &ChainId) -
449446
}
450447
}
451448

452-
/// Convert a DeclareTransactionV1 to a SNOS InternalTransaction
453-
pub fn to_internal_declare_v1_tx(
449+
/// Convert a DeclareTransactionV0V1 to a SNOS InternalTransaction
450+
pub fn to_internal_declare_v0_v1_tx(
454451
account_tx: &AccountTransaction,
455452
tx: &DeclareTransactionV0V1,
456453
chain_id: &ChainId,
454+
version: Felt252,
457455
) -> InternalTransaction {
458456
let hash_value;
459457
let sender_address;
@@ -475,7 +473,7 @@ pub fn to_internal_declare_v1_tx(
475473

476474
InternalTransaction {
477475
hash_value,
478-
version: Some(Felt252::ONE),
476+
version: Some(version),
479477
nonce: Some(nonce),
480478
sender_address: Some(sender_address),
481479
entry_point_type: Some("EXTERNAL".to_string()),

tests/integration/declare_txn_tests.rs

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ use blockifier::execution::contract_class::ClassInfo;
66
use blockifier::test_utils::{NonceManager, BALANCE};
77
use blockifier::transaction::test_utils::{calculate_class_info_for_testing, max_fee};
88
use rstest::{fixture, rstest};
9+
use starknet_api::contract_address;
910
use starknet_api::core::CompiledClassHash;
1011
use starknet_api::transaction::{Fee, Resource, ResourceBounds, ResourceBoundsMapping, TransactionVersion};
1112
use starknet_os::crypto::poseidon::PoseidonHash;
@@ -288,3 +289,44 @@ async fn declare_cairo0_with_tx_info(
288289
.await
289290
.expect("OS run failed");
290291
}
292+
293+
#[rstest]
294+
#[tokio::test(flavor = "multi_thread", worker_threads = 1)]
295+
async fn declare_v0_cairo0_account(block_context: BlockContext) {
296+
use starknet_api::core::{ContractAddress, PatriciaKey};
297+
use starknet_api::{felt, patricia_key};
298+
299+
let initial_state = StarknetStateBuilder::new(&block_context).build().await;
300+
301+
let tx_version = TransactionVersion::ZERO;
302+
303+
let (_, account_with_dummy_validate) = load_cairo0_feature_contract("account_with_dummy_validate");
304+
let class_hash = account_with_dummy_validate.class_hash().unwrap();
305+
306+
let blockifier_class = account_with_dummy_validate.to_blockifier_contract_class().unwrap();
307+
let class_info = calculate_class_info_for_testing(blockifier_class.into());
308+
309+
let sender_address = contract_address!("0x1");
310+
let declare_tx = blockifier::test_utils::declare::declare_tx(
311+
declare_tx_args! {
312+
max_fee: Fee(0),
313+
sender_address,
314+
version: tx_version,
315+
class_hash: class_hash.into(),
316+
},
317+
class_info,
318+
);
319+
320+
let txs = vec![declare_tx].into_iter().map(Into::into).collect();
321+
let _ = execute_txs_and_run_os(
322+
crate::common::DEFAULT_COMPILED_OS,
323+
initial_state.cached_state,
324+
block_context,
325+
txs,
326+
initial_state.cairo0_compiled_classes,
327+
initial_state.cairo1_compiled_classes,
328+
HashMap::default(),
329+
)
330+
.await
331+
.expect("OS run failed");
332+
}

0 commit comments

Comments
 (0)