Skip to content

Commit

Permalink
feat(coonsensus): add cetral deploy account transaction written object
Browse files Browse the repository at this point in the history
  • Loading branch information
Yael-Starkware committed Dec 25, 2024
1 parent 2fc1955 commit ccefd67
Show file tree
Hide file tree
Showing 2 changed files with 112 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,17 @@ use starknet_api::block::{
};
use starknet_api::core::{ClassHash, CompiledClassHash, ContractAddress, Nonce};
use starknet_api::data_availability::DataAvailabilityMode;
use starknet_api::executable_transaction::{AccountTransaction, InvokeTransaction, Transaction};
use starknet_api::executable_transaction::{
AccountTransaction,
DeployAccountTransaction,
InvokeTransaction,
Transaction,
};
use starknet_api::state::StorageKey;
use starknet_api::transaction::fields::{
AccountDeploymentData,
Calldata,
ContractAddressSalt,
PaymasterData,
Tip,
TransactionSignature,
Expand Down Expand Up @@ -101,6 +107,13 @@ impl From<(CommitmentStateDiff, BlockInfo, StarknetVersion)> for CentralStateDif
}
}

fn into_u8(data_availability_mode: DataAvailabilityMode) -> u8 {
match data_availability_mode {
DataAvailabilityMode::L1 => 0,
DataAvailabilityMode::L2 => 1,
}
}

#[derive(Debug, PartialEq, Deserialize, Serialize)]
pub struct CentralInvokeTransactionV3 {
pub sender_address: ContractAddress,
Expand Down Expand Up @@ -136,33 +149,73 @@ impl From<InvokeTransaction> for CentralInvokeTransactionV3 {
}
}

