Skip to content
This repository has been archived by the owner on Aug 2, 2024. It is now read-only.

Wip rebase latest blockifier #1527

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
643 changes: 382 additions & 261 deletions Cargo.lock

Large diffs are not rendered by default.

190 changes: 89 additions & 101 deletions Cargo.toml

Large diffs are not rendered by default.

3 changes: 2 additions & 1 deletion configs/genesis-assets/genesis.json
Original file line number Diff line number Diff line change
Expand Up @@ -295,5 +295,6 @@
"0x1"
]
],
"fee_token_address": "0x49d36570d4e46f48e99674bd3fcc84644ddd6b96f7c741b1562b82f9e004dc7"
"eth_fee_token_address": "0x49d36570d4e46f48e99674bd3fcc84644ddd6b96f7c741b1562b82f9e004dc7",
"strk_fee_token_address": "0x49d36570d4e46f48e99674bd3fcc84644ddd6b96f7c741b1562b82f9e004dc8"
}
2 changes: 1 addition & 1 deletion configs/index.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
},
{
"name": "genesis.json",
"sha3_256": "954b26a63a70ba92cbc79028fbdaf4f70105ba32ff71b60a24ecce775b4dd90c"
"sha3_256": "4e7792f7155d504e87fc57b6bed255543cdf355400feaa81890f3842cf6e3839"
},
{
"name": "NoValidateAccount.casm.json",
Expand Down
2 changes: 1 addition & 1 deletion crates/client/commitment-state-diff/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ use sc_client_api::{StorageEventStream, StorageNotification};
use sp_api::ProvideRuntimeApi;
use sp_blockchain::HeaderBackend;
use sp_runtime::traits::{Block as BlockT, Header};
use starknet_api::api_core::{ClassHash, CompiledClassHash, ContractAddress, Nonce, PatriciaKey};
use starknet_api::block::BlockHash;
use starknet_api::core::{ClassHash, CompiledClassHash, ContractAddress, Nonce, PatriciaKey};
use starknet_api::hash::{StarkFelt, StarkHash};
use starknet_api::state::{StorageKey as StarknetStorageKey, ThinStateDiff};
use thiserror::Error;
Expand Down
2 changes: 1 addition & 1 deletion crates/client/data-availability/src/utils.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use ethers::types::U256;
use mc_commitment_state_diff::BlockDAData;
use starknet_api::api_core::{Nonce, PatriciaKey};
use starknet_api::core::{Nonce, PatriciaKey};
use starknet_api::hash::StarkFelt;
use url::{ParseError, Url};

Expand Down
4 changes: 1 addition & 3 deletions crates/client/db/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,7 @@ sc-client-db = { workspace = true, default-features = true }
sp-core = { workspace = true, default-features = true }
sp-database = { workspace = true, default-features = true }
sp-runtime = { workspace = true, default-features = true }
starknet_api = { workspace = true, default-features = true, features = [
"parity-scale-codec",
] }
starknet_api = { workspace = true, default-features = true }
thiserror = { workspace = true }
uuid = "1.7.0"

Expand Down
15 changes: 10 additions & 5 deletions crates/client/db/src/mapping_db.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,18 @@ use std::sync::{Arc, Mutex};
use parity_scale_codec::{Decode, Encode};
use sp_database::Database;
use sp_runtime::traits::Block as BlockT;
use starknet_api::block::BlockHash;
use starknet_api::hash::StarkHash;
use starknet_api::transaction::TransactionHash;

use crate::{DbError, DbHash};

/// The mapping to write in db
#[derive(Debug)]
pub struct MappingCommitment<B: BlockT> {
pub block_hash: B::Hash,
pub starknet_block_hash: StarkHash,
pub starknet_transaction_hashes: Vec<StarkHash>,
pub starknet_block_hash: BlockHash,
pub starknet_transaction_hashes: Vec<TransactionHash>,
}

/// Allow interaction with the mapping db
Expand Down Expand Up @@ -44,7 +46,7 @@ impl<B: BlockT> MappingDb<B> {
///
/// Under some circumstances it can return multiples blocks hashes, meaning that the result has
/// to be checked against the actual blockchain state in order to find the good one.
pub fn block_hash(&self, starknet_block_hash: StarkHash) -> Result<Option<Vec<B::Hash>>, DbError> {
pub fn block_hash(&self, starknet_block_hash: BlockHash) -> Result<Option<Vec<B::Hash>>, DbError> {
match self.db.get(crate::columns::BLOCK_MAPPING, &starknet_block_hash.encode()) {
Some(raw) => Ok(Some(Vec::<B::Hash>::decode(&mut &raw[..])?)),
None => Ok(None),
Expand Down Expand Up @@ -121,7 +123,10 @@ impl<B: BlockT> MappingDb<B> {
/// * `transaction_hash` - the transaction hash to search for. H256 is used here because it's a
/// native type of substrate, and we are sure it's SCALE encoding is optimized and will not
/// change.
pub fn block_hash_from_transaction_hash(&self, transaction_hash: StarkHash) -> Result<Option<B::Hash>, DbError> {
pub fn block_hash_from_transaction_hash(
&self,
transaction_hash: TransactionHash,
) -> Result<Option<B::Hash>, DbError> {
match self.db.get(crate::columns::TRANSACTION_MAPPING, &transaction_hash.encode()) {
Some(raw) => Ok(Some(<B::Hash>::decode(&mut &raw[..])?)),
None => Ok(None),
Expand All @@ -144,7 +149,7 @@ impl<B: BlockT> MappingDb<B> {
/// - The provided `starknet_hash` is not present in the cache.
pub fn cached_transaction_hashes_from_block_hash(
&self,
starknet_block_hash: StarkHash,
starknet_block_hash: BlockHash,
) -> Result<Option<Vec<StarkHash>>, DbError> {
if !self.cache_more_things {
// The cache is not enabled, no need to even touch the database.
Expand Down
2 changes: 1 addition & 1 deletion crates/client/db/src/sierra_classes_db.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use std::sync::Arc;

use parity_scale_codec::{Decode, Encode};
use sp_database::Database;
use starknet_api::api_core::ClassHash;
use starknet_api::core::ClassHash;
use starknet_api::state::ContractClass;

use crate::{DbError, DbHash};
Expand Down
2 changes: 2 additions & 0 deletions crates/client/l1-messages/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ pallet-starknet-runtime-api = { workspace = true, default-features = true }

# Starknet dependencies
starknet_api = { workspace = true, default-features = true }
blockifier = { workspace = true, default-features = true }

# Starknet
mc-db = { workspace = true, default-features = true }
Expand All @@ -29,6 +30,7 @@ mc-eth-client = { workspace = true }
# Madara Primitives
mp-felt = { workspace = true, default-features = true }
mp-transactions = { workspace = true, default-features = true }
mp-hashers = { workspace = true, default-features = true }

# Substrate Primitives
sp-api = { workspace = true, default-features = true }
Expand Down
46 changes: 30 additions & 16 deletions crates/client/l1-messages/src/contract.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
use std::sync::Arc;

use mp_felt::{Felt252Wrapper, Felt252WrapperError};
use mp_transactions::HandleL1MessageTransaction;
use starknet_api::hash::StarkFelt;
use starknet_api::transaction::{Calldata, L1HandlerTransaction, TransactionVersion};
use starknet_core_contract_client::interfaces::LogMessageToL2Filter;

#[derive(thiserror::Error, Debug, PartialEq)]
Expand All @@ -19,35 +22,46 @@ pub enum L1EventToTransactionError {

pub fn parse_handle_l1_message_transaction(
event: LogMessageToL2Filter,
) -> Result<HandleL1MessageTransaction, L1EventToTransactionError> {
) -> Result<L1HandlerTransaction, L1EventToTransactionError> {
// L1 from address.
let from_address = Felt252Wrapper::try_from(sp_core::U256::from_big_endian(event.from_address.as_bytes()))
.map_err(L1EventToTransactionError::InvalidFromAddress)?;
.map_err(L1EventToTransactionError::InvalidFromAddress)?
.into();

// L2 contract to call.
let contract_address = Felt252Wrapper::try_from(sp_core::U256(event.to_address.0))
.map_err(L1EventToTransactionError::InvalidContractAddress)?;
.map_err(L1EventToTransactionError::InvalidContractAddress)?
.into();

// Function of the contract to call.
let entry_point_selector = Felt252Wrapper::try_from(sp_core::U256(event.selector.0))
.map_err(L1EventToTransactionError::InvalidEntryPointSelector)?;
.map_err(L1EventToTransactionError::InvalidEntryPointSelector)?
.into();

// L1 message nonce.
let nonce: u64 = Felt252Wrapper::try_from(sp_core::U256(event.nonce.0))
.map_err(L1EventToTransactionError::InvalidNonce)?
.try_into()
.map_err(L1EventToTransactionError::InvalidNonce)?;
let nonce =
Felt252Wrapper::try_from(sp_core::U256(event.nonce.0)).map_err(L1EventToTransactionError::InvalidNonce)?.into();

let event_payload: Vec<Felt252Wrapper> = event
let event_payload: Vec<_> = event
.payload
.iter()
.map(|param| Felt252Wrapper::try_from(sp_core::U256(param.0)))
.collect::<Result<Vec<Felt252Wrapper>, Felt252WrapperError>>()
.map(|param| Felt252Wrapper::try_from(sp_core::U256(param.0)).map(StarkFelt::from))
.collect::<Result<Vec<_>, Felt252WrapperError>>()
.map_err(L1EventToTransactionError::InvalidCalldata)?;

let mut calldata: Vec<Felt252Wrapper> = Vec::with_capacity(event.payload.len() + 1);
calldata.push(from_address);
event_payload.iter().collect_into(&mut calldata);
let calldata = {
let mut calldata: Vec<_> = Vec::with_capacity(event.payload.len() + 1);
calldata.push(from_address);
event_payload.iter().collect_into(&mut calldata);

Calldata(Arc::new(calldata))
};

Ok(HandleL1MessageTransaction { nonce, contract_address, entry_point_selector, calldata })
Ok(L1HandlerTransaction {
nonce,
contract_address,
entry_point_selector,
calldata,
version: TransactionVersion(StarkFelt::ZERO),
})
}
22 changes: 13 additions & 9 deletions crates/client/l1-messages/src/worker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,12 @@ use std::sync::Arc;
use ethers::providers::{Http, Provider, StreamExt};
use ethers::types::U256;
pub use mc_eth_client::config::EthereumClientConfig;
use mp_transactions::HandleL1MessageTransaction;
use mp_transactions::compute_hash::ComputeTransactionHash;
use pallet_starknet_runtime_api::{ConvertTransactionRuntimeApi, StarknetRuntimeApi};
use sc_client_api::HeaderBackend;
use sc_transaction_pool_api::{TransactionPool, TransactionSource};
use sp_api::ProvideRuntimeApi;
use sp_runtime::traits::Block as BlockT;
use starknet_api::api_core::Nonce;
use starknet_api::hash::StarkFelt;
use starknet_api::transaction::Fee;
use starknet_core_contract_client::interfaces::{LogMessageToL2Filter, StarknetMessagingEvents};

Expand Down Expand Up @@ -74,7 +72,7 @@ pub async fn run_worker<C, P, B>(
meta.log_index
);

match process_l1_message(
match process_l1_message::<_, _, _, _>(
event,
&client,
&pool,
Expand Down Expand Up @@ -127,19 +125,19 @@ where
{
// Check against panic
// https://docs.rs/ethers/latest/ethers/types/struct.U256.html#method.as_u128
let fee: Fee = if event.fee > U256::from_big_endian(&(u128::MAX.to_be_bytes())) {
let paid_fee_on_l1: Fee = if event.fee > U256::from_big_endian(&(u128::MAX.to_be_bytes())) {
return Err(L1MessagesWorkerError::ToFeeError);
} else {
Fee(event.fee.as_u128())
};
let transaction: HandleL1MessageTransaction = parse_handle_l1_message_transaction(event)?;
let tx = parse_handle_l1_message_transaction(event)?;

let best_block_hash = client.info().best_hash;

match client.runtime_api().l1_nonce_unused(best_block_hash, Nonce(StarkFelt::from(transaction.nonce))) {
match client.runtime_api().l1_nonce_unused(best_block_hash, tx.nonce) {
Ok(true) => Ok(()),
Ok(false) => {
log::debug!("⟠ Event already processed: {:?}", transaction);
log::debug!("⟠ Event already processed: {:?}", tx);
return Ok(None);
}
Err(e) => {
Expand All @@ -148,7 +146,13 @@ where
}
}?;

let extrinsic = client.runtime_api().convert_l1_transaction(best_block_hash, transaction, fee).map_err(|e| {
let chain_id =
client.runtime_api().chain_id(best_block_hash).map_err(|e| L1MessagesWorkerError::RuntimeApiError(e.into()))?;
// TODO: Find a way not to hardcode that
let tx_hash = tx.compute_hash(chain_id, false);
let transaction = blockifier::transaction::transactions::L1HandlerTransaction { tx, tx_hash, paid_fee_on_l1 };

let extrinsic = client.runtime_api().convert_l1_transaction(best_block_hash, transaction).map_err(|e| {
log::error!("⟠ Failed to convert L1 Transaction via Runtime Api: {:?}", e);
L1MessagesWorkerError::ConvertTransactionRuntimeApiError(e)
})?;
Expand Down
1 change: 1 addition & 0 deletions crates/client/mapping-sync/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -30,3 +30,4 @@ sp-api = { workspace = true }
sp-blockchain = { workspace = true }
sp-core = { workspace = true }
sp-runtime = { workspace = true }
blockifier = { workspace = true }
8 changes: 5 additions & 3 deletions crates/client/mapping-sync/src/sync_blocks.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
use blockifier::transaction::account_transaction::AccountTransaction;
use blockifier::transaction::transaction_execution::Transaction;
use mc_rpc_core::utils::get_block_by_block_hash;
use mp_digest_log::{find_starknet_block, FindLogError};
use mp_hashers::HasherT;
use mp_transactions::compute_hash::ComputeTransactionHash;
use mp_transactions::get_transaction_hash;
use pallet_starknet_runtime_api::StarknetRuntimeApi;
use sc_client_api::backend::{Backend, StorageProvider};
use sp_api::ProvideRuntimeApi;
Expand Down Expand Up @@ -36,16 +39,15 @@ where
db state ({storage_starknet_block_hash:?})"
))
} else {
let chain_id = client.runtime_api().chain_id(substrate_block_hash)?;

// Success, we write the Starknet to Substate hashes mapping to db
let mapping_commitment = mc_db::MappingCommitment {
block_hash: substrate_block_hash,
starknet_block_hash: digest_starknet_block_hash.into(),
starknet_transaction_hashes: digest_starknet_block
.transactions()
.iter()
.map(|tx| tx.compute_hash::<H>(chain_id, false).into())
.map(get_transaction_hash)
.cloned()
.collect(),
};

Expand Down
2 changes: 1 addition & 1 deletion crates/client/rpc-core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ targets = ["x86_64-unknown-linux-gnu"]
anyhow = { workspace = true }
blockifier = { workspace = true, default-features = true }
cairo-lang-casm = { workspace = true }
cairo-lang-casm-contract-class = { workspace = true }
cairo-lang-starknet-classes = { workspace = true }
cairo-lang-starknet = { workspace = true }
cairo-lang-utils = { workspace = true }
cairo-vm = { workspace = true, default-features = true }
Expand Down
Loading