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

Commit 74996f7

Browse files
fmolettajuanbono
authored andcommitted
Check that running a declare v1 yields a higher fee than simulating it without validation (#1076)
* Reorder DeployAccount::apply * Revert "Reorder DeployAccount::apply" This reverts commit 11b0c39. * Add test * Add test * clippy + fmt --------- Co-authored-by: Juan Bono <juanbono94@gmail.com>
1 parent 887ddc1 commit 74996f7

File tree

2 files changed

+213
-0
lines changed

2 files changed

+213
-0
lines changed

src/lib.rs

Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1049,4 +1049,117 @@ mod test {
10491049
)
10501050
);
10511051
}
1052+
1053+
#[test]
1054+
fn test_simulate_declare_v1_compare_fees() {
1055+
// accounts contract class must be stored before running declaration of fibonacci
1056+
let contract_class = ContractClass::from_path("starknet_programs/Account.json").unwrap();
1057+
1058+
// Instantiate CachedState
1059+
let mut contract_class_cache = HashMap::new();
1060+
1061+
// ------------ contract data --------------------
1062+
let hash = compute_deprecated_class_hash(&contract_class).unwrap();
1063+
let class_hash = hash.to_be_bytes();
1064+
1065+
contract_class_cache.insert(
1066+
class_hash,
1067+
CompiledClass::Deprecated(Arc::new(contract_class)),
1068+
);
1069+
1070+
// store sender_address
1071+
let sender_address = Address(1.into());
1072+
// this is not conceptually correct as the sender address would be an
1073+
// Account contract (not the contract that we are currently declaring)
1074+
// but for testing reasons its ok
1075+
1076+
let mut state_reader = InMemoryStateReader::default();
1077+
state_reader
1078+
.address_to_class_hash_mut()
1079+
.insert(sender_address.clone(), class_hash);
1080+
state_reader
1081+
.address_to_nonce_mut()
1082+
.insert(sender_address.clone(), Felt252::new(1));
1083+
1084+
let mut state = CachedState::new(Arc::new(state_reader), contract_class_cache);
1085+
// Insert pubkey storage var to pass validation
1086+
let storage_entry = &(
1087+
sender_address,
1088+
felt_str!(
1089+
"1672321442399497129215646424919402195095307045612040218489019266998007191460"
1090+
)
1091+
.to_be_bytes(),
1092+
);
1093+
state.set_storage_at(
1094+
storage_entry,
1095+
felt_str!(
1096+
"1735102664668487605176656616876767369909409133946409161569774794110049207117"
1097+
),
1098+
);
1099+
1100+
//* ---------------------------------------
1101+
//* Test declare with previous data
1102+
//* ---------------------------------------
1103+
1104+
let fib_contract_class =
1105+
ContractClass::from_path("starknet_programs/fibonacci.json").unwrap();
1106+
1107+
let chain_id = StarknetChainId::TestNet.to_felt();
1108+
1109+
// declare tx
1110+
// Signature & tx hash values are hand-picked for account validations to pass
1111+
let mut declare = Declare::new(
1112+
fib_contract_class,
1113+
chain_id,
1114+
Address(Felt252::one()),
1115+
60000,
1116+
1.into(),
1117+
vec![
1118+
felt_str!(
1119+
"3086480810278599376317923499561306189851900463386393948998357832163236918254"
1120+
),
1121+
felt_str!(
1122+
"598673427589502599949712887611119751108407514580626464031881322743364689811"
1123+
),
1124+
],
1125+
Felt252::one(),
1126+
)
1127+
.unwrap();
1128+
declare.hash_value = felt_str!("2718");
1129+
1130+
let mut block_context = BlockContext::default();
1131+
block_context.starknet_os_config_mut().gas_price = 12;
1132+
1133+
let declare_tx = Transaction::Declare(declare);
1134+
1135+
let without_validate_fee = simulate_transaction(
1136+
&[&declare_tx],
1137+
state.clone(),
1138+
&block_context,
1139+
100_000_000,
1140+
true,
1141+
false,
1142+
true,
1143+
false,
1144+
false,
1145+
)
1146+
.unwrap()[0]
1147+
.actual_fee;
1148+
1149+
let with_validate_fee = simulate_transaction(
1150+
&[&declare_tx],
1151+
state,
1152+
&block_context,
1153+
100_000_000,
1154+
false,
1155+
false,
1156+
true,
1157+
false,
1158+
false,
1159+
)
1160+
.unwrap()[0]
1161+
.actual_fee;
1162+
1163+
assert!(with_validate_fee > without_validate_fee)
1164+
}
10521165
}