fn into_u8(data_availability_mode: DataAvailabilityMode) -> u8 {
match data_availability_mode {
DataAvailabilityMode::L1 => 0,
DataAvailabilityMode::L2 => 1,
#[derive(Debug, PartialEq, Deserialize, Serialize)]
#[serde(tag = "version")]
pub enum CentralInvokeTransaction {
#[serde(rename = "0x3")]
V3(CentralInvokeTransactionV3),
}

#[derive(Debug, PartialEq, Deserialize, Serialize)]
pub struct CentralDeployAccountTransactionV3 {
pub resource_bounds: ValidResourceBounds,
pub tip: Tip,
pub signature: TransactionSignature,
pub nonce: Nonce,
pub class_hash: ClassHash,
pub contract_address_salt: ContractAddressSalt,
pub constructor_calldata: Calldata,
pub nonce_data_availability_mode: u8,
pub fee_data_availability_mode: u8,
pub paymaster_data: PaymasterData,
pub hash_value: TransactionHash,
pub sender_address: ContractAddress,
}

impl From<DeployAccountTransaction> for CentralDeployAccountTransactionV3 {
fn from(tx: DeployAccountTransaction) -> CentralDeployAccountTransactionV3 {
CentralDeployAccountTransactionV3 {
resource_bounds: tx.resource_bounds(),
tip: tx.tip(),
signature: tx.signature(),
nonce: tx.nonce(),
class_hash: tx.class_hash(),
contract_address_salt: tx.contract_address_salt(),
constructor_calldata: tx.constructor_calldata(),
nonce_data_availability_mode: into_u8(tx.nonce_data_availability_mode()),
fee_data_availability_mode: into_u8(tx.fee_data_availability_mode()),
paymaster_data: tx.paymaster_data(),
hash_value: tx.tx_hash(),
sender_address: tx.contract_address,
}
}
}

#[derive(Debug, PartialEq, Deserialize, Serialize)]
#[serde(tag = "version")]
pub enum CentralInvokeTransaction {
pub enum CentralDeployAccountTransaction {
#[serde(rename = "0x3")]
V3(CentralInvokeTransactionV3),
V3(CentralDeployAccountTransactionV3),
}

#[derive(Debug, PartialEq, Deserialize, Serialize)]
#[serde(tag = "type")]
pub enum CentralTransaction {
#[serde(rename = "INVOKE_FUNCTION")]
Invoke(CentralInvokeTransaction),
#[serde(rename = "DEPLOY_ACCOUNT")]
DeployAccount(CentralDeployAccountTransaction),
}

impl From<Transaction> for CentralTransaction {
fn from(tx: Transaction) -> CentralTransaction {
match tx {
Transaction::Account(AccountTransaction::Invoke(invoke_tx)) => {
CentralTransaction::Invoke(CentralInvokeTransaction::V3(
CentralInvokeTransactionV3::from(invoke_tx),
CentralTransaction::Invoke(CentralInvokeTransaction::V3(invoke_tx.into()))
}
Transaction::Account(AccountTransaction::DeployAccount(deploy_tx)) => {
CentralTransaction::DeployAccount(CentralDeployAccountTransaction::V3(
deploy_tx.into(),
))
}
Transaction::Account(_) => unimplemented!(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,20 +12,22 @@ use starknet_api::block::{
};
use starknet_api::core::{ClassHash, CompiledClassHash, Nonce};
use starknet_api::data_availability::DataAvailabilityMode;
use starknet_api::executable_transaction::InvokeTransaction;
use starknet_api::executable_transaction::{DeployAccountTransaction, InvokeTransaction};
use starknet_api::execution_resources::GasAmount;
use starknet_api::test_utils::read_json_file;
use starknet_api::transaction::fields::{
AllResourceBounds,
Calldata,
ContractAddressSalt,
ResourceBounds,
ValidResourceBounds,
};
use starknet_api::transaction::{InvokeTransactionV3, TransactionHash};
use starknet_api::transaction::{DeployAccountTransactionV3, InvokeTransactionV3, TransactionHash};
use starknet_api::{contract_address, felt, storage_key};

use super::{
CentralBlockInfo,
CentralDeployAccountTransaction,
CentralInvokeTransaction,
CentralResourcePrice,
CentralStateDiff,
Expand All @@ -35,6 +37,7 @@ use super::{

pub const CENTRAL_STATE_DIFF_JSON_PATH: &str = "central_state_diff.json";
pub const CENTRAL_INVOKE_TX_JSON_PATH: &str = "central_invoke_tx.json";
pub const CENTRAL_DEPLOY_ACCOUNT_TX_JSON_PATH: &str = "central_deploy_account_tx.json";

fn central_state_diff() -> CentralStateDiff {
// TODO(yael): compute the CentralStateDiff with into().
Expand Down Expand Up @@ -74,7 +77,7 @@ fn central_state_diff() -> CentralStateDiff {
}
}

fn central_invoke_transaction_json() -> Value {
fn central_invoke_tx_json() -> Value {
let invoke_tx = InvokeTransaction {
tx: starknet_api::transaction::InvokeTransaction::V3(InvokeTransactionV3 {
resource_bounds: ValidResourceBounds::AllResources(AllResourceBounds {
Expand Down Expand Up @@ -111,9 +114,52 @@ fn central_invoke_transaction_json() -> Value {
serde_json::to_value(central_transaction_written).unwrap()
}

fn central_deploy_account_tx_json() -> Value {
let deploy_account_tx = DeployAccountTransaction {
tx: starknet_api::transaction::DeployAccountTransaction::V3(DeployAccountTransactionV3 {
resource_bounds: ValidResourceBounds::AllResources(AllResourceBounds {
l1_gas: ResourceBounds {
max_amount: GasAmount(1),
max_price_per_unit: GasPrice(1),
},
l2_gas: ResourceBounds::default(),
l1_data_gas: ResourceBounds::default(),
}),
signature: Default::default(),
nonce: Default::default(),
tip: Default::default(),
paymaster_data: Default::default(),
nonce_data_availability_mode: DataAvailabilityMode::L1,
fee_data_availability_mode: DataAvailabilityMode::L1,

class_hash: ClassHash(felt!(
"0x1b5a0b09f23b091d5d1fa2f660ddfad6bcfce607deba23806cd7328ccfb8ee9"
)),
contract_address_salt: ContractAddressSalt(felt!(2_u8)),
constructor_calldata: Default::default(),
}),
tx_hash: TransactionHash(felt!(
"0x429cb4dc45610a80a96800ab350a11ff50e2d69e25c7723c002934e66b5a282"
)),
contract_address: contract_address!(
"0x4c2e031b0ddaa38e06fd9b1bf32bff739965f9d64833006204c67cbc879a57c"
),
};

let central_transaction_written = CentralTransactionWritten {
tx: CentralTransaction::DeployAccount(CentralDeployAccountTransaction::V3(
deploy_account_tx.into(),
)),
time_created: 1734601616,
};

serde_json::to_value(central_transaction_written).unwrap()
}

#[rstest]
#[case::state_diff(serde_json::to_value(central_state_diff()).unwrap(), CENTRAL_STATE_DIFF_JSON_PATH)]
#[case::invoke_tx(central_invoke_transaction_json(), CENTRAL_INVOKE_TX_JSON_PATH)]
#[case::invoke_tx(central_invoke_tx_json(), CENTRAL_INVOKE_TX_JSON_PATH)]
#[case::deploy_account_tx(central_deploy_account_tx_json(), CENTRAL_DEPLOY_ACCOUNT_TX_JSON_PATH)]
fn serialize_central_objects(#[case] rust_json: Value, #[case] python_json_path: &str) {
let python_json = read_json_file(python_json_path);

Expand Down

0 comments on commit ccefd67

Please sign in to comment.