Skip to content

Commit

Permalink
feat(consensus): add central invoke transaction
Browse files Browse the repository at this point in the history
  • Loading branch information
Yael-Starkware committed Dec 22, 2024
1 parent db6c7a2 commit c414502
Show file tree
Hide file tree
Showing 5 changed files with 207 additions and 7 deletions.
2 changes: 2 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions crates/sequencing/papyrus_consensus_orchestrator/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ papyrus_network.workspace = true
papyrus_protobuf.workspace = true
papyrus_storage.workspace = true
serde.workspace = true
serde_repr.workspace = true
starknet-types-core.workspace = true
starknet_api.workspace = true
starknet_batcher_types = { workspace = true, features = ["testing"] }
Expand All @@ -31,6 +32,7 @@ mockall.workspace = true
papyrus_network = { workspace = true, features = ["testing"] }
papyrus_storage = { workspace = true, features = ["testing"] }
papyrus_test_utils.workspace = true
rstest.workspace = true
serde_json.workspace = true
starknet_batcher_types = { workspace = true, features = ["testing"] }
test-case.workspace = true
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use blockifier::state::cached_state::CommitmentStateDiff;
use indexmap::{indexmap, IndexMap};
use serde::{Deserialize, Serialize};
use serde_repr::*;
use starknet_api::block::{
BlockInfo,
BlockNumber,
Expand All @@ -10,7 +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::state::StorageKey;
use starknet_api::transaction::fields::{
AccountDeploymentData,
Calldata,
PaymasterData,
Tip,
TransactionSignature,
ValidResourceBounds,
};
use starknet_api::transaction::TransactionHash;
use starknet_types_core::felt::Felt;

/// Central objects are required in order to continue processing the block by the centralized
Expand Down Expand Up @@ -89,3 +100,105 @@ impl From<(CommitmentStateDiff, BlockInfo, StarknetVersion)> for CentralStateDif
}
}
}

// This object is serialized as a string by starknet_api, but the Python implementation requires it
// to be serialized as an integer. To address this, a separate object is provided that serializes
// into an integer specifically for cende.
#[derive(Clone, Debug, Default, PartialEq, Deserialize_repr, Serialize_repr)]
#[serde(into = "u8", try_from = "u8")]
#[repr(u8)]
pub enum CentralDataAvailabilityMode {
#[default]
L1 = 0,
L2 = 1,
}

impl From<DataAvailabilityMode> for CentralDataAvailabilityMode {
fn from(mode: DataAvailabilityMode) -> CentralDataAvailabilityMode {
match mode {
DataAvailabilityMode::L1 => CentralDataAvailabilityMode::L1,
DataAvailabilityMode::L2 => CentralDataAvailabilityMode::L2,
}
}
}

#[derive(Debug, PartialEq, Deserialize, Serialize)]
pub struct CentralTransactionTimestamp(pub u64);

#[derive(Debug, PartialEq, Deserialize, Serialize)]
pub struct CentralInvokeTransactionV3 {
pub sender_address: ContractAddress,
pub calldata: Calldata,
pub signature: TransactionSignature,
pub nonce: Nonce,
pub resource_bounds: ValidResourceBounds,
pub tip: Tip,
pub paymaster_data: PaymasterData,
pub account_deployment_data: AccountDeploymentData,
pub nonce_data_availability_mode: CentralDataAvailabilityMode,
pub fee_data_availability_mode: CentralDataAvailabilityMode,
pub hash_value: TransactionHash,
}

impl From<InvokeTransaction> for CentralInvokeTransactionV3 {
fn from(tx: InvokeTransaction) -> CentralInvokeTransactionV3 {
CentralInvokeTransactionV3 {
sender_address: tx.sender_address(),
calldata: tx.calldata(),
signature: tx.signature(),
nonce: tx.nonce(),
resource_bounds: tx.resource_bounds(),
tip: tx.tip(),
paymaster_data: tx.paymaster_data(),
account_deployment_data: tx.account_deployment_data(),
nonce_data_availability_mode: tx.nonce_data_availability_mode().into(),
fee_data_availability_mode: tx.fee_data_availability_mode().into(),
hash_value: tx.tx_hash(),
}
}
}

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

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

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),
))
}
Transaction::Account(_) => unimplemented!(),
Transaction::L1Handler(_) => unimplemented!(),
}
}
}

#[derive(Debug, PartialEq, Deserialize, Serialize)]
pub struct CentralTransactionWritten {
pub tx: CentralTransaction,
pub time_created: CentralTransactionTimestamp,
}

