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

Commit

Permalink
New methods for tx creation w hash (#805)
Browse files Browse the repository at this point in the history
* add new_with_tx_hash methods for deploy account and invoke

* add new_with_tx_hash method for declare

* add new_with_tx_hash method for declarev2

* add new_with_tx_hash method for deploy

* add doc for declare v2 new method

---------

Co-authored-by: Juan Bono <juanbono94@gmail.com>
  • Loading branch information
SantiagoPittella and juanbono authored Jul 11, 2023
1 parent 1a3a52f commit 7d9b7a6
Show file tree
Hide file tree
Showing 16 changed files with 293 additions and 158 deletions.
5 changes: 0 additions & 5 deletions bench/internals.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,6 @@ fn deploy_account() {
signature,
SALT.clone(),
StarknetChainId::TestNet.to_felt(),
None,
)
.unwrap();
internal_deploy_account.execute(&mut state_copy, block_context)
Expand Down Expand Up @@ -116,7 +115,6 @@ fn declare() {
0.into(),
vec![],
Felt252::zero(),
None,
)
.expect("couldn't create transaction");

Expand Down Expand Up @@ -153,7 +151,6 @@ fn deploy() {
vec![],
StarknetChainId::TestNet.to_felt(),
0.into(),
None,
)
.unwrap();
internal_deploy.execute(&mut state_copy, block_context)
Expand Down Expand Up @@ -184,7 +181,6 @@ fn invoke() {
vec![],
StarknetChainId::TestNet.to_felt(),
0.into(),
None,
)
.unwrap();

Expand All @@ -207,7 +203,6 @@ fn invoke() {
signature,
StarknetChainId::TestNet.to_felt(),
Some(Felt252::zero()),
None,
)
.unwrap();
internal_invoke.execute(&mut state_copy, block_context, 2_000_000)
Expand Down
5 changes: 2 additions & 3 deletions cli/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -181,16 +181,15 @@ fn invoke_parser(
Some(vec) => vec.iter().map(|&n| n.into()).collect(),
None => Vec::new(),
};
let internal_invoke = InvokeFunction::new(
let internal_invoke = InvokeFunction::new_with_tx_hash(
contract_address.clone(),
entrypoint_selector.clone(),
0,
TRANSACTION_VERSION.clone(),
calldata.clone(),
vec![],
Felt252::zero(),
Some(Felt252::zero()),
transaction_hash,
transaction_hash.unwrap(),
)?;
let _tx_info = internal_invoke.apply(cached_state, &BlockContext::default(), 0)?;

Expand Down
2 changes: 1 addition & 1 deletion examples/contract_execution/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ fn test_contract(
//* Declare new contract class
//* --------------------------------------------
state
.declare(contract_class.clone(), None)
.declare(contract_class.clone())
.expect("Could not declare the contract class");

//* --------------------------------------------
Expand Down
2 changes: 0 additions & 2 deletions src/bin/invoke_with_cachedstate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,6 @@ fn main() {
vec![],
StarknetChainId::TestNet.to_felt(),
Some(Felt252::from(i * 2)),
None,
)
.unwrap()
.execute(&mut cached_state, &block_context, 0)
Expand All @@ -77,7 +76,6 @@ fn main() {
vec![],
StarknetChainId::TestNet.to_felt(),
Some(Felt252::from((i * 2) + 1)),
None,
)
.unwrap()
.execute(&mut cached_state, &block_context, 0)
Expand Down
13 changes: 0 additions & 13 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -273,7 +273,6 @@ mod test {
vec![],
StarknetChainId::TestNet.to_felt(),
Some(0.into()),
None,
)
.unwrap();
let transaction = Transaction::InvokeFunction(invoke_function);
Expand Down Expand Up @@ -415,7 +414,6 @@ mod test {
vec![],
StarknetChainId::TestNet.to_felt(),
None,
None,
)
.unwrap();

Expand Down Expand Up @@ -506,7 +504,6 @@ mod test {
vec![],
StarknetChainId::TestNet.to_felt(),
Some(1.into()),
None,
)
.unwrap(),
);
Expand All @@ -521,7 +518,6 @@ mod test {
vec![],
StarknetChainId::TestNet.to_felt(),
Some(2.into()),
None,
)
.unwrap(),
);
Expand All @@ -536,7 +532,6 @@ mod test {
vec![],
StarknetChainId::TestNet.to_felt(),
Some(3.into()),
None,
)
.unwrap(),
);
Expand Down Expand Up @@ -634,7 +629,6 @@ mod test {
vec![],
StarknetChainId::TestNet.to_felt(),
Some(1.into()),
None,
)
.unwrap(),
);
Expand Down Expand Up @@ -678,7 +672,6 @@ mod test {
vec![],
StarknetChainId::TestNet.to_felt(),
0.into(),
None,
)
.unwrap(),
);
Expand Down Expand Up @@ -714,7 +707,6 @@ mod test {
0.into(),
vec![],
Felt252::zero(),
None,
)
.expect("couldn't create transaction"),
);
Expand Down Expand Up @@ -752,7 +744,6 @@ mod test {
vec![],
StarknetChainId::TestNet.to_felt(),
0.into(),
None,
)
.unwrap();

