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

Commit 3c75978

Browse files
committed
add test_tx function and tests
1 parent bc9d4f2 commit 3c75978

File tree

1 file changed

+97
-127
lines changed

1 file changed

+97
-127
lines changed

rpc_state_reader/src/lib.rs

Lines changed: 97 additions & 127 deletions
Original file line numberDiff line numberDiff line change
@@ -729,41 +729,33 @@ mod transaction_tests {
729729
DEFAULT_VALIDATE_MAX_N_STEPS,
730730
},
731731
},
732+
execution::TransactionExecutionInfo,
732733
felt::felt_str,
733734
state::cached_state::CachedState,
734735
};
735736
use std::sync::Arc;
736737

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";
738+
fn test_tx(
739+
tx_hash: &str,
740+
network: RpcChain,
741+
block_number: u64,
742+
gas_price: u128,
743+
) -> TransactionExecutionInfo {
744+
let tx_hash = tx_hash.strip_prefix("0x").unwrap();
747745

748746
// 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-
));
747+
let block = BlockValue::Number(serde_json::to_value(block_number).unwrap());
748+
let rpc_state = Arc::new(RpcState::new(network, block));
753749
let mut state = CachedState::new(rpc_state.clone(), None, None);
754750

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-
760751
let fee_token_address = Address(felt_str!(
761752
"049d36570d4e46f48e99674bd3fcc84644ddd6b96f7c741b1562b82f9e004dc7",
762753
16
763754
));
755+
764756
let network: StarknetChainId = rpc_state.chain.into();
765757
let starknet_os_config =
766-
StarknetOsConfig::new(network.to_felt(), fee_token_address, gas_price_u128);
758+
StarknetOsConfig::new(network.to_felt(), fee_token_address, gas_price);
767759

768760
let block_info = rpc_state.get_block_info(starknet_os_config.clone());
769761

@@ -780,7 +772,26 @@ mod transaction_tests {
780772
);
781773

