This repository has been archived by the owner on Jul 22, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 53
Add new constructor declare v2 #823
Merged
Merged
Changes from 8 commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
fe0aed3
Add casm class hash
SantiagoPittella 8d2b745
add casm class hash check
SantiagoPittella 60a3db2
fix tests
SantiagoPittella 87c75cc
Update src/lib.rs
SantiagoPittella 6a43a9e
Merge branch 'main' into add-class-hash-check-for-declarev2
SantiagoPittella 939fffd
Add casm contract class as optional
SantiagoPittella 67898ad
Add test with casm contract class
SantiagoPittella 7ff2e8a
update doc
SantiagoPittella a47b47b
Merge branch 'main' into add-class-hash-check-for-declarev2
SantiagoPittella 499b8c3
Update src/lib.rs
SantiagoPittella 5e01742
Update src/lib.rs
SantiagoPittella a0cbb66
Add missed import
SantiagoPittella 476919e
Merge branch 'add-class-hash-check-for-declarev2' into add-new-constr…
SantiagoPittella 5227390
fix test
SantiagoPittella 5e4a64f
add tests
SantiagoPittella 2f73f71
Merge branch 'main' into add-new-constructor-declare-v2
SantiagoPittella File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -836,7 +836,7 @@ mod test { | |||||
tx_type: TransactionType::Declare, | ||||||
validate_entry_point_selector: VALIDATE_DECLARE_ENTRY_POINT_SELECTOR.clone(), | ||||||
version: 1.into(), | ||||||
max_fee: 2, | ||||||
max_fee: 100_000_000, | ||||||
signature: vec![], | ||||||
nonce: 0.into(), | ||||||
hash_value: 0.into(), | ||||||
|
@@ -998,4 +998,18 @@ mod test { | |||||
[(0, 1224), (0, 0)] | ||||||
); | ||||||
} | ||||||
|
||||||
#[test] | ||||||
fn test_declare_v2_with_invalid_compiled_class_hash() { | ||||||
let (block_context, mut state) = create_account_tx_test_state().unwrap(); | ||||||
let mut declare_v2 = declarev2_tx(); | ||||||
declare_v2.compiled_class_hash = Felt252::from(1); | ||||||
let declare_tx = Transaction::DeclareV2(Box::new(declare_v2)); | ||||||
|
||||||
let err = declare_tx | ||||||
.execute(&mut state, &block_context, 100_000_000) | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
.unwrap_err(); | ||||||
|
||||||
assert_eq!(err.to_string(), "Invalid compiled class, expected class hash: \"1948962768849191111780391610229754715773924969841143100991524171924131413970\", but received: \"1\"".to_string()); | ||||||
} | ||||||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
use super::{verify_version, Transaction}; | ||
use crate::core::contract_address::compute_sierra_class_hash; | ||
use crate::core::contract_address::{compute_casm_class_hash, compute_sierra_class_hash}; | ||
use crate::services::api::contract_classes::deprecated_contract_class::EntryPointType; | ||
|
||
use crate::{ | ||
|
@@ -44,7 +44,7 @@ pub struct DeclareV2 { | |
pub sierra_contract_class: SierraContractClass, | ||
pub sierra_class_hash: Felt252, | ||
pub hash_value: Felt252, | ||
pub casm_class: once_cell::unsync::OnceCell<CasmContractClass>, | ||
pub casm_class: Option<CasmContractClass>, | ||
pub skip_validate: bool, | ||
pub skip_execute: bool, | ||
pub skip_fee_transfer: bool, | ||
|
@@ -55,6 +55,7 @@ impl DeclareV2 { | |
/// It will calculate the sierra class hash and the transaction hash. | ||
/// ## Parameters: | ||
/// - sierra_contract_class: The sierra contract class of the contract to declare | ||
/// - casm_contract_class: The casm contract class of the contract to declare. This is optional. | ||
/// - compiled_class_hash: the class hash of the contract compiled with Cairo1 or newer. | ||
/// - chain_id: Id of the network where is going to be declare, those can be: Mainnet, Testnet. | ||
/// - sender_address: The address of the account declaring the contract. | ||
|
@@ -65,6 +66,7 @@ impl DeclareV2 { | |
#[allow(clippy::too_many_arguments)] | ||
pub fn new( | ||
sierra_contract_class: &SierraContractClass, | ||
casm_contract_class: Option<CasmContractClass>, | ||
compiled_class_hash: Felt252, | ||
chain_id: Felt252, | ||
sender_address: Address, | ||
|
@@ -88,6 +90,7 @@ impl DeclareV2 { | |
Self::new_with_sierra_class_hash_and_tx_hash( | ||
sierra_contract_class, | ||
sierra_class_hash, | ||
casm_contract_class, | ||
compiled_class_hash, | ||
sender_address, | ||
max_fee, | ||
|
@@ -102,6 +105,7 @@ impl DeclareV2 { | |
/// ## Parameters: | ||
/// - sierra_contract_class: The sierra contract class of the contract to declare | ||
/// - sierra_class_hash: The precomputed hash for the sierra contract | ||
/// - casm_contract_class: The casm contract class of the contract to declare. This is optional. | ||
/// - compiled_class_hash: the class hash of the contract compiled with Cairo1 or newer. | ||
/// - sender_address: The address of the account declaring the contract. | ||
/// - max_fee: refers to max amount of fee that a declare takes. | ||
|
@@ -115,6 +119,7 @@ impl DeclareV2 { | |
pub fn new_with_sierra_class_hash_and_tx_hash( | ||
sierra_contract_class: &SierraContractClass, | ||
sierra_class_hash: Felt252, | ||
casm_contract_class: Option<CasmContractClass>, | ||
compiled_class_hash: Felt252, | ||
sender_address: Address, | ||
max_fee: u128, | ||
|
@@ -137,7 +142,7 @@ impl DeclareV2 { | |
nonce, | ||
compiled_class_hash, | ||
hash_value, | ||
casm_class: Default::default(), | ||
casm_class: casm_contract_class, | ||
skip_execute: false, | ||
skip_validate: false, | ||
skip_fee_transfer: false, | ||
|
@@ -155,7 +160,8 @@ impl DeclareV2 { | |
|
||
// creates a new instance of a declare but without the computation of the transaction hash. | ||
/// ## Parameters: | ||
/// - sierra_contract_class: The sierra contract class of the contract to declare | ||
/// - sierra_contract_class: The sierra contract class of the contract to declare. | ||
/// - casm_contract_class: The casm contract class of the contract to declare. This is optional. | ||
/// - compiled_class_hash: the class hash of the contract compiled with Cairo1 or newer. | ||
/// - sender_address: The address of the account declaring the contract. | ||
/// - max_fee: refers to max amount of fee that a declare takes. | ||
|
@@ -166,6 +172,7 @@ impl DeclareV2 { | |
#[allow(clippy::too_many_arguments)] | ||
pub fn new_with_tx_hash( | ||
sierra_contract_class: &SierraContractClass, | ||
casm_contract_class: Option<CasmContractClass>, | ||
compiled_class_hash: Felt252, | ||
sender_address: Address, | ||
max_fee: u128, | ||
|
@@ -179,6 +186,7 @@ impl DeclareV2 { | |
Self::new_with_sierra_class_hash_and_tx_hash( | ||
sierra_contract_class, | ||
sierra_class_hash, | ||
casm_contract_class, | ||
compiled_class_hash, | ||
sender_address, | ||
max_fee, | ||
|
@@ -193,6 +201,7 @@ impl DeclareV2 { | |
/// ## Parameters: | ||
/// - sierra_contract_class: The sierra contract class of the contract to declare | ||
/// - sierra_class_hash: The precomputed hash for the sierra contract | ||
/// - casm_contract_class: The casm contract class of the contract to declare. This is optional. | ||
/// - compiled_class_hash: the class hash of the contract compiled with Cairo1 or newer. | ||
/// - chain_id: Id of the network where is going to be declare, those can be: Mainnet, Testnet. | ||
/// - sender_address: The address of the account declaring the contract. | ||
|
@@ -204,6 +213,7 @@ impl DeclareV2 { | |
pub fn new_with_sierra_class_hash( | ||
sierra_contract_class: &SierraContractClass, | ||
sierra_class_hash: Felt252, | ||
casm_contract_class: Option<CasmContractClass>, | ||
compiled_class_hash: Felt252, | ||
chain_id: Felt252, | ||
sender_address: Address, | ||
|
@@ -225,6 +235,7 @@ impl DeclareV2 { | |
Self::new_with_sierra_class_hash_and_tx_hash( | ||
sierra_contract_class, | ||
sierra_class_hash, | ||
casm_contract_class, | ||
compiled_class_hash, | ||
sender_address, | ||
max_fee, | ||
|
@@ -373,15 +384,23 @@ impl DeclareV2 { | |
&self, | ||
state: &mut S, | ||
) -> Result<(), TransactionError> { | ||
let casm_class = self | ||
.casm_class | ||
.get_or_try_init(|| { | ||
let casm_class = match &self.casm_class { | ||
None => { | ||
CasmContractClass::from_contract_class(self.sierra_contract_class.clone(), true) | ||
}) | ||
.map_err(|e| TransactionError::SierraCompileError(e.to_string()))?; | ||
.map_err(|e| TransactionError::SierraCompileError(e.to_string()))? | ||
} | ||
Some(casm_contract_class) => casm_contract_class.clone(), | ||
}; | ||
|
||
let casm_class_hash = compute_casm_class_hash(&casm_class)?; | ||
if casm_class_hash != self.compiled_class_hash { | ||
return Err(TransactionError::InvalidCompiledClassHash( | ||
casm_class_hash.to_string(), | ||
self.compiled_class_hash.to_string(), | ||
)); | ||
} | ||
state.set_compiled_class_hash(&self.sierra_class_hash, &self.compiled_class_hash)?; | ||
state.set_compiled_class(&self.compiled_class_hash, casm_class.clone())?; | ||
state.set_compiled_class(&self.compiled_class_hash, casm_class)?; | ||
|
||
Ok(()) | ||
} | ||
|
@@ -452,6 +471,7 @@ mod tests { | |
use std::{collections::HashMap, fs::File, io::BufReader, path::PathBuf}; | ||
|
||
use super::DeclareV2; | ||
use crate::core::contract_address::compute_casm_class_hash; | ||
use crate::services::api::contract_classes::compiled_class::CompiledClass; | ||
use crate::state::state_api::StateReader; | ||
use crate::{ | ||
|
@@ -463,7 +483,7 @@ mod tests { | |
use num_traits::{One, Zero}; | ||
|
||
#[test] | ||
fn create_declare_v2_test() { | ||
fn create_declare_v2_without_casm_contract_class_test() { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I would add a test with a none DeclareV2.casm_class to check that everything is working OK |
||
// read file to create sierra contract class | ||
let version; | ||
let path; | ||
|
@@ -484,12 +504,85 @@ mod tests { | |
let sierra_contract_class: cairo_lang_starknet::contract_class::ContractClass = | ||
serde_json::from_reader(reader).unwrap(); | ||
let sender_address = Address(1.into()); | ||
let casm_class = | ||
CasmContractClass::from_contract_class(sierra_contract_class.clone(), true).unwrap(); | ||
let casm_class_hash = compute_casm_class_hash(&casm_class).unwrap(); | ||
|
||
// create internal declare v2 | ||
|
||
let internal_declare = DeclareV2::new_with_tx_hash( | ||
&sierra_contract_class, | ||
None, | ||
casm_class_hash, | ||
sender_address, | ||
0, | ||
version, | ||
[1.into()].to_vec(), | ||
Felt252::zero(), | ||
Felt252::one(), | ||
) | ||
.unwrap(); | ||
|
||
// crate state to store casm contract class | ||
let casm_contract_class_cache = HashMap::new(); | ||
let state_reader = InMemoryStateReader::default(); | ||
let mut state = CachedState::new(state_reader, None, Some(casm_contract_class_cache)); | ||
|
||
// call compile and store | ||
assert!(internal_declare | ||
.compile_and_store_casm_class(&mut state) | ||
.is_ok()); | ||
|
||
// test we can retreive the data | ||
let expected_casm_class = CasmContractClass::from_contract_class( | ||
internal_declare.sierra_contract_class.clone(), | ||
true, | ||
) | ||
.unwrap(); | ||
|
||
let casm_class = match state | ||
.get_contract_class(&internal_declare.compiled_class_hash.to_be_bytes()) | ||
.unwrap() | ||
{ | ||
CompiledClass::Casm(casm) => *casm, | ||
_ => unreachable!(), | ||
}; | ||
|
||
assert_eq!(expected_casm_class, casm_class); | ||
} | ||
|
||
#[test] | ||
fn create_declare_v2_with_casm_contract_class_test() { | ||
// read file to create sierra contract class | ||
let version; | ||
let path; | ||
#[cfg(not(feature = "cairo_1_tests"))] | ||
{ | ||
version = Felt252::from(2); | ||
path = PathBuf::from("starknet_programs/cairo2/fibonacci.sierra"); | ||
} | ||
|
||
#[cfg(feature = "cairo_1_tests")] | ||
{ | ||
version = Felt252::from(1); | ||
path = PathBuf::from("starknet_programs/cairo1/fibonacci.sierra"); | ||
} | ||
|
||
let file = File::open(path).unwrap(); | ||
let reader = BufReader::new(file); | ||
let sierra_contract_class: cairo_lang_starknet::contract_class::ContractClass = | ||
serde_json::from_reader(reader).unwrap(); | ||
let sender_address = Address(1.into()); | ||
let casm_class = | ||
CasmContractClass::from_contract_class(sierra_contract_class.clone(), true).unwrap(); | ||
let casm_class_hash = compute_casm_class_hash(&casm_class).unwrap(); | ||
|
||
// create internal declare v2 | ||
|
||
let internal_declare = DeclareV2::new_with_tx_hash( | ||
&sierra_contract_class, | ||
Some(casm_class), | ||
casm_class_hash, | ||
sender_address, | ||
0, | ||
version, | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.