Expand All @@ -775,7 +766,6 @@ mod test {
SIGNATURE.clone(),
StarknetChainId::TestNet.to_felt(),
Some(Felt252::zero()),
None,
)
.unwrap(),
);
Expand Down Expand Up @@ -814,7 +804,6 @@ mod test {
SIGNATURE.clone(),
SALT.clone(),
StarknetChainId::TestNet.to_felt(),
None,
)
.unwrap(),
);
Expand Down Expand Up @@ -964,7 +953,6 @@ mod test {
vec![],
StarknetChainId::TestNet.to_felt(),
0.into(),
None,
)
.unwrap(),
);
Expand All @@ -986,7 +974,6 @@ mod test {
SIGNATURE.clone(),
StarknetChainId::TestNet.to_felt(),
Some(Felt252::zero()),
None,
)
.unwrap(),
);
Expand Down
1 change: 0 additions & 1 deletion src/syscalls/deprecated_syscall_handler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1161,7 +1161,6 @@ mod tests {
Vec::new(),
0.into(),
Some(0.into()),
None,
)
.unwrap();

Expand Down
2 changes: 0 additions & 2 deletions src/testing/erc20.rs
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,6 @@ fn test_erc20_cairo2() {
],
contract_address_salt,
StarknetChainId::TestNet.to_felt(),
None,
)
.unwrap();

Expand Down Expand Up @@ -197,7 +196,6 @@ fn test_erc20_cairo2() {
],
contract_address_salt,
StarknetChainId::TestNet.to_felt(),
None,
)
.unwrap();

Expand Down
64 changes: 40 additions & 24 deletions src/testing/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,6 @@ impl StarknetState {
pub fn declare(
&mut self,
contract_class: ContractClass,
hash_value: Option<Felt252>,
) -> Result<(ClassHash, TransactionExecutionInfo), TransactionError> {
let tx = Declare::new(
contract_class,
Expand All @@ -89,7 +88,6 @@ impl StarknetState {
0.into(),
Vec::new(),
0.into(),
hash_value,
)?;

let tx_execution_info = tx.execute(&mut self.state, &self.block_context)?;
Expand Down Expand Up @@ -176,14 +174,22 @@ impl StarknetState {
remaining_gas: u128,
) -> Result<(Address, TransactionExecutionInfo), StarknetStateError> {
let chain_id = self.block_context.starknet_os_config.chain_id.to_felt();
let deploy = Deploy::new(
contract_address_salt,
contract_class.clone(),
constructor_calldata,
chain_id,
TRANSACTION_VERSION.clone(),
hash_value,
)?;
let deploy = match hash_value {
None => Deploy::new(
contract_address_salt,
contract_class.clone(),
constructor_calldata,
chain_id,
TRANSACTION_VERSION.clone(),
)?,
Some(hash_value) => Deploy::new_with_tx_hash(
contract_address_salt,
contract_class.clone(),
constructor_calldata,
TRANSACTION_VERSION.clone(),
hash_value,
)?,
};
let contract_address = deploy.contract_address.clone();
let contract_hash = deploy.contract_hash;
let mut tx = Transaction::Deploy(deploy);
Expand Down Expand Up @@ -277,17 +283,28 @@ impl StarknetState {
None => self.state.get_nonce_at(&contract_address)?,
};

InvokeFunction::new(
contract_address,
entry_point_selector,
max_fee,
TRANSACTION_VERSION.clone(),
calldata,
signature,
self.chain_id(),
Some(nonce),
hash_value,
)
match hash_value {
None => InvokeFunction::new(
contract_address,
entry_point_selector,
max_fee,
TRANSACTION_VERSION.clone(),
calldata,
signature,
self.chain_id(),
Some(nonce),
),
Some(hash_value) => InvokeFunction::new_with_tx_hash(
contract_address,
entry_point_selector,
max_fee,
TRANSACTION_VERSION.clone(),
calldata,
signature,
Some(nonce),
hash_value,
),
}
}
}

Expand Down Expand Up @@ -455,9 +472,8 @@ mod tests {
let fib_contract_class =
ContractClass::from_path("starknet_programs/fibonacci.json").unwrap();

let (ret_class_hash, _exec_info) = starknet_state
.declare(fib_contract_class.clone(), None)
.unwrap();
let (ret_class_hash, _exec_info) =
starknet_state.declare(fib_contract_class.clone()).unwrap();

//* ---------------------------------------
// Expected result
Expand Down
Loading

0 comments on commit 7d9b7a6

Please # to comment.