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

Commit 4ff4538

Browse files
fmolettapefontana
andauthored
[0.13] Convert between sn_api & sir V3 transactions (#1210)
* Add GasPricess & FeeTokenAddresses structs * Update code * RpcStateReader::get_gas_price fetch both * Update code * Clippy * Fix comment * Add VersionSpecificAccountTxFields struct * Integrate into TransactionExecutionContext * Replace max_fee field with account_tx_fields for InvokeFunction tx * Check tx account fields version when creating Invoke tx * Replace max_fee field with account_tx_fields for DeclareV3 tx * Replace max_fee field with account_tx_fields for DeployAccount tx * Update get_transaction_context for Txs not affected by this change * Fixes * Fixes * Fixes * Fix handling of ignore_max_fee * Improve placeholder method * Handle TODOs * Implement get_onchain_data_cost * Refactor * Generalize fn * Start implementing check_fee_bounds * Finish implementing check_fee_bounds * Replace old code * Fixes * Implement FeeType for VersionSpecificAccountTxFields * Update other txs * Clippy * Add test * Simplify * Move test * Return the appropiate error whe converting to deprecated declare * Fix conversion between sn api Invoke and sir InvokeFunction tx * Fix conversion between sn api and sir DeployAccount tx * Implement declare_tx_from_sn_api_transaction * Remove error * Clippy * Add V3 deser --------- Co-authored-by: Pedro Fontana <[email protected]>
1 parent 4a764a5 commit 4ff4538

File tree

6 files changed

+268
-170
lines changed

6 files changed

+268
-170
lines changed

rpc_state_reader/src/sir_state_reader.rs

Lines changed: 10 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,10 @@ use starknet_api::{
66
core::{ClassHash as SNClassHash, ContractAddress, PatriciaKey},
77
hash::{StarkFelt, StarkHash},
88
state::StorageKey,
9-
transaction::{Transaction as SNTransaction, TransactionHash, TransactionVersion},
9+
transaction::{Transaction as SNTransaction, TransactionHash},
1010
};
1111
use starknet_in_rust::{
12-
core::{contract_address::compute_casm_class_hash, errors::state_errors::StateError},
12+
core::errors::state_errors::StateError,
1313
definitions::{
1414
block_context::{BlockContext, FeeTokenAddresses, StarknetChainId, StarknetOsConfig},
1515
constants::{
@@ -28,8 +28,8 @@ use starknet_in_rust::{
2828
BlockInfo,
2929
},
3030
transaction::{
31-
error::TransactionError, Declare, DeclareV2, DeployAccount, InvokeFunction, L1Handler,
32-
VersionSpecificAccountTxFields,
31+
declare_tx_from_sn_api_transaction, error::TransactionError, DeployAccount, InvokeFunction,
32+
L1Handler,
3333
},
3434
utils::{Address, ClassHash},
3535
};
@@ -224,74 +224,12 @@ pub fn execute_tx_configurable_with_state(
224224
contract_class
225225
};
226226

227-
if tx.version() != TransactionVersion(2_u8.into()) {
228-
let contract_class = match contract_class {
229-
CompiledClass::Deprecated(cc) => cc.as_ref().clone(),
230-
_ => unreachable!(),
231-
};
232-
233-
let declare = Declare::new_with_tx_and_class_hash(
234-
contract_class,
235-
Address(Felt252::from_bytes_be_slice(
236-
tx.sender_address().0.key().bytes(),
237-
)),
238-
match tx {
239-
starknet_api::transaction::DeclareTransaction::V0(ref tx) => tx.max_fee.0,
240-
starknet_api::transaction::DeclareTransaction::V1(ref tx) => tx.max_fee.0,
241-
starknet_api::transaction::DeclareTransaction::V2(ref tx) => tx.max_fee.0,
242-
starknet_api::transaction::DeclareTransaction::V3(_) => {
243-
return Err(TransactionError::UnsuportedV3Transaction)
244-
}
245-
},
246-
Felt252::from_bytes_be_slice(tx.version().0.bytes()),
247-
tx.signature()
248-
.0
249-
.iter()
250-
.map(|f| Felt252::from_bytes_be_slice(f.bytes()))
251-
.collect(),
252-
Felt252::from_bytes_be_slice(tx.nonce().0.bytes()),
253-
Felt252::from_bytes_be_slice(tx_hash.0.bytes()),
254-
class_hash,
255-
)
256-
.unwrap();
257-
declare.create_for_simulation(skip_validate, false, false, false, skip_nonce_check)
258-
} else {
259-
let contract_class = match contract_class {
260-
CompiledClass::Casm { casm, .. } => casm.as_ref().clone(),
261-
_ => unreachable!(),
262-
};
263-
264-
let compiled_class_hash = compute_casm_class_hash(&contract_class).unwrap();
265-
266-
let declare = DeclareV2::new_with_sierra_class_hash_and_tx_hash(
267-
None,
268-
Felt252::from_bytes_be_slice(tx.class_hash().0.bytes()),
269-
Some(contract_class),
270-
compiled_class_hash,
271-
Address(Felt252::from_bytes_be_slice(
272-
tx.sender_address().0.key().bytes(),
273-
)),
274-
// TODO[0.13] Properly convert between V3 tx fields
275-
VersionSpecificAccountTxFields::new_deprecated(match tx {
276-
starknet_api::transaction::DeclareTransaction::V0(ref tx) => tx.max_fee.0,
277-
starknet_api::transaction::DeclareTransaction::V1(ref tx) => tx.max_fee.0,
278-
starknet_api::transaction::DeclareTransaction::V2(ref tx) => tx.max_fee.0,
279-
starknet_api::transaction::DeclareTransaction::V3(_) => {
280-
return Err(TransactionError::UnsuportedV3Transaction)
281-
}
282-
}),
283-
Felt252::from_bytes_be_slice(tx.version().0.bytes()),
284-
tx.signature()
285-
.0
286-
.iter()
287-
.map(|f| Felt252::from_bytes_be_slice(f.bytes()))
288-
.collect(),
289-
Felt252::from_bytes_be_slice(tx.nonce().0.bytes()),
290-
Felt252::from_bytes_be_slice(tx_hash.0.bytes()),
291-
)
292-
.unwrap();
293-
declare.create_for_simulation(skip_validate, false, false, false, skip_nonce_check)
294-
}
227+
let declare = declare_tx_from_sn_api_transaction(
228+
tx,
229+
Felt252::from_bytes_be_slice(tx_hash.0.bytes()),
230+
contract_class,
231+
)?;
232+
declare.create_for_simulation(skip_validate, false, false, false, skip_nonce_check)
295233
}
296234
SNTransaction::L1Handler(tx) => L1Handler::from_sn_api_tx(
297235
tx,

rpc_state_reader/src/utils.rs

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -78,13 +78,24 @@ pub fn deserialize_transaction_json(
7878
"0x1" => Ok(Transaction::Invoke(InvokeTransaction::V1(
7979
serde_json::from_value(transaction)?,
8080
))),
81+
"0x3" => Ok(Transaction::Invoke(InvokeTransaction::V3(
82+
serde_json::from_value(transaction)?,
83+
))),
8184
x => Err(serde::de::Error::custom(format!(
8285
"unimplemented invoke version: {x}"
8386
))),
8487
},
85-
"DEPLOY_ACCOUNT" => Ok(Transaction::DeployAccount(DeployAccountTransaction::V1(
86-
serde_json::from_value(transaction)?,
87-
))),
88+
"DEPLOY_ACCOUNT" => match tx_version.as_str() {
89+
"0x1" => Ok(Transaction::DeployAccount(DeployAccountTransaction::V1(
90+
serde_json::from_value(transaction)?,
91+
))),
92+
"0x3" => Ok(Transaction::DeployAccount(DeployAccountTransaction::V3(
93+
serde_json::from_value(transaction)?,
94+
))),
95+
x => Err(serde::de::Error::custom(format!(
96+
"unimplemented declare version: {x}"
97+
))),
98+
},
8899
"DECLARE" => match tx_version.as_str() {
89100
"0x0" => Ok(Transaction::Declare(DeclareTransaction::V0(
90101
serde_json::from_value(transaction)?,
@@ -95,6 +106,9 @@ pub fn deserialize_transaction_json(
95106
"0x2" => Ok(Transaction::Declare(DeclareTransaction::V2(
96107
serde_json::from_value(transaction)?,
97108
))),
109+
"0x3" => Ok(Transaction::Declare(DeclareTransaction::V3(
110+
serde_json::from_value(transaction)?,
111+
))),
98112
x => Err(serde::de::Error::custom(format!(
99113
"unimplemented declare version: {x}"
100114
))),

src/transaction/deploy_account.rs

Lines changed: 33 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
use super::fee::{calculate_tx_fee, charge_fee, check_fee_bounds, run_post_execution_fee_checks};
22
use super::{
3-
check_account_tx_fields_version, get_tx_version, ResourceBounds, VersionSpecificAccountTxFields,
3+
check_account_tx_fields_version, get_tx_version, CurrentAccountTxFields, ResourceBounds,
4+
VersionSpecificAccountTxFields,
45
};
56
use super::{invoke_function::verify_no_calls_to_other_contracts, Transaction};
67
use crate::definitions::constants::VALIDATE_RETDATA;
@@ -39,6 +40,7 @@ use crate::{
3940
use cairo_vm::Felt252;
4041
use getset::Getters;
4142
use num_traits::Zero;
43+
use starknet_api::transaction::Resource;
4244
use std::fmt::Debug;
4345

4446
#[cfg(feature = "cairo-native")]
@@ -560,12 +562,37 @@ impl DeployAccount {
560562
value: starknet_api::transaction::DeployAccountTransaction,
561563
tx_hash: Felt252,
562564
) -> Result<Self, TransactionError> {
563-
let max_fee = match value {
564-
starknet_api::transaction::DeployAccountTransaction::V1(ref tx) => tx.max_fee,
565-
starknet_api::transaction::DeployAccountTransaction::V3(_) => {
566-
return Err(TransactionError::UnsuportedV3Transaction)
565+
let account_tx_fields = match &value {
566+
starknet_api::transaction::DeployAccountTransaction::V1(tx) => {
567+
VersionSpecificAccountTxFields::Deprecated(tx.max_fee.0)
568+
}
569+
starknet_api::transaction::DeployAccountTransaction::V3(tx) => {
570+
VersionSpecificAccountTxFields::Current(CurrentAccountTxFields {
571+
l1_resource_bounds: tx
572+
.resource_bounds
573+
.0
574+
.get(&Resource::L1Gas)
575+
.map(|r| r.into())
576+
.unwrap_or_default(),
577+
l2_resource_bounds: tx
578+
.resource_bounds
579+
.0
580+
.get(&Resource::L2Gas)
581+
.map(|r| r.into()),
582+
tip: tx.tip.0,
583+
nonce_data_availability_mode: tx.nonce_data_availability_mode.into(),
584+
fee_data_availability_mode: tx.fee_data_availability_mode.into(),
585+
paymaster_data: tx
586+
.paymaster_data
587+
.0
588+
.iter()
589+
.map(|f| Felt252::from_bytes_be_slice(f.bytes()))
590+
.collect(),
591+
account_deployment_data: Default::default(),
592+
})
567593
}
568594
};
595+
569596
let version = Felt252::from_bytes_be_slice(value.version().0.bytes());
570597
let nonce = Felt252::from_bytes_be_slice(value.nonce().0.bytes());
571598
let class_hash: ClassHash = ClassHash(value.class_hash().0.bytes().try_into().unwrap());
@@ -588,8 +615,7 @@ impl DeployAccount {
588615

589616
DeployAccount::new_with_tx_hash(
590617
class_hash,
591-
// TODO[0.13] Properly convert between V3 tx fields
592-
VersionSpecificAccountTxFields::Deprecated(max_fee.0),
618+
account_tx_fields,
593619
version,
594620
nonce,
595621
constructor_calldata,

src/transaction/error.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -162,8 +162,6 @@ pub enum TransactionError {
162162
MaxL1GasPriceTooLow(u128, u128),
163163
#[error("Max fee ({0}) exceeds balance (Uint256({1}, {2})).")]
164164
MaxFeeExceedsBalance(u128, Felt252, Felt252),
165-
#[error("V3 Transactions not Supported Yet")]
166-
UnsuportedV3Transaction,
167165
#[error("V3 Transactions can't be created with deprecated account tx fields")]
168166
DeprecatedAccountTxFieldsVInV3TX,
169167
#[error("Non V3 Transactions can't be created with non deprecated account tx fields")]

0 commit comments

Comments
 (0)