diff --git a/CHANGELOG.md b/CHANGELOG.md index 477efe0721..51da47bd6a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -37,6 +37,7 @@ as source of data in the client - perf: use perfect hash function in calculate_l1_gas_by_vm_usage - chore: add tests for tx hashing +- split `primitives` crates into multiple smaller crates ## v0.2.0 diff --git a/Cargo.lock b/Cargo.lock index c3506e2041..c12c12bff5 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -6097,8 +6097,10 @@ dependencies = [ "mc-rpc", "mc-storage", "mc-transaction-pool", + "mp-block", "mp-digest-log", - "mp-starknet", + "mp-felt", + "mp-sequencer-address", "pallet-starknet", "sc-cli", "sc-client-api", @@ -6148,7 +6150,9 @@ dependencies = [ "frame-system-benchmarking", "frame-system-rpc-runtime-api", "frame-try-runtime", - "mp-starknet", + "mp-felt", + "mp-hashers", + "mp-transactions", "pallet-aura", "pallet-grandpa", "pallet-starknet", @@ -6264,7 +6268,7 @@ dependencies = [ "lazy_static", "log", "mc-db", - "mp-starknet", + "mp-storage", "reqwest", "sc-client-api", "serde", @@ -6310,7 +6314,8 @@ dependencies = [ "mc-rpc-core", "mc-storage", "mp-digest-log", - "mp-starknet", + "mp-hashers", + "mp-transactions", "pallet-starknet", "sc-client-api", "sp-api", @@ -6333,7 +6338,10 @@ dependencies = [ "mc-rpc-core", "mc-storage", "mc-transaction-pool", - "mp-starknet", + "mp-block", + "mp-felt", + "mp-hashers", + "mp-transactions", "pallet-starknet", "pretty_assertions", "rstest", @@ -6368,8 +6376,8 @@ dependencies = [ "frame-support", "hex", "jsonrpsee 0.16.3", + "mp-block", "mp-digest-log", - "mp-starknet", "num-bigint", "serde", "serde_json", @@ -6391,7 +6399,7 @@ dependencies = [ "frame-support", "frame-system", "madara-runtime", - "mp-starknet", + "mp-storage", "pallet-starknet", "parity-scale-codec", "sc-client-api", @@ -6588,23 +6596,117 @@ dependencies = [ "syn 1.0.109", ] +[[package]] +name = "mp-block" +version = "0.2.0" +dependencies = [ + "blockifier", + "mp-felt", + "mp-hashers", + "mp-transactions", + "parity-scale-codec", + "serde", + "sp-core 7.0.0", + "starknet_api", +] + +[[package]] +name = "mp-commitments" +version = "0.2.0" +dependencies = [ + "bitvec", + "derive_more", + "mp-felt", + "mp-hashers", + "mp-transactions", + "parity-scale-codec", + "scale-info", + "serde", + "starknet-core", + "starknet-crypto 0.6.0", + "starknet-ff", + "starknet_api", +] + [[package]] name = "mp-digest-log" version = "0.2.0" dependencies = [ "assert_matches", - "mp-starknet", + "mp-block", "parity-scale-codec", "sp-runtime 7.0.0", ] [[package]] -name = "mp-starknet" +name = "mp-fee" +version = "0.2.0" +dependencies = [ + "blockifier", + "mp-state", + "phf", + "starknet_api", +] + +[[package]] +name = "mp-felt" +version = "0.2.0" +dependencies = [ + "cairo-vm", + "hex", + "parity-scale-codec", + "scale-info", + "serde", + "sp-core 7.0.0", + "starknet-ff", + "starknet_api", + "thiserror-no-std", +] + +[[package]] +name = "mp-hashers" +version = "0.2.0" +dependencies = [ + "mp-felt", + "parity-scale-codec", + "scale-info", + "serde", + "starknet-core", + "starknet-crypto 0.6.0", +] + +[[package]] +name = "mp-sequencer-address" version = "0.2.0" dependencies = [ - "assert_matches", "async-trait", - "bitvec", + "parity-scale-codec", + "sp-core 7.0.0", + "sp-inherents", + "thiserror-no-std", +] + +[[package]] +name = "mp-state" +version = "0.2.0" +dependencies = [ + "blockifier", + "starknet_api", +] + +[[package]] +name = "mp-storage" +version = "0.2.0" +dependencies = [ + "parity-scale-codec", + "serde", +] + +[[package]] +name = "mp-transactions" +version = "0.2.0" +dependencies = [ + "assert_matches", "blockifier", "cairo-lang-casm-contract-class", "cairo-lang-starknet", @@ -6612,23 +6714,20 @@ dependencies = [ "cairo-vm", "derive_more", "flate2", - "hex", - "lazy_static", - "log", + "mp-fee", + "mp-felt", + "mp-hashers", + "mp-state", "num-bigint", "parity-scale-codec", - "phf", "scale-info", "serde", "serde_json", - "sp-core 7.0.0", - "sp-inherents", - "sp-std 5.0.0", "starknet-core", "starknet-crypto 0.6.0", "starknet-ff", "starknet_api", - "thiserror-no-std", + "thiserror", ] [[package]] @@ -7495,8 +7594,16 @@ dependencies = [ "indexmap 2.0.0-pre", "lazy_static", "log", + "mp-block", + "mp-commitments", "mp-digest-log", - "mp-starknet", + "mp-fee", + "mp-felt", + "mp-hashers", + "mp-sequencer-address", + "mp-state", + "mp-storage", + "mp-transactions", "pallet-timestamp", "parity-scale-codec", "pretty_assertions", @@ -7514,6 +7621,7 @@ dependencies = [ "sp-std 5.0.0", "starknet-core", "starknet-crypto 0.6.0", + "starknet-ff", "starknet_api", "test-case", ] diff --git a/Cargo.toml b/Cargo.toml index 65ae598042..cf1802ae1c 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -4,8 +4,16 @@ members = [ "crates/node", "crates/runtime", "crates/pallets/starknet", - "crates/primitives/starknet", "crates/primitives/digest-log", + "crates/primitives/transactions", + "crates/primitives/felt", + "crates/primitives/hashers", + "crates/primitives/fee", + "crates/primitives/state", + "crates/primitives/block", + "crates/primitives/sequencer-address", + "crates/primitives/storage", + "crates/primitives/commitments", "crates/client/block-proposer", "crates/client/db", "crates/client/rpc-core", @@ -112,8 +120,16 @@ pallet-timestamp = { default-features = false, git = "https://github.com/parityt pallet-starknet = { path = "crates/pallets/starknet", default-features = false } # Madara primtitives -mp-starknet = { path = "crates/primitives/starknet", default-features = false } mp-digest-log = { path = "crates/primitives/digest-log", default-features = false } +mp-block = { path = "crates/primitives/block", default-features = false } +mp-fee = { path = "crates/primitives/fee", default-features = false } +mp-felt = { path = "crates/primitives/felt", default-features = false } +mp-hashers = { path = "crates/primitives/hashers", default-features = false } +mp-sequencer-address = { path = "crates/primitives/sequencer-address", default-features = false } +mp-state = { path = "crates/primitives/state", default-features = false } +mp-storage = { path = "crates/primitives/storage", default-features = false } +mp-transactions = { path = "crates/primitives/transactions", default-features = false } +mp-commitments = { path = "crates/primitives/commitments", default-features = false } # Madara client mc-mapping-sync = { path = "crates/client/mapping-sync" } @@ -186,3 +202,4 @@ async-trait = "0.1.73" indexmap = { git = "https://github.com/bluss/indexmap", rev = "ca5f848e10c31e80aeaad0720d14aa2f6dd6cfb1", default-features = false } num-traits = "0.2.16" num-bigint = "0.4.4" +phf = { version = "0.11", default-features = false } diff --git a/crates/client/data-availability/Cargo.toml b/crates/client/data-availability/Cargo.toml index f5acf0b918..4e001d6e4c 100644 --- a/crates/client/data-availability/Cargo.toml +++ b/crates/client/data-availability/Cargo.toml @@ -41,7 +41,6 @@ sp-runtime = { workspace = true } # Starknet mc-db = { workspace = true, default-features = true } -mp-starknet = { workspace = true, default-features = true } starknet_api = { workspace = true, default-features = true } # Ethereum @@ -55,3 +54,6 @@ subxt = "0.29" # Celestia celestia-rpc = { git = "https://github.com/eigerco/celestia-node-rs", rev = "bd6394b66b11065c543ab3f19fd66000a72b6236" } celestia-types = { git = "https://github.com/eigerco/celestia-node-rs", rev = "bd6394b66b11065c543ab3f19fd66000a72b6236" } + +# Madara +mp-storage = { workspace = true, default-features = true } diff --git a/crates/client/data-availability/src/utils.rs b/crates/client/data-availability/src/utils.rs index 2c4e62c49c..c4b13c70da 100644 --- a/crates/client/data-availability/src/utils.rs +++ b/crates/client/data-availability/src/utils.rs @@ -2,7 +2,7 @@ use std::collections::HashMap; use ethers::types::U256; use lazy_static::lazy_static; -use mp_starknet::storage::{ +use mp_storage::{ PALLET_STARKNET, STARKNET_CONTRACT_CLASS, STARKNET_CONTRACT_CLASS_HASH, STARKNET_NONCE, STARKNET_STORAGE, }; use sp_io::hashing::twox_128; diff --git a/crates/client/mapping-sync/Cargo.toml b/crates/client/mapping-sync/Cargo.toml index d277abe0c0..cdd0b2ea3d 100644 --- a/crates/client/mapping-sync/Cargo.toml +++ b/crates/client/mapping-sync/Cargo.toml @@ -20,7 +20,8 @@ mc-db = { workspace = true } mc-rpc-core = { workspace = true } mc-storage = { workspace = true } mp-digest-log = { workspace = true } -mp-starknet = { workspace = true } +mp-hashers = { workspace = true } +mp-transactions = { workspace = true } pallet-starknet = { workspace = true } sc-client-api = { workspace = true } sp-api = { workspace = true } diff --git a/crates/client/mapping-sync/src/lib.rs b/crates/client/mapping-sync/src/lib.rs index e6148f07b8..870ca1f0e7 100644 --- a/crates/client/mapping-sync/src/lib.rs +++ b/crates/client/mapping-sync/src/lib.rs @@ -19,7 +19,7 @@ use futures::prelude::*; use futures::task::{Context, Poll}; use futures_timer::Delay; use log::debug; -use mp_starknet::traits::hash::HasherT; +use mp_hashers::HasherT; use pallet_starknet::runtime_api::StarknetRuntimeApi; use sc_client_api::backend::{Backend, StorageProvider}; use sc_client_api::client::ImportNotifications; diff --git a/crates/client/mapping-sync/src/sync_blocks.rs b/crates/client/mapping-sync/src/sync_blocks.rs index 198d368c80..f9ddfbbaf5 100644 --- a/crates/client/mapping-sync/src/sync_blocks.rs +++ b/crates/client/mapping-sync/src/sync_blocks.rs @@ -1,7 +1,7 @@ use mc_rpc_core::utils::get_block_by_block_hash; use mp_digest_log::{find_starknet_block, FindLogError}; -use mp_starknet::traits::hash::HasherT; -use mp_starknet::transaction::compute_hash::ComputeTransactionHash; +use mp_hashers::HasherT; +use mp_transactions::compute_hash::ComputeTransactionHash; use pallet_starknet::runtime_api::StarknetRuntimeApi; use sc_client_api::backend::{Backend, StorageProvider}; use sp_api::ProvideRuntimeApi; diff --git a/crates/client/rpc-core/Cargo.toml b/crates/client/rpc-core/Cargo.toml index a1428867ae..ec4186b724 100644 --- a/crates/client/rpc-core/Cargo.toml +++ b/crates/client/rpc-core/Cargo.toml @@ -30,10 +30,8 @@ jsonrpsee = { workspace = true, features = [ "server", "macros", ], default-features = true } +mp-block = { workspace = true } mp-digest-log = { workspace = true } -mp-starknet = { workspace = true, default-features = true, features = [ - "serde", -] } num-bigint = { workspace = true } serde = { workspace = true, default-features = true } serde_json = { workspace = true } diff --git a/crates/client/rpc-core/src/utils.rs b/crates/client/rpc-core/src/utils.rs index 5b45f68eb7..9e27dea176 100644 --- a/crates/client/rpc-core/src/utils.rs +++ b/crates/client/rpc-core/src/utils.rs @@ -10,8 +10,8 @@ use cairo_lang_starknet::contract_class::{ }; use cairo_lang_starknet::contract_class_into_casm_contract_class::StarknetSierraCompilationError; use cairo_lang_utils::bigint::BigUintAsHex; +use mp_block::Block as StarknetBlock; use mp_digest_log::find_starknet_block; -use mp_starknet::block::Block as StarknetBlock; use num_bigint::{BigInt, BigUint, Sign}; use sp_api::{BlockT, HeaderT}; use sp_blockchain::HeaderBackend; diff --git a/crates/client/rpc/Cargo.toml b/crates/client/rpc/Cargo.toml index 645b254b5d..9abfca0a5a 100644 --- a/crates/client/rpc/Cargo.toml +++ b/crates/client/rpc/Cargo.toml @@ -23,7 +23,6 @@ mc-db = { workspace = true } mc-rpc-core = { workspace = true } mc-storage = { workspace = true } mc-transaction-pool = { workspace = true } -mp-starknet = { workspace = true, features = ["client"] } # Substate primitives frame-support = { workspace = true } frame-system = { workspace = true } @@ -50,6 +49,10 @@ jsonrpsee = { workspace = true, default-features = true, features = [ "macros", ] } log = { workspace = true, default-features = true } +mp-block = { workspace = true } +mp-felt = { workspace = true } +mp-hashers = { workspace = true } +mp-transactions = { workspace = true, features = ["client"] } serde_json = { workspace = true, default-features = true } thiserror = { workspace = true } diff --git a/crates/client/rpc/src/events/mod.rs b/crates/client/rpc/src/events/mod.rs index 581781def9..c9c58411fb 100644 --- a/crates/client/rpc/src/events/mod.rs +++ b/crates/client/rpc/src/events/mod.rs @@ -8,9 +8,8 @@ use jsonrpsee::core::RpcResult; use log::error; use mc_rpc_core::utils::get_block_by_block_hash; use mc_transaction_pool::ChainApi; -use mp_starknet::execution::types::Felt252Wrapper; -use mp_starknet::traits::hash::HasherT; -use mp_starknet::traits::SendSyncStatic; +use mp_felt::Felt252Wrapper; +use mp_hashers::HasherT; use pallet_starknet::runtime_api::{ConvertTransactionRuntimeApi, StarknetRuntimeApi}; use sc_client_api::backend::{Backend, StorageProvider}; use sc_client_api::BlockBackend; @@ -31,7 +30,7 @@ where C: ProvideRuntimeApi, C::Api: StarknetRuntimeApi + ConvertTransactionRuntimeApi, BE: Backend, - H: HasherT + SendSyncStatic, + H: HasherT + Send + Sync + 'static, { /// Helper function to get Starknet block details /// diff --git a/crates/client/rpc/src/events/tests.rs b/crates/client/rpc/src/events/tests.rs index ad7b015a24..330282f05d 100644 --- a/crates/client/rpc/src/events/tests.rs +++ b/crates/client/rpc/src/events/tests.rs @@ -1,6 +1,6 @@ use std::iter::zip; -use mp_starknet::execution::types::Felt252Wrapper; +use mp_felt::Felt252Wrapper; use rstest::*; use starknet_core::types::EmittedEvent; use starknet_ff::FieldElement; diff --git a/crates/client/rpc/src/lib.rs b/crates/client/rpc/src/lib.rs index 0c9805964a..664680a705 100644 --- a/crates/client/rpc/src/lib.rs +++ b/crates/client/rpc/src/lib.rs @@ -19,12 +19,11 @@ use mc_rpc_core::Felt; pub use mc_rpc_core::StarknetRpcApiServer; use mc_storage::OverrideHandle; use mc_transaction_pool::{ChainApi, Pool}; -use mp_starknet::execution::types::Felt252Wrapper; -use mp_starknet::traits::hash::HasherT; -use mp_starknet::traits::SendSyncStatic; -use mp_starknet::transaction::compute_hash::ComputeTransactionHash; -use mp_starknet::transaction::to_starknet_core_transaction::to_starknet_core_tx; -use mp_starknet::transaction::UserTransaction; +use mp_felt::Felt252Wrapper; +use mp_hashers::HasherT; +use mp_transactions::compute_hash::ComputeTransactionHash; +use mp_transactions::to_starknet_core_transaction::to_starknet_core_tx; +use mp_transactions::UserTransaction; use pallet_starknet::runtime_api::{ConvertTransactionRuntimeApi, StarknetRuntimeApi}; use sc_client_api::backend::{Backend, StorageProvider}; use sc_client_api::BlockBackend; @@ -108,7 +107,7 @@ where C: ProvideRuntimeApi, C::Api: StarknetRuntimeApi + ConvertTransactionRuntimeApi, BE: Backend, - H: HasherT + SendSyncStatic, + H: HasherT + Send + Sync + 'static, { pub fn current_block_hash(&self) -> Result { let substrate_block_hash = self.client.info().best_hash; @@ -174,7 +173,7 @@ where C: HeaderBackend + BlockBackend + StorageProvider + 'static, C: ProvideRuntimeApi, C::Api: StarknetRuntimeApi + ConvertTransactionRuntimeApi, - H: HasherT + SendSyncStatic, + H: HasherT + Send + Sync + 'static, { fn block_number(&self) -> RpcResult { self.current_block_number() @@ -849,7 +848,7 @@ where } }; - let block: mp_starknet::block::Block = + let block: mp_block::Block = get_block_by_block_hash(self.client.as_ref(), substrate_block_hash).unwrap_or_default(); let block_header = block.header(); let block_hash = block_header.hash::().into(); @@ -901,7 +900,7 @@ where } let receipt = match tx_type { - mp_starknet::transaction::TxType::Declare => TransactionReceipt::Declare(DeclareTransactionReceipt { + mp_transactions::TxType::Declare => TransactionReceipt::Declare(DeclareTransactionReceipt { transaction_hash, actual_fee: Default::default(), finality_status: TransactionFinalityStatus::AcceptedOnL2, @@ -911,7 +910,7 @@ where events: events.into_iter().map(event_conversion).collect(), execution_result, }), - mp_starknet::transaction::TxType::DeployAccount => { + mp_transactions::TxType::DeployAccount => { TransactionReceipt::DeployAccount(DeployAccountTransactionReceipt { transaction_hash, actual_fee: Default::default(), @@ -924,7 +923,7 @@ where execution_result, }) } - mp_starknet::transaction::TxType::Invoke => TransactionReceipt::Invoke(InvokeTransactionReceipt { + mp_transactions::TxType::Invoke => TransactionReceipt::Invoke(InvokeTransactionReceipt { transaction_hash, actual_fee: Default::default(), finality_status: TransactionFinalityStatus::AcceptedOnL2, @@ -934,7 +933,7 @@ where events: events.into_iter().map(event_conversion).collect(), execution_result, }), - mp_starknet::transaction::TxType::L1Handler => TransactionReceipt::L1Handler(L1HandlerTransactionReceipt { + mp_transactions::TxType::L1Handler => TransactionReceipt::L1Handler(L1HandlerTransactionReceipt { transaction_hash, actual_fee: Default::default(), finality_status: TransactionFinalityStatus::AcceptedOnL2, diff --git a/crates/client/rpc/src/madara_backend_client.rs b/crates/client/rpc/src/madara_backend_client.rs index 1061aa816c..859f9f26cd 100644 --- a/crates/client/rpc/src/madara_backend_client.rs +++ b/crates/client/rpc/src/madara_backend_client.rs @@ -1,5 +1,5 @@ use mc_rpc_core::utils::get_block_by_block_hash; -use mp_starknet::block::Block; +use mp_block::Block; use sc_client_api::backend::{Backend, StorageProvider}; use sp_api::BlockId; use sp_blockchain::HeaderBackend; diff --git a/crates/client/rpc/src/types.rs b/crates/client/rpc/src/types.rs index db45942a34..c098eb4360 100644 --- a/crates/client/rpc/src/types.rs +++ b/crates/client/rpc/src/types.rs @@ -1,7 +1,7 @@ use std::num::ParseIntError; use std::{fmt, u64}; -use mp_starknet::execution::types::Felt252Wrapper; +use mp_felt::Felt252Wrapper; use starknet_ff::FieldElement; pub struct RpcEventFilter { diff --git a/crates/client/storage/Cargo.toml b/crates/client/storage/Cargo.toml index 251baff145..9a6037b197 100644 --- a/crates/client/storage/Cargo.toml +++ b/crates/client/storage/Cargo.toml @@ -18,7 +18,7 @@ blockifier = { workspace = true, features = ["std"] } frame-support = { workspace = true, features = ["std"] } frame-system = { workspace = true, features = ["std"] } madara-runtime = { workspace = true, features = ["std"] } -mp-starknet = { workspace = true, features = ["std"] } +mp-storage = { workspace = true, features = ["std"] } pallet-starknet = { workspace = true, features = ["std"] } sc-client-api = { workspace = true } scale-codec = { workspace = true, features = ["std"] } diff --git a/crates/client/storage/src/lib.rs b/crates/client/storage/src/lib.rs index 9eb297e601..33ceaa1d98 100644 --- a/crates/client/storage/src/lib.rs +++ b/crates/client/storage/src/lib.rs @@ -11,7 +11,7 @@ mod overrides; use std::collections::BTreeMap; use std::sync::Arc; -use mp_starknet::storage::{StarknetStorageSchemaVersion, PALLET_STARKNET_SCHEMA}; +use mp_storage::{StarknetStorageSchemaVersion, PALLET_STARKNET_SCHEMA}; pub use overrides::*; use pallet_starknet::runtime_api::StarknetRuntimeApi; use sc_client_api::backend::{Backend, StorageProvider}; diff --git a/crates/client/storage/src/overrides/mod.rs b/crates/client/storage/src/overrides/mod.rs index 5e68159684..5c4ab67860 100644 --- a/crates/client/storage/src/overrides/mod.rs +++ b/crates/client/storage/src/overrides/mod.rs @@ -4,7 +4,7 @@ use std::sync::Arc; use blockifier::execution::contract_class::ContractClass; use frame_support::{Identity, StorageHasher}; -use mp_starknet::storage::StarknetStorageSchemaVersion; +use mp_storage::StarknetStorageSchemaVersion; use pallet_starknet::runtime_api::StarknetRuntimeApi; use sc_client_api::{Backend, HeaderBackend, StorageProvider}; use sp_api::ProvideRuntimeApi; diff --git a/crates/client/storage/src/overrides/schema_v1_override.rs b/crates/client/storage/src/overrides/schema_v1_override.rs index 3d82f25222..fd101831c9 100644 --- a/crates/client/storage/src/overrides/schema_v1_override.rs +++ b/crates/client/storage/src/overrides/schema_v1_override.rs @@ -2,7 +2,7 @@ use std::marker::PhantomData; use std::sync::Arc; use blockifier::execution::contract_class::ContractClass; -use mp_starknet::storage::{ +use mp_storage::{ PALLET_STARKNET, STARKNET_CONTRACT_CLASS, STARKNET_CONTRACT_CLASS_HASH, STARKNET_NONCE, STARKNET_STORAGE, }; // Substrate diff --git a/crates/node/Cargo.toml b/crates/node/Cargo.toml index d90207e519..018484f4ec 100644 --- a/crates/node/Cargo.toml +++ b/crates/node/Cargo.toml @@ -81,11 +81,15 @@ mc-mapping-sync = { workspace = true } mc-rpc = { workspace = true } mc-storage = { workspace = true } mc-transaction-pool = { workspace = true } -mp-digest-log = { workspace = true } -mp-starknet = { workspace = true } pallet-starknet = { workspace = true } starknet-core = { workspace = true } +# Primitives +mp-block = { workspace = true } +mp-digest-log = { workspace = true } +mp-felt = { workspace = true } +mp-sequencer-address = { workspace = true, features = ["client"] } + # CLI-specific dependencies try-runtime-cli = { optional = true, git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.43" } diff --git a/crates/node/src/chain_spec.rs b/crates/node/src/chain_spec.rs index 7c2f45ec09..6bdd48558d 100644 --- a/crates/node/src/chain_spec.rs +++ b/crates/node/src/chain_spec.rs @@ -1,7 +1,7 @@ use std::path::PathBuf; use madara_runtime::{AuraConfig, EnableManualSeal, GenesisConfig, GrandpaConfig, SystemConfig, WASM_BINARY}; -use mp_starknet::execution::types::Felt252Wrapper; +use mp_felt::Felt252Wrapper; use pallet_starknet::genesis_loader::{GenesisLoader, HexFelt}; use pallet_starknet::utils; use sc_service::ChainType; diff --git a/crates/node/src/genesis_block.rs b/crates/node/src/genesis_block.rs index 5ebbf1d80f..4e2207e928 100644 --- a/crates/node/src/genesis_block.rs +++ b/crates/node/src/genesis_block.rs @@ -1,8 +1,8 @@ use std::marker::PhantomData; use std::sync::Arc; +use mp_block::Block as StarknetBlock; use mp_digest_log::{Log, MADARA_ENGINE_ID}; -use mp_starknet::block::Block as StarknetBlock; use sc_client_api::backend::Backend; use sc_client_api::BlockImportOperation; use sc_executor::RuntimeVersionOf; diff --git a/crates/node/src/service.rs b/crates/node/src/service.rs index ad1c4627b2..8bffe87571 100644 --- a/crates/node/src/service.rs +++ b/crates/node/src/service.rs @@ -22,7 +22,7 @@ use mc_data_availability::{DaClient, DaLayer, DataAvailabilityWorker}; use mc_mapping_sync::MappingSyncWorker; use mc_storage::overrides_handle; use mc_transaction_pool::FullPool; -use mp_starknet::sequencer_address::{ +use mp_sequencer_address::{ InherentDataProvider as SeqAddrInherentDataProvider, DEFAULT_SEQUENCER_ADDRESS, SEQ_ADDR_STORAGE_KEY, }; use prometheus_endpoint::Registry; diff --git a/crates/pallets/starknet/Cargo.toml b/crates/pallets/starknet/Cargo.toml index 3e7f7d30de..216cd0a652 100644 --- a/crates/pallets/starknet/Cargo.toml +++ b/crates/pallets/starknet/Cargo.toml @@ -15,12 +15,16 @@ targets = ["x86_64-unknown-linux-gnu"] [dependencies] # Madara primitives +mp-block = { workspace = true } +mp-commitments = { workspace = true } mp-digest-log = { workspace = true } -mp-starknet = { workspace = true, features = [ - "parity-scale-codec", - "scale-info", - "serde", -] } +mp-fee = { workspace = true } +mp-felt = { workspace = true, features = ["parity-scale-codec", "serde"] } +mp-hashers = { workspace = true } +mp-sequencer-address = { workspace = true, features = ["parity-scale-codec"] } +mp-state = { workspace = true } +mp-storage = { workspace = true, features = ["parity-scale-codec"] } +mp-transactions = { workspace = true, features = ["scale-info"] } blockifier = { workspace = true, features = [ "testing", @@ -73,6 +77,7 @@ test-case = "3.1.0" lazy_static = "1.4.0" hexlit = "0.5.5" assert_matches = "1.5.0" +starknet-ff = { workspace = true } [features] default = ["std"] @@ -87,8 +92,6 @@ std = [ "scale-info/std", "pallet-timestamp/std", "sp-inherents/std", - # Madara - "mp-starknet/std", # Starknet "starknet-crypto/std", "blockifier/std", diff --git a/crates/pallets/starknet/src/blockifier_state_adapter.rs b/crates/pallets/starknet/src/blockifier_state_adapter.rs index c4bacd26af..22e28a1cf7 100644 --- a/crates/pallets/starknet/src/blockifier_state_adapter.rs +++ b/crates/pallets/starknet/src/blockifier_state_adapter.rs @@ -6,8 +6,8 @@ use blockifier::state::cached_state::{CommitmentStateDiff, ContractStorageKey}; use blockifier::state::errors::StateError; use blockifier::state::state_api::{State, StateReader, StateResult}; use indexmap::IndexMap; -use mp_starknet::execution::types::Felt252Wrapper; -use mp_starknet::state::{FeeConfig, StateChanges}; +use mp_felt::Felt252Wrapper; +use mp_state::{FeeConfig, StateChanges}; use sp_core::Get; use starknet_api::api_core::{ClassHash, CompiledClassHash, ContractAddress, Nonce}; use starknet_api::hash::StarkFelt; diff --git a/crates/pallets/starknet/src/genesis_loader.rs b/crates/pallets/starknet/src/genesis_loader.rs index c9a7ad1abc..4f6e9defed 100644 --- a/crates/pallets/starknet/src/genesis_loader.rs +++ b/crates/pallets/starknet/src/genesis_loader.rs @@ -3,7 +3,7 @@ use std::string::String; use std::vec::Vec; use blockifier::execution::contract_class::ContractClass as StarknetContractClass; -use mp_starknet::execution::types::Felt252Wrapper; +use mp_felt::Felt252Wrapper; use serde::{Deserialize, Serialize}; use serde_with::serde_as; use starknet_core::serde::unsigned_field_element::UfeHex; diff --git a/crates/pallets/starknet/src/lib.rs b/crates/pallets/starknet/src/lib.rs index e8be2d5c9b..aa1ff8ea6a 100644 --- a/crates/pallets/starknet/src/lib.rs +++ b/crates/pallets/starknet/src/lib.rs @@ -33,7 +33,6 @@ #![cfg_attr(not(feature = "std"), no_std)] #![allow(clippy::large_enum_variant)] -use mp_starknet::constants::INITIAL_GAS; /// Starknet pallet. /// Definition of the pallet's runtime storage items, events, errors, and dispatchable /// functions. @@ -83,16 +82,16 @@ use blockifier_state_adapter::BlockifierStateAdapter; use frame_support::pallet_prelude::*; use frame_support::traits::Time; use frame_system::pallet_prelude::*; +use mp_block::{Block as StarknetBlock, Header as StarknetHeader}; use mp_digest_log::MADARA_ENGINE_ID; -use mp_starknet::block::{Block as StarknetBlock, Header as StarknetHeader}; -use mp_starknet::crypto::commitment; -use mp_starknet::execution::types::Felt252Wrapper; -use mp_starknet::sequencer_address::{InherentError, InherentType, DEFAULT_SEQUENCER_ADDRESS, INHERENT_IDENTIFIER}; -use mp_starknet::state::{FeeConfig, StateChanges}; -use mp_starknet::storage::{StarknetStorageSchemaVersion, PALLET_STARKNET_SCHEMA}; -use mp_starknet::traits::hash::HasherT; -use mp_starknet::transaction::execution::{Execute, Validate}; -use mp_starknet::transaction::{ +use mp_fee::INITIAL_GAS; +use mp_felt::Felt252Wrapper; +use mp_hashers::HasherT; +use mp_sequencer_address::{InherentError, InherentType, DEFAULT_SEQUENCER_ADDRESS, INHERENT_IDENTIFIER}; +use mp_state::{FeeConfig, StateChanges}; +use mp_storage::{StarknetStorageSchemaVersion, PALLET_STARKNET_SCHEMA}; +use mp_transactions::execution::{Execute, Validate}; +use mp_transactions::{ DeclareTransaction, DeployAccountTransaction, HandleL1MessageTransaction, InvokeTransaction, Transaction, UserAndL1HandlerTransaction, UserTransaction, }; @@ -989,7 +988,7 @@ impl Pallet { let chain_id = Self::chain_id(); let (transaction_commitment, event_commitment) = - commitment::calculate_commitments::(&transactions, &events, chain_id); + mp_commitments::calculate_commitments::(&transactions, &events, chain_id); let protocol_version = T::ProtocolVersion::get(); let extra_data = None; diff --git a/crates/pallets/starknet/src/message.rs b/crates/pallets/starknet/src/message.rs index 3c26d06f51..0e1ec357dc 100644 --- a/crates/pallets/starknet/src/message.rs +++ b/crates/pallets/starknet/src/message.rs @@ -1,5 +1,5 @@ -use mp_starknet::execution::types::Felt252Wrapper; -use mp_starknet::transaction::HandleL1MessageTransaction; +use mp_felt::Felt252Wrapper; +use mp_transactions::HandleL1MessageTransaction; use parity_scale_codec::{Decode, Encode}; use serde::Deserialize; diff --git a/crates/pallets/starknet/src/runtime_api.rs b/crates/pallets/starknet/src/runtime_api.rs index 217b677bcc..19d9fa1c38 100644 --- a/crates/pallets/starknet/src/runtime_api.rs +++ b/crates/pallets/starknet/src/runtime_api.rs @@ -6,8 +6,8 @@ #![allow(clippy::extra_unused_type_parameters)] use blockifier::execution::contract_class::ContractClass; -use mp_starknet::execution::types::Felt252Wrapper; -use mp_starknet::transaction::{Transaction, TxType, UserTransaction}; +use mp_felt::Felt252Wrapper; +use mp_transactions::{Transaction, TxType, UserTransaction}; use sp_api::BlockT; pub extern crate alloc; use alloc::vec::Vec; diff --git a/crates/pallets/starknet/src/tests/account_helper.rs b/crates/pallets/starknet/src/tests/account_helper.rs index 5749db062d..2e032c1054 100644 --- a/crates/pallets/starknet/src/tests/account_helper.rs +++ b/crates/pallets/starknet/src/tests/account_helper.rs @@ -1,4 +1,4 @@ -use mp_starknet::execution::types::Felt252Wrapper; +use mp_felt::Felt252Wrapper; use super::mock::AccountType; use crate::tests::mock::{get_account_address, AccountTypeV0Inner}; diff --git a/crates/pallets/starknet/src/tests/block.rs b/crates/pallets/starknet/src/tests/block.rs index 93d9d30a73..3c47519814 100644 --- a/crates/pallets/starknet/src/tests/block.rs +++ b/crates/pallets/starknet/src/tests/block.rs @@ -3,8 +3,8 @@ use std::collections::HashMap; use frame_support::assert_ok; use mp_digest_log::{ensure_log, find_starknet_block}; -use mp_starknet::execution::types::Felt252Wrapper; -use mp_starknet::sequencer_address::DEFAULT_SEQUENCER_ADDRESS; +use mp_felt::Felt252Wrapper; +use mp_sequencer_address::DEFAULT_SEQUENCER_ADDRESS; use starknet_api::api_core::{ChainId, ContractAddress, PatriciaKey}; use starknet_api::block::{BlockNumber, BlockTimestamp}; use starknet_api::hash::StarkFelt; diff --git a/crates/pallets/starknet/src/tests/call_contract.rs b/crates/pallets/starknet/src/tests/call_contract.rs index f008e67713..13248853fd 100644 --- a/crates/pallets/starknet/src/tests/call_contract.rs +++ b/crates/pallets/starknet/src/tests/call_contract.rs @@ -1,6 +1,6 @@ use frame_support::assert_ok; -use mp_starknet::execution::types::Felt252Wrapper; -use mp_starknet::transaction::InvokeTransactionV1; +use mp_felt::Felt252Wrapper; +use mp_transactions::InvokeTransactionV1; use starknet_api::api_core::{ContractAddress, EntryPointSelector, PatriciaKey}; use starknet_api::hash::StarkFelt; use starknet_api::transaction::Calldata; diff --git a/crates/pallets/starknet/src/tests/constants.rs b/crates/pallets/starknet/src/tests/constants.rs index a121096903..069d3d998d 100644 --- a/crates/pallets/starknet/src/tests/constants.rs +++ b/crates/pallets/starknet/src/tests/constants.rs @@ -1,5 +1,5 @@ use lazy_static::lazy_static; -use mp_starknet::execution::types::Felt252Wrapper; +use mp_felt::Felt252Wrapper; pub const ACCOUNT_PRIVATE_KEY: &str = "0x00c1cf1490de1352865301bb8705143f3ef938f97fdf892f1090dcb5ac7bcd1d"; pub const ACCOUNT_PUBLIC_KEY: &str = "0x03603a2692a2ae60abb343e832ee53b55d6b25f02a3ef1565ec691edc7a209b2"; diff --git a/crates/pallets/starknet/src/tests/declare_tx.rs b/crates/pallets/starknet/src/tests/declare_tx.rs index 0b8d88adbb..c2b2a994b3 100644 --- a/crates/pallets/starknet/src/tests/declare_tx.rs +++ b/crates/pallets/starknet/src/tests/declare_tx.rs @@ -1,8 +1,8 @@ use assert_matches::assert_matches; use frame_support::{assert_err, assert_ok}; -use mp_starknet::execution::types::Felt252Wrapper; -use mp_starknet::transaction::compute_hash::ComputeTransactionHash; -use mp_starknet::transaction::{DeclareTransactionV1, DeclareTransactionV2}; +use mp_felt::Felt252Wrapper; +use mp_transactions::compute_hash::ComputeTransactionHash; +use mp_transactions::{DeclareTransactionV1, DeclareTransactionV2}; use sp_runtime::traits::ValidateUnsigned; use sp_runtime::transaction_validity::{TransactionSource, TransactionValidityError, ValidTransaction}; use starknet_api::api_core::ClassHash; diff --git a/crates/pallets/starknet/src/tests/deploy_account_tx.rs b/crates/pallets/starknet/src/tests/deploy_account_tx.rs index 9f0645aa23..9397f58980 100644 --- a/crates/pallets/starknet/src/tests/deploy_account_tx.rs +++ b/crates/pallets/starknet/src/tests/deploy_account_tx.rs @@ -1,7 +1,7 @@ use frame_support::{assert_err, assert_ok}; -use mp_starknet::execution::types::Felt252Wrapper; -use mp_starknet::transaction::compute_hash::ComputeTransactionHash; -use mp_starknet::transaction::DeployAccountTransaction; +use mp_felt::Felt252Wrapper; +use mp_transactions::compute_hash::ComputeTransactionHash; +use mp_transactions::DeployAccountTransaction; use sp_runtime::traits::ValidateUnsigned; use sp_runtime::transaction_validity::TransactionSource; use starknet_api::api_core::ContractAddress; diff --git a/crates/pallets/starknet/src/tests/erc20.rs b/crates/pallets/starknet/src/tests/erc20.rs index deb6ced0fb..afbcd02e06 100644 --- a/crates/pallets/starknet/src/tests/erc20.rs +++ b/crates/pallets/starknet/src/tests/erc20.rs @@ -1,8 +1,8 @@ use blockifier::execution::contract_class::ContractClass; use frame_support::assert_ok; use lazy_static::lazy_static; -use mp_starknet::execution::types::Felt252Wrapper; -use mp_starknet::transaction::InvokeTransactionV1; +use mp_felt::Felt252Wrapper; +use mp_transactions::InvokeTransactionV1; use starknet_api::api_core::{ContractAddress, PatriciaKey}; use starknet_api::hash::StarkFelt; use starknet_api::state::StorageKey; diff --git a/crates/pallets/starknet/src/tests/events.rs b/crates/pallets/starknet/src/tests/events.rs index 3c6f0a9c80..f6a9d6bcd9 100644 --- a/crates/pallets/starknet/src/tests/events.rs +++ b/crates/pallets/starknet/src/tests/events.rs @@ -1,6 +1,6 @@ -use mp_starknet::execution::types::Felt252Wrapper; -use mp_starknet::transaction::compute_hash::ComputeTransactionHash; -use mp_starknet::transaction::InvokeTransactionV1; +use mp_felt::Felt252Wrapper; +use mp_transactions::compute_hash::ComputeTransactionHash; +use mp_transactions::InvokeTransactionV1; use starknet_api::transaction::TransactionHash; use starknet_core::utils::get_selector_from_name; diff --git a/crates/pallets/starknet/src/tests/fees_disabled.rs b/crates/pallets/starknet/src/tests/fees_disabled.rs index 50fbebb9c7..b0f134c0a0 100644 --- a/crates/pallets/starknet/src/tests/fees_disabled.rs +++ b/crates/pallets/starknet/src/tests/fees_disabled.rs @@ -1,6 +1,6 @@ use frame_support::assert_ok; -use mp_starknet::execution::types::Felt252Wrapper; -use mp_starknet::transaction::InvokeTransaction; +use mp_felt::Felt252Wrapper; +use mp_transactions::InvokeTransaction; use starknet_api::api_core::{ContractAddress, EntryPointSelector, PatriciaKey}; use starknet_api::hash::StarkFelt; use starknet_api::transaction::Calldata; diff --git a/crates/pallets/starknet/src/tests/invoke_tx.rs b/crates/pallets/starknet/src/tests/invoke_tx.rs index a8a179121a..1fd32d8f99 100644 --- a/crates/pallets/starknet/src/tests/invoke_tx.rs +++ b/crates/pallets/starknet/src/tests/invoke_tx.rs @@ -1,8 +1,8 @@ use blockifier::abi::abi_utils::get_storage_var_address; use frame_support::{assert_err, assert_ok}; -use mp_starknet::execution::types::Felt252Wrapper; -use mp_starknet::transaction::compute_hash::ComputeTransactionHash; -use mp_starknet::transaction::{InvokeTransaction, InvokeTransactionV1}; +use mp_felt::Felt252Wrapper; +use mp_transactions::compute_hash::ComputeTransactionHash; +use mp_transactions::{InvokeTransaction, InvokeTransactionV1}; use pretty_assertions::assert_eq; use sp_runtime::traits::ValidateUnsigned; use sp_runtime::transaction_validity::{TransactionSource, TransactionValidityError, ValidTransaction}; diff --git a/crates/pallets/starknet/src/tests/l1_message.rs b/crates/pallets/starknet/src/tests/l1_message.rs index 0957852382..a048f48995 100644 --- a/crates/pallets/starknet/src/tests/l1_message.rs +++ b/crates/pallets/starknet/src/tests/l1_message.rs @@ -1,6 +1,6 @@ use frame_support::assert_err; -use mp_starknet::execution::types::Felt252Wrapper; -use mp_starknet::transaction::{DeclareTransactionV1, HandleL1MessageTransaction}; +use mp_felt::Felt252Wrapper; +use mp_transactions::{DeclareTransactionV1, HandleL1MessageTransaction}; use sp_runtime::traits::ValidateUnsigned; use sp_runtime::transaction_validity::TransactionSource; use starknet_api::transaction::Fee; diff --git a/crates/pallets/starknet/src/tests/mock/helpers.rs b/crates/pallets/starknet/src/tests/mock/helpers.rs index 65807e3957..414c4f8be8 100644 --- a/crates/pallets/starknet/src/tests/mock/helpers.rs +++ b/crates/pallets/starknet/src/tests/mock/helpers.rs @@ -1,7 +1,7 @@ use alloc::sync::Arc; -use mp_starknet::execution::types::Felt252Wrapper; -use mp_starknet::transaction::DeployAccountTransaction; +use mp_felt::Felt252Wrapper; +use mp_transactions::DeployAccountTransaction; use sp_core::H256; use starknet_api::api_core::{ClassHash, ContractAddress, PatriciaKey}; use starknet_api::hash::StarkFelt; diff --git a/crates/pallets/starknet/src/tests/mock/setup_mock.rs b/crates/pallets/starknet/src/tests/mock/setup_mock.rs index b790ccce5a..23148b535e 100644 --- a/crates/pallets/starknet/src/tests/mock/setup_mock.rs +++ b/crates/pallets/starknet/src/tests/mock/setup_mock.rs @@ -1,8 +1,17 @@ use frame_support::traits::GenesisBuild; +use mp_felt::Felt252Wrapper; use crate::genesis_loader::GenesisLoader; use crate::{Config, GenesisConfig}; +/// ChainId for Starknet Goerli testnet +pub const SN_GOERLI_CHAIN_ID: Felt252Wrapper = Felt252Wrapper(starknet_ff::FieldElement::from_mont([ + 3753493103916128178, + 18446744073709548950, + 18446744073709551615, + 398700013197595345, +])); + // Configure a mock runtime to test the pallet. macro_rules! mock_runtime { ($mock_runtime:ident, $disable_transaction_fee:expr, $disable_nonce_validation: expr) => { @@ -15,9 +24,8 @@ macro_rules! mock_runtime { use {crate as pallet_starknet, frame_system as system}; use crate::{ SeqAddrUpdate, SequencerAddress}; use frame_support::traits::Hooks; - use mp_starknet::sequencer_address::DEFAULT_SEQUENCER_ADDRESS; - use mp_starknet::execution::types::Felt252Wrapper; - use mp_starknet::constants::SN_GOERLI_CHAIN_ID; + use mp_sequencer_address::DEFAULT_SEQUENCER_ADDRESS; + use mp_felt::Felt252Wrapper; use starknet_api::api_core::{PatriciaKey, ContractAddress}; use starknet_api::hash::StarkFelt; @@ -79,13 +87,13 @@ macro_rules! mock_runtime { pub const DisableTransactionFee: bool = $disable_transaction_fee; pub const DisableNonceValidation: bool = $disable_nonce_validation; pub const ProtocolVersion: u8 = 0; - pub const ChainId: Felt252Wrapper = SN_GOERLI_CHAIN_ID; + pub const ChainId: Felt252Wrapper = crate::tests::mock::SN_GOERLI_CHAIN_ID; pub const MaxRecursionDepth: u32 = 50; } impl pallet_starknet::Config for MockRuntime { type RuntimeEvent = RuntimeEvent; - type SystemHash = mp_starknet::crypto::hash::pedersen::PedersenHasher; + type SystemHash = mp_hashers::pedersen::PedersenHasher; type TimestampProvider = Timestamp; type UnsignedPriority = UnsignedPriority; type TransactionLongevity = TransactionLongevity; diff --git a/crates/pallets/starknet/src/tests/mod.rs b/crates/pallets/starknet/src/tests/mod.rs index a5a22e37bf..72a6203f50 100644 --- a/crates/pallets/starknet/src/tests/mod.rs +++ b/crates/pallets/starknet/src/tests/mod.rs @@ -1,10 +1,8 @@ use blockifier::abi::abi_utils::get_erc20_balance_var_addresses; use blockifier::state::state_api::State; -use mp_starknet::execution::types::Felt252Wrapper; -use mp_starknet::transaction::compute_hash::ComputeTransactionHash; -use mp_starknet::transaction::{ - DeclareTransaction, DeclareTransactionV1, DeployAccountTransaction, InvokeTransactionV1, -}; +use mp_felt::Felt252Wrapper; +use mp_transactions::compute_hash::ComputeTransactionHash; +use mp_transactions::{DeclareTransaction, DeclareTransactionV1, DeployAccountTransaction, InvokeTransactionV1}; use starknet_api::api_core::ContractAddress; use starknet_api::hash::StarkFelt; diff --git a/crates/pallets/starknet/src/tests/no_nonce_validation.rs b/crates/pallets/starknet/src/tests/no_nonce_validation.rs index c5d415e04d..52284fcb60 100644 --- a/crates/pallets/starknet/src/tests/no_nonce_validation.rs +++ b/crates/pallets/starknet/src/tests/no_nonce_validation.rs @@ -1,5 +1,5 @@ use frame_support::assert_ok; -use mp_starknet::execution::types::Felt252Wrapper; +use mp_felt::Felt252Wrapper; use starknet_api::api_core::{ClassHash, ContractAddress, Nonce}; use starknet_api::hash::StarkFelt; diff --git a/crates/pallets/starknet/src/tests/query_tx.rs b/crates/pallets/starknet/src/tests/query_tx.rs index 3119384d38..15a602dedb 100644 --- a/crates/pallets/starknet/src/tests/query_tx.rs +++ b/crates/pallets/starknet/src/tests/query_tx.rs @@ -1,7 +1,7 @@ use frame_support::{assert_err, assert_ok}; -use mp_starknet::execution::types::Felt252Wrapper; -use mp_starknet::transaction::compute_hash::ComputeTransactionHash; -use mp_starknet::transaction::UserTransaction; +use mp_felt::Felt252Wrapper; +use mp_transactions::compute_hash::ComputeTransactionHash; +use mp_transactions::UserTransaction; use super::mock::default_mock::*; use super::mock::*; diff --git a/crates/pallets/starknet/src/tests/sequencer_address.rs b/crates/pallets/starknet/src/tests/sequencer_address.rs index f00d3b69de..473f56db9f 100644 --- a/crates/pallets/starknet/src/tests/sequencer_address.rs +++ b/crates/pallets/starknet/src/tests/sequencer_address.rs @@ -1,6 +1,6 @@ use frame_support::assert_ok; use frame_support::traits::Hooks; -use mp_starknet::sequencer_address::{DEFAULT_SEQUENCER_ADDRESS, SEQ_ADDR_STORAGE_KEY}; +use mp_sequencer_address::{DEFAULT_SEQUENCER_ADDRESS, SEQ_ADDR_STORAGE_KEY}; use starknet_api::api_core::{ContractAddress, PatriciaKey}; use starknet_api::hash::StarkFelt; diff --git a/crates/pallets/starknet/src/tests/utils.rs b/crates/pallets/starknet/src/tests/utils.rs index b0bf6a7f0f..7f6859ee1b 100644 --- a/crates/pallets/starknet/src/tests/utils.rs +++ b/crates/pallets/starknet/src/tests/utils.rs @@ -4,8 +4,8 @@ use std::path::PathBuf; use std::{env, fs}; use blockifier::execution::contract_class::ContractClass; -use mp_starknet::execution::types::Felt252Wrapper; -use mp_starknet::transaction::{InvokeTransaction, InvokeTransactionV1}; +use mp_felt::Felt252Wrapper; +use mp_transactions::{InvokeTransaction, InvokeTransactionV1}; use starknet_api::api_core::EntryPointSelector; use starknet_api::hash::StarkFelt; use starknet_api::transaction::Calldata; diff --git a/crates/pallets/starknet/src/types.rs b/crates/pallets/starknet/src/types.rs index 92729fa1dd..2d6e7835c0 100644 --- a/crates/pallets/starknet/src/types.rs +++ b/crates/pallets/starknet/src/types.rs @@ -1,6 +1,6 @@ //! Starknet pallet custom types. use blockifier::execution::contract_class::ContractClass; -use mp_starknet::execution::types::Felt252Wrapper; +use mp_felt::Felt252Wrapper; use sp_core::ConstU32; use sp_std::vec::Vec; use starknet_api::api_core::{ClassHash, ContractAddress}; diff --git a/crates/primitives/block/Cargo.toml b/crates/primitives/block/Cargo.toml new file mode 100644 index 0000000000..671aa45d76 --- /dev/null +++ b/crates/primitives/block/Cargo.toml @@ -0,0 +1,51 @@ +[package] +name = "mp-block" +version.workspace = true +edition.workspace = true +license = "MIT" +description = "The starknet block" +authors = { workspace = true } +repository = { workspace = true } + +[package.metadata.docs.rs] +targets = ["x86_64-unknown-linux-gnu"] + +[dependencies] +blockifier = { workspace = true } +mp-felt = { workspace = true } +mp-hashers = { workspace = true } +mp-transactions = { workspace = true } +sp-core = { workspace = true } +starknet_api = { workspace = true } + +# Optionals +parity-scale-codec = { workspace = true, features = [ + "derive", +], optional = true } +serde = { workspace = true, features = ["derive"], optional = true } + +[dev-dependencies] +mp-hashers = { workspace = true } +mp-felt = { workspace = true } + +[features] +default = ["std"] +std = [ + "sp-core/std", + "mp-felt/std", + "mp-transactions/std", + "mp-hashers/std", + "starknet_api/std", + "blockifier/std", + # Optionals + "parity-scale-codec?/std", + "serde?/std", +] +parity-scale-codec = [ + "dep:parity-scale-codec", + "mp-felt/parity-scale-codec", + "mp-transactions/parity-scale-codec", + "starknet_api/parity-scale-codec", + "blockifier/parity-scale-codec", +] +serde = ["dep:serde", "mp-felt/serde", "mp-transactions/serde"] diff --git a/crates/primitives/starknet/src/block/header.rs b/crates/primitives/block/src/header.rs similarity index 97% rename from crates/primitives/starknet/src/block/header.rs rename to crates/primitives/block/src/header.rs index e7a657ab4a..5b2caaae47 100644 --- a/crates/primitives/starknet/src/block/header.rs +++ b/crates/primitives/block/src/header.rs @@ -1,15 +1,14 @@ use alloc::sync::Arc; use blockifier::block_context::BlockContext; +use mp_felt::Felt252Wrapper; +use mp_hashers::HasherT; use sp_core::U256; use starknet_api::api_core::{ChainId, ContractAddress}; use starknet_api::block::{BlockNumber, BlockTimestamp}; use starknet_api::hash::StarkHash; use starknet_api::stdlib::collections::HashMap; -use crate::execution::types::Felt252Wrapper; -use crate::traits::hash::HasherT; - #[derive(Clone, Debug, PartialEq, Eq, Default)] #[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))] #[cfg_attr(feature = "parity-scale-codec", derive(parity_scale_codec::Encode, parity_scale_codec::Decode))] diff --git a/crates/primitives/starknet/src/block/mod.rs b/crates/primitives/block/src/lib.rs similarity index 83% rename from crates/primitives/starknet/src/block/mod.rs rename to crates/primitives/block/src/lib.rs index beae71c438..61f1e8d88d 100644 --- a/crates/primitives/starknet/src/block/mod.rs +++ b/crates/primitives/block/src/lib.rs @@ -1,14 +1,18 @@ //! StarkNet block primitives. +#![cfg_attr(not(feature = "std"), no_std)] + +#[doc(hidden)] +pub extern crate alloc; mod header; + use alloc::vec::Vec; pub use header::*; - -use crate::execution::types::Felt252Wrapper; -use crate::traits::hash::HasherT; -use crate::transaction::compute_hash::ComputeTransactionHash; -use crate::transaction::Transaction; +use mp_felt::Felt252Wrapper; +use mp_hashers::HasherT; +use mp_transactions::compute_hash::ComputeTransactionHash; +use mp_transactions::Transaction; /// Block Transactions pub type BlockTransactions = Vec; @@ -49,3 +53,6 @@ impl Block { self.transactions.iter().map(|tx| tx.compute_hash::(chain_id, false)).collect() } } + +#[cfg(test)] +mod tests; diff --git a/crates/primitives/starknet/src/tests/block.rs b/crates/primitives/block/src/tests.rs similarity index 96% rename from crates/primitives/starknet/src/tests/block.rs rename to crates/primitives/block/src/tests.rs index ff33502724..827564bed1 100644 --- a/crates/primitives/starknet/src/tests/block.rs +++ b/crates/primitives/block/src/tests.rs @@ -1,13 +1,13 @@ use core::convert::TryFrom; +use mp_felt::Felt252Wrapper; +use mp_hashers::pedersen::PedersenHasher; use sp_core::U256; use starknet_api::api_core::{ChainId, ContractAddress, PatriciaKey}; use starknet_api::block::{BlockNumber, BlockTimestamp}; use starknet_api::hash::{StarkFelt, StarkHash}; -use crate::block::Header; -use crate::crypto::hash::pedersen::PedersenHasher; -use crate::execution::felt252_wrapper::Felt252Wrapper; +use crate::Header; fn generate_dummy_header() -> Header { Header::new( diff --git a/crates/primitives/commitments/Cargo.toml b/crates/primitives/commitments/Cargo.toml new file mode 100644 index 0000000000..154690a91a --- /dev/null +++ b/crates/primitives/commitments/Cargo.toml @@ -0,0 +1,46 @@ +[package] +name = "mp-commitments" +version.workspace = true +edition.workspace = true +license = "MIT" +description = "Starknet commitment computation logic" +authors = { workspace = true } +repository = { workspace = true } + +[package.metadata.docs.rs] +targets = ["x86_64-unknown-linux-gnu"] + +[dependencies] +bitvec = { workspace = true } +derive_more = { workspace = true, features = ["constructor"] } +mp-felt = { workspace = true } +mp-hashers = { workspace = true } +mp-transactions = { workspace = true } +starknet-crypto = { workspace = true } +starknet_api = { workspace = true } + +# Optional +parity-scale-codec = { workspace = true, features = [ + "derive", +], optional = true } +scale-info = { workspace = true, features = ["derive"], optional = true } +serde = { workspace = true, features = ["derive"], optional = true } +starknet-ff = { workspace = true, optional = true } + +[dev-dependencies] +starknet-core = { workspace = true } + + +[features] +default = ["std"] +std = [ + "mp-hashers/std", + "mp-transactions/std", + "mp-felt/std", + "starknet-crypto/std", + "bitvec/std", + "starknet_api/std", +] +parity-scale-codec = ["dep:parity-scale-codec", "mp-felt/parity-scale-codec"] +scale-info = ["dep:scale-info", "mp-felt/scale-info"] +serde = ["dep:serde", "dep:starknet-ff", "starknet-ff/serde", "mp-felt/serde"] diff --git a/crates/primitives/starknet/src/crypto/commitment/mod.rs b/crates/primitives/commitments/src/lib.rs similarity index 95% rename from crates/primitives/starknet/src/crypto/commitment/mod.rs rename to crates/primitives/commitments/src/lib.rs index 75c03b78da..ce113e6ef4 100644 --- a/crates/primitives/starknet/src/crypto/commitment/mod.rs +++ b/crates/primitives/commitments/src/lib.rs @@ -1,16 +1,22 @@ +#![cfg_attr(not(feature = "std"), no_std)] + +#[doc(hidden)] +pub extern crate alloc; + +mod merkle_patricia_tree; + use alloc::vec::Vec; use bitvec::vec::BitVec; +use merkle_patricia_tree::merkle_tree::{MerkleTree, NodesMapping, ProofNode}; +use merkle_patricia_tree::ref_merkle_tree::RefMerkleTree; +use mp_felt::Felt252Wrapper; +use mp_hashers::HasherT; +use mp_transactions::compute_hash::ComputeTransactionHash; +use mp_transactions::Transaction; use starknet_api::transaction::Event; use starknet_crypto::FieldElement; -use super::merkle_patricia_tree::merkle_tree::{MerkleTree, NodesMapping, ProofNode}; -use super::merkle_patricia_tree::ref_merkle_tree::RefMerkleTree; -use crate::execution::types::Felt252Wrapper; -use crate::traits::hash::HasherT; -use crate::transaction::compute_hash::ComputeTransactionHash; -use crate::transaction::Transaction; - /// Hash of the leaf of the ClassCommitment tree pub type ClassCommitmentLeafHash = Felt252Wrapper; @@ -284,3 +290,6 @@ pub fn calculate_event_hash(event: &Event) -> FieldElement { let from_address = FieldElement::from(event.from_address.0.0); H::compute_hash_on_elements(&[from_address, keys_hash, data_hash]) } + +#[cfg(test)] +mod tests; diff --git a/crates/primitives/starknet/src/crypto/merkle_patricia_tree/merkle_node.rs b/crates/primitives/commitments/src/merkle_patricia_tree/merkle_node.rs similarity index 99% rename from crates/primitives/starknet/src/crypto/merkle_patricia_tree/merkle_node.rs rename to crates/primitives/commitments/src/merkle_patricia_tree/merkle_node.rs index 06fe06dce9..8d75bcceeb 100644 --- a/crates/primitives/starknet/src/crypto/merkle_patricia_tree/merkle_node.rs +++ b/crates/primitives/commitments/src/merkle_patricia_tree/merkle_node.rs @@ -7,11 +7,10 @@ use bitvec::order::Msb0; use bitvec::prelude::BitVec; use bitvec::slice::BitSlice; +use mp_felt::Felt252Wrapper; +use mp_hashers::HasherT; use starknet_api::stdlib::collections::HashMap; -use crate::execution::felt252_wrapper::Felt252Wrapper; -use crate::traits::hash::HasherT; - /// Id of a Node within the tree #[derive(Copy, Clone, Debug, PartialEq, Eq, Default, PartialOrd, Ord, Hash)] #[cfg_attr(feature = "parity-scale-codec", derive(parity_scale_codec::Encode, parity_scale_codec::Decode))] diff --git a/crates/primitives/starknet/src/crypto/merkle_patricia_tree/merkle_tree.rs b/crates/primitives/commitments/src/merkle_patricia_tree/merkle_tree.rs similarity index 99% rename from crates/primitives/starknet/src/crypto/merkle_patricia_tree/merkle_tree.rs rename to crates/primitives/commitments/src/merkle_patricia_tree/merkle_tree.rs index 1e66b399fd..72cb247ef7 100644 --- a/crates/primitives/starknet/src/crypto/merkle_patricia_tree/merkle_tree.rs +++ b/crates/primitives/commitments/src/merkle_patricia_tree/merkle_tree.rs @@ -5,6 +5,8 @@ use core::marker::PhantomData; use bitvec::prelude::{BitSlice, BitVec, Msb0}; use derive_more::Constructor; +use mp_felt::Felt252Wrapper; +use mp_hashers::HasherT; #[cfg(feature = "parity-scale-codec")] use parity_scale_codec::{Decode, Encode, Error, Input, Output}; #[cfg(feature = "scale-info")] @@ -13,9 +15,7 @@ use scale_info::{build::Fields, Path, Type, TypeInfo}; use serde::{ser::SerializeStructVariant, Serialize}; use starknet_api::stdlib::collections::HashMap; -use crate::crypto::merkle_patricia_tree::merkle_node::{BinaryNode, Direction, EdgeNode, Node, NodeId}; -use crate::execution::types::Felt252Wrapper; -use crate::traits::hash::HasherT; +use super::merkle_node::{BinaryNode, Direction, EdgeNode, Node, NodeId}; /// Wrapper type for a [HashMap] object. (It's not really a wrapper it's a /// copy of the type but we implement the necessary traits.) diff --git a/crates/primitives/starknet/src/crypto/merkle_patricia_tree/mod.rs b/crates/primitives/commitments/src/merkle_patricia_tree/mod.rs similarity index 100% rename from crates/primitives/starknet/src/crypto/merkle_patricia_tree/mod.rs rename to crates/primitives/commitments/src/merkle_patricia_tree/mod.rs diff --git a/crates/primitives/starknet/src/crypto/merkle_patricia_tree/ref_merkle_node.rs b/crates/primitives/commitments/src/merkle_patricia_tree/ref_merkle_node.rs similarity index 98% rename from crates/primitives/starknet/src/crypto/merkle_patricia_tree/ref_merkle_node.rs rename to crates/primitives/commitments/src/merkle_patricia_tree/ref_merkle_node.rs index 20e253f748..671a35c074 100644 --- a/crates/primitives/starknet/src/crypto/merkle_patricia_tree/ref_merkle_node.rs +++ b/crates/primitives/commitments/src/merkle_patricia_tree/ref_merkle_node.rs @@ -10,9 +10,8 @@ use core::cell::RefCell; use bitvec::order::Msb0; use bitvec::prelude::BitVec; use bitvec::slice::BitSlice; - -use crate::execution::felt252_wrapper::Felt252Wrapper; -use crate::traits::hash::HasherT; +use mp_felt::Felt252Wrapper; +use mp_hashers::HasherT; /// A node in a Binary Merkle-Patricia Tree graph. #[derive(Clone, Debug, PartialEq)] diff --git a/crates/primitives/starknet/src/crypto/merkle_patricia_tree/ref_merkle_tree.rs b/crates/primitives/commitments/src/merkle_patricia_tree/ref_merkle_tree.rs similarity index 99% rename from crates/primitives/starknet/src/crypto/merkle_patricia_tree/ref_merkle_tree.rs rename to crates/primitives/commitments/src/merkle_patricia_tree/ref_merkle_tree.rs index e19b0886d0..c9f51a2c87 100644 --- a/crates/primitives/starknet/src/crypto/merkle_patricia_tree/ref_merkle_tree.rs +++ b/crates/primitives/commitments/src/merkle_patricia_tree/ref_merkle_tree.rs @@ -6,10 +6,10 @@ use core::iter::once; use core::marker::PhantomData; use bitvec::prelude::{BitSlice, BitVec, Msb0}; +use mp_felt::Felt252Wrapper; +use mp_hashers::HasherT; -use crate::crypto::merkle_patricia_tree::ref_merkle_node::{BinaryNode, Direction, EdgeNode, Node}; -use crate::execution::felt252_wrapper::Felt252Wrapper; -use crate::traits::hash::HasherT; +use super::ref_merkle_node::{BinaryNode, Direction, EdgeNode, Node}; /// Lightweight representation of [BinaryNode]. Only holds left and right hashes. #[derive(Debug, PartialEq, Eq)] @@ -348,6 +348,7 @@ impl RefMerkleTree { /// # Returns /// /// The value of the key. + #[allow(unused)] pub fn get(&self, key: &BitSlice) -> Option { self.traverse(key).last().and_then(|node| match &*node.borrow() { Node::Leaf(value) if !value.eq(&Felt252Wrapper::ZERO) => Some(*value), @@ -374,6 +375,7 @@ impl RefMerkleTree { /// # Returns /// /// The merkle proof and all the child nodes hashes. + #[allow(unused)] pub fn get_proof(&self, key: &BitSlice) -> Vec { let mut nodes = self.traverse(key); diff --git a/crates/primitives/starknet/src/tests/crypto.rs b/crates/primitives/commitments/src/tests.rs similarity index 92% rename from crates/primitives/starknet/src/tests/crypto.rs rename to crates/primitives/commitments/src/tests.rs index fcf5e232b5..0b715aa63f 100644 --- a/crates/primitives/starknet/src/tests/crypto.rs +++ b/crates/primitives/commitments/src/tests.rs @@ -1,12 +1,13 @@ +use mp_felt::Felt252Wrapper; +use mp_hashers::pedersen::PedersenHasher; +use mp_hashers::HasherT; use starknet_api::stdlib::collections::HashMap; use starknet_core::crypto::compute_hash_on_elements; use starknet_crypto::FieldElement; -use crate::crypto::hash::pedersen::PedersenHasher; -use crate::crypto::merkle_patricia_tree::merkle_node::{BinaryNode, Direction, Node, NodeId}; -use crate::execution::types::Felt252Wrapper; -use crate::tests::utils::PEDERSEN_ZERO_HASH; -use crate::traits::hash::HasherT; +use super::merkle_patricia_tree::merkle_node::{BinaryNode, Direction, Node, NodeId}; + +pub const PEDERSEN_ZERO_HASH: &str = "0x49EE3EBA8C1600700EE1B87EB599F16716B0B1022947733551FDE4050CA6804"; #[derive(Default)] struct TestHasher; diff --git a/crates/primitives/digest-log/Cargo.toml b/crates/primitives/digest-log/Cargo.toml index fda5c9f261..6e7849e1fa 100644 --- a/crates/primitives/digest-log/Cargo.toml +++ b/crates/primitives/digest-log/Cargo.toml @@ -8,9 +8,8 @@ authors = { workspace = true } repository = { workspace = true } [dependencies] -mp-starknet = { workspace = true, features = ["parity-scale-codec"] } +mp-block = { workspace = true, features = ["parity-scale-codec"] } parity-scale-codec = { workspace = true } -# Substrate sp-runtime = { workspace = true } [dev-dependencies] @@ -18,4 +17,4 @@ assert_matches = "1.5.0" [features] default = ["std"] -std = ["mp-starknet/std", "parity-scale-codec/std", "sp-runtime/std"] +std = ["parity-scale-codec/std", "sp-runtime/std", "mp-block/std"] diff --git a/crates/primitives/digest-log/src/lib.rs b/crates/primitives/digest-log/src/lib.rs index bf0c09c670..830eb89dcc 100644 --- a/crates/primitives/digest-log/src/lib.rs +++ b/crates/primitives/digest-log/src/lib.rs @@ -20,7 +20,7 @@ mod error; mod tests; pub use error::FindLogError; -use mp_starknet::block::Block as StarknetBlock; +use mp_block::Block as StarknetBlock; use parity_scale_codec::{Decode, Encode}; use sp_runtime::generic::{Digest, OpaqueDigestItemId}; use sp_runtime::ConsensusEngineId; diff --git a/crates/primitives/fee/Cargo.toml b/crates/primitives/fee/Cargo.toml new file mode 100644 index 0000000000..374f81e1be --- /dev/null +++ b/crates/primitives/fee/Cargo.toml @@ -0,0 +1,21 @@ +[package] +name = "mp-fee" +version.workspace = true +edition.workspace = true +license = "MIT" +description = "Starknet fee related logic" +authors = { workspace = true } +repository = { workspace = true } + +[package.metadata.docs.rs] +targets = ["x86_64-unknown-linux-gnu"] + +[dependencies] +blockifier = { workspace = true } +mp-state = { workspace = true } +phf = { workspace = true } +starknet_api = { workspace = true } + +[features] +default = ["std"] +std = ["blockifier/std", "starknet_api/std", "mp-state/std"] diff --git a/crates/primitives/starknet/src/fees/mod.rs b/crates/primitives/fee/src/lib.rs similarity index 95% rename from crates/primitives/starknet/src/fees/mod.rs rename to crates/primitives/fee/src/lib.rs index a169df516c..a4838da243 100644 --- a/crates/primitives/starknet/src/fees/mod.rs +++ b/crates/primitives/fee/src/lib.rs @@ -1,3 +1,9 @@ +//! Starknet fee logic +#![cfg_attr(not(feature = "std"), no_std)] + +#[doc(hidden)] +pub extern crate alloc; + use alloc::vec; use alloc::vec::Vec; @@ -12,6 +18,7 @@ use blockifier::transaction::errors::TransactionExecutionError; use blockifier::transaction::objects::{AccountTransactionContext, ResourcesMapping, TransactionExecutionResult}; use blockifier::transaction::transaction_types::TransactionType; use blockifier::transaction::transaction_utils::{calculate_l1_gas_usage, calculate_tx_resources}; +use mp_state::{FeeConfig, StateChanges}; use phf::phf_map; use starknet_api::api_core::EntryPointSelector; use starknet_api::calldata; @@ -19,9 +26,8 @@ use starknet_api::deprecated_contract_class::EntryPointType; use starknet_api::hash::StarkFelt; use starknet_api::transaction::{Calldata, Fee}; -use super::state::StateChanges; -use crate::state::FeeConfig; - +/// Initial gas for a transaction +pub const INITIAL_GAS: u64 = u64::MAX; /// Number of storage updates for the fee transfer tx. pub const FEE_TRANSFER_N_STORAGE_CHANGES: u8 = 2; // Sender and sequencer balance update. /// Number of storage updates to actually charge for the fee transfer tx. @@ -108,9 +114,6 @@ fn execute_fee_transfer( // The most significant 128 bits of the amount transferred. let msb_amount = StarkFelt::from(0_u64); - // The fee-token contract is a Cairo 0 contract, hence the initial gas is irrelevant. - let initial_gas = super::constants::INITIAL_GAS; - let storage_address = block_context.fee_token_address; let fee_transfer_call = CallEntryPoint { class_hash: None, @@ -132,7 +135,8 @@ fn execute_fee_transfer( storage_address, caller_address: account_tx_context.sender_address, call_type: CallType::Call, - initial_gas, + // The fee-token contract is a Cairo 0 contract, hence the initial gas is irrelevant. + initial_gas: INITIAL_GAS, }; let max_steps = block_context.invoke_tx_max_n_steps; diff --git a/crates/primitives/felt/Cargo.toml b/crates/primitives/felt/Cargo.toml new file mode 100644 index 0000000000..9fee03efdf --- /dev/null +++ b/crates/primitives/felt/Cargo.toml @@ -0,0 +1,44 @@ +[package] +name = "mp-felt" +version.workspace = true +edition.workspace = true +license = "MIT" +description = "Madara felt type" +authors = { workspace = true } +repository = { workspace = true } + +[package.metadata.docs.rs] +targets = ["x86_64-unknown-linux-gnu"] + +[dependencies] +cairo-vm = { workspace = true } +sp-core = { workspace = true } +starknet-ff = { workspace = true } +starknet_api = { workspace = true } +thiserror-no-std = { workspace = true } + +# Optional +hex = { workspace = true, optional = true } +parity-scale-codec = { workspace = true, features = [ + "derive", +], optional = true } +scale-info = { workspace = true, features = ["derive"], optional = true } +serde = { workspace = true, features = ["derive"], optional = true } + +[features] +default = ["std"] +parity-scale-codec = ["dep:parity-scale-codec", "dep:hex"] +scale-info = ["dep:scale-info"] +serde = ["dep:serde", "starknet-ff/serde"] +std = [ + "cairo-vm/std", + "thiserror-no-std/std", + "starknet_api/std", + "sp-core/std", + "starknet-ff/std", + # Optional + "parity-scale-codec?/std", + "scale-info?/std", + "serde?/std", + "hex?/std", +] diff --git a/crates/primitives/starknet/src/execution/felt252_wrapper.rs b/crates/primitives/felt/src/lib.rs similarity index 99% rename from crates/primitives/starknet/src/execution/felt252_wrapper.rs rename to crates/primitives/felt/src/lib.rs index 59a42f7ac7..3c57af9313 100644 --- a/crates/primitives/starknet/src/execution/felt252_wrapper.rs +++ b/crates/primitives/felt/src/lib.rs @@ -7,6 +7,13 @@ //! The [`Felt252Wrapper`] implements the traits for SCALE encoding, and wrap //! the [`FieldElement`] type from starknet-ff. +#![cfg_attr(not(feature = "std"), no_std)] + +#[doc(hidden)] +pub extern crate alloc; + +mod starkware_types_conversions; + use alloc::string::{String, ToString}; use cairo_vm::felt::Felt252; diff --git a/crates/primitives/felt/src/starkware_types_conversions.rs b/crates/primitives/felt/src/starkware_types_conversions.rs new file mode 100644 index 0000000000..f73420ab86 --- /dev/null +++ b/crates/primitives/felt/src/starkware_types_conversions.rs @@ -0,0 +1,136 @@ +use starknet_api::state::StorageKey; +use starknet_api::{api_core as stcore, transaction as sttx}; + +use super::Felt252Wrapper; + +impl From for sttx::TransactionHash { + fn from(value: Felt252Wrapper) -> Self { + Self(value.into()) + } +} + +impl From for Felt252Wrapper { + fn from(value: sttx::TransactionHash) -> Self { + value.0.into() + } +} + +impl From for stcore::Nonce { + fn from(value: Felt252Wrapper) -> Self { + Self(value.into()) + } +} + +impl From for Felt252Wrapper { + fn from(value: stcore::Nonce) -> Self { + value.0.into() + } +} + +impl From for stcore::ClassHash { + fn from(value: Felt252Wrapper) -> Self { + Self(value.into()) + } +} + +impl From for Felt252Wrapper { + fn from(value: stcore::ClassHash) -> Self { + value.0.into() + } +} + +impl From for stcore::CompiledClassHash { + fn from(value: Felt252Wrapper) -> Self { + Self(value.into()) + } +} + +impl From for Felt252Wrapper { + fn from(value: stcore::CompiledClassHash) -> Self { + value.0.into() + } +} + +impl From for stcore::PatriciaKey { + fn from(value: Felt252Wrapper) -> Self { + Self(value.into()) + } +} + +impl From for Felt252Wrapper { + fn from(value: stcore::PatriciaKey) -> Self { + value.0.into() + } +} + +impl From for stcore::ContractAddress { + fn from(value: Felt252Wrapper) -> Self { + Self(value.into()) + } +} + +impl From for Felt252Wrapper { + fn from(value: stcore::ContractAddress) -> Self { + value.0.into() + } +} + +impl From for stcore::EntryPointSelector { + fn from(value: Felt252Wrapper) -> Self { + Self(value.into()) + } +} + +impl From for Felt252Wrapper { + fn from(value: stcore::EntryPointSelector) -> Self { + value.0.into() + } +} + +impl From for sttx::ContractAddressSalt { + fn from(value: Felt252Wrapper) -> Self { + Self(value.into()) + } +} + +impl From for Felt252Wrapper { + fn from(value: sttx::ContractAddressSalt) -> Self { + value.0.into() + } +} + +impl From for StorageKey { + fn from(value: Felt252Wrapper) -> Self { + Self(value.into()) + } +} + +impl From for Felt252Wrapper { + fn from(value: StorageKey) -> Self { + value.0.0.into() + } +} + +impl From for sttx::TransactionVersion { + fn from(value: Felt252Wrapper) -> Self { + Self(value.into()) + } +} + +impl From for Felt252Wrapper { + fn from(value: sttx::TransactionVersion) -> Self { + value.0.into() + } +} + +impl From for sttx::EventKey { + fn from(value: Felt252Wrapper) -> Self { + Self(value.into()) + } +} + +impl From for Felt252Wrapper { + fn from(value: sttx::EventKey) -> Self { + value.0.into() + } +} diff --git a/crates/primitives/hashers/Cargo.toml b/crates/primitives/hashers/Cargo.toml new file mode 100644 index 0000000000..52f19e3956 --- /dev/null +++ b/crates/primitives/hashers/Cargo.toml @@ -0,0 +1,36 @@ +[package] +name = "mp-hashers" +version.workspace = true +edition.workspace = true +license = "MIT" +description = "Starknet hashers" +authors = { workspace = true } +repository = { workspace = true } + +[package.metadata.docs.rs] +targets = ["x86_64-unknown-linux-gnu"] + +[dependencies] +mp-felt = { workspace = true } +starknet-core = { workspace = true } +starknet-crypto = { workspace = true, features = ["alloc"] } + +# Optional +parity-scale-codec = { workspace = true, optional = true } +scale-info = { workspace = true, optional = true } +serde = { workspace = true, optional = true } + +[features] +default = ["std"] +std = [ + "starknet-crypto/std", + "starknet-core/std", + "mp-felt/std", + # Optional + "serde?/std", + "parity-scale-codec?/std", + "scale-info?/std", +] +parity-scale-codec = ["dep:parity-scale-codec", "mp-felt/parity-scale-codec"] +scale-info = ["dep:scale-info", "mp-felt/scale-info"] +serde = ["dep:serde", "mp-felt/serde"] diff --git a/crates/primitives/starknet/src/traits/hash.rs b/crates/primitives/hashers/src/lib.rs similarity index 90% rename from crates/primitives/starknet/src/traits/hash.rs rename to crates/primitives/hashers/src/lib.rs index 9872c98b96..11ea680830 100644 --- a/crates/primitives/starknet/src/traits/hash.rs +++ b/crates/primitives/hashers/src/lib.rs @@ -1,8 +1,14 @@ //! Traits for hashing. +#![cfg_attr(not(feature = "std"), no_std)] -use starknet_crypto::FieldElement; +#[doc(hidden)] +pub extern crate alloc; + +pub mod pedersen; +pub mod poseidon; -use crate::execution::felt252_wrapper::Felt252Wrapper; +use mp_felt::Felt252Wrapper; +use starknet_crypto::FieldElement; /// A trait for hashing. pub trait HasherT { diff --git a/crates/primitives/starknet/src/crypto/hash/pedersen.rs b/crates/primitives/hashers/src/pedersen.rs similarity index 95% rename from crates/primitives/starknet/src/crypto/hash/pedersen.rs rename to crates/primitives/hashers/src/pedersen.rs index 8b253d2628..f81b693213 100644 --- a/crates/primitives/starknet/src/crypto/hash/pedersen.rs +++ b/crates/primitives/hashers/src/pedersen.rs @@ -1,12 +1,11 @@ //! Pedersen hash module. use alloc::vec::Vec; +use mp_felt::Felt252Wrapper; use starknet_core::crypto::compute_hash_on_elements; use starknet_crypto::{pedersen_hash, FieldElement}; -use crate::execution::felt252_wrapper::Felt252Wrapper; -use crate::traits::hash::HasherT; -use crate::traits::SendSyncStatic; +use super::HasherT; /// The Pedersen hasher. #[derive(Clone, Copy)] @@ -15,7 +14,8 @@ use crate::traits::SendSyncStatic; #[cfg_attr(feature = "scale-info", derive(scale_info::TypeInfo))] pub struct PedersenHasher; -impl SendSyncStatic for PedersenHasher {} +unsafe impl Send for PedersenHasher {} +unsafe impl Sync for PedersenHasher {} /// The Pedersen hasher implementation. impl HasherT for PedersenHasher { diff --git a/crates/primitives/starknet/src/crypto/hash/poseidon.rs b/crates/primitives/hashers/src/poseidon.rs similarity index 96% rename from crates/primitives/starknet/src/crypto/hash/poseidon.rs rename to crates/primitives/hashers/src/poseidon.rs index 1f7de837fc..fcbfa051cd 100644 --- a/crates/primitives/starknet/src/crypto/hash/poseidon.rs +++ b/crates/primitives/hashers/src/poseidon.rs @@ -1,10 +1,10 @@ //! Poseidon hash module. use alloc::vec::Vec; +use mp_felt::Felt252Wrapper; use starknet_crypto::{poseidon_hash, poseidon_hash_many, FieldElement}; -use crate::execution::felt252_wrapper::Felt252Wrapper; -use crate::traits::hash::HasherT; +use super::HasherT; /// The poseidon hasher. #[derive(Clone, Copy)] @@ -13,6 +13,9 @@ use crate::traits::hash::HasherT; #[cfg_attr(feature = "scale-info", derive(scale_info::TypeInfo))] pub struct PoseidonHasher; +unsafe impl Send for PoseidonHasher {} +unsafe impl Sync for PoseidonHasher {} + impl HasherT for PoseidonHasher { /// The Poseidon hash function. /// # Arguments diff --git a/crates/primitives/sequencer-address/Cargo.toml b/crates/primitives/sequencer-address/Cargo.toml new file mode 100644 index 0000000000..81a66a7abf --- /dev/null +++ b/crates/primitives/sequencer-address/Cargo.toml @@ -0,0 +1,28 @@ +[package] +name = "mp-sequencer-address" +version.workspace = true +edition.workspace = true +license = "MIT" +description = "Sequencer address inherent logic" +authors = { workspace = true } +repository = { workspace = true } + +[package.metadata.docs.rs] +targets = ["x86_64-unknown-linux-gnu"] + +[dependencies] +sp-core = { workspace = true } +sp-inherents = { workspace = true } +thiserror-no-std = { workspace = true } + +# Optional +async-trait = { workspace = true, optional = true } +parity-scale-codec = { workspace = true, features = [ + "derive", +], optional = true } + +[features] +default = ["std"] +std = ["sp-inherents/std", "thiserror-no-std/std", "parity-scale-codec?/std"] +parity-scale-codec = ["dep:parity-scale-codec"] +client = ["std", "parity-scale-codec", "dep:async-trait"] diff --git a/crates/primitives/starknet/src/sequencer_address/mod.rs b/crates/primitives/sequencer-address/src/lib.rs similarity index 94% rename from crates/primitives/starknet/src/sequencer_address/mod.rs rename to crates/primitives/sequencer-address/src/lib.rs index f8cb1f4a5f..f612638c63 100644 --- a/crates/primitives/starknet/src/sequencer_address/mod.rs +++ b/crates/primitives/sequencer-address/src/lib.rs @@ -1,3 +1,6 @@ +//! The address of the account receiving the network fee +#![cfg_attr(not(feature = "std"), no_std)] + use sp_inherents::{InherentData, InherentIdentifier, IsFatalError}; use thiserror_no_std::Error; @@ -43,10 +46,10 @@ impl SequencerAddressInherentData for InherentData { } } -#[cfg(all(feature = "std", feature = "parity-scale-codec"))] +#[cfg(feature = "client")] mod reexport_for_client_only { - use alloc::boxed::Box; - use core::array::TryFromSliceError; + use std::array::TryFromSliceError; + use std::boxed::Box; use parity_scale_codec::{Decode, Encode}; @@ -115,5 +118,5 @@ mod reexport_for_client_only { } } -#[cfg(all(feature = "std", feature = "parity-scale-codec"))] +#[cfg(feature = "client")] pub use reexport_for_client_only::*; diff --git a/crates/primitives/starknet/src/constants.rs b/crates/primitives/starknet/src/constants.rs deleted file mode 100644 index cfc54f4a38..0000000000 --- a/crates/primitives/starknet/src/constants.rs +++ /dev/null @@ -1,15 +0,0 @@ -use starknet_ff::FieldElement; - -use crate::execution::felt252_wrapper::Felt252Wrapper; - -/// Initial gas for a transaction -pub const INITIAL_GAS: u64 = u64::MAX; - -// Need to use `from_mont` because this needs to be a constant function call -/// ChainId for Starknet Goerli testnet -pub const SN_GOERLI_CHAIN_ID: Felt252Wrapper = Felt252Wrapper(FieldElement::from_mont([ - 3753493103916128178, - 18446744073709548950, - 18446744073709551615, - 398700013197595345, -])); diff --git a/crates/primitives/starknet/src/crypto/hash/mod.rs b/crates/primitives/starknet/src/crypto/hash/mod.rs deleted file mode 100644 index 04b0ea0713..0000000000 --- a/crates/primitives/starknet/src/crypto/hash/mod.rs +++ /dev/null @@ -1,4 +0,0 @@ -//! This module contains the hash functions used in the StarkNet protocol. - -pub mod pedersen; -pub mod poseidon; diff --git a/crates/primitives/starknet/src/crypto/mod.rs b/crates/primitives/starknet/src/crypto/mod.rs deleted file mode 100644 index 6bd3323f0c..0000000000 --- a/crates/primitives/starknet/src/crypto/mod.rs +++ /dev/null @@ -1,9 +0,0 @@ -/// Compute the various commitments. -pub mod commitment; -/// Hasher related functionality. -pub mod hash; -/// Merkle patricia tree for starknet. -pub mod merkle_patricia_tree; - -/// Compute state related commitments and trees. -pub mod state; diff --git a/crates/primitives/starknet/src/crypto/state/mod.rs b/crates/primitives/starknet/src/crypto/state/mod.rs deleted file mode 100644 index 2bd781bbed..0000000000 --- a/crates/primitives/starknet/src/crypto/state/mod.rs +++ /dev/null @@ -1,31 +0,0 @@ -use core::marker::PhantomData; - -use crate::execution::types::Felt252Wrapper; -use crate::traits::hash::HasherT; - -/// Root of the Merkle-Patricia tree whose leaves are the contracts states -pub type StorageCommitment = Felt252Wrapper; -/// Root of the Merkle-Patricia tree whose leaves are the compiled class hashes -pub type ClassCommitment = Felt252Wrapper; - -/// Global Starknet State Commitment -pub struct StateCommitment(Felt252Wrapper, PhantomData); - -impl StateCommitment { - /// Calculates global state commitment by combining the storage and class commitment. - /// - /// See - /// - /// for details. - pub fn calculate(storage_commitment: StorageCommitment, class_commitment: ClassCommitment) -> Felt252Wrapper { - if class_commitment == ClassCommitment::ZERO { - storage_commitment - } else { - let global_state_version = Felt252Wrapper::try_from("STARKNET_STATE_V0".as_bytes()).unwrap(); // Unwrap is safu here - - let hash = H::compute_hash_on_elements(&[global_state_version.0, storage_commitment.0, class_commitment.0]); - - hash.into() - } - } -} diff --git a/crates/primitives/starknet/src/execution/mod.rs b/crates/primitives/starknet/src/execution/mod.rs deleted file mode 100644 index a9dc0d9f95..0000000000 --- a/crates/primitives/starknet/src/execution/mod.rs +++ /dev/null @@ -1,8 +0,0 @@ -//! Starknet execution functionality. - -/// Felt252Wrapper type -pub mod felt252_wrapper; - -pub mod types { - pub use super::felt252_wrapper::*; -} diff --git a/crates/primitives/starknet/src/lib.rs b/crates/primitives/starknet/src/lib.rs deleted file mode 100644 index 083d63abc2..0000000000 --- a/crates/primitives/starknet/src/lib.rs +++ /dev/null @@ -1,41 +0,0 @@ -//! Starknet primitives. - -#![cfg_attr(not(feature = "std"), no_std)] - -#[doc(hidden)] -pub extern crate alloc; - -// Include modules. -/// Starknet transaction constants. -pub mod constants; - -/// Starknet block related functionality. -pub mod block; - -/// Starknet transaction related functionality. -pub mod transaction; - -/// Starknet crypto related functionality. -pub mod crypto; - -/// Starknet storage primitives. -pub mod storage; - -/// Starknet state related functionality. -pub mod state; - -/// Starknet primitives traits. -pub mod traits; - -/// Starknet Execution related functionality. -pub mod execution; - -/// Starknet Fees related functionality. -pub mod fees; - -/// Sequencer address inherent data. -pub mod sequencer_address; - -/// Tests. -#[cfg(test)] -pub mod tests; diff --git a/crates/primitives/starknet/src/tests/mod.rs b/crates/primitives/starknet/src/tests/mod.rs deleted file mode 100644 index 838f3d0435..0000000000 --- a/crates/primitives/starknet/src/tests/mod.rs +++ /dev/null @@ -1,4 +0,0 @@ -pub mod block; -pub mod crypto; -pub mod state; -pub mod utils; diff --git a/crates/primitives/starknet/src/tests/utils.rs b/crates/primitives/starknet/src/tests/utils.rs deleted file mode 100644 index ab2f6bdf45..0000000000 --- a/crates/primitives/starknet/src/tests/utils.rs +++ /dev/null @@ -1,84 +0,0 @@ -use std::collections::HashMap; -use std::fs; -use std::path::PathBuf; - -use blockifier::execution::contract_class::ContractClass; -use blockifier::state::cached_state::CachedState; -use cairo_lang_casm_contract_class::CasmContractClass; -use starknet_api::api_core::{ClassHash, ContractAddress, PatriciaKey}; -use starknet_api::hash::StarkFelt; - -use crate::block::Block; -use crate::state::DictStateReader; - -// Addresses. -pub const TEST_CONTRACT_ADDRESS: &str = "0x100"; -pub const TEST_CONTRACT_ADDRESS_2: &str = "0x200"; -pub const SECURITY_TEST_CONTRACT_ADDRESS: &str = "0x300"; -pub const TEST_ACCOUNT_CONTRACT_ADDRESS: &str = "0x101"; -pub const TEST_FAULTY_ACCOUNT_CONTRACT_ADDRESS: &str = "0x102"; -pub const TEST_SEQUENCER_ADDRESS: &str = "0x05a2b92d9a36509a3d651e7df99144a4ad8301e2caf42465ee6ab0451ae91882"; -pub const TEST_ERC20_CONTRACT_ADDRESS: &str = "0x1001"; - -// Class hashes. -pub const TEST_CLASS_HASH: &str = "0x110"; -pub const TEST_ACCOUNT_CONTRACT_CLASS_HASH: &str = "0x111"; -pub const TEST_EMPTY_CONTRACT_CLASS_HASH: &str = "0x112"; -pub const TEST_FAULTY_ACCOUNT_CONTRACT_CLASS_HASH: &str = "0x113"; -pub const SECURITY_TEST_CLASS_HASH: &str = "0x114"; - -// Paths. -pub const TEST_CONTRACT_PATH: &str = "../../../cairo-contracts/build/test.json"; -pub const SECURITY_TEST_CONTRACT_PATH: &str = "../../../cairo-contracts/build/security_test.json"; - -pub const PEDERSEN_ZERO_HASH: &str = "0x49EE3EBA8C1600700EE1B87EB599F16716B0B1022947733551FDE4050CA6804"; - -impl Block { - /// Creates a mock block. - pub fn create_for_testing() -> Block { - Block::default() - } -} - -pub fn create_test_state() -> CachedState { - let class_hash_to_class = HashMap::from([ - (ClassHash(StarkFelt::try_from(TEST_CLASS_HASH).unwrap()), get_contract_class(TEST_CONTRACT_PATH, 0)), - ( - ClassHash(StarkFelt::try_from(SECURITY_TEST_CLASS_HASH).unwrap()), - get_contract_class(SECURITY_TEST_CONTRACT_PATH, 0), - ), - ]); - - // Two instances of a test contract and one instance of another (different) test contract. - let address_to_class_hash = HashMap::from([ - ( - ContractAddress(PatriciaKey(StarkFelt::try_from(TEST_CONTRACT_ADDRESS).unwrap())), - ClassHash(StarkFelt::try_from(TEST_CLASS_HASH).unwrap()), - ), - ( - ContractAddress(PatriciaKey(StarkFelt::try_from(TEST_CONTRACT_ADDRESS_2).unwrap())), - ClassHash(StarkFelt::try_from(TEST_CLASS_HASH).unwrap()), - ), - ( - ContractAddress(PatriciaKey(StarkFelt::try_from(SECURITY_TEST_CONTRACT_ADDRESS).unwrap())), - ClassHash(StarkFelt::try_from(SECURITY_TEST_CLASS_HASH).unwrap()), - ), - ]); - - CachedState::new( - DictStateReader { class_hash_to_class, address_to_class_hash, ..Default::default() }, - Default::default(), - ) -} - -pub fn get_contract_class(contract_path: &str, version: u8) -> ContractClass { - let path: PathBuf = [contract_path].iter().collect(); - let raw_contract_class = fs::read_to_string(path).unwrap(); - if version == 0 { - return ContractClass::V0(serde_json::from_str(&raw_contract_class).unwrap()); - } else if version == 1 { - let casm_contract_class: CasmContractClass = serde_json::from_str(&raw_contract_class).unwrap(); - return ContractClass::V1(casm_contract_class.try_into().unwrap()); - } - unimplemented!("version {} is not supported to get contract class from JSON", version); -} diff --git a/crates/primitives/starknet/src/traits/mod.rs b/crates/primitives/starknet/src/traits/mod.rs deleted file mode 100644 index b220886ddf..0000000000 --- a/crates/primitives/starknet/src/traits/mod.rs +++ /dev/null @@ -1,5 +0,0 @@ -/// Define traits related to hash functions. -pub mod hash; - -/// A trait for types that can be shared between threads + copied. -pub trait SendSyncStatic: Send + Sync + 'static {} diff --git a/crates/primitives/state/Cargo.toml b/crates/primitives/state/Cargo.toml new file mode 100644 index 0000000000..29aac8b4fb --- /dev/null +++ b/crates/primitives/state/Cargo.toml @@ -0,0 +1,19 @@ +[package] +name = "mp-state" +version.workspace = true +edition.workspace = true +license = "MIT" +description = "Starknet state logic" +authors = { workspace = true } +repository = { workspace = true } + +[package.metadata.docs.rs] +targets = ["x86_64-unknown-linux-gnu"] + +[dependencies] +blockifier = { workspace = true } +starknet_api = { workspace = true } + +[features] +default = ["std"] +std = ["blockifier/std", "starknet_api/std"] diff --git a/crates/primitives/starknet/src/state/mod.rs b/crates/primitives/state/src/lib.rs similarity index 97% rename from crates/primitives/starknet/src/state/mod.rs rename to crates/primitives/state/src/lib.rs index 57aa9214cb..1278cb41ae 100644 --- a/crates/primitives/starknet/src/state/mod.rs +++ b/crates/primitives/state/src/lib.rs @@ -1,3 +1,5 @@ +#![cfg_attr(not(feature = "std"), no_std)] + use blockifier::execution::contract_class::ContractClass; use blockifier::state::cached_state::ContractStorageKey; use blockifier::state::errors::StateError; @@ -77,3 +79,6 @@ impl StateReader for DictStateReader { Ok(class_hash) } } + +#[cfg(test)] +mod tests; diff --git a/crates/primitives/starknet/src/tests/state.rs b/crates/primitives/state/src/tests.rs similarity index 99% rename from crates/primitives/starknet/src/tests/state.rs rename to crates/primitives/state/src/tests.rs index 24e213cdab..7fe3917241 100644 --- a/crates/primitives/starknet/src/tests/state.rs +++ b/crates/primitives/state/src/tests.rs @@ -5,7 +5,7 @@ use starknet_api::api_core::{ClassHash, ContractAddress, Nonce}; use starknet_api::hash::StarkFelt; use starknet_api::state::StorageKey; -use crate::state::*; +use crate::*; #[test] fn test_get_storage_at() { diff --git a/crates/primitives/storage/Cargo.toml b/crates/primitives/storage/Cargo.toml new file mode 100644 index 0000000000..c161154483 --- /dev/null +++ b/crates/primitives/storage/Cargo.toml @@ -0,0 +1,24 @@ +[package] +name = "mp-storage" +version.workspace = true +edition.workspace = true +license = "MIT" +description = "Starknet substrate storage const and types" +authors = { workspace = true } +repository = { workspace = true } + +[package.metadata.docs.rs] +targets = ["x86_64-unknown-linux-gnu"] + +[dependencies] +# Optional +parity-scale-codec = { workspace = true, features = [ + "derive", +], optional = true } +serde = { workspace = true, optional = true, features = ["derive"] } + +[features] +default = ["std"] +std = ["serde?/std", "parity-scale-codec?/std"] +serde = ["dep:serde"] +parity-scale-codec = ["dep:parity-scale-codec"] diff --git a/crates/primitives/starknet/src/storage/mod.rs b/crates/primitives/storage/src/lib.rs similarity index 97% rename from crates/primitives/starknet/src/storage/mod.rs rename to crates/primitives/storage/src/lib.rs index b12637fa06..ae13f09fd2 100644 --- a/crates/primitives/starknet/src/storage/mod.rs +++ b/crates/primitives/storage/src/lib.rs @@ -1,4 +1,5 @@ //! StarkNet storage primitives. +#![cfg_attr(not(feature = "std"), no_std)] /// Current version of pallet Starknet's storage schema is stored under this key. pub const PALLET_STARKNET_SCHEMA: &[u8] = b":starknet_schema"; diff --git a/crates/primitives/starknet/Cargo.toml b/crates/primitives/transactions/Cargo.toml similarity index 56% rename from crates/primitives/starknet/Cargo.toml rename to crates/primitives/transactions/Cargo.toml index 264e38216e..cd443d159c 100644 --- a/crates/primitives/starknet/Cargo.toml +++ b/crates/primitives/transactions/Cargo.toml @@ -1,5 +1,5 @@ [package] -name = "mp-starknet" +name = "mp-transactions" version.workspace = true edition.workspace = true license = "MIT" @@ -11,65 +11,53 @@ repository = { workspace = true } targets = ["x86_64-unknown-linux-gnu"] [dependencies] -# Substrate -sp-core = { workspace = true } -sp-inherents = { workspace = true } -sp-std = { workspace = true } - -# Starknet blockifier = { workspace = true } -cairo-lang-casm-contract-class = { workspace = true } -cairo-vm = { workspace = true } +derive_more = { workspace = true, features = ["from"] } +mp-fee = { workspace = true } +mp-felt = { workspace = true } +mp-hashers = { workspace = true } +mp-state = { workspace = true } starknet-core = { workspace = true } starknet-crypto = { workspace = true, features = ["alloc"] } -starknet-ff = { workspace = true, features = ["alloc", "serde"] } +starknet-ff = { workspace = true, features = ["alloc"] } starknet_api = { workspace = true } -# Other third party dependencies -bitvec = { workspace = true, features = ["alloc"] } -derive_more = { workspace = true, features = ["constructor"] } -lazy_static = { workspace = true } -thiserror-no-std = { workspace = true } - -# Optional deps -async-trait = { workspace = true, optional = true } +# Optional (client) +cairo-lang-casm-contract-class = { workspace = true, optional = true } cairo-lang-starknet = { workspace = true, optional = true } cairo-lang-utils = { workspace = true, optional = true } +cairo-vm = { workspace = true, optional = true } flate2 = { workspace = true, optional = true } -hex = { workspace = true, optional = true } -log = { workspace = true } num-bigint = { workspace = true, optional = true } +serde_json = { workspace = true, optional = true } +thiserror = { workspace = true, optional = true } + +# Other optional parity-scale-codec = { workspace = true, features = [ "derive", - "bit-vec", -], optional = true } -phf = { version = "0.11", default-features = false } -scale-info = { workspace = true, features = [ - "derive", - "bit-vec", - "decode", ], optional = true } +scale-info = { workspace = true, features = ["derive"], optional = true } serde = { workspace = true, features = ["derive"], optional = true } -serde_json = { workspace = true, optional = true } [dev-dependencies] assert_matches = "1.5.0" [features] -default = ["std", "client"] -parity-scale-codec = [ - "dep:parity-scale-codec", - "dep:hex", - "dep:async-trait", - "starknet_api/parity-scale-codec", - "blockifier/parity-scale-codec", -] -scale-info = [ - "dep:scale-info", - "starknet_api/scale-info", - "blockifier/scale-info", +default = ["std"] +std = [ + "starknet_api/std", + "starknet-ff/std", + "starknet-core/std", + "blockifier/std", + # Optional + "parity-scale-codec?/std", + "scale-info?/std", + "serde?/std", + "serde_json?/std", + "cairo-lang-casm-contract-class?/std", + "cairo-lang-utils?/std", + "cairo-vm?/std", ] -serde = ["dep:serde", "dep:serde_json"] client = [ "std", "dep:flate2", @@ -77,28 +65,20 @@ client = [ "dep:cairo-lang-utils", "dep:num-bigint", "dep:serde_json", + "dep:thiserror", + "dep:cairo-vm", + "dep:cairo-lang-casm-contract-class", ] -std = [ - # Starknet - "starknet-crypto/std", - "starknet-ff/std", - "starknet-ff/serde", - "starknet-core/std", - "blockifier/std", - "starknet_api/std", - # Substrate - "sp-core/std", - "sp-std/std", - "sp-inherents/std", - # Other - "thiserror-no-std/std", - "bitvec/std", - # Optional deps - "serde?/std", - "serde_json?/std", - "parity-scale-codec?/std", - "scale-info?/std", - "hex?/std", - "cairo-lang-utils?/std", - "num-bigint?/std", +parity-scale-codec = [ + "dep:parity-scale-codec", + "mp-felt/parity-scale-codec", + "starknet_api/parity-scale-codec", + "blockifier/parity-scale-codec", +] +scale-info = [ + "dep:scale-info", + "mp-felt/scale-info", + "starknet_api/scale-info", + "blockifier/scale-info", ] +serde = ["dep:serde", "mp-felt/serde"] diff --git a/crates/primitives/starknet/src/transaction/compute_hash.rs b/crates/primitives/transactions/src/compute_hash.rs similarity index 99% rename from crates/primitives/starknet/src/transaction/compute_hash.rs rename to crates/primitives/transactions/src/compute_hash.rs index 0a2af3d806..4ea4245613 100644 --- a/crates/primitives/starknet/src/transaction/compute_hash.rs +++ b/crates/primitives/transactions/src/compute_hash.rs @@ -1,5 +1,7 @@ use alloc::vec::Vec; +use mp_felt::Felt252Wrapper; +use mp_hashers::HasherT; use starknet_core::crypto::compute_hash_on_elements; use starknet_crypto::FieldElement; @@ -8,8 +10,6 @@ use super::{ HandleL1MessageTransaction, InvokeTransaction, InvokeTransactionV0, InvokeTransactionV1, Transaction, UserTransaction, SIMULATE_TX_VERSION_OFFSET, }; -use crate::execution::felt252_wrapper::Felt252Wrapper; -use crate::traits::hash::HasherT; const DECLARE_PREFIX: &[u8] = b"declare"; const DEPLOY_ACCOUNT_PREFIX: &[u8] = b"deploy_account"; diff --git a/crates/primitives/starknet/src/transaction/compute_hash_tests.rs b/crates/primitives/transactions/src/compute_hash_tests.rs similarity index 98% rename from crates/primitives/starknet/src/transaction/compute_hash_tests.rs rename to crates/primitives/transactions/src/compute_hash_tests.rs index ee61e533c9..a68c460404 100644 --- a/crates/primitives/starknet/src/transaction/compute_hash_tests.rs +++ b/crates/primitives/transactions/src/compute_hash_tests.rs @@ -1,15 +1,15 @@ use alloc::sync::Arc; use blockifier::execution::contract_class::ContractClass; +use mp_felt::Felt252Wrapper; +use mp_hashers::pedersen::PedersenHasher; use starknet_api::api_core::{calculate_contract_address, ContractAddress, PatriciaKey}; use starknet_api::hash::StarkFelt; use starknet_api::transaction::Calldata; use starknet_crypto::FieldElement; -use crate::crypto::hash::pedersen::PedersenHasher; -use crate::execution::types::Felt252Wrapper; -use crate::transaction::compute_hash::ComputeTransactionHash; -use crate::transaction::{ +use crate::compute_hash::ComputeTransactionHash; +use crate::{ DeclareTransaction, DeclareTransactionV0, DeclareTransactionV1, DeclareTransactionV2, DeployAccountTransaction, HandleL1MessageTransaction, InvokeTransaction, InvokeTransactionV0, InvokeTransactionV1, Transaction, UserTransaction, diff --git a/crates/primitives/starknet/src/transaction/conversions.rs b/crates/primitives/transactions/src/conversions.rs similarity index 70% rename from crates/primitives/starknet/src/transaction/conversions.rs rename to crates/primitives/transactions/src/conversions.rs index 6db5a76683..53efff2784 100644 --- a/crates/primitives/starknet/src/transaction/conversions.rs +++ b/crates/primitives/transactions/src/conversions.rs @@ -3,19 +3,18 @@ use alloc::sync::Arc; use blockifier::execution::contract_class::ContractClass; use blockifier::transaction::objects::TransactionExecutionResult; use blockifier::transaction::transactions as btx; +use mp_felt::Felt252Wrapper; +use mp_hashers::HasherT; use starknet_api::api_core::Nonce; use starknet_api::hash::StarkFelt; -use starknet_api::state::StorageKey; -use starknet_api::transaction::{EventKey, Fee, TransactionVersion}; -use starknet_api::{api_core as stcore, transaction as sttx}; +use starknet_api::transaction as sttx; +use starknet_api::transaction::{Fee, TransactionVersion}; use super::compute_hash::ComputeTransactionHash; use super::{ DeclareTransaction, DeclareTransactionV0, DeclareTransactionV1, DeclareTransactionV2, DeployAccountTransaction, HandleL1MessageTransaction, InvokeTransaction, InvokeTransactionV0, InvokeTransactionV1, }; -use crate::execution::felt252_wrapper::Felt252Wrapper; -use crate::traits::hash::HasherT; impl DeclareTransactionV0 { fn try_into_executable( @@ -193,138 +192,6 @@ impl HandleL1MessageTransaction { } } -impl From for sttx::TransactionHash { - fn from(value: Felt252Wrapper) -> Self { - Self(value.into()) - } -} - -impl From for Felt252Wrapper { - fn from(value: sttx::TransactionHash) -> Self { - value.0.into() - } -} - -impl From for stcore::Nonce { - fn from(value: Felt252Wrapper) -> Self { - Self(value.into()) - } -} - -impl From for Felt252Wrapper { - fn from(value: stcore::Nonce) -> Self { - value.0.into() - } -} - -impl From for stcore::ClassHash { - fn from(value: Felt252Wrapper) -> Self { - Self(value.into()) - } -} - -impl From for Felt252Wrapper { - fn from(value: stcore::ClassHash) -> Self { - value.0.into() - } -} - -impl From for stcore::CompiledClassHash { - fn from(value: Felt252Wrapper) -> Self { - Self(value.into()) - } -} - -impl From for Felt252Wrapper { - fn from(value: stcore::CompiledClassHash) -> Self { - value.0.into() - } -} - -impl From for stcore::PatriciaKey { - fn from(value: Felt252Wrapper) -> Self { - Self(value.into()) - } -} - -impl From for Felt252Wrapper { - fn from(value: stcore::PatriciaKey) -> Self { - value.0.into() - } -} - -impl From for stcore::ContractAddress { - fn from(value: Felt252Wrapper) -> Self { - Self(value.into()) - } -} - -impl From for Felt252Wrapper { - fn from(value: stcore::ContractAddress) -> Self { - value.0.into() - } -} - -impl From for stcore::EntryPointSelector { - fn from(value: Felt252Wrapper) -> Self { - Self(value.into()) - } -} - -impl From for Felt252Wrapper { - fn from(value: stcore::EntryPointSelector) -> Self { - value.0.into() - } -} - -impl From for sttx::ContractAddressSalt { - fn from(value: Felt252Wrapper) -> Self { - Self(value.into()) - } -} - -impl From for Felt252Wrapper { - fn from(value: sttx::ContractAddressSalt) -> Self { - value.0.into() - } -} - -impl From for StorageKey { - fn from(value: Felt252Wrapper) -> Self { - Self(value.into()) - } -} - -impl From for Felt252Wrapper { - fn from(value: StorageKey) -> Self { - value.0.0.into() - } -} - -impl From for TransactionVersion { - fn from(value: Felt252Wrapper) -> Self { - Self(value.into()) - } -} - -impl From for Felt252Wrapper { - fn from(value: TransactionVersion) -> Self { - value.0.into() - } -} - -impl From for EventKey { - fn from(value: Felt252Wrapper) -> Self { - Self(value.into()) - } -} - -impl From for Felt252Wrapper { - fn from(value: EventKey) -> Self { - value.0.into() - } -} - fn vec_of_felt_to_signature(felts: &[Felt252Wrapper]) -> sttx::TransactionSignature { sttx::TransactionSignature(felts.iter().map(|&f| f.into()).collect()) } diff --git a/crates/primitives/starknet/src/transaction/execution.rs b/crates/primitives/transactions/src/execution.rs similarity index 99% rename from crates/primitives/starknet/src/transaction/execution.rs rename to crates/primitives/transactions/src/execution.rs index 3021ef91a8..102823e2b1 100644 --- a/crates/primitives/starknet/src/transaction/execution.rs +++ b/crates/primitives/transactions/src/execution.rs @@ -22,15 +22,15 @@ use blockifier::transaction::transaction_utils::{update_remaining_gas, verify_no use blockifier::transaction::transactions::{ DeclareTransaction, DeployAccountTransaction, Executable, InvokeTransaction, L1HandlerTransaction, }; +use mp_fee::{calculate_tx_fee, charge_fee, compute_transaction_resources}; +use mp_felt::Felt252Wrapper; +use mp_state::{FeeConfig, StateChanges}; use starknet_api::api_core::{ContractAddress, EntryPointSelector, Nonce}; use starknet_api::deprecated_contract_class::EntryPointType; use starknet_api::hash::StarkFelt; use starknet_api::transaction::{Calldata, Fee, TransactionSignature, TransactionVersion}; use super::SIMULATE_TX_VERSION_OFFSET; -use crate::execution::felt252_wrapper::Felt252Wrapper; -use crate::fees::{calculate_tx_fee, charge_fee, compute_transaction_resources}; -use crate::state::{FeeConfig, StateChanges}; const TX_INITIAL_AVAILABLE_GAS: u64 = INITIAL_GAS_COST - TRANSACTION_GAS_COST; diff --git a/crates/primitives/starknet/src/transaction/from_broadcasted_transactions.rs b/crates/primitives/transactions/src/from_broadcasted_transactions.rs similarity index 98% rename from crates/primitives/starknet/src/transaction/from_broadcasted_transactions.rs rename to crates/primitives/transactions/src/from_broadcasted_transactions.rs index 948c99b088..d0fde1252f 100644 --- a/crates/primitives/starknet/src/transaction/from_broadcasted_transactions.rs +++ b/crates/primitives/transactions/src/from_broadcasted_transactions.rs @@ -10,6 +10,7 @@ use cairo_lang_starknet::contract_class_into_casm_contract_class::StarknetSierra use cairo_lang_utils::bigint::BigUintAsHex; use cairo_vm::types::program::Program; use flate2::read::GzDecoder; +use mp_felt::Felt252Wrapper; use num_bigint::{BigInt, BigUint, Sign}; use starknet_api::api_core::EntryPointSelector; use starknet_api::deprecated_contract_class::{EntryPoint, EntryPointOffset, EntryPointType}; @@ -25,10 +26,9 @@ use starknet_core::types::{ LegacyEntryPointsByType, SierraEntryPoint, }; use starknet_crypto::FieldElement; -use thiserror_no_std::Error; +use thiserror::Error; use super::{DeclareTransaction, DeclareTransactionV1, DeclareTransactionV2, UserTransaction}; -use crate::execution::felt252_wrapper::Felt252Wrapper; #[derive(Debug, Error)] pub enum BroadcastedTransactionConversionError { @@ -353,7 +353,7 @@ mod tests { const CAIRO_1_NO_VALIDATE_ACCOUNT_COMPILED_CLASS_HASH: &str = "0xdf4d3042eec107abe704619f13d92bbe01a58029311b7a1886b23dcbb4ea87"; fn get_compressed_legacy_contract_class() -> CompressedLegacyContractClass { - let contract_class_bytes = include_bytes!("../../../../../cairo-contracts/build/test.json"); + let contract_class_bytes = include_bytes!("../../../../cairo-contracts/build/test.json"); let contract_class: LegacyContractClass = serde_json::from_slice(contract_class_bytes).unwrap(); let compressed_contract_class: CompressedLegacyContractClass = contract_class.compress().unwrap(); @@ -365,7 +365,7 @@ mod tests { // when HelloStarknet is compiled into Sierra, the output does not have inputs: [] in the events ABI // this has been manually added right now because starknet-rs expects it let contract_class_bytes = - include_bytes!("../../../../../cairo-contracts/build/cairo_1/HelloStarknet.sierra.json"); + include_bytes!("../../../../cairo-contracts/build/cairo_1/HelloStarknet.sierra.json"); let contract_class: SierraClass = serde_json::from_slice(contract_class_bytes).unwrap(); let flattened_contract_class: FlattenedSierraClass = contract_class.flatten().unwrap(); diff --git a/crates/primitives/starknet/src/transaction/getters.rs b/crates/primitives/transactions/src/getters.rs similarity index 99% rename from crates/primitives/starknet/src/transaction/getters.rs rename to crates/primitives/transactions/src/getters.rs index 9f955d1bdd..68c9589017 100644 --- a/crates/primitives/starknet/src/transaction/getters.rs +++ b/crates/primitives/transactions/src/getters.rs @@ -1,7 +1,8 @@ use alloc::vec::Vec; +use mp_felt::Felt252Wrapper; + use super::{DeclareTransaction, DeployAccountTransaction, InvokeTransaction, Transaction, UserTransaction}; -use crate::execution::felt252_wrapper::Felt252Wrapper; impl Transaction { pub fn signature(&self) -> Vec { diff --git a/crates/primitives/starknet/src/transaction/mod.rs b/crates/primitives/transactions/src/lib.rs similarity index 98% rename from crates/primitives/starknet/src/transaction/mod.rs rename to crates/primitives/transactions/src/lib.rs index a903df5f3f..0b8de96c01 100644 --- a/crates/primitives/starknet/src/transaction/mod.rs +++ b/crates/primitives/transactions/src/lib.rs @@ -1,4 +1,9 @@ //! Starknet transaction related functionality. +#![cfg_attr(not(feature = "std"), no_std)] + +#[doc(hidden)] +pub extern crate alloc; + pub mod compute_hash; pub mod conversions; pub mod execution; @@ -7,8 +12,7 @@ pub mod from_broadcasted_transactions; pub mod getters; #[cfg(feature = "client")] pub mod to_starknet_core_transaction; -/// Types related to transactions. -// pub mod types; + use alloc::vec::Vec; use blockifier::execution::contract_class::ContractClass; @@ -22,7 +26,7 @@ const SIMULATE_TX_VERSION_OFFSET: FieldElement = /// Functions related to transaction conversions // pub mod utils; -use crate::execution::felt252_wrapper::Felt252Wrapper; +use mp_felt::Felt252Wrapper; /// Wrapper type for transaction execution error. /// Different tx types. diff --git a/crates/primitives/starknet/src/transaction/to_starknet_core_transaction.rs b/crates/primitives/transactions/src/to_starknet_core_transaction.rs similarity index 98% rename from crates/primitives/starknet/src/transaction/to_starknet_core_transaction.rs rename to crates/primitives/transactions/src/to_starknet_core_transaction.rs index 4acf3e38f3..53cef64453 100644 --- a/crates/primitives/starknet/src/transaction/to_starknet_core_transaction.rs +++ b/crates/primitives/transactions/src/to_starknet_core_transaction.rs @@ -1,10 +1,10 @@ use std::vec::Vec; +use mp_felt::Felt252Wrapper; +use mp_hashers::HasherT; use starknet_crypto::FieldElement; use super::compute_hash::ComputeTransactionHash; -use crate::execution::felt252_wrapper::Felt252Wrapper; -use crate::traits::hash::HasherT; fn cast_vec_of_felt_252_wrappers(data: Vec) -> Vec { // Non-copy but less dangerous than transmute diff --git a/crates/primitives/starknet/src/transaction/utils.rs b/crates/primitives/transactions/src/utils.rs similarity index 100% rename from crates/primitives/starknet/src/transaction/utils.rs rename to crates/primitives/transactions/src/utils.rs diff --git a/crates/runtime/Cargo.toml b/crates/runtime/Cargo.toml index 42197c47dd..4debe3151f 100644 --- a/crates/runtime/Cargo.toml +++ b/crates/runtime/Cargo.toml @@ -49,8 +49,11 @@ frame-system-benchmarking = { workspace = true, optional = true } # Madara Local Dependencies # Madara Pallets pallet-starknet = { workspace = true } + # Madara Primitives -mp-starknet = { workspace = true } +mp-felt = { workspace = true } +mp-hashers = { workspace = true } +mp-transactions = { workspace = true } # Starknet dependencies blockifier = { workspace = true } starknet-ff = { workspace = true } diff --git a/crates/runtime/src/lib.rs b/crates/runtime/src/lib.rs index 26c107e214..9a6c4e66e7 100644 --- a/crates/runtime/src/lib.rs +++ b/crates/runtime/src/lib.rs @@ -27,9 +27,9 @@ pub use frame_support::weights::{IdentityFee, Weight}; pub use frame_support::{construct_runtime, parameter_types, StorageValue}; pub use frame_system::Call as SystemCall; use frame_system::{EventRecord, Phase}; -use mp_starknet::execution::types::Felt252Wrapper; -use mp_starknet::transaction::compute_hash::ComputeTransactionHash; -use mp_starknet::transaction::{Transaction, TxType, UserTransaction}; +use mp_felt::Felt252Wrapper; +use mp_transactions::compute_hash::ComputeTransactionHash; +use mp_transactions::{Transaction, TxType, UserTransaction}; use pallet_grandpa::{fg_primitives, AuthorityId as GrandpaId, AuthorityList as GrandpaAuthorityList}; /// Import the StarkNet pallet. pub use pallet_starknet; diff --git a/crates/runtime/src/pallets.rs b/crates/runtime/src/pallets.rs index ba045bd1fb..e6ba418192 100644 --- a/crates/runtime/src/pallets.rs +++ b/crates/runtime/src/pallets.rs @@ -10,7 +10,6 @@ pub use frame_support::weights::constants::{ pub use frame_support::weights::{IdentityFee, Weight}; pub use frame_support::{construct_runtime, parameter_types, StorageValue}; pub use frame_system::Call as SystemCall; -use mp_starknet::constants::SN_GOERLI_CHAIN_ID; /// Import the StarkNet pallet. pub use pallet_starknet; pub use pallet_timestamp::Call as TimestampCall; @@ -24,6 +23,14 @@ use sp_std::marker::PhantomData; use crate::*; +/// ChainId for Starknet Goerli testnet +pub const SN_GOERLI_CHAIN_ID: Felt252Wrapper = Felt252Wrapper(starknet_ff::FieldElement::from_mont([ + 3753493103916128178, + 18446744073709548950, + 18446744073709551615, + 398700013197595345, +])); + // Configure FRAME pallets to include in runtime. // -------------------------------------- diff --git a/crates/runtime/src/types.rs b/crates/runtime/src/types.rs index 8cbaca9de2..37e6c5f467 100644 --- a/crates/runtime/src/types.rs +++ b/crates/runtime/src/types.rs @@ -28,4 +28,4 @@ pub type InvokeTxMaxNSteps = u32; /// The maximum amount of steps allowed for validation. (?) pub type ValidateMaxNSteps = u32; -pub type StarknetHasher = mp_starknet::crypto::hash::pedersen::PedersenHasher; +pub type StarknetHasher = mp_hashers::pedersen::PedersenHasher;