Skip to content

Commit

Permalink
Add version + validation retdata checks for InvokeFunction, `Declar…
Browse files Browse the repository at this point in the history
…e`, `DeclareV2` & `DeployAccount` txs (lambdaclass#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
  • Loading branch information
fmoletta authored Nov 9, 2023
1 parent 047497b commit 677a657
Show file tree
Hide file tree
Showing 15 changed files with 316 additions and 432 deletions.
2 changes: 1 addition & 1 deletion bench/internals.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ fn deploy_account() {
let internal_deploy_account = DeployAccount::new(
class_hash,
0,
0.into(),
1.into(),
Felt252::zero(),
vec![],
signature,
Expand Down
13 changes: 6 additions & 7 deletions examples/contract_execution/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,6 @@ fn test_contract(
//* Initialize needed variables
//* --------------------------------------------
let block_context = BlockContext::default();
let chain_id = block_context.starknet_os_config().chain_id().clone();
// Values hardcoded to pass signature validation
let signature = vec![
felt_str!("3086480810278599376317923499561306189851900463386393948998357832163236918254"),
Expand Down Expand Up @@ -104,18 +103,18 @@ fn test_contract(
)
.unwrap();

let internal_deploy = DeployAccount::new(
let internal_deploy = DeployAccount::new_with_tx_hash(
account_contract_class_hash,
0,
0.into(),
1.into(),
0.into(),
// Values hardcoded to pass signature validation
vec![felt_str!(
"1735102664668487605176656616876767369909409133946409161569774794110049207117"
)],
signature.clone(),
felt_str!("2669425616857739096022668060305620640217901643963991674344872184515580705509"),
chain_id.clone(),
2718.into(),
)
.unwrap();

Expand Down Expand Up @@ -147,9 +146,9 @@ fn test_contract(
compiled_class_hash.clone(),
account_contract_address.clone(),
0, // max fee
1.into(),
2.into(),
signature.clone(),
0.into(), // nonce
1.into(), // nonce
// Value hardcoded to pass signature validation
2718.into(),
)
Expand Down Expand Up @@ -213,7 +212,7 @@ fn test_contract(
1.into(),
account_execute_calldata,
signature,
Some(1.into()),
Some(2.into()),
// Value hardcoded to pass signature validation
2718.into(),
)
Expand Down
30 changes: 14 additions & 16 deletions src/definitions/constants.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,6 @@ pub(crate) const N_DEFAULT_TOPICS: usize = 1; // Events have one default topic.
pub(crate) const CONSUMED_MSG_TO_L2_ENCODED_DATA_SIZE: usize =
(L1_TO_L2_MSG_HEADER_SIZE + 1) - CONSUMED_MSG_TO_L2_N_TOPICS;

lazy_static! {
pub(crate) static ref QUERY_VERSION_BASE: Felt252 =
felt_str!("340282366920938463463374607431768211456");
}

pub(crate) const LOG_MSG_TO_L1_ENCODED_DATA_SIZE: usize =
(L2_TO_L1_MSG_HEADER_SIZE + 1) - LOG_MSG_TO_L1_N_TOPICS;

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

lazy_static! {
pub static ref SUPPORTED_VERSIONS: [Felt252; 6] = [
0.into(),
1.into(),
2.into(),
&Into::<Felt252>::into(0) | &QUERY_VERSION_BASE.clone(),
&Into::<Felt252>::into(1) | &QUERY_VERSION_BASE.clone(),
&Into::<Felt252>::into(2) | &QUERY_VERSION_BASE.clone(),
];
}

lazy_static! {
// Ratios are taken from the `starknet_instance` CairoLayout object in cairo-lang.
pub static ref DEFAULT_CAIRO_RESOURCE_FEE_WEIGHTS: HashMap<String, f64> =
Expand Down Expand Up @@ -113,5 +97,19 @@ lazy_static! {
pub static ref VALIDATE_ENTRY_POINT_SELECTOR: Felt252 =
felt_str!("626969833899987279399947180575486623810258720106406659648356883742278317941");

pub static ref VALIDATE_RETDATA: Felt252 =
felt_str!("370462705988");

pub static ref BLOCK_HASH_CONTRACT_ADDRESS: Address = Address(1.into());
}

// Indentation for transactions meant to query and not addressed to the OS.
lazy_static! {
static ref QUERY_VERSION_BASE: Felt252 = felt_str!("340282366920938463463374607431768211456");
pub(crate) static ref QUERY_VERSION_0: Felt252 =
&Into::<Felt252>::into(0) | &*QUERY_VERSION_BASE;
pub(crate) static ref QUERY_VERSION_1: Felt252 =
&Into::<Felt252>::into(1) | &*QUERY_VERSION_BASE;
pub(crate) static ref QUERY_VERSION_2: Felt252 =
&Into::<Felt252>::into(2) | &*QUERY_VERSION_BASE;
}
6 changes: 2 additions & 4 deletions src/execution/mod.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
pub mod execution_entry_point;
pub mod gas_usage;
pub mod os_usage;

use crate::definitions::constants::QUERY_VERSION_BASE;
use crate::services::api::contract_classes::deprecated_contract_class::EntryPointType;
use crate::utils::parse_felt_array;
use crate::{
Expand Down Expand Up @@ -383,8 +381,8 @@ impl TransactionExecutionContext {
n_steps: u64,
version: Felt252,
) -> Self {
let nonce = if version == 0.into() || version == *QUERY_VERSION_BASE {
0.into()
let nonce = if version == 0.into() {
Felt252::zero()
} else {
nonce
};
Expand Down
4 changes: 2 additions & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -830,7 +830,7 @@ mod test {
DeployAccount::new(
*CLASS_HASH_BYTES,
0,
0.into(),
1.into(),
Felt252::zero(),
vec![],
SIGNATURE.clone(),
Expand Down Expand Up @@ -864,7 +864,7 @@ mod test {
DeclareV2 {
sender_address: TEST_ACCOUNT_CONTRACT_ADDRESS.clone(),
validate_entry_point_selector: VALIDATE_DECLARE_ENTRY_POINT_SELECTOR.clone(),
version: 1.into(),
version: 2.into(),
max_fee: INITIAL_GAS_COST,
signature: vec![],
nonce: 0.into(),
Expand Down
Loading

0 comments on commit 677a657

Please sign in to comment.