impl From<(Transaction, u64)> for CentralTransactionWritten {
fn from((tx, timestamp): (Transaction, u64)) -> CentralTransactionWritten {
CentralTransactionWritten {
tx: CentralTransaction::from(tx),
// This timestamp is required for metrics data. Yoni and Noa approved that it is
// sufficient to take the time during the batcher run.
time_created: CentralTransactionTimestamp(timestamp),
}
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
use std::fs::File;
use std::sync::Arc;

use indexmap::indexmap;
use infra_utils::path::resolve_project_relative_path;
use rstest::rstest;
use serde_json::Value;
use starknet_api::block::{
BlockNumber,
Expand All @@ -12,9 +14,26 @@ use starknet_api::block::{
};
use starknet_api::core::{ClassHash, CompiledClassHash, Nonce};
use starknet_api::data_availability::DataAvailabilityMode;
use starknet_api::execution_resources::GasAmount;
use starknet_api::transaction::fields::{
AllResourceBounds,
Calldata,
ResourceBounds,
ValidResourceBounds,
};
use starknet_api::transaction::TransactionHash;
use starknet_api::{contract_address, felt, storage_key};

use crate::cende::central_objects::{CentralBlockInfo, CentralResourcePrice, CentralStateDiff};
use crate::cende::central_objects::{
CentralBlockInfo,
CentralInvokeTransaction,
CentralInvokeTransactionV3,
CentralResourcePrice,
CentralStateDiff,
CentralTransaction,
CentralTransactionTimestamp,
CentralTransactionWritten,
};

macro_rules! test_resources_dir {
() => {
Expand All @@ -23,6 +42,8 @@ macro_rules! test_resources_dir {
}
pub const CENTRAL_STATE_DIFF_JSON_PATH: &str =
concat!(test_resources_dir!(), "/central_state_diff.json");
pub const CENTRAL_INVOKE_TX_JSON_PATH: &str =
concat!(test_resources_dir!(), "/central_invoke_tx.json");

fn central_state_diff() -> CentralStateDiff {
CentralStateDiff {
Expand Down Expand Up @@ -61,13 +82,41 @@ fn central_state_diff() -> CentralStateDiff {
}
}

#[test]
fn serialize_central_state_diff() {
let rust_central_state_diff = central_state_diff();
let rust_json = serde_json::to_value(&rust_central_state_diff).unwrap();
fn invoke_transaction() -> CentralTransactionWritten {
CentralTransactionWritten {
tx: CentralTransaction::Invoke(CentralInvokeTransaction::V3(CentralInvokeTransactionV3 {
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(),
}),
sender_address: contract_address!(
"0x14abfd58671a1a9b30de2fcd2a42e8bff2ce1096a7c70bc7995904965f277e"
),
calldata: Calldata(Arc::new(vec![felt!(0_u8), felt!(1_u8)])),
signature: Default::default(),
nonce: Default::default(),
tip: Default::default(),
paymaster_data: Default::default(),
account_deployment_data: Default::default(),
nonce_data_availability_mode: Default::default(),
fee_data_availability_mode: Default::default(),
hash_value: TransactionHash(felt!(
"0x6efd067c859e6469d0f6d158e9ae408a9552eb8cc11f618ab3aef3e52450666"
)),
})),
time_created: CentralTransactionTimestamp(1734601615),
}
}

let file =
File::open(resolve_project_relative_path(CENTRAL_STATE_DIFF_JSON_PATH).unwrap()).unwrap();
#[rstest]
#[case::state_diff(serde_json::to_value(central_state_diff()).unwrap(), CENTRAL_STATE_DIFF_JSON_PATH)]
#[case::invoke_tx(serde_json::to_value(invoke_transaction()).unwrap(), CENTRAL_INVOKE_TX_JSON_PATH)]
fn serialize_central_objects(#[case] rust_json: Value, #[case] python_json_path: &str) {
let file = File::open(resolve_project_relative_path(python_json_path).unwrap()).unwrap();
let python_json: Value = serde_json::from_reader(file).unwrap();

assert_eq!(rust_json, python_json,);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
{
"tx": {
"hash_value": "0x6efd067c859e6469d0f6d158e9ae408a9552eb8cc11f618ab3aef3e52450666",
"version": "0x3",
"signature": [],
"nonce": "0x0",
"sender_address": "0x14abfd58671a1a9b30de2fcd2a42e8bff2ce1096a7c70bc7995904965f277e",
"nonce_data_availability_mode": 0,
"fee_data_availability_mode": 0,
"resource_bounds": {
"L1_GAS": {
"max_amount": "0x1",
"max_price_per_unit": "0x1"
},
"L2_GAS": {
"max_amount": "0x0",
"max_price_per_unit": "0x0"
},
"L1_DATA": {
"max_amount": "0x0",
"max_price_per_unit": "0x0"
}
},
"tip": "0x0",
"paymaster_data": [],
"calldata": [
"0x0",
"0x1"
],
"account_deployment_data": [],
"type": "INVOKE_FUNCTION"
},
"time_created": 1734601615
}

0 comments on commit c414502

Please sign in to comment.