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

Commit 677a657

Browse files
authored
Add version + validation retdata checks for InvokeFunction, Declare, DeclareV2 & DeployAccount txs (#1128)
* Check InvokeFuncion tx version * Remove invalid code * Add retdata validation * Check tx version for Declare * Simplify verify_version * Check DeclareV2 version * Unify unsupported version errors * Reorder checks * Add retdata validation * Add retdata validation * Add version check for DeployAccount * fmt * Remove no longer used verify_version function * Fix tx versions in tests * Remove test for removed check * Remove test for removed check * Remove unreachable case * Fix test values * Fix test values * Fix test values * Remove unused constants * Remove old `QUERY_BASE` constant * Fix test values * Clippy * Add back QUERY_VERSION_BASE` constant * Code cleanup * Restore * Add a function to parse query versions * Add comments * Restore test * Fix deploy account version in becnh * Add tests for version checks
1 parent 047497b commit 677a657

File tree

15 files changed

+316
-432
lines changed

15 files changed

+316
-432
lines changed

bench/internals.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ fn deploy_account() {
8383
let internal_deploy_account = DeployAccount::new(
8484
class_hash,
8585
0,
86-
0.into(),
86+
1.into(),
8787
Felt252::zero(),
8888
vec![],
8989
signature,

examples/contract_execution/src/main.rs

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,6 @@ fn test_contract(
5757
//* Initialize needed variables
5858
//* --------------------------------------------
5959
let block_context = BlockContext::default();
60-
let chain_id = block_context.starknet_os_config().chain_id().clone();
6160
// Values hardcoded to pass signature validation
6261
let signature = vec![
6362
felt_str!("3086480810278599376317923499561306189851900463386393948998357832163236918254"),
@@ -104,18 +103,18 @@ fn test_contract(
104103
)
105104
.unwrap();
106105

107-
let internal_deploy = DeployAccount::new(
106+
let internal_deploy = DeployAccount::new_with_tx_hash(
108107
account_contract_class_hash,
109108
0,
110-
0.into(),
109+
1.into(),
111110
0.into(),
112111
// Values hardcoded to pass signature validation
113112
vec![felt_str!(
114113
"1735102664668487605176656616876767369909409133946409161569774794110049207117"
115114
)],
116115
signature.clone(),
117116
felt_str!("2669425616857739096022668060305620640217901643963991674344872184515580705509"),
118-
chain_id.clone(),
117+
2718.into(),
119118
)
120119
.unwrap();
121120

@@ -147,9 +146,9 @@ fn test_contract(
147146
compiled_class_hash.clone(),
148147
account_contract_address.clone(),
149148
0, // max fee
150-
1.into(),
149+
2.into(),
151150
signature.clone(),
152-
0.into(), // nonce
151+
1.into(), // nonce
153152
// Value hardcoded to pass signature validation
154153
2718.into(),
155154
)
@@ -213,7 +212,7 @@ fn test_contract(
213212
1.into(),
214213
account_execute_calldata,
215214
signature,
216-
Some(1.into()),
215+
Some(2.into()),
217216
// Value hardcoded to pass signature validation
218217
2718.into(),
219218
)

src/definitions/constants.rs

Lines changed: 14 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,6 @@ pub(crate) const N_DEFAULT_TOPICS: usize = 1; // Events have one default topic.
1414
pub(crate) const CONSUMED_MSG_TO_L2_ENCODED_DATA_SIZE: usize =
1515
(L1_TO_L2_MSG_HEADER_SIZE + 1) - CONSUMED_MSG_TO_L2_N_TOPICS;
1616

17-
lazy_static! {
18-
pub(crate) static ref QUERY_VERSION_BASE: Felt252 =
19-
felt_str!("340282366920938463463374607431768211456");
20-
}
21-
2217
pub(crate) const LOG_MSG_TO_L1_ENCODED_DATA_SIZE: usize =
2318
(L2_TO_L1_MSG_HEADER_SIZE + 1) - LOG_MSG_TO_L1_N_TOPICS;
2419

@@ -32,17 +27,6 @@ pub(crate) const N_STEPS_FEE_WEIGHT: f64 = 0.01;
3227
/// The version is considered 0 for L1-Handler transaction hash calculation purposes.
3328
pub(crate) const L1_HANDLER_VERSION: u64 = 0;
3429

35-
lazy_static! {
36-
pub static ref SUPPORTED_VERSIONS: [Felt252; 6] = [
37-
0.into(),
38-
1.into(),
39-
2.into(),
40-
&Into::<Felt252>::into(0) | &QUERY_VERSION_BASE.clone(),
41-
&Into::<Felt252>::into(1) | &QUERY_VERSION_BASE.clone(),
42-
&Into::<Felt252>::into(2) | &QUERY_VERSION_BASE.clone(),
43-
];
44-
}
45-
4630
lazy_static! {
4731
// Ratios are taken from the `starknet_instance` CairoLayout object in cairo-lang.
4832
pub static ref DEFAULT_CAIRO_RESOURCE_FEE_WEIGHTS: HashMap<String, f64> =
@@ -113,5 +97,19 @@ lazy_static! {
11397
pub static ref VALIDATE_ENTRY_POINT_SELECTOR: Felt252 =
11498
felt_str!("626969833899987279399947180575486623810258720106406659648356883742278317941");
11599

100+
pub static ref VALIDATE_RETDATA: Felt252 =
101+
felt_str!("370462705988");
102+
116103
pub static ref BLOCK_HASH_CONTRACT_ADDRESS: Address = Address(1.into());
117104
}
105+
106+
// Indentation for transactions meant to query and not addressed to the OS.
107+
lazy_static! {
108+
static ref QUERY_VERSION_BASE: Felt252 = felt_str!("340282366920938463463374607431768211456");
109+
pub(crate) static ref QUERY_VERSION_0: Felt252 =
110+
&Into::<Felt252>::into(0) | &*QUERY_VERSION_BASE;
111+
pub(crate) static ref QUERY_VERSION_1: Felt252 =
112+
&Into::<Felt252>::into(1) | &*QUERY_VERSION_BASE;
113+
pub(crate) static ref QUERY_VERSION_2: Felt252 =
114+
&Into::<Felt252>::into(2) | &*QUERY_VERSION_BASE;
115+
}

src/execution/mod.rs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
pub mod execution_entry_point;
22
pub mod gas_usage;
33
pub mod os_usage;
4-
5-
use crate::definitions::constants::QUERY_VERSION_BASE;
64
use crate::services::api::contract_classes::deprecated_contract_class::EntryPointType;
75
use crate::utils::parse_felt_array;
86
use crate::{
@@ -383,8 +381,8 @@ impl TransactionExecutionContext {
383381
n_steps: u64,
384382
version: Felt252,
385383
) -> Self {
386-
let nonce = if version == 0.into() || version == *QUERY_VERSION_BASE {
387-
0.into()
384+
let nonce = if version == 0.into() {
385+
Felt252::zero()
388386
} else {
389387
nonce
390388
};

src/lib.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -830,7 +830,7 @@ mod test {
830830
DeployAccount::new(
831831
*CLASS_HASH_BYTES,
832832
0,
833-
0.into(),
833+
1.into(),
834834
Felt252::zero(),
835835
vec![],
836836
SIGNATURE.clone(),
@@ -864,7 +864,7 @@ mod test {
864864
DeclareV2 {
865865
sender_address: TEST_ACCOUNT_CONTRACT_ADDRESS.clone(),
866866
validate_entry_point_selector: VALIDATE_DECLARE_ENTRY_POINT_SELECTOR.clone(),
867-
version: 1.into(),
867+
version: 2.into(),
868868
max_fee: INITIAL_GAS_COST,
869869
signature: vec![],
870870
nonce: 0.into(),

0 commit comments

Comments
 (0)