Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
0xLucqs committed Jan 29, 2024
1 parent e88a7b6 commit d1e68cb
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 11 deletions.
18 changes: 16 additions & 2 deletions crates/client/commitment-state-diff/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ use blockifier::transaction::objects::TransactionExecutionInfo;
use futures::channel::mpsc;
use futures::Stream;
use indexmap::{IndexMap, IndexSet};
use mc_db::DbError;
use mp_hashers::HasherT;
use mp_storage::{SN_COMPILED_CLASS_HASH_PREFIX, SN_CONTRACT_CLASS_HASH_PREFIX, SN_NONCE_PREFIX, SN_STORAGE_PREFIX};
use mp_transactions::Transaction;
Expand All @@ -22,7 +23,7 @@ use sp_runtime::traits::{Block as BlockT, Header};
use starknet_api::api_core::{ClassHash, CompiledClassHash, ContractAddress, GlobalRoot, Nonce, PatriciaKey};
use starknet_api::block::{BlockHash, BlockHeader, BlockNumber, BlockTimestamp, GasPrice};
use starknet_api::hash::{StarkFelt, StarkHash};
use starknet_api::state::{StorageKey as StarknetStorageKey, ThinStateDiff};
use starknet_api::state::{ContractClass, StorageKey as StarknetStorageKey, ThinStateDiff};
use thiserror::Error;

#[derive(Debug, Encode, Decode, Serialize, Deserialize)]
Expand All @@ -40,6 +41,7 @@ pub struct BlockDAData {
pub config_hash: StarkHash,
pub new_state_root: StarkHash,
pub previous_state_root: StarkHash,
pub sierra_class_hash_to_contract_class: IndexMap<ClassHash, ContractClass>,
}

pub struct CommitmentStateDiffWorker<B: BlockT, C, H> {
Expand Down Expand Up @@ -146,6 +148,10 @@ enum BuildCommitmentStateDiffError {
DigestLogNotFound(#[from] mp_digest_log::FindLogError),
#[error("failed to get config hash")]
FailedToGetConfigHash(#[from] sp_api::ApiError),
#[error("failed to fetch contract class from class hash")]
FailedToFetchContractClass(#[from] DbError),
#[error("contract class not found")]
ContractClassNotFound,
}

fn build_commitment_state_diff<B: BlockT, C, H>(
Expand Down Expand Up @@ -174,7 +180,7 @@ where
deprecated_declared_classes: Vec::new(),
replaced_classes: IndexMap::new(),
};

let mut sierra_class_hash_to_contract_class = IndexMap::new();
for (_prefix, full_storage_key, change) in storage_notification.changes.iter() {
// The storages we are interested in all have prefix of length 32 bytes.
// The pallet identifier takes 16 bytes, the storage one 16 bytes.
Expand Down Expand Up @@ -240,6 +246,13 @@ where
CompiledClassHash(change.map(|data| StarkFelt(data.0.clone().try_into().unwrap())).unwrap_or_default());

commitment_state_diff.declared_classes.insert(class_hash, compiled_class_hash);
sierra_class_hash_to_contract_class.insert(
class_hash,
backend
.sierra_classes()
.get_sierra_class(class_hash.0)?
.ok_or(BuildCommitmentStateDiffError::ContractClassNotFound)?,
);
}
}

Expand All @@ -264,6 +277,7 @@ where
transactions: TransactionsWithExecInfo { transactions: starknet_block.transactions().clone() },
new_state_root: backend.temporary_global_state_root_getter(),
previous_state_root: backend.temporary_global_state_root_getter(),
sierra_class_hash_to_contract_class,
};
file.write_all(&serde_json::to_vec(&da).unwrap()).unwrap();

Expand Down
21 changes: 15 additions & 6 deletions crates/node/src/commands/run.rs
Original file line number Diff line number Diff line change
Expand Up @@ -183,16 +183,25 @@ pub fn run_node(mut cli: Cli) -> Result<()> {
let runner = cli.create_runner(&cli.run.base)?;

let chain_config_dir = cli.run.chain_config_dir()?;
println!("{:?}", cli.run.da_layer);

let da_client = match cli.run.da_layer {
Some(da_layer) => {
let da_conf = cli.run.clone().da_conf.unwrap_or({
let path_da_conf_json = chain_config_dir.join(format!("{da_layer}.json"));
if !path_da_conf_json.exists() {
return Err(sc_cli::Error::Input(format!("no file {da_layer}.json in base_path")));
let da_conf = match cli.run.clone().da_conf {
Some(da_conf) => da_conf,
None => {
let path_da_conf_json = chain_config_dir.join(format!("{}.json", da_layer));
if !path_da_conf_json.exists() {
return Err(sc_cli::Error::Input(format!(
"no file {} in base_path",
path_da_conf_json.to_string_lossy()
)));
}
path_da_conf_json
}
path_da_conf_json
});
};

log::info!("Initializing DA client with layer: {:?}", da_layer);

Some(init_da_client(da_layer, da_conf)?)
}
Expand Down
1 change: 1 addition & 0 deletions crates/pallets/starknet/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ use blockifier::execution::entry_point::{
};
use blockifier::execution::errors::{EntryPointExecutionError, PreExecutionError};
use blockifier::state::cached_state::ContractStorageKey;
use blockifier::transaction::objects::TransactionExecutionInfo;
use blockifier_state_adapter::BlockifierStateAdapter;
use frame_support::pallet_prelude::*;
use frame_support::traits::Time;
Expand Down
6 changes: 3 additions & 3 deletions starknet-rpc-test/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -86,9 +86,9 @@ path = "get_transaction_by_blockid_and_index.rs"
name = "starknet_add_invoke_transaction"
path = "add_invoke_transaction.rs"

# [[test]]
# name = "starknet_add_declare_transaction"
# path = "add_declare_transaction.rs"
[[test]]
name = "starknet_add_declare_transaction"
path = "add_declare_transaction.rs"

[[test]]
name = "starknet_add_deploy_account_transaction"
Expand Down

0 comments on commit d1e68cb

Please sign in to comment.