src/transaction/declare.rs

Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -895,4 +895,104 @@ mod tests {
895895
Err(TransactionError::FeeTransferError(_))
896896
);
897897
}
898+
899+
#[test]
900+
fn declare_v1_with_validation_fee_higher_than_no_validation() {
901+
// accounts contract class must be stored before running declaration of fibonacci
902+
let contract_class = ContractClass::from_path("starknet_programs/Account.json").unwrap();
903+
904+
// Instantiate CachedState
905+
let mut contract_class_cache = HashMap::new();
906+
907+
// ------------ contract data --------------------
908+
let hash = compute_deprecated_class_hash(&contract_class).unwrap();
909+
let class_hash = hash.to_be_bytes();
910+
911+
contract_class_cache.insert(
912+
class_hash,
913+
CompiledClass::Deprecated(Arc::new(contract_class)),
914+
);
915+
916+
// store sender_address
917+
let sender_address = Address(1.into());
918+
// this is not conceptually correct as the sender address would be an
919+
// Account contract (not the contract that we are currently declaring)
920+
// but for testing reasons its ok
921+
922+
let mut state_reader = InMemoryStateReader::default();
923+
state_reader
924+
.address_to_class_hash_mut()
925+
.insert(sender_address.clone(), class_hash);
926+
state_reader
927+
.address_to_nonce_mut()
928+
.insert(sender_address.clone(), Felt252::new(1));
929+
930+
let mut state = CachedState::new(Arc::new(state_reader), contract_class_cache);
931+
// Insert pubkey storage var to pass validation
932+
let storage_entry = &(
933+
sender_address,
934+
felt_str!(
935+
"1672321442399497129215646424919402195095307045612040218489019266998007191460"
936+
)
937+
.to_be_bytes(),
938+
);
939+
state.set_storage_at(
940+
storage_entry,
941+
felt_str!(
942+
"1735102664668487605176656616876767369909409133946409161569774794110049207117"
943+
),
944+
);
945+
946+
//* ---------------------------------------
947+
//* Test declare with previous data
948+
//* ---------------------------------------
949+
950+
let fib_contract_class =
951+
ContractClass::from_path("starknet_programs/fibonacci.json").unwrap();
952+
953+
let chain_id = StarknetChainId::TestNet.to_felt();
954+
955+
// declare tx
956+
// Signature & tx hash values are hand-picked for account validations to pass
957+
let mut declare = Declare::new(
958+
fib_contract_class,
959+
chain_id,
960+
Address(Felt252::one()),
961+
60000,
962+
1.into(),
963+
vec![
964+
felt_str!(
965+
"3086480810278599376317923499561306189851900463386393948998357832163236918254"
966+
),
967+
felt_str!(
968+
"598673427589502599949712887611119751108407514580626464031881322743364689811"
969+
),
970+
],
971+
Felt252::one(),
972+
)
973+
.unwrap();
974+
declare.skip_fee_transfer = true;
975+
declare.hash_value = felt_str!("2718");
976+
977+
let simulate_declare = declare
978+
.clone()
979+
.create_for_simulation(true, false, true, false);
980+
981+
// ---------------------
982+
// Comparison
983+
// ---------------------
984+
let mut state_copy = state.clone();
985+
let mut bock_context = BlockContext::default();
986+
bock_context.starknet_os_config.gas_price = 12;
987+
assert!(
988+
declare
989+
.execute(&mut state, &bock_context)
990+
.unwrap()
991+
.actual_fee
992+
> simulate_declare
993+
.execute(&mut state_copy, &bock_context, 0)
994+
.unwrap()
995+
.actual_fee,
996+
);
997+
}
898998
}

0 commit comments

Comments
 (0)