Skip to content
This repository was archived by the owner on Jul 22, 2024. It is now read-only.

Commit 96148e7

Browse files
authored
Add function to simplify tests and test recent txs (#921)
* add test_tx function and tests * add option to skip nonce checking and add tests
1 parent bc9d4f2 commit 96148e7

File tree

4 files changed

+142
-133
lines changed

4 files changed

+142
-133
lines changed

rpc_state_reader/src/lib.rs

Lines changed: 109 additions & 130 deletions
Original file line numberDiff line numberDiff line change
@@ -296,7 +296,15 @@ impl RpcState {
296296
)
297297
.unwrap();
298298

299-
Transaction::InvokeFunction(tx)
299+
// Note: we skip nonce checking because it can be increased twice in a single block
300+
// and it leads to a buggy behaviour when that's the case because the get_nonce_at method
301+
// returns a possibly higher nonce than the one we have in the transaction.
302+
// Example: Block contains 2 transactions that execute the same entrypoint with nonce=20.
303+
// - First tx has entrypoint with nonce=20
304+
// - Second tx has nonce=21
305+
// If we want to execute the first transaction the nonce check fails
306+
// since get_nonce_at for that block returns 21 and the first tx has 20.
307+
tx.create_for_simulation(false, false, false, false, true)
300308
}
301309

302310
_ => unimplemented!(),
@@ -729,41 +737,33 @@ mod transaction_tests {
729737
DEFAULT_VALIDATE_MAX_N_STEPS,
730738
},
731739
},
740+
execution::TransactionExecutionInfo,
732741
felt::felt_str,
733742
state::cached_state::CachedState,
734743
};
735744
use std::sync::Arc;
736745

