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

Commit

Permalink
refacto(simulate): move type conversion logic into client (#1371)
Browse files Browse the repository at this point in the history
  • Loading branch information
tdelabro authored Jan 15, 2024
1 parent d818e83 commit 2583265
Show file tree
Hide file tree
Showing 17 changed files with 432 additions and 634 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

- refacto(simulate_tx): move logic to the client
- chore: added ca-certificate in DockerFile for SSL related issues
- chore(primitives/commitment): remove crate
- chore(primitives/block/header): remove starknet-trie dependent fields
Expand Down
20 changes: 11 additions & 9 deletions Cargo.lock

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

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -269,6 +269,7 @@ subxt = "0.29"
assert_matches = "1.5.0"
async-lock = "3.1.0"
rustc-hex = { version = "2.0.0" }
itertools = "0.12.0"

[patch."https://github.com/w3f/ring-vrf"]
bandersnatch_vrfs = { git = "https://github.com/w3f/ring-vrf?rev=3ddc20", version = "0.0.4", rev = "3ddc20" }
3 changes: 1 addition & 2 deletions crates/client/rpc-core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ use serde_with::serde_as;

pub mod utils;

use mp_simulations::{SimulatedTransaction, SimulationFlag};
use mp_transactions::TransactionStatus;
use pallet_starknet::genesis_loader::PredeployedAccount;
use starknet_core::serde::unsigned_field_element::UfeHex;
Expand All @@ -23,7 +22,7 @@ use starknet_core::types::{
BroadcastedInvokeTransaction, BroadcastedTransaction, ContractClass, DeclareTransactionResult,
DeployAccountTransactionResult, EventFilterWithPage, EventsPage, FeeEstimate, FieldElement, FunctionCall,
InvokeTransactionResult, MaybePendingBlockWithTxHashes, MaybePendingBlockWithTxs, MaybePendingTransactionReceipt,
StateUpdate, SyncStatusType, Transaction,
SimulatedTransaction, SimulationFlag, StateUpdate, SyncStatusType, Transaction,
};

#[serde_as]
Expand Down
1 change: 1 addition & 0 deletions crates/client/rpc/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ starknet_api = { workspace = true, default-features = false }
# Others
anyhow = { workspace = true }
hex = { workspace = true, default-features = true }
itertools = { workspace = true }
jsonrpsee = { workspace = true, default-features = true, features = [
"server",
"macros",
Expand Down
119 changes: 51 additions & 68 deletions crates/client/rpc/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ mod constants;
mod errors;
mod events;
mod madara_backend_client;
mod trace_api;
mod types;
use std::marker::PhantomData;
use std::sync::Arc;
Expand All @@ -24,9 +25,7 @@ pub use mc_rpc_core::{
use mc_storage::OverrideHandle;
use mp_felt::{Felt252Wrapper, Felt252WrapperError};
use mp_hashers::HasherT;
use mp_simulations::{SimulatedTransaction, SimulationFlag, SimulationFlags};
use mp_transactions::compute_hash::ComputeTransactionHash;
use mp_transactions::execution::StarknetRPCExecutionResources;
use mp_transactions::to_starknet_core_transaction::to_starknet_core_tx;
use mp_transactions::{TransactionStatus, UserTransaction};
use pallet_starknet_runtime_api::{ConvertTransactionRuntimeApi, StarknetRuntimeApi};
Expand All @@ -48,10 +47,11 @@ use starknet_core::types::{
BlockHashAndNumber, BlockId, BlockStatus, BlockTag, BlockWithTxHashes, BlockWithTxs, BroadcastedDeclareTransaction,
BroadcastedDeployAccountTransaction, BroadcastedInvokeTransaction, BroadcastedTransaction, ContractClass,
DeclareTransactionReceipt, DeclareTransactionResult, DeployAccountTransactionReceipt,
DeployAccountTransactionResult, EventFilterWithPage, EventsPage, ExecutionResult, FeeEstimate, FieldElement,
FunctionCall, Hash256, InvokeTransactionReceipt, InvokeTransactionResult, L1HandlerTransactionReceipt,
MaybePendingBlockWithTxHashes, MaybePendingBlockWithTxs, MaybePendingTransactionReceipt, StateDiff, StateUpdate,
SyncStatus, SyncStatusType, Transaction, TransactionExecutionStatus, TransactionFinalityStatus, TransactionReceipt,
DeployAccountTransactionResult, EventFilterWithPage, EventsPage, ExecutionResources, ExecutionResult, FeeEstimate,
FieldElement, FunctionCall, Hash256, InvokeTransactionReceipt, InvokeTransactionResult,
L1HandlerTransactionReceipt, MaybePendingBlockWithTxHashes, MaybePendingBlockWithTxs,
MaybePendingTransactionReceipt, StateDiff, StateUpdate, SyncStatus, SyncStatusType, Transaction,
TransactionExecutionStatus, TransactionFinalityStatus, TransactionReceipt,
};
use starknet_core::utils::get_selector_from_name;

Expand Down Expand Up @@ -1439,9 +1439,6 @@ where
}
};

// TODO(#1291): compute execution_resources correctly to the receipt
let execution_resources = StarknetRPCExecutionResources::default();

// TODO(#1291): compute message hash correctly to L1HandlerTransactionReceipt
let message_hash: Hash256 = Hash256::from_felt(&FieldElement::default());

Expand Down Expand Up @@ -1491,6 +1488,7 @@ where
}
}

// TODO: use actual execution ressources
let receipt = match transaction {
mp_transactions::Transaction::Declare(_) => TransactionReceipt::Declare(DeclareTransactionReceipt {
transaction_hash,
Expand All @@ -1501,7 +1499,17 @@ where
messages_sent: messages.into_iter().map(message_conversion).collect(),
events: events_converted,
execution_result,
execution_resources: execution_resources.into(),
execution_resources: ExecutionResources {
steps: 0,
memory_holes: None,
range_check_builtin_applications: 0,
pedersen_builtin_applications: 0,
poseidon_builtin_applications: 0,
ec_op_builtin_applications: 0,
ecdsa_builtin_applications: 0,
bitwise_builtin_applications: 0,
keccak_builtin_applications: 0,
},
}),
mp_transactions::Transaction::DeployAccount(tx) => {
TransactionReceipt::DeployAccount(DeployAccountTransactionReceipt {
Expand All @@ -1514,7 +1522,17 @@ where
events: events_converted,
contract_address: tx.get_account_address(),
execution_result,
execution_resources: execution_resources.into(),
execution_resources: ExecutionResources {
steps: 0,
memory_holes: None,
range_check_builtin_applications: 0,
pedersen_builtin_applications: 0,
poseidon_builtin_applications: 0,
ec_op_builtin_applications: 0,
ecdsa_builtin_applications: 0,
bitwise_builtin_applications: 0,
keccak_builtin_applications: 0,
},
})
}
mp_transactions::Transaction::Invoke(_) => TransactionReceipt::Invoke(InvokeTransactionReceipt {
Expand All @@ -1526,7 +1544,17 @@ where
messages_sent: messages.into_iter().map(message_conversion).collect(),
events: events_converted,
execution_result,
execution_resources: execution_resources.into(),
execution_resources: ExecutionResources {
steps: 0,
memory_holes: None,
range_check_builtin_applications: 0,
pedersen_builtin_applications: 0,
poseidon_builtin_applications: 0,
ec_op_builtin_applications: 0,
ecdsa_builtin_applications: 0,
bitwise_builtin_applications: 0,
keccak_builtin_applications: 0,
},
}),
mp_transactions::Transaction::L1Handler(_) => TransactionReceipt::L1Handler(L1HandlerTransactionReceipt {
message_hash,
Expand All @@ -1538,69 +1566,24 @@ where
messages_sent: messages.into_iter().map(message_conversion).collect(),
events: events_converted,
execution_result,
execution_resources: execution_resources.into(),
execution_resources: ExecutionResources {
steps: 0,
memory_holes: None,
range_check_builtin_applications: 0,
pedersen_builtin_applications: 0,
poseidon_builtin_applications: 0,
ec_op_builtin_applications: 0,
ecdsa_builtin_applications: 0,
bitwise_builtin_applications: 0,
keccak_builtin_applications: 0,
},
}),
};

Ok(MaybePendingTransactionReceipt::Receipt(receipt))
}
}

#[async_trait]
#[allow(unused_variables)]
impl<A, B, BE, G, C, P, H> StarknetTraceRpcApiServer for Starknet<A, B, BE, G, C, P, H>
where
A: ChainApi<Block = B> + 'static,
B: BlockT,
BE: Backend<B> + 'static,
G: GenesisProvider + Send + Sync + 'static,
C: HeaderBackend<B> + BlockBackend<B> + StorageProvider<B, BE> + 'static,
C: ProvideRuntimeApi<B>,
C::Api: StarknetRuntimeApi<B> + ConvertTransactionRuntimeApi<B>,
P: TransactionPool<Block = B> + 'static,
H: HasherT + Send + Sync + 'static,
{
async fn simulate_transactions(
&self,
block_id: BlockId,
transactions: Vec<BroadcastedTransaction>,
simulation_flags: Vec<SimulationFlag>,
) -> RpcResult<Vec<SimulatedTransaction>> {
let substrate_block_hash = self.substrate_block_hash_from_starknet_block(block_id).map_err(|e| {
error!("'{e}'");
StarknetRpcApiError::BlockNotFound
})?;
let best_block_hash = self.client.info().best_hash;
let chain_id = Felt252Wrapper(self.chain_id()?.0);

let mut user_transactions = vec![];
for tx in transactions {
let tx = tx.try_into().map_err(|e| {
error!("Failed to convert BroadcastedTransaction to UserTransaction: {e}");
StarknetRpcApiError::InternalServerError
})?;
user_transactions.push(tx);
}

let simulation_flags: SimulationFlags = simulation_flags.into();

let fee_estimates = self
.client
.runtime_api()
.simulate_transactions(substrate_block_hash, user_transactions, simulation_flags)
.map_err(|e| {
error!("Request parameters error: {e}");
StarknetRpcApiError::InternalServerError
})?
.map_err(|e| {
error!("Failed to call function: {:#?}", e);
StarknetRpcApiError::ContractError
})?;

Ok(fee_estimates)
}
}

async fn submit_extrinsic<P, B>(
pool: Arc<P>,
best_block_hash: <B as BlockT>::Hash,
Expand Down
Loading

0 comments on commit 2583265

Please sign in to comment.