782774
let tx = rpc_state.get_transaction(tx_hash);
783-
let result = tx.execute(&mut state, &block_context, 0).unwrap();
775+
776+
tx.execute(&mut state, &block_context, 0).unwrap()
777+
}
778+
779+
/// - Transaction Hash: `0x014640564509873cf9d24a311e1207040c8b60efd38d96caef79855f0b0075d5`
780+
/// - Network: `mainnet`
781+
/// - Type: `Invoke`
782+
/// - Contract: StarkGate `0x049d36570d4e46f48e99674bd3fcc84644ddd6b96f7c741b1562b82f9e004dc7`
783+
/// - Entrypoint: `transfer(recipient, amount)`
784+
/// - Fee discrepancy: test=83714806176032, explorer=67749104314311, diff=15965701861721 (23%)
785+
/// - Link to Explorer: https://starkscan.co/tx/0x014640564509873cf9d24a311e1207040c8b60efd38d96caef79855f0b0075d5
786+
#[test]
787+
fn test_invoke_0x014640564509873cf9d24a311e1207040c8b60efd38d96caef79855f0b0075d5() {
788+
let result = test_tx(
789+
"0x014640564509873cf9d24a311e1207040c8b60efd38d96caef79855f0b0075d5",
790+
RpcChain::MainNet,
791+
90_006,
792+
13563643256,
793+
);
794+
784795
dbg!(&result.actual_resources);
785796
dbg!(&result.actual_fee); // test=83714806176032, explorer=67749104314311, diff=15965701861721 (23%)
786797
dbg!(&result.call_info.clone().unwrap().execution_resources); // Ok with explorer
@@ -796,45 +807,13 @@ mod transaction_tests {
796807
/// - Link to Explorer: https://starkscan.co/tx/0x06da92cfbdceac5e5e94a1f40772d6c79d34f011815606742658559ec77b6955
797808
#[test]
798809
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(
810+
let result = test_tx(
811+
"0x06da92cfbdceac5e5e94a1f40772d6c79d34f011815606742658559ec77b6955",
804812
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,
813+
90_002,
814+
13572248835,
834815
);
835816

836-
let tx = rpc_state.get_transaction(tx_hash);
837-
let result = tx.execute(&mut state, &block_context, 0).unwrap();
838817
dbg!(&result.actual_resources);
839818
dbg!(&result.actual_fee); // test=267319013054160, explorer=219298652474858, diff=48020360579302 (22%)
840819
dbg!(&result.call_info.clone().unwrap().execution_resources); // Ok with explorer
@@ -849,47 +828,14 @@ mod transaction_tests {
849828
/// - Fee discrepancy: test=7252831227950, explorer=7207614784695, diff=45216443255 (0.06%)
850829
/// - Link to Explorer: https://testnet.starkscan.co/tx/0x074dab0828ec1b6cfde5188c41d41af1c198192a7d118217f95a802aa923dacf
851830
#[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(
831+
fn test_0x074dab0828ec1b6cfde5188c41d41af1c198192a7d118217f95a802aa923dacf() {
832+
let result = test_tx(
833+
"0x074dab0828ec1b6cfde5188c41d41af1c198192a7d118217f95a802aa923dacf",
858834
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,
835+
838683,
836+
2917470325,
889837
);
890-
let tx = rpc_state.get_transaction(tx_hash_str);
891838

892-
let result = tx.execute(&mut state, &block_context, 0).unwrap();
893839
dbg!(&result.actual_resources);
894840
dbg!(&result.actual_fee); // test=7252831227950, explorer=7207614784695, diff=45216443255 (0.06%)
895841
dbg!(&result.call_info.clone().unwrap().execution_resources); // Ok with explorer
@@ -905,46 +851,70 @@ mod transaction_tests {
905851
/// - Link to Explorer: https://testnet-2.starkscan.co/tx/0x019feb888a2d53ffddb7a1750264640afab8e9c23119e648b5259f1b5e7d51bc
906852
#[test]
907853
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(
854+
let result = test_tx(
855+
"0x019feb888a2d53ffddb7a1750264640afab8e9c23119e648b5259f1b5e7d51bc",
913856
RpcChain::TestNet2,
914-
BlockValue::Number(serde_json::to_value(123001).unwrap()),
915-
));
857+
123001,
858+
272679647,
859+
);
916860

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();
861+
dbg!(&result.actual_resources);
862+
dbg!(&result.actual_fee); // test=6361070805216, explorer=47292465953700, diff=5888146145679 (0.13%)
863+
dbg!(&result.call_info.clone().unwrap().execution_resources); // Ok with explorer
864+
dbg!(&result.call_info.unwrap().internal_calls.len()); // Ok with explorer
865+
}
921866

922-
let fee_token_address = Address(felt_str!(
923-
"49d36570d4e46f48e99674bd3fcc84644ddd6b96f7c741b1562b82f9e004dc7",
924-
16
925-
));
867+
// tx_hash = 0x02e31c976f649ba05da82e4c6a054a9a41961adda4c3dea26e6b523f4f18b382
868+
// testnet
869+
// freeMint entrypoint
870+
// 6164000061640
871+
// 6191000061910
872+
// 0.004% difference
873+
// Link to explorer: https://testnet.starkscan.co/tx/0x02e31c976f649ba05da82e4c6a054a9a41961adda4c3dea26e6b523f4f18b382
874+
#[test]
875+
fn test_0x02e31c976f649ba05da82e4c6a054a9a41961adda4c3dea26e6b523f4f18b382() {
876+
let result = test_tx(
877+
"0x02e31c976f649ba05da82e4c6a054a9a41961adda4c3dea26e6b523f4f18b382",
878+
RpcChain::TestNet,
879+
846582,
880+
1000000010,
881+
);
926882

927-
let mut state = CachedState::new(rpc_state.clone(), None, None);
883+
dbg!(&result.actual_resources);
884+
dbg!(&result.actual_fee); // test=6361070805216, explorer=47292465953700, diff=5888146145679 (0.13%)
885+
dbg!(&result.call_info.clone().unwrap().execution_resources); // Ok with explorer
886+
dbg!(&result.call_info.unwrap().internal_calls.len()); // Ok with explorer
887+
}
928888

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);
889+
/// tx: 0x26a1a5b5f2b3390302ade67c766cc94804fd41c86c5ee37e20c6415dc39358c
890+
/// Network: mainnet
891+
/// Fee: test=263050867669716, explorer=306031925226186, diff=16%
892+
/// Entrypoint evolve(game_id)
893+
/// Link to explorer: https://starkscan.co/tx/0x026a1a5b5f2b3390302ade67c766cc94804fd41c86c5ee37e20c6415dc39358c
894+
#[test]
895+
fn test_0x26a1a5b5f2b3390302ade67c766cc94804fd41c86c5ee37e20c6415dc39358c() {
896+
let result = test_tx(
897+
"0x26a1a5b5f2b3390302ade67c766cc94804fd41c86c5ee37e20c6415dc39358c",
898+
RpcChain::MainNet,
899+
155054,
900+
33977120598,
901+
);
932902

933-
let block_info = rpc_state.get_block_info(starknet_os_config.clone());
903+
dbg!(&result.actual_resources);
904+
dbg!(&result.actual_fee); // test=6361070805216, explorer=47292465953700, diff=5888146145679 (0.13%)
905+
dbg!(&result.call_info.clone().unwrap().execution_resources); // Ok with explorer
906+
dbg!(&result.call_info.unwrap().internal_calls.len()); // Ok with explorer
907+
}
934908

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,
909+
#[test]
910+
fn test_0x00eef6ba6741da8769192fac9d28c6631cf66f9e7c4e880b886ef6a2e550e4e2() {
911+
let result = test_tx(
912+
"0x00eef6ba6741da8769192fac9d28c6631cf66f9e7c4e880b886ef6a2e550e4e2",
913+
RpcChain::MainNet,
914+
156105,
915+
18348936116,
945916
);
946-
let tx = rpc_state.get_transaction(tx_hash_str);
947-
let result = tx.execute(&mut state, &block_context, 0).unwrap();
917+
948918
dbg!(&result.actual_resources);
949919
dbg!(&result.actual_fee); // test=6361070805216, explorer=47292465953700, diff=5888146145679 (0.13%)
950920
dbg!(&result.call_info.clone().unwrap().execution_resources); // Ok with explorer

0 commit comments

Comments
 (0)