737-
/// - Transaction Hash: `0x014640564509873cf9d24a311e1207040c8b60efd38d96caef79855f0b0075d5`
738-
/// - Network: `mainnet`
739-
/// - Type: `Invoke`
740-
/// - Contract: StarkGate `0x049d36570d4e46f48e99674bd3fcc84644ddd6b96f7c741b1562b82f9e004dc7`
741-
/// - Entrypoint: `transfer(recipient, amount)`
742-
/// - Fee discrepancy: test=83714806176032, explorer=67749104314311, diff=15965701861721 (23%)
743-
/// - Link to Explorer: https://starkscan.co/tx/0x014640564509873cf9d24a311e1207040c8b60efd38d96caef79855f0b0075d5
744-
#[test]
745-
fn test_invoke_0x014640564509873cf9d24a311e1207040c8b60efd38d96caef79855f0b0075d5() {
746-
let tx_hash = "014640564509873cf9d24a311e1207040c8b60efd38d96caef79855f0b0075d5";
746+
fn test_tx(
747+
tx_hash: &str,
748+
network: RpcChain,
749+
block_number: u64,
750+
gas_price: u128,
751+
) -> TransactionExecutionInfo {
752+
let tx_hash = tx_hash.strip_prefix("0x").unwrap();
747753

748754
// Instantiate the RPC StateReader and the CachedState
749-
let rpc_state = Arc::new(RpcState::new(
750-
RpcChain::MainNet,
751-
BlockValue::Number(serde_json::to_value(90_006).unwrap()),
752-
));
755+
let block = BlockValue::Number(serde_json::to_value(block_number).unwrap());
756+
let rpc_state = Arc::new(RpcState::new(network, block));
753757
let mut state = CachedState::new(rpc_state.clone(), None, None);
754758

755-
// BlockContext with mainnet data.
756-
// TODO look how to get this value from RPC call.
757-
let gas_price_str = "13563643256";
758-
let gas_price_u128 = gas_price_str.parse::<u128>().unwrap();
759-
760759
let fee_token_address = Address(felt_str!(
761760
"049d36570d4e46f48e99674bd3fcc84644ddd6b96f7c741b1562b82f9e004dc7",
762761
16
763762
));
763+
764764
let network: StarknetChainId = rpc_state.chain.into();
765765
let starknet_os_config =
766-
StarknetOsConfig::new(network.to_felt(), fee_token_address, gas_price_u128);
766+
StarknetOsConfig::new(network.to_felt(), fee_token_address, gas_price);
767767

768768
let block_info = rpc_state.get_block_info(starknet_os_config.clone());
769769

@@ -780,7 +780,26 @@ mod transaction_tests {
780780
);
781781

782782
let tx = rpc_state.get_transaction(tx_hash);
783-
let result = tx.execute(&mut state, &block_context, 0).unwrap();
783+
784+
tx.execute(&mut state, &block_context, 0).unwrap()
785+
}
786+
787+
/// - Transaction Hash: `0x014640564509873cf9d24a311e1207040c8b60efd38d96caef79855f0b0075d5`
788+
/// - Network: `mainnet`
789+
/// - Type: `Invoke`
790+
/// - Contract: StarkGate `0x049d36570d4e46f48e99674bd3fcc84644ddd6b96f7c741b1562b82f9e004dc7`
791+
/// - Entrypoint: `transfer(recipient, amount)`
792+
/// - Fee discrepancy: test=83714806176032, explorer=67749104314311, diff=15965701861721 (23%)
793+
/// - Link to Explorer: https://starkscan.co/tx/0x014640564509873cf9d24a311e1207040c8b60efd38d96caef79855f0b0075d5
794+
#[test]
795+
fn test_invoke_0x014640564509873cf9d24a311e1207040c8b60efd38d96caef79855f0b0075d5() {
796+
let result = test_tx(
797+
"0x014640564509873cf9d24a311e1207040c8b60efd38d96caef79855f0b0075d5",
798+
RpcChain::MainNet,
799+
90_006,
800+
13563643256,
801+
);
802+
784803
dbg!(&result.actual_resources);
785804
dbg!(&result.actual_fee); // test=83714806176032, explorer=67749104314311, diff=15965701861721 (23%)
786805
dbg!(&result.call_info.clone().unwrap().execution_resources); // Ok with explorer
@@ -796,45 +815,13 @@ mod transaction_tests {
796815
/// - Link to Explorer: https://starkscan.co/tx/0x06da92cfbdceac5e5e94a1f40772d6c79d34f011815606742658559ec77b6955
797816
#[test]
798817
fn test_invoke_mainnet_0x06da92cfbdceac5e5e94a1f40772d6c79d34f011815606742658559ec77b6955() {
799-
// Tx Hash without the "0x" prefix.
800-
let tx_hash = "06da92cfbdceac5e5e94a1f40772d6c79d34f011815606742658559ec77b6955";
801-
802-
// Create RPC StateReader and CachedState
803-
let rpc_state = Arc::new(RpcState::new(
818+
let result = test_tx(
819+
"0x06da92cfbdceac5e5e94a1f40772d6c79d34f011815606742658559ec77b6955",
804820
RpcChain::MainNet,
805-
BlockValue::Number(serde_json::to_value(90_002).unwrap()),
806-
));
807-
let mut state = CachedState::new(rpc_state.clone(), None, None);
808-
809-
// BlockContext with mainnet data.
810-
// TODO look how to get this value from RPC call.
811-
let gas_price_str = "13572248835"; // from block 90_002
812-
let gas_price_u128 = gas_price_str.parse::<u128>().unwrap();
813-
814-
let fee_token_address = Address(felt_str!(
815-
"049d36570d4e46f48e99674bd3fcc84644ddd6b96f7c741b1562b82f9e004dc7",
816-
16
817-
));
818-
let network: StarknetChainId = rpc_state.chain.into();
819-
let starknet_os_config =
820-
StarknetOsConfig::new(network.to_felt(), fee_token_address, gas_price_u128);
821-
822-
let block_info = rpc_state.get_block_info(starknet_os_config.clone());
823-
824-
let block_context = BlockContext::new(
825-
starknet_os_config,
826-
DEFAULT_CONTRACT_STORAGE_COMMITMENT_TREE_HEIGHT,
827-
DEFAULT_GLOBAL_STATE_COMMITMENT_TREE_HEIGHT,
828-
DEFAULT_CAIRO_RESOURCE_FEE_WEIGHTS.clone(),
829-
DEFAULT_INVOKE_TX_MAX_N_STEPS,
830-
DEFAULT_VALIDATE_MAX_N_STEPS,
831-
block_info,
832-
Default::default(),
833-
true,
821+
90_002,
822+
13572248835,
834823
);
835824

836-
let tx = rpc_state.get_transaction(tx_hash);
837-
let result = tx.execute(&mut state, &block_context, 0).unwrap();
838825
dbg!(&result.actual_resources);
839826
dbg!(&result.actual_fee); // test=267319013054160, explorer=219298652474858, diff=48020360579302 (22%)
840827
dbg!(&result.call_info.clone().unwrap().execution_resources); // Ok with explorer
@@ -849,47 +836,14 @@ mod transaction_tests {
849836
/// - Fee discrepancy: test=7252831227950, explorer=7207614784695, diff=45216443255 (0.06%)
850837
/// - Link to Explorer: https://testnet.starkscan.co/tx/0x074dab0828ec1b6cfde5188c41d41af1c198192a7d118217f95a802aa923dacf
851838
#[test]
852-
fn test_invoke_mainnet_0x074dab0828ec1b6cfde5188c41d41af1c198192a7d118217f95a802aa923dacf() {
853-
// Tx Hash without the "0x" prefix.
854-
let tx_hash_str = "074dab0828ec1b6cfde5188c41d41af1c198192a7d118217f95a802aa923dacf";
855-
856-
// Instantiate CachedState
857-
let rpc_state = Arc::new(RpcState::new(
839+
fn test_0x074dab0828ec1b6cfde5188c41d41af1c198192a7d118217f95a802aa923dacf() {
840+
let result = test_tx(
841+
"0x074dab0828ec1b6cfde5188c41d41af1c198192a7d118217f95a802aa923dacf",
858842
RpcChain::TestNet,
859-
BlockValue::Number(serde_json::to_value(838683).unwrap()),
860-
));
861-
862-
let mut state = CachedState::new(rpc_state.clone(), None, None);
863-
864-
// BlockContext with mainnet data.
865-
// TODO look how to get this value from RPC call.
866-
let gas_price_str = "2917470325"; // from block 838683
867-
let gas_price_u128 = gas_price_str.parse::<u128>().unwrap();
868-
869-
let fee_token_address = Address(felt_str!(
870-
"049d36570d4e46f48e99674bd3fcc84644ddd6b96f7c741b1562b82f9e004dc7",
871-
16
872-
));
873-
let network: StarknetChainId = rpc_state.chain.into();
874-
let starknet_os_config =
875-
StarknetOsConfig::new(network.to_felt(), fee_token_address, gas_price_u128);
876-
877-
let block_info = rpc_state.get_block_info(starknet_os_config.clone());
878-
879-
let block_context = BlockContext::new(
880-
starknet_os_config,
881-
DEFAULT_CONTRACT_STORAGE_COMMITMENT_TREE_HEIGHT,
882-
DEFAULT_GLOBAL_STATE_COMMITMENT_TREE_HEIGHT,
883-
DEFAULT_CAIRO_RESOURCE_FEE_WEIGHTS.clone(),
884-
DEFAULT_INVOKE_TX_MAX_N_STEPS,
885-
DEFAULT_VALIDATE_MAX_N_STEPS,
886-
block_info,
887-
Default::default(),
888-
true,
843+
838683,
844+
2917470325,
889845
);
890-
let tx = rpc_state.get_transaction(tx_hash_str);
891846

892-
let result = tx.execute(&mut state, &block_context, 0).unwrap();
893847
dbg!(&result.actual_resources);
894848
dbg!(&result.actual_fee); // test=7252831227950, explorer=7207614784695, diff=45216443255 (0.06%)
895849
dbg!(&result.call_info.clone().unwrap().execution_resources); // Ok with explorer
@@ -905,49 +859,74 @@ mod transaction_tests {
905859
/// - Link to Explorer: https://testnet-2.starkscan.co/tx/0x019feb888a2d53ffddb7a1750264640afab8e9c23119e648b5259f1b5e7d51bc
906860
#[test]
907861
fn test_invoke_testnet2_0x019feb888a2d53ffddb7a1750264640afab8e9c23119e648b5259f1b5e7d51bc() {
908-
// Tx Hash without the "0x" prefix.
909-
let tx_hash_str = "019feb888a2d53ffddb7a1750264640afab8e9c23119e648b5259f1b5e7d51bc";
910-
911-
// Instantiate the RPC StateReader and the CachedState
912-
let rpc_state = Arc::new(RpcState::new(
862+
let result = test_tx(
863+
"0x019feb888a2d53ffddb7a1750264640afab8e9c23119e648b5259f1b5e7d51bc",
913864
RpcChain::TestNet2,
914-
BlockValue::Number(serde_json::to_value(123001).unwrap()),
915-
));
916-
917-
// BlockContext with mainnet data.
918-
// TODO look how to get this value from RPC call.
919-
let gas_price_str = "272679647"; // from block 123001
920-
let gas_price_u128 = gas_price_str.parse::<u128>().unwrap();
921-
922-
let fee_token_address = Address(felt_str!(
923-
"49d36570d4e46f48e99674bd3fcc84644ddd6b96f7c741b1562b82f9e004dc7",
924-
16
925-
));
865+
123001,
866+
272679647,
867+
);
926868

927-
let mut state = CachedState::new(rpc_state.clone(), None, None);
869+
dbg!(&result.actual_resources);
870+
dbg!(&result.actual_fee); // test=6361070805216, explorer=47292465953700, diff=5888146145679 (0.13%)
871+
dbg!(&result.call_info.clone().unwrap().execution_resources); // Ok with explorer
872+
dbg!(&result.call_info.unwrap().internal_calls.len()); // Ok with explorer
873+
}
928874

929-
let network: StarknetChainId = rpc_state.chain.into();
930-
let starknet_os_config =
931-
StarknetOsConfig::new(network.to_felt(), fee_token_address, gas_price_u128);
875+
/// - Transaction Hash: 0x02e31c976f649ba05da82e4c6a054a9a41961adda4c3dea26e6b523f4f18b382
876+
/// - Network: testnet
877+
/// - Type: Invoke
878+
/// - Entrypoint: freeMint
879+
/// - Fee discrepancy: test=4940000049400, explorer=6191000061910, diff=25%
880+
/// - Link to explorer: https://testnet.starkscan.co/tx/0x02e31c976f649ba05da82e4c6a054a9a41961adda4c3dea26e6b523f4f18b382
881+
#[test]
882+
fn test_0x02e31c976f649ba05da82e4c6a054a9a41961adda4c3dea26e6b523f4f18b382() {
883+
let result = test_tx(
884+
"0x02e31c976f649ba05da82e4c6a054a9a41961adda4c3dea26e6b523f4f18b382",
885+
RpcChain::TestNet,
886+
846582,
887+
1000000010,
888+
);
932889

933-
let block_info = rpc_state.get_block_info(starknet_os_config.clone());
890+
dbg!(&result.actual_resources);
891+
dbg!(&result.actual_fee); // test=6361070805216, explorer=47292465953700, diff=5888146145679 (0.13%)
892+
dbg!(&result.call_info.clone().unwrap().execution_resources); // Ok with explorer
893+
dbg!(&result.call_info.unwrap().internal_calls.len()); // Ok with explorer
894+
}
934895

935-
let block_context = BlockContext::new(
936-
starknet_os_config,
937-
DEFAULT_CONTRACT_STORAGE_COMMITMENT_TREE_HEIGHT,
938-
DEFAULT_GLOBAL_STATE_COMMITMENT_TREE_HEIGHT,
939-
DEFAULT_CAIRO_RESOURCE_FEE_WEIGHTS.clone(),
940-
DEFAULT_INVOKE_TX_MAX_N_STEPS,
941-
DEFAULT_VALIDATE_MAX_N_STEPS,
942-
block_info,
943-
Default::default(),
944-
true,
896+
/// - Transaction Hash: 0x26a1a5b5f2b3390302ade67c766cc94804fd41c86c5ee37e20c6415dc39358c
897+
/// - Network: mainnet
898+
/// - Type: Invoke
899+
/// - Entrypoint: evolve(game_id)
900+
/// - Fee discrepancy: test=263050867669716, explorer=306031925226186, diff=16%
901+
/// - Link to explorer: https://starkscan.co/tx/0x026a1a5b5f2b3390302ade67c766cc94804fd41c86c5ee37e20c6415dc39358c
902+
#[test]
903+
fn test_0x26a1a5b5f2b3390302ade67c766cc94804fd41c86c5ee37e20c6415dc39358c() {
904+
let result = test_tx(
905+
"0x26a1a5b5f2b3390302ade67c766cc94804fd41c86c5ee37e20c6415dc39358c",
906+
RpcChain::MainNet,
907+
155054,
908+
33977120598,
945909
);
946-
let tx = rpc_state.get_transaction(tx_hash_str);
947-
let result = tx.execute(&mut state, &block_context, 0).unwrap();
910+
948911
dbg!(&result.actual_resources);
949912
dbg!(&result.actual_fee); // test=6361070805216, explorer=47292465953700, diff=5888146145679 (0.13%)
950913
dbg!(&result.call_info.clone().unwrap().execution_resources); // Ok with explorer
951914
dbg!(&result.call_info.unwrap().internal_calls.len()); // Ok with explorer
952915
}
916+
917+
// Fails because there is a problem with get_compiled_class_hash
918+
// #[test]
919+
// fn test_0x00eef6ba6741da8769192fac9d28c6631cf66f9e7c4e880b886ef6a2e550e4e2() {
920+
// let result = test_tx(
921+
// "0x00eef6ba6741da8769192fac9d28c6631cf66f9e7c4e880b886ef6a2e550e4e2",
922+
// RpcChain::MainNet,
923+
// 156105,
924+
// 18348936116,
925+
// );
926+
927+
// dbg!(&result.actual_resources);
928+
// dbg!(&result.actual_fee);
929+
// dbg!(&result.call_info.clone().unwrap().execution_resources);
930+
// dbg!(&result.call_info.unwrap().internal_calls.len());
931+
// }
953932
}

0 commit comments

Comments
 (0)