Skip to content

Commit

Permalink
Fix/state root (#68)
Browse files Browse the repository at this point in the history
  • Loading branch information
antiyro authored Apr 21, 2024
1 parent 65cc66e commit 98ed1d0
Show file tree
Hide file tree
Showing 5 changed files with 86 additions and 16 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ git # Deoxys Changelog

## Next release

- fix(root): fixed state commitments broken due to genesis loader
- feat(docker): add dockerfile and docker-compose
- fix: fix implementation `get_storage_at()` for `BlockifierStateAdapter`
- fix(sync): Fix end condition of the l2 sync
Expand Down
31 changes: 26 additions & 5 deletions crates/client/sync/src/commitments/transactions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,19 +37,40 @@ where
let include_signature = block_number >= 61394;

let signature_hash = match transaction {
Transaction::Invoke(invoke_tx) if include_signature => {
Transaction::Invoke(invoke_tx) => {
// Include signatures for Invoke transactions or for all transactions
// starting from block 61394
let signature = invoke_tx.signature();

H::compute_hash_on_elements(
&signature.0.iter().map(|x| Felt252Wrapper::from(*x).into()).collect::<Vec<FieldElement>>(),
)
}
_ => {
// Before block 61394, and for non-Invoke transactions, signatures are not included
H::compute_hash_on_elements(&[])
Transaction::Declare(declare_tx) => {
// Include signatures for Declare transactions if the block number is greater than 61394 (mainnet)
if include_signature {
let signature = declare_tx.signature();

H::compute_hash_on_elements(
&signature.0.iter().map(|x| Felt252Wrapper::from(*x).into()).collect::<Vec<FieldElement>>(),
)
} else {
H::compute_hash_on_elements(&[])
}
}
Transaction::DeployAccount(deploy_account_tx) => {
// Include signatures for DeployAccount transactions if the block number is greater than 61394
// (mainnet)
if include_signature {
let signature = deploy_account_tx.signature();

H::compute_hash_on_elements(
&signature.0.iter().map(|x| Felt252Wrapper::from(*x).into()).collect::<Vec<FieldElement>>(),
)
} else {
H::compute_hash_on_elements(&[])
}
}
_ => H::compute_hash_on_elements(&[]),
};

H::hash_elements(
Expand Down
8 changes: 6 additions & 2 deletions crates/node/src/chain_spec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ pub fn authority_keys_from_seed(s: &str) -> (AuraId, GrandpaId) {
pub fn development_config(sealing: SealingMode) -> Result<DevChainSpec, String> {
let wasm_binary = WASM_BINARY.ok_or_else(|| "Development wasm not available".to_string())?;
let chain_id = DEV_CHAIN_ID;
let genesis_loader = load_genesis()?;

Ok(DevChainSpec::from_genesis(
// Name
Expand All @@ -68,6 +69,7 @@ pub fn development_config(sealing: SealingMode) -> Result<DevChainSpec, String>
move || {
DevGenesisExt {
genesis_config: testnet_genesis(
genesis_loader.clone(),
wasm_binary,
// Initial PoA authorities
vec![authority_keys_from_seed("Alice")],
Expand All @@ -92,6 +94,7 @@ pub fn development_config(sealing: SealingMode) -> Result<DevChainSpec, String>

pub fn deoxys_config(sealing: SealingMode, chain_id: &str) -> Result<DevChainSpec, String> {
let wasm_binary = WASM_BINARY.ok_or_else(|| "Development wasm not available".to_string())?;
let genesis_loader = load_genesis()?;

Ok(DevChainSpec::from_genesis(
// Name
Expand All @@ -102,6 +105,7 @@ pub fn deoxys_config(sealing: SealingMode, chain_id: &str) -> Result<DevChainSpe
move || {
DevGenesisExt {
genesis_config: testnet_genesis(
genesis_loader.clone(),
wasm_binary,
// Initial PoA authorities
vec![authority_keys_from_seed("Alice")],
Expand Down Expand Up @@ -142,12 +146,12 @@ fn load_genesis() -> Result<GenesisData, String> {

/// Configure initial storage state for FRAME modules.
fn testnet_genesis(
genesis_loader: GenesisData,
wasm_binary: &[u8],
initial_authorities: Vec<(AuraId, GrandpaId)>,
_enable_println: bool,
) -> RuntimeGenesisConfig {
// TODO: ensure this defaulted config isnt disturbing the genesis state
let starknet_genesis_config = GenesisConfig::default();
let starknet_genesis_config = GenesisConfig::from(genesis_loader);

RuntimeGenesisConfig {
system: SystemConfig {
Expand Down
43 changes: 43 additions & 0 deletions crates/pallets/starknet/src/genesis_loader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,49 @@ impl<T: crate::Config> From<GenesisLoader> for GenesisConfig<T> {
}
}

impl<T: crate::Config> From<GenesisData> for GenesisConfig<T> {
fn from(data: GenesisData) -> Self {
let contracts = data
.contracts
.clone()
.into_iter()
.map(|(address, hash)| {
let address = Felt252Wrapper(address.0).into();
let hash = Felt252Wrapper(hash.0).into();
(address, hash)
})
.collect::<Vec<_>>();
let sierra_to_casm_class_hash = data
.sierra_class_hash_to_casm_class_hash
.clone()
.into_iter()
.map(|(sierra_hash, casm_hash)| {
let sierra_hash = Felt252Wrapper(sierra_hash.0).into();
let casm_hash = Felt252Wrapper(casm_hash.0).into();
(sierra_hash, casm_hash)
})
.collect::<Vec<_>>();
let storage = data
.storage
.clone()
.into_iter()
.map(|(contract_address, storage)| {
(
ContractAddress(PatriciaKey(StarkFelt(contract_address.0.to_bytes_be()))),
storage
.into_iter()
.map(|(key, value)| {
(StorageKey(PatriciaKey(StarkFelt(key.0.to_bytes_be()))), StarkFelt(value.0.to_bytes_be()))
})
.collect(),
)
})
.collect();

GenesisConfig { contracts, sierra_to_casm_class_hash, storage, ..Default::default() }
}
}

/// Create a `ContractClass` from a JSON string
///
/// This function takes a JSON string (`json_str`) containing the JSON representation of a
Expand Down
19 changes: 10 additions & 9 deletions crates/primitives/transactions/src/compute_hash.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ use starknet_core::utils::starknet_keccak;
use starknet_crypto::FieldElement;

use super::SIMULATE_TX_VERSION_OFFSET;
use crate::LEGACY_BLOCK_NUMBER;
use crate::{LEGACY_BLOCK_NUMBER, LEGACY_L1_HANDLER_BLOCK};

const DECLARE_PREFIX: &[u8] = b"declare";
const DEPLOY_ACCOUNT_PREFIX: &[u8] = b"deploy_account";
Expand Down Expand Up @@ -495,35 +495,36 @@ impl ComputeTransactionHash for L1HandlerTransaction {
let entrypoint_selector = Felt252Wrapper::from(self.entry_point_selector).into();
let calldata_hash = compute_hash_on_elements(&convert_calldata(self.calldata.clone()));
let nonce = Felt252Wrapper::from(self.nonce).into();
let chain_id = chain_id.into();

if block_number > Some(LEGACY_BLOCK_NUMBER) && block_number.is_some() {
Felt252Wrapper(H::compute_hash_on_elements(&[
if block_number < Some(LEGACY_L1_HANDLER_BLOCK) && block_number.is_some() {
Felt252Wrapper::from(H::compute_hash_on_elements(&[
invoke_prefix,
contract_address,
entrypoint_selector,
calldata_hash,
chain_id.into(),
chain_id,
]))
.into()
} else if block_number < Some(LEGACY_BLOCK_NUMBER) && block_number.is_some() {
Felt252Wrapper(H::compute_hash_on_elements(&[
Felt252Wrapper::from(H::compute_hash_on_elements(&[
prefix,
contract_address,
entrypoint_selector,
calldata_hash,
chain_id.into(),
chain_id,
nonce,
]))
.into()
} else {
Felt252Wrapper(H::compute_hash_on_elements(&[
Felt252Wrapper::from(H::compute_hash_on_elements(&[
prefix,
version,
contract_address,
entrypoint_selector,
calldata_hash,
FieldElement::ZERO, // fees are set to 0 on l1 handlerTx
chain_id.into(),
FieldElement::ZERO, // Fees are set to zero on L1 Handler txs
chain_id,
nonce,
]))
.into()
Expand Down

0 comments on commit 98ed1d0

Please sign in to comment.