Skip to content

Commit

Permalink
refactor: replace starknet-rs BlockId with types-rs BlockId (#384)
Browse files Browse the repository at this point in the history
  • Loading branch information
jbcaron authored Nov 19, 2024
1 parent 9db5cef commit e8b71ff
Show file tree
Hide file tree
Showing 56 changed files with 239 additions and 379 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 @@

## Next release

- refactor: replace starknet-rs BlockId with types-rs BlockId and remove redundant mp_block::BlockId
- feat(fgw): added `add_transaction` for gateway client
- fix(fgw): include `l1_to_l2_consumed_message` in L1 handler receipt
- build: up starknet-rs, starknet-types, blockifier(v0.8.0), cairo
Expand Down
2 changes: 2 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion crates/client/block_import/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ use anyhow::Context;
use mc_db::{MadaraBackend, MadaraStorageError};
use metrics::BlockMetrics;
use mp_class::{class_hash::ComputeClassHashError, compile::ClassCompilationError};
use starknet_core::types::Felt;
use starknet_types_core::felt::Felt;
use std::{borrow::Cow, sync::Arc};

mod metrics;
Expand Down
2 changes: 1 addition & 1 deletion crates/client/block_import/src/pre_validate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use mp_transactions::Transaction;
use rayon::iter::{IntoParallelIterator, IntoParallelRefIterator};
use rayon::prelude::*;
use starknet_api::core::ChainId;
use starknet_core::types::Felt;
use starknet_types_core::felt::Felt;
use starknet_types_core::hash::{Pedersen, Poseidon, StarkHash};
use std::mem;
use std::sync::Arc;
Expand Down
2 changes: 1 addition & 1 deletion crates/client/block_import/src/tests/block_import_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use mp_block::header::{GasPrices, L1DataAvailabilityMode};
use mp_block::Header;
use mp_chain_config::StarknetVersion;
use mp_state_update::StateDiff;
use starknet_core::types::Felt;
use starknet_types_core::felt::Felt;

use crate::{
BlockValidationContext, PreValidatedBlock, PreValidatedPendingBlock, UnverifiedCommitments, UnverifiedFullBlock,
Expand Down
2 changes: 1 addition & 1 deletion crates/client/block_import/src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ use mp_state_update::StateDiff;
use mp_transactions::Transaction;
use serde::{Deserialize, Serialize};
use starknet_api::core::ChainId;
use starknet_core::types::Felt;
use starknet_types_core::felt::Felt;

#[derive(Clone, Debug, Eq, PartialEq, Default, Serialize, Deserialize)]
pub struct UnverifiedHeader {
Expand Down
5 changes: 3 additions & 2 deletions crates/client/block_import/src/verify_apply.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,14 @@ use crate::{
};
use itertools::Itertools;
use mc_db::{MadaraBackend, MadaraStorageError};
use mp_block::BlockTag;
use mp_block::{
header::PendingHeader, BlockId, BlockTag, Header, MadaraBlockInfo, MadaraBlockInner, MadaraMaybePendingBlock,
header::PendingHeader, BlockId, Header, MadaraBlockInfo, MadaraBlockInner, MadaraMaybePendingBlock,
MadaraMaybePendingBlockInfo, MadaraPendingBlockInfo,
};
use mp_convert::{FeltHexDisplay, ToFelt};
use starknet_api::core::ChainId;
use starknet_core::types::Felt;
use starknet_types_core::felt::Felt;
use starknet_types_core::hash::{Poseidon, StarkHash};
use std::{borrow::Cow, sync::Arc};

Expand Down
2 changes: 1 addition & 1 deletion crates/client/db/src/contract_db.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use std::sync::Arc;
use rayon::{iter::ParallelIterator, slice::ParallelSlice};
use rocksdb::{BoundColumnFamily, IteratorMode, ReadOptions, WriteOptions};
use serde::Serialize;
use starknet_core::types::Felt;
use starknet_types_core::felt::Felt;

use crate::{
db_block_id::{DbBlockId, DbBlockIdResolvable},
Expand Down
6 changes: 0 additions & 6 deletions crates/client/db/src/db_block_id.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,6 @@ impl DbBlockIdResolvable for BlockId {
}
}

impl DbBlockIdResolvable for starknet_core::types::BlockId {
fn resolve_db_block_id(&self, backend: &MadaraBackend) -> Result<Option<DbBlockId>, MadaraStorageError> {
backend.id_to_storage_type(&(*self).into())
}
}

impl DbBlockIdResolvable for DbBlockId {
fn resolve_db_block_id(&self, _backend: &MadaraBackend) -> Result<Option<DbBlockId>, MadaraStorageError> {
Ok(Some(*self))
Expand Down
2 changes: 1 addition & 1 deletion crates/client/db/src/devnet_db.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use crate::DatabaseExt;
use crate::{Column, MadaraBackend, MadaraStorageError};
use rocksdb::WriteOptions;
use serde::{Deserialize, Serialize};
use starknet_core::types::Felt;
use starknet_types_core::felt::Felt;

pub const DEVNET_KEYS: &[u8] = b"DEVNET_KEYS";

Expand Down
3 changes: 1 addition & 2 deletions crates/client/db/src/tests/test_block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,7 @@ mod block_tests {
use super::super::common::*;
use crate::db_block_id::DbBlockIdResolvable;
use crate::{block_db::TxIndex, db_block_id::DbBlockId};
use mp_block::BlockId;
use mp_block::Header;
use mp_block::{BlockId, Header};
use mp_chain_config::ChainConfig;
use starknet_api::felt;

Expand Down
2 changes: 1 addition & 1 deletion crates/client/devnet/src/predeployed_contracts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ use blockifier::abi::{abi_utils::get_fee_token_var_address, sierra_types::next_s
use core::fmt;
use mc_db::MadaraBackend;
use mp_block::{BlockId, BlockTag};
use starknet_core::types::Felt;
use starknet_signers::SigningKey;
use starknet_types_core::felt::Felt;

use crate::{
ContractFeeTokensBalance, ERC20_ETH_CONTRACT_ADDRESS, ERC20_STRK_CONTRACT_ADDRESS, ETH_WEI_DECIMALS,
Expand Down
13 changes: 6 additions & 7 deletions crates/client/gateway/client/src/methods.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ impl GatewayProvider {
let request = RequestBuilder::new(&self.client, self.feeder_gateway_url.clone(), self.headers.clone())
.add_uri_segment("get_block")
.expect("Failed to add URI segment. This should not fail in prod.")
.with_block_id(block_id);
.with_block_id(&block_id);

match block_id {
BlockId::Tag(BlockTag::Pending) => {
Expand All @@ -41,7 +41,7 @@ impl GatewayProvider {
let request = RequestBuilder::new(&self.client, self.feeder_gateway_url.clone(), self.headers.clone())
.add_uri_segment("get_state_update")
.expect("Failed to add URI segment. This should not fail in prod")
.with_block_id(block_id);
.with_block_id(&block_id);

match block_id {
BlockId::Tag(BlockTag::Pending) => {
Expand All @@ -58,7 +58,7 @@ impl GatewayProvider {
let request = RequestBuilder::new(&self.client, self.feeder_gateway_url.clone(), self.headers.clone())
.add_uri_segment("get_state_update")
.expect("Failed to add URI segment. This should not fail in prod")
.with_block_id(block_id)
.with_block_id(&block_id)
.add_param(Cow::from("includeBlock"), "true");

match block_id {
Expand All @@ -79,7 +79,7 @@ impl GatewayProvider {
let request = RequestBuilder::new(&self.client, self.feeder_gateway_url.clone(), self.headers.clone())
.add_uri_segment("get_signature")
.expect("Failed to add URI segment. This should not fail in prod")
.with_block_id(block_id);
.with_block_id(&block_id);

request.send_get::<ProviderBlockSignature>().await
}
Expand All @@ -92,7 +92,7 @@ impl GatewayProvider {
let request = RequestBuilder::new(&self.client, self.feeder_gateway_url.clone(), self.headers.clone())
.add_uri_segment("get_class_by_hash")
.expect("Failed to add URI segment. This should not fail in prod.")
.with_block_id(block_id)
.with_block_id(&block_id)
.with_class_hash(class_hash);

let value = request.send_get::<Value>().await?;
Expand Down Expand Up @@ -149,12 +149,11 @@ mod tests {
bufread::{GzDecoder, GzEncoder},
Compression,
};
use mp_block::BlockTag;
use mp_class::CompressedLegacyContractClass;
use mp_gateway::error::{SequencerError, StarknetError, StarknetErrorCode};
use rstest::*;
use serde::de::DeserializeOwned;
use starknet_core::types::Felt;
use starknet_types_core::felt::Felt;
use std::fs::{remove_file, File};
use std::io::{BufReader, BufWriter, Read, Write};
use std::ops::Drop;
Expand Down
2 changes: 1 addition & 1 deletion crates/client/gateway/client/src/request_builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ impl<'a> RequestBuilder<'a> {
self
}

pub fn with_block_id(mut self, block_id: BlockId) -> Self {
pub fn with_block_id(mut self, block_id: &BlockId) -> Self {
match block_id {
BlockId::Hash(hash) => {
self = self.add_param(Cow::from("blockHash"), &format!("0x{hash:x}"));
Expand Down
13 changes: 6 additions & 7 deletions crates/client/gateway/server/src/handler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,12 +46,12 @@ pub async fn handle_get_block(

let block_info = backend
.get_block_info(&block_id)
.or_internal_server_error(format!("Retrieving block {block_id}"))?
.or_internal_server_error(format!("Retrieving block {block_id:?}"))?
.ok_or(StarknetError::block_not_found())?;

match block_info {
MadaraMaybePendingBlockInfo::Pending(_) => Err(GatewayError::InternalServerError(format!(
"Retrieved pending block info from db for non-pending block {block_id}"
"Retrieved pending block info from db for non-pending block {block_id:?}"
))),
MadaraMaybePendingBlockInfo::NotPending(block_info) => {
let body = json!({
Expand All @@ -64,7 +64,7 @@ pub async fn handle_get_block(
} else {
let block = backend
.get_block(&block_id)
.or_internal_server_error(format!("Retrieving block {block_id}"))?
.or_internal_server_error(format!("Retrieving block {block_id:?}"))?
.ok_or(StarknetError::block_not_found())?;

if let Ok(block) = MadaraBlock::try_from(block.clone()) {
Expand Down Expand Up @@ -101,12 +101,12 @@ pub async fn handle_get_signature(

let block_info = backend
.get_block_info(&block_id)
.or_internal_server_error(format!("Retrieving block info for block {block_id}"))?
.or_internal_server_error(format!("Retrieving block info for block {block_id:?}"))?
.ok_or(StarknetError::block_not_found())?;

match block_info {
MadaraMaybePendingBlockInfo::Pending(_) => Err(GatewayError::InternalServerError(format!(
"Retrieved pending block info from db for non-pending block {block_id}"
"Retrieved pending block info from db for non-pending block {block_id:?}"
))),
MadaraMaybePendingBlockInfo::NotPending(block_info) => {
let private_key = &backend.chain_config().private_key;
Expand Down Expand Up @@ -238,8 +238,7 @@ pub async fn handle_get_block_traces(
traces: Vec<TransactionTraceWithHash>,
}

let traces =
v0_7_1_trace_block_transactions(&Starknet::new(backend, add_transaction_provider), block_id.into()).await?;
let traces = v0_7_1_trace_block_transactions(&Starknet::new(backend, add_transaction_provider), block_id).await?;
let block_traces = BlockTraces { traces };

Ok(create_json_response(hyper::StatusCode::OK, &block_traces))
Expand Down
2 changes: 1 addition & 1 deletion crates/client/mempool/src/block_production.rs
Original file line number Diff line number Diff line change
Expand Up @@ -578,7 +578,7 @@ mod test {
core::{ClassHash, ContractAddress, PatriciaKey},
felt, patricia_key,
};
use starknet_core::types::Felt;
use starknet_types_core::felt::Felt;

#[test]
fn state_map_to_state_diff() {
Expand Down
29 changes: 11 additions & 18 deletions crates/client/mempool/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,19 +1,16 @@
use blockifier::blockifier::stateful_validator::StatefulValidatorError;
use blockifier::transaction::account_transaction::AccountTransaction;
use blockifier::transaction::transaction_execution::Transaction;
use blockifier::transaction::transactions::DeployAccountTransaction;
use blockifier::transaction::transactions::InvokeTransaction;
use blockifier::transaction::transactions::{DeclareTransaction, L1HandlerTransaction};
use blockifier::transaction::transactions::{
DeclareTransaction, DeployAccountTransaction, InvokeTransaction, L1HandlerTransaction,
};
use header::make_pending_header;
use inner::MempoolInner;
use mc_db::db_block_id::DbBlockId;
use mc_db::MadaraBackend;
use mc_db::MadaraStorageError;
use mc_db::{MadaraBackend, MadaraStorageError};
use mc_exec::ExecutionContext;
use metrics::MempoolMetrics;
use mp_block::BlockId;
use mp_block::BlockTag;
use mp_block::MadaraPendingBlockInfo;
use mp_block::{BlockId, BlockTag, MadaraPendingBlockInfo};
use mp_class::ConvertedClass;
use mp_convert::ToFelt;
use mp_transactions::{
Expand All @@ -22,16 +19,12 @@ use mp_transactions::{
use mp_transactions::{BroadcastedToBlockifierError, L1HandlerTransactionResult};
use starknet_api::core::{ContractAddress, Nonce};
use starknet_api::transaction::TransactionHash;
use starknet_core::types::BroadcastedDeclareTransaction;
use starknet_core::types::BroadcastedDeployAccountTransaction;
use starknet_core::types::BroadcastedInvokeTransaction;
use starknet_core::types::BroadcastedTransaction;
use starknet_core::types::DeclareTransactionResult;
use starknet_core::types::DeployAccountTransactionResult;
use starknet_core::types::InvokeTransactionResult;
use starknet_core::types::{
BroadcastedDeclareTransaction, BroadcastedDeployAccountTransaction, BroadcastedInvokeTransaction,
BroadcastedTransaction, DeclareTransactionResult, DeployAccountTransactionResult, InvokeTransactionResult,
};
use starknet_types_core::felt::Felt;
use std::sync::Arc;
use std::sync::RwLock;
use std::sync::{Arc, RwLock};

pub use inner::TxInsersionError;
pub use inner::{ArrivedAtTimestamp, MempoolTransaction};
Expand Down Expand Up @@ -359,7 +352,7 @@ pub(crate) fn clone_transaction(tx: &Transaction) -> Transaction {
mod test {
use std::sync::Arc;

use starknet_core::types::Felt;
use starknet_types_core::felt::Felt;

use crate::MockL1DataProvider;

Expand Down
4 changes: 2 additions & 2 deletions crates/client/rpc/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ use std::sync::Arc;

use mc_db::db_block_id::DbBlockIdResolvable;
use mc_db::MadaraBackend;
use mp_block::{MadaraMaybePendingBlock, MadaraMaybePendingBlockInfo};
use mp_block::{BlockId, BlockTag, MadaraMaybePendingBlock, MadaraMaybePendingBlockInfo};
use mp_chain_config::{ChainConfig, RpcVersion};
use mp_convert::ToFelt;

Expand Down Expand Up @@ -74,7 +74,7 @@ impl Starknet {
}

pub fn current_block_number(&self) -> StarknetRpcResult<u64> {
self.get_block_n(&mp_block::BlockId::Tag(mp_block::BlockTag::Latest))
self.get_block_n(&BlockId::Tag(BlockTag::Latest))
}

pub fn current_spec_version(&self) -> RpcVersion {
Expand Down
3 changes: 2 additions & 1 deletion crates/client/rpc/src/test_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,9 @@ use mp_transactions::{BroadcastedDeclareTransactionV0, InvokeTransaction, Invoke
use rstest::fixture;
use starknet_core::types::{
BroadcastedDeclareTransaction, BroadcastedDeployAccountTransaction, BroadcastedInvokeTransaction,
DeclareTransactionResult, DeployAccountTransactionResult, Felt, InvokeTransactionResult,
DeclareTransactionResult, DeployAccountTransactionResult, InvokeTransactionResult,
};
use starknet_types_core::felt::Felt;
use std::sync::Arc;

use crate::{providers::AddTransactionProvider, Starknet};
Expand Down
7 changes: 4 additions & 3 deletions crates/client/rpc/src/versions/v0_7_1/api.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
use jsonrpsee::core::RpcResult;
use m_proc_macros::versioned_rpc;
use mp_block::BlockId;
use mp_transactions::BroadcastedDeclareTransactionV0;
use starknet_core::types::{
BlockHashAndNumber, BlockId, BroadcastedDeclareTransaction, BroadcastedDeployAccountTransaction,
BlockHashAndNumber, BroadcastedDeclareTransaction, BroadcastedDeployAccountTransaction,
BroadcastedInvokeTransaction, BroadcastedTransaction, ContractClass, DeclareTransactionResult,
DeployAccountTransactionResult, EventFilterWithPage, EventsPage, FeeEstimate, FunctionCall,
InvokeTransactionResult, MaybePendingBlockWithReceipts, MaybePendingBlockWithTxHashes, MaybePendingBlockWithTxs,
Expand All @@ -9,8 +12,6 @@ use starknet_core::types::{
};
use starknet_types_core::felt::Felt;

use m_proc_macros::versioned_rpc;
use mp_transactions::BroadcastedDeclareTransactionV0;
// Starknet RPC API trait and types
//
// Starkware maintains [a description of the Starknet API](https://github.com/starkware-libs/starknet-specs/blob/master/api/starknet_api_openrpc.json)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ mod tests {
};
use mp_state_update::StateDiff;
use rstest::rstest;
use starknet_core::types::Felt;
use starknet_types_core::felt::Felt;

#[rstest]
fn test_block_hash_and_number(rpc_test_setup: (Arc<MadaraBackend>, Starknet)) {
Expand Down
6 changes: 3 additions & 3 deletions crates/client/rpc/src/versions/v0_7_1/methods/read/call.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
use std::sync::Arc;

use starknet_core::types::{BlockId, FunctionCall};
use starknet_types_core::felt::Felt;

use mc_exec::ExecutionContext;
use mp_block::BlockId;
use starknet_core::types::FunctionCall;
use starknet_types_core::felt::Felt;

use crate::errors::StarknetRpcApiError;
use crate::errors::StarknetRpcResult;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
use std::sync::Arc;

use starknet_core::types::{BlockId, BroadcastedTransaction, FeeEstimate, SimulationFlagForEstimateFee};
use starknet_core::types::{BroadcastedTransaction, FeeEstimate, SimulationFlagForEstimateFee};

use mc_exec::ExecutionContext;
use mp_block::BlockId;
use mp_transactions::broadcasted_to_blockifier;

use crate::errors::StarknetRpcApiError;
Expand Down
Loading

0 comments on commit e8b71ff

Please sign in to comment.