-
Notifications
You must be signed in to change notification settings - Fork 26
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feature(consensus): add CentralStateDiff object in the aerospike format
- Loading branch information
1 parent
3f75d13
commit 8eac87b
Showing
7 changed files
with
211 additions
and
0 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
3 changes: 3 additions & 0 deletions
3
crates/sequencing/papyrus_consensus_orchestrator/src/central_communication.rs
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 |
---|---|---|
@@ -0,0 +1,3 @@ | ||
mod central_objects; | ||
#[cfg(test)] | ||
mod central_objects_test; |
84 changes: 84 additions & 0 deletions
84
...es/sequencing/papyrus_consensus_orchestrator/src/central_communication/central_objects.rs
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 |
---|---|---|
@@ -0,0 +1,84 @@ | ||
use blockifier::state::cached_state::CommitmentStateDiff; | ||
use indexmap::{indexmap, IndexMap}; | ||
use serde::{Deserialize, Serialize}; | ||
use starknet_api::block::{ | ||
BlockInfo, | ||
BlockNumber, | ||
BlockTimestamp, | ||
NonzeroGasPrice, | ||
StarknetVersion, | ||
}; | ||
use starknet_api::core::{ClassHash, CompiledClassHash, ContractAddress, Nonce}; | ||
use starknet_api::data_availability::DataAvailabilityMode; | ||
use starknet_api::state::StorageKey; | ||
use starknet_types_core::felt::Felt; | ||
|
||
#[derive(Debug, PartialEq, Deserialize, Serialize)] | ||
pub struct CentralResourcePrice { | ||
pub price_in_wei: NonzeroGasPrice, | ||
pub price_in_fri: NonzeroGasPrice, | ||
} | ||
|
||
#[derive(Debug, PartialEq, Deserialize, Serialize)] | ||
pub struct CentralBlockInfo { | ||
pub block_number: BlockNumber, | ||
pub block_timestamp: BlockTimestamp, | ||
pub sequencer_address: ContractAddress, | ||
pub l1_gas_price: CentralResourcePrice, | ||
pub l1_data_gas_price: CentralResourcePrice, | ||
pub l2_gas_price: CentralResourcePrice, | ||
pub use_kzg_da: bool, | ||
pub starknet_version: Option<StarknetVersion>, | ||
} | ||
|
||
impl From<(BlockInfo, StarknetVersion)> for CentralBlockInfo { | ||
fn from((block_info, starknet_version): (BlockInfo, StarknetVersion)) -> CentralBlockInfo { | ||
CentralBlockInfo { | ||
block_number: block_info.block_number, | ||
block_timestamp: block_info.block_timestamp, | ||
sequencer_address: block_info.sequencer_address, | ||
l1_gas_price: CentralResourcePrice { | ||
price_in_wei: block_info.gas_prices.eth_gas_prices.l1_gas_price, | ||
price_in_fri: block_info.gas_prices.strk_gas_prices.l1_gas_price, | ||
}, | ||
l1_data_gas_price: CentralResourcePrice { | ||
price_in_wei: block_info.gas_prices.eth_gas_prices.l1_data_gas_price, | ||
price_in_fri: block_info.gas_prices.strk_gas_prices.l1_data_gas_price, | ||
}, | ||
l2_gas_price: CentralResourcePrice { | ||
price_in_wei: block_info.gas_prices.eth_gas_prices.l2_gas_price, | ||
price_in_fri: block_info.gas_prices.strk_gas_prices.l2_gas_price, | ||
}, | ||
use_kzg_da: block_info.use_kzg_da, | ||
starknet_version: Some(starknet_version), | ||
} | ||
} | ||
} | ||
|
||
#[derive(Debug, PartialEq, Deserialize, Serialize)] | ||
pub struct CentralStateDiff { | ||
pub address_to_class_hash: IndexMap<ContractAddress, ClassHash>, | ||
pub nonces: IndexMap<DataAvailabilityMode, IndexMap<ContractAddress, Nonce>>, | ||
pub storage_updates: | ||
IndexMap<DataAvailabilityMode, IndexMap<ContractAddress, IndexMap<StorageKey, Felt>>>, | ||
pub declared_classes: IndexMap<ClassHash, CompiledClassHash>, | ||
pub block_info: CentralBlockInfo, | ||
} | ||
|
||
impl From<(CommitmentStateDiff, BlockInfo, StarknetVersion)> for CentralStateDiff { | ||
fn from( | ||
(state_diff, block_info, starknet_version): ( | ||
CommitmentStateDiff, | ||
BlockInfo, | ||
StarknetVersion, | ||
), | ||
) -> CentralStateDiff { | ||
CentralStateDiff { | ||
address_to_class_hash: state_diff.address_to_class_hash, | ||
nonces: indexmap!(DataAvailabilityMode::L1=> state_diff.address_to_nonce), | ||
storage_updates: indexmap!(DataAvailabilityMode::L1=> state_diff.storage_updates), | ||
declared_classes: state_diff.class_hash_to_compiled_class_hash, | ||
block_info: CentralBlockInfo::from((block_info, starknet_version)), | ||
} | ||
} | ||
} |
74 changes: 74 additions & 0 deletions
74
...quencing/papyrus_consensus_orchestrator/src/central_communication/central_objects_test.rs
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 |
---|---|---|
@@ -0,0 +1,74 @@ | ||
use std::fs; | ||
|
||
use indexmap::indexmap; | ||
use infra_utils::path::resolve_project_relative_path; | ||
use serde_json::Value; | ||
use starknet_api::block::{ | ||
BlockNumber, | ||
BlockTimestamp, | ||
GasPrice, | ||
NonzeroGasPrice, | ||
StarknetVersion, | ||
}; | ||
use starknet_api::core::{ClassHash, CompiledClassHash, Nonce}; | ||
use starknet_api::data_availability::DataAvailabilityMode; | ||
use starknet_api::{contract_address, felt, storage_key}; | ||
|
||
use super::central_objects::{CentralBlockInfo, CentralResourcePrice, CentralStateDiff}; | ||
|
||
pub const CENTRAL_STATE_DIFF_JSON_PATH: &str = "crates/sequencing/papyrus_consensus_orchestrator/\ | ||
src/central_communication/resources/\ | ||
central_state_diff.json"; | ||
|
||
fn central_state_diff() -> CentralStateDiff { | ||
let rust_central_state_diff = CentralStateDiff { | ||
address_to_class_hash: indexmap! { | ||
contract_address!(1_u8) => | ||
ClassHash(felt!(1_u8)), | ||
}, | ||
nonces: indexmap!( | ||
DataAvailabilityMode::L1 => | ||
indexmap!(contract_address!(2_u8)=> Nonce(felt!(2_u8))), | ||
), | ||
storage_updates: indexmap!( | ||
DataAvailabilityMode::L1=> | ||
indexmap!(contract_address!(3_u8) => indexmap!(storage_key!(3_u8) => felt!(3_u8))), | ||
), | ||
declared_classes: indexmap!(ClassHash(felt!(4_u8))=> CompiledClassHash(felt!(4_u8))), | ||
block_info: CentralBlockInfo { | ||
block_number: BlockNumber(5), | ||
block_timestamp: BlockTimestamp(6), | ||
sequencer_address: contract_address!(7_u8), | ||
l1_gas_price: CentralResourcePrice { | ||
price_in_wei: NonzeroGasPrice::new(GasPrice(8)).unwrap(), | ||
price_in_fri: NonzeroGasPrice::new(GasPrice(9)).unwrap(), | ||
}, | ||
l1_data_gas_price: CentralResourcePrice { | ||
price_in_wei: NonzeroGasPrice::new(GasPrice(10)).unwrap(), | ||
price_in_fri: NonzeroGasPrice::new(GasPrice(11)).unwrap(), | ||
}, | ||
l2_gas_price: CentralResourcePrice { | ||
price_in_wei: NonzeroGasPrice::new(GasPrice(12)).unwrap(), | ||
price_in_fri: NonzeroGasPrice::new(GasPrice(13)).unwrap(), | ||
}, | ||
use_kzg_da: true, | ||
starknet_version: Some(StarknetVersion::default()), | ||
}, | ||
}; | ||
rust_central_state_diff | ||
} | ||
|
||
#[test] | ||
fn serialize_central_state_diff() { | ||
let rust_central_state_diff = central_state_diff(); | ||
|
||
let rust_serialized = serde_json::to_string(&rust_central_state_diff).unwrap(); | ||
let rust_json: Value = serde_json::from_str(&rust_serialized).unwrap(); | ||
|
||
let python_json_string = | ||
fs::read_to_string(resolve_project_relative_path(CENTRAL_STATE_DIFF_JSON_PATH).unwrap()) | ||
.unwrap(); | ||
let python_json: Value = serde_json::from_str(&python_json_string).unwrap(); | ||
|
||
assert_eq!(rust_json, python_json,); | ||
} |
39 changes: 39 additions & 0 deletions
39
...apyrus_consensus_orchestrator/src/central_communication/resources/central_state_diff.json
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 |
---|---|---|
@@ -0,0 +1,39 @@ | ||
{ | ||
"address_to_class_hash": { | ||
"0x1": "0x1" | ||
}, | ||
"nonces": { | ||
"L1": { | ||
"0x2": "0x2" | ||
} | ||
}, | ||
"storage_updates": { | ||
"L1": { | ||
"0x3": { | ||
"0x3": "0x3" | ||
} | ||
} | ||
}, | ||
"declared_classes": { | ||
"0x4": "0x4" | ||
}, | ||
"block_info": { | ||
"block_number": 5, | ||
"block_timestamp": 6, | ||
"l1_gas_price": { | ||
"price_in_wei": "0x8", | ||
"price_in_fri": "0x9" | ||
}, | ||
"l1_data_gas_price": { | ||
"price_in_wei": "0xa", | ||
"price_in_fri": "0xb" | ||
}, | ||
"l2_gas_price": { | ||
"price_in_wei": "0xc", | ||
"price_in_fri": "0xd" | ||
}, | ||
"sequencer_address": "0x7", | ||
"starknet_version": "0.13.4", | ||
"use_kzg_da": true | ||
} | ||
} |
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