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

Commit

Permalink
rebase blockifier
Browse files Browse the repository at this point in the history
it compiles

madara runs again

wip

compiles again: only the tests left to fix

tests compile, time to fix them

remove all default-features option from Cargo.toml

new custom execution logic compiles, fewer test fail

pallet unitest green

all rpc tests ok

clippy
  • Loading branch information
tdelabro committed Apr 17, 2024
1 parent b1ba579 commit 1b06b3b
Show file tree
Hide file tree
Showing 130 changed files with 5,871 additions and 6,144 deletions.
668 changes: 382 additions & 286 deletions Cargo.lock

Large diffs are not rendered by default.

195 changes: 92 additions & 103 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 @@ -280,6 +280,7 @@
"0x1"
]
],
"fee_token_address": "0x49d36570d4e46f48e99674bd3fcc84644ddd6b96f7c741b1562b82f9e004dc7",
"eth_fee_token_address": "0x49d36570d4e46f48e99674bd3fcc84644ddd6b96f7c741b1562b82f9e004dc7",
"strk_fee_token_address": "0x49d36570d4e46f48e99674bd3fcc84644ddd6b96f7c741b1562b82f9e004dc8",
"chain_id": "MADARA"
}
2 changes: 1 addition & 1 deletion configs/index.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
},
{
"name": "genesis.json",
"sha3_256": "c98a3e0e31a2b2c284323b73029edfeec1b8095f39c44298bc29e98551bcc0d6"
"sha3_256": "ec869153b70b838f5c58d602948145552bf9e8fcc1e22f28bd9250d3cf321993"
},
{
"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
4 changes: 2 additions & 2 deletions 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 Expand Up @@ -30,7 +30,7 @@ pub fn block_data_to_calldata(mut block_da_data: BlockDAData) -> Vec<U256> {
.get(&addr)
.or_else(|| block_da_data.state_diff.replaced_classes.get(&addr));

let nonce = block_da_data.state_diff.nonces.remove(&addr);
let nonce = block_da_data.state_diff.nonces.swap_remove(&addr);
calldata.push(da_word(class_flag.is_some(), nonce, writes.len() as u64));

if let Some(class_hash) = class_flag {
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),
})
}
21 changes: 12 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,12 @@ 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(L1MessagesWorkerError::RuntimeApiError)?;
// 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 @@ -32,3 +32,4 @@ sp-api = { workspace = true }
sp-blockchain = { workspace = true }
sp-core = { workspace = true }
sp-runtime = { workspace = true }
blockifier = { workspace = true }
23 changes: 18 additions & 5 deletions crates/client/mapping-sync/src/block_metrics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,10 @@ pub struct BlockMetrics {
pub block_height: Gauge,
pub transaction_count: Counter,
pub event_count: Counter,
pub l1_gas_price_wei: Gauge,
pub l1_gas_price_strk: Gauge,
pub eth_l1_gas_price_wei: Gauge,
pub strk_l1_gas_price_fri: Gauge,
pub eth_l1_data_gas_price_wei: Gauge,
pub strk_l1_data_gas_price_fri: Gauge,
}

impl BlockMetrics {
Expand All @@ -19,9 +21,20 @@ impl BlockMetrics {
registry,
)?,
event_count: register(Counter::new("madara_event_count", "Counter for madara event count")?, registry)?,
l1_gas_price_wei: register(Gauge::new("madara_l1_gas_price", "Gauge for madara l1 gas price")?, registry)?,
l1_gas_price_strk: register(
Gauge::new("madara_l1_gas_price_strk", "Gauge for madara l1 gas price in strk")?,
eth_l1_gas_price_wei: register(
Gauge::new("madara_l1_gas_price_eth", "Gauge for madara l1 gas price in eth wei")?,
registry,
)?,
strk_l1_gas_price_fri: register(
Gauge::new("madara_l1_gas_price_strk", "Gauge for madara l1 gas price in strk fri")?,
registry,
)?,
eth_l1_data_gas_price_wei: register(
Gauge::new("madara_l1_data_gas_price_eth", "Gauge for madara l1 data gas price in eth wei")?,
registry,
)?,
strk_l1_data_gas_price_fri: register(
Gauge::new("madara_l1_data_gas_price_strk", "Gauge for madara l1 data gas price in strk fri")?,
registry,
)?,
})
Expand Down
28 changes: 18 additions & 10 deletions crates/client/mapping-sync/src/sync_blocks.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
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 num_traits::FromPrimitive;
use pallet_starknet_runtime_api::StarknetRuntimeApi;
use prometheus_endpoint::prometheus::core::Number;
Expand Down Expand Up @@ -45,16 +45,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 All @@ -70,13 +69,22 @@ where
block_metrics
.event_count
.inc_by(f64::from_u128(starknet_block.header().event_count).unwrap_or(f64::MIN));
block_metrics.l1_gas_price_wei.set(
f64::from_u128(starknet_block.header().l1_gas_price.price_in_wei).unwrap_or(f64::MIN),
block_metrics.eth_l1_gas_price_wei.set(
f64::from_u128(starknet_block.header().l1_gas_price.eth_l1_gas_price.into())
.unwrap_or(f64::MIN),
);
block_metrics.strk_l1_gas_price_fri.set(
f64::from_u128(starknet_block.header().l1_gas_price.strk_l1_gas_price.into())
.unwrap_or(f64::MIN),
);
block_metrics.eth_l1_gas_price_wei.set(
f64::from_u128(starknet_block.header().l1_gas_price.eth_l1_data_gas_price.into())
.unwrap_or(f64::MIN),
);
block_metrics.strk_l1_data_gas_price_fri.set(
f64::from_u128(starknet_block.header().l1_gas_price.strk_l1_data_gas_price.into())
.unwrap_or(f64::MIN),
);

block_metrics
.l1_gas_price_strk
.set(starknet_block.header().l1_gas_price.price_in_strk.unwrap_or(0).into_f64());
}

backend.mapping().write_hashes(mapping_commitment).map_err(|e| anyhow::anyhow!(e))
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
5 changes: 3 additions & 2 deletions crates/client/rpc-core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ use starknet_core::types::{
BroadcastedInvokeTransaction, BroadcastedTransaction, ContractClass, DeclareTransactionResult,
DeployAccountTransactionResult, EventFilterWithPage, EventsPage, FeeEstimate, FieldElement, FunctionCall,
InvokeTransactionResult, MaybePendingBlockWithTxHashes, MaybePendingBlockWithTxs, MaybePendingStateUpdate,
MaybePendingTransactionReceipt, MsgFromL1, SimulatedTransaction, SimulationFlag, SyncStatusType, Transaction,
TransactionTrace, TransactionTraceWithHash,
MaybePendingTransactionReceipt, MsgFromL1, SimulatedTransaction, SimulationFlag, SimulationFlagForEstimateFee,
SyncStatusType, Transaction, TransactionTrace, TransactionTraceWithHash,
};

#[serde_as]
Expand Down Expand Up @@ -137,6 +137,7 @@ pub trait StarknetReadRpcApi {
async fn estimate_fee(
&self,
request: Vec<BroadcastedTransaction>,
simulation_flags: Vec<SimulationFlagForEstimateFee>,
block_id: BlockId,
) -> RpcResult<Vec<FeeEstimate>>;

Expand Down
Loading

0 comments on commit 1b06b3b

Please sign in to comment.