Skip to content

Commit

Permalink
Merge pull request #57 from Tbelleng/feat/up_blockifier
Browse files Browse the repository at this point in the history
Feat : ✅ Add Pending_block for block_with_receipt RPC methods + resolve fews conflicts
  • Loading branch information
antiyro authored Apr 17, 2024
2 parents bf69333 + eb60c61 commit ef90311
Show file tree
Hide file tree
Showing 8 changed files with 50 additions and 39 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ git # Deoxys Changelog

## Next release

- feat(rpc): add pending block to `get_block_with_receipts` rpc call
- chore: update bonsai-trie (benefit from perf boost)
- feat(rpc): add `get_block_with_receipts` rpc call
- refactor: remove crate mp-state, mp-fee, mp-messages
Expand Down
2 changes: 1 addition & 1 deletion crates/client/rpc/src/methods/read/estimate_message_fee.rs
Original file line number Diff line number Diff line change
Expand Up @@ -108,5 +108,5 @@ pub fn convert_message_into_tx<H: HasherT + Send + Sync + 'static>(
};
let tx_hash = tx.compute_hash::<H>(chain_id, true, block_number);

L1HandlerTransaction { tx, tx_hash, paid_fee_on_l1: Fee(10) }
L1HandlerTransaction { tx, tx_hash, paid_fee_on_l1: Fee(10) } //TODO: fix with real fee
}
67 changes: 37 additions & 30 deletions crates/client/rpc/src/methods/read/get_block_with_receipts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,10 @@ use sc_transaction_pool::ChainApi;
use sc_transaction_pool_api::TransactionPool;
use sp_api::ProvideRuntimeApi;
use sp_blockchain::HeaderBackend;
use starknet_core::types::{BlockId, BlockWithReceipts, MaybePendingBlockWithReceipts, TransactionWithReceipt};
use starknet_core::types::{
BlockId, BlockTag, BlockWithReceipts, MaybePendingBlockWithReceipts, PendingBlockWithReceipts,
TransactionWithReceipt,
};

use super::get_transaction_receipt::get_transaction_receipt_finalized;
use crate::errors::StarknetRpcApiError;
Expand All @@ -22,7 +25,6 @@ use crate::utils::{
};
use crate::Starknet;

// TODO: Implem pending block
pub fn get_block_with_receipts<A, BE, G, C, P, H>(
starknet: &Starknet<A, BE, G, C, P, H>,
block_id: BlockId,
Expand All @@ -42,11 +44,13 @@ where
StarknetRpcApiError::BlockNotFound
})?;

let is_pending = matches!(block_id, BlockId::Tag(BlockTag::Pending));

let starknet_block = get_block_by_block_hash(starknet.client.as_ref(), substrate_block_hash).map_err(|e| {
log::error!("Failed to get block for block hash {substrate_block_hash}: '{e}'");
StarknetRpcApiError::InternalServerError
})?;
let block_hash = starknet_block.header().hash::<H>();

let chain_id = starknet.chain_id()?;

let transactions_with_receipts = starknet_block
Expand All @@ -73,32 +77,35 @@ where
})
.collect::<Vec<TransactionWithReceipt>>();

let status = status(starknet_block.header().block_number);
let parent_hash = parent_hash(&starknet_block);
let new_root = new_root(&starknet_block);
let timestamp = timestamp(&starknet_block);
let sequencer_address = sequencer_address(&starknet_block);
let l1_gas_price = l1_gas_price(&starknet_block);
let l1_data_gas_price = l1_data_gas_price(&starknet_block);
let l1_da_mode = l1_da_mode(&starknet_block);
let starknet_version = starknet_version(&starknet_block);

let block_with_receipts = BlockWithReceipts {
status,
block_hash: block_hash.into(),
parent_hash,
block_number: starknet_block.header().block_number,
new_root,
timestamp,
sequencer_address,
l1_gas_price,
l1_data_gas_price,
l1_da_mode,
starknet_version,
transactions: transactions_with_receipts,
};

let block = MaybePendingBlockWithReceipts::Block(block_with_receipts);
if is_pending {
let pending_block_with_receipts = PendingBlockWithReceipts {
transactions: transactions_with_receipts,
parent_hash: parent_hash(&starknet_block),
timestamp: timestamp(&starknet_block),
sequencer_address: sequencer_address(&starknet_block),
l1_gas_price: l1_gas_price(&starknet_block),
l1_data_gas_price: l1_data_gas_price(&starknet_block),
l1_da_mode: l1_da_mode(&starknet_block),
starknet_version: starknet_version(&starknet_block),
};

Ok(block)
let pending_block = MaybePendingBlockWithReceipts::PendingBlock(pending_block_with_receipts);
Ok(pending_block)
} else {
let block_with_receipts = BlockWithReceipts {
status: status(starknet_block.header().block_number),
block_hash: starknet_block.header().hash::<H>().into(),
parent_hash: parent_hash(&starknet_block),
block_number: starknet_block.header().block_number,
new_root: new_root(&starknet_block),
timestamp: timestamp(&starknet_block),
sequencer_address: sequencer_address(&starknet_block),
l1_gas_price: l1_gas_price(&starknet_block),
l1_data_gas_price: l1_data_gas_price(&starknet_block),
l1_da_mode: l1_da_mode(&starknet_block),
starknet_version: starknet_version(&starknet_block),
transactions: transactions_with_receipts,
};
Ok(MaybePendingBlockWithReceipts::Block(block_with_receipts))
}
}
6 changes: 2 additions & 4 deletions crates/client/rpc/src/methods/read/get_events.rs
Original file line number Diff line number Diff line change
Expand Up @@ -137,17 +137,15 @@ where
///
/// * `(block_events: Vec<EventWrapper>, continuation_token: usize)` - A tuple of the filtered
/// events and the first index which still hasn't been processed block_id and an instance of Block
fn filter_events_by_params<'a, 'b: 'a>(
fn filter_events_by_params(
events: Vec<EmittedEvent>,
address: Option<Felt252Wrapper>,
keys: &'a [Vec<FieldElement>],
keys: &[Vec<FieldElement>],
) -> Vec<EmittedEvent> {
let mut filtered_events = vec![];

// Iterate on block events.
for event in events {
let match_from_address = address.map_or(true, |addr| addr.0 == event.from_address);
// Based on https://github.com/starkware-libs/papyrus
let match_keys = keys
.iter()
.enumerate()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -247,7 +247,7 @@ where
Transaction::L1Handler(l1_handler) => {
tx_l1_handler::<H>(chain_id, block_number, l1_handler.clone())
}
Transaction::Deploy(_) => todo!(),
_ => unreachable!("Deploy transactions are not supported"),
})
.collect::<Result<Vec<_>, _>>()?;

Expand Down
7 changes: 5 additions & 2 deletions crates/client/rpc/src/methods/trace/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -280,7 +280,7 @@ pub fn tx_execution_infos_to_tx_trace<B: BlockT>(
Ok(tx_trace)
}

pub fn map_transaction_to_user_transaction<A, BE, G, C, P, H>(
pub(crate) fn map_transaction_to_user_transaction<A, BE, G, C, P, H>(
starknet: &Starknet<A, BE, G, C, P, H>,
starknet_block: DeoxysBlock,
substrate_block_hash: DHashT,
Expand Down Expand Up @@ -439,7 +439,10 @@ where
StarknetRpcApiError::InternalServerError
})?;
let block_number = starknet_block.header().block_number;
let previous_block_number = block_number - 1;
let mut previous_block_number = block_number - 1;
if previous_block_number == 0 {
previous_block_number = 0;
}
let substrate_block_hash =
starknet.substrate_block_hash_from_starknet_block(BlockId::Number(previous_block_number)).map_err(|e| {
log::error!("Failed to retrieve previous block substrate hash: {e}");
Expand Down
2 changes: 1 addition & 1 deletion crates/pallets/starknet/src/genesis_loader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ impl<T: crate::Config> From<GenesisLoader> for GenesisConfig<T> {
storage,
strk_fee_token_address: Felt252Wrapper(loader.data().strk_fee_token_address.0).into(),
eth_fee_token_address: Felt252Wrapper(loader.data().eth_fee_token_address.0).into(),
..Default::default()
_phantom: Default::default(),
}
}
}
Expand Down
2 changes: 2 additions & 0 deletions crates/primitives/transactions/src/compute_hash.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ fn convert_calldata(calldata: Calldata) -> Vec<FieldElement> {
calldata.0.iter().map(|f| Felt252Wrapper::from(*f).into()).collect()
}

// Use a mapping from execution resources to get corresponding fee bounds
// Encodes this information into 32-byte buffer then converts it into FieldElement
fn prepare_resource_bound_value(resource_bounds_mapping: &ResourceBoundsMapping, resource: Resource) -> FieldElement {
let mut buffer = [0u8; 32];
buffer[2..8].copy_from_slice(match resource {
Expand Down

0 comments on commit ef90311

Please sign in to comment.