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

Commit

Permalink
Merge branch 'main' into add_pragma_api
Browse files Browse the repository at this point in the history
  • Loading branch information
azurwastaken authored Jun 20, 2024
2 parents 3e318cf + f5dce7e commit 02d92a0
Show file tree
Hide file tree
Showing 22 changed files with 60,432 additions and 171 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

## v0.8.0

- feat: Declare V0 RPC call
- feat: add `TransactionFilter<TxType>` to pallet-starknet `Config`
- chore: remove `ignore` from
`storage_changes_should_revert_on_transaction_revert` test
Expand Down
57 changes: 1 addition & 56 deletions Cargo.lock

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

8 changes: 8 additions & 0 deletions crates/client/rpc-core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ mod tests;

use jsonrpsee::core::RpcResult;
use jsonrpsee::proc_macros::rpc;
use mp_transactions::BroadcastedDeclareTransactionV0;
use serde::{Deserialize, Serialize};
use serde_with::serde_as;

Expand Down Expand Up @@ -41,6 +42,13 @@ pub struct PredeployedAccountWithBalance {
pub trait MadaraRpcApi: StarknetReadRpcApi {
#[method(name = "predeployedAccounts")]
fn predeployed_accounts(&self) -> RpcResult<Vec<PredeployedAccountWithBalance>>;

/// Submit a declare transaction for cairo 0 contract to be initialised with given address.
#[method(name = "addDeclareTransactionV0")]
async fn add_declare_transaction_v0(
&self,
params: BroadcastedDeclareTransactionV0,
) -> RpcResult<DeclareTransactionResult>;
}

/// Starknet write rpc interface.
Expand Down
1 change: 0 additions & 1 deletion crates/client/rpc/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,6 @@ mp-hashers = { workspace = true }
mp-simulations = { workspace = true }
mp-transactions = { workspace = true, features = ["client"] }
serde = { workspace = true }
serde_json = { workspace = true }
starknet-core = { workspace = true }
starknet-ff = { workspace = true }
starknet_api = { workspace = true }
Expand Down
85 changes: 34 additions & 51 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 madara_routes;
mod runtime_api;
pub mod starknetrpcwrapper;
mod trace_api;
Expand All @@ -17,7 +18,7 @@ use std::sync::Arc;

use blockifier::transaction::account_transaction::AccountTransaction;
use blockifier::transaction::objects::{ResourcesMapping, TransactionExecutionInfo};
use blockifier::transaction::transactions::L1HandlerTransaction;
use blockifier::transaction::transactions::{DeclareTransaction, L1HandlerTransaction};
use errors::StarknetRpcApiError;
use jsonrpsee::core::{async_trait, RpcResult};
use log::error;
Expand Down Expand Up @@ -52,7 +53,7 @@ use sp_blockchain::HeaderBackend;
use sp_core::H256;
use sp_runtime::traits::{Block as BlockT, Header as HeaderT};
use sp_runtime::transaction_validity::InvalidTransaction;
use starknet_api::core::Nonce;
use starknet_api::core::{ClassHash, Nonce};
use starknet_api::hash::StarkFelt;
use starknet_api::transaction::{Calldata, Fee, TransactionHash, TransactionVersion};
use starknet_core::types::{
Expand All @@ -68,7 +69,6 @@ use starknet_core::types::{
SimulationFlagForEstimateFee, StateDiff, StateUpdate, SyncStatus, SyncStatusType, Transaction,
TransactionExecutionStatus, TransactionFinalityStatus, TransactionReceipt,
};
use starknet_core::utils::get_selector_from_name;
use trace_api::get_previous_block_substrate_hash;

use crate::constants::{MAX_EVENTS_CHUNK_SIZE, MAX_EVENTS_KEYS};
Expand Down Expand Up @@ -220,50 +220,49 @@ where
}
}

/// Taken from https://github.com/paritytech/substrate/blob/master/client/rpc/src/author/mod.rs#L78
const TX_SOURCE: TransactionSource = TransactionSource::External;

impl<A, B, BE, G, C, P, H> MadaraRpcApiServer for Starknet<A, B, BE, G, C, P, H>
impl<A, B, BE, G, C, P, H> Starknet<A, B, BE, G, C, P, H>
where
A: ChainApi<Block = B> + 'static,
B: BlockT,
P: TransactionPool<Block = B> + 'static,
BE: Backend<B> + 'static,
C: HeaderBackend<B> + BlockBackend<B> + StorageProvider<B, BE> + 'static,
C: ProvideRuntimeApi<B>,
G: GenesisProvider + Send + Sync + 'static,
C::Api: StarknetRuntimeApi<B> + ConvertTransactionRuntimeApi<B>,
P: TransactionPool<Block = B> + 'static,
G: GenesisProvider + Send + Sync + 'static,
H: HasherT + Send + Sync + 'static,
{
fn predeployed_accounts(&self) -> RpcResult<Vec<PredeployedAccountWithBalance>> {
let genesis_data = self.genesis_provider.load_genesis_data()?;
let block_id = BlockId::Tag(BlockTag::Latest);
let fee_token_address: FieldElement = genesis_data.eth_fee_token_address.0;
async fn declare_tx_common(
&self,
txn: DeclareTransaction,
) -> Result<(TransactionHash, ClassHash), StarknetRpcApiError> {
let best_block_hash = self.get_best_block_hash();
let current_block_hash = self.get_best_block_hash();
let contract_class = self
.overrides
.for_block_hash(self.client.as_ref(), current_block_hash)
.contract_class_by_class_hash(current_block_hash, txn.class_hash());

Ok(genesis_data
.predeployed_accounts
.into_iter()
.map(|account| {
let contract_address: FieldElement = account.contract_address.into();
let balance_string = &self
.call(
FunctionCall {
contract_address: fee_token_address,
entry_point_selector: get_selector_from_name("balanceOf")
.expect("the provided method name should be a valid ASCII string."),
calldata: vec![contract_address],
},
block_id,
)
.expect("FunctionCall attributes should be correct.")[0];
let balance =
Felt252Wrapper::from_hex_be(balance_string).expect("`balanceOf` should return a Felt").into();
PredeployedAccountWithBalance { account, balance }
})
.collect::<Vec<_>>())
if let Some(contract_class) = contract_class {
log::debug!("Contract class already exists: {:?}", contract_class);
return Err(StarknetRpcApiError::ClassAlreadyDeclared);
}

let extrinsic =
self.convert_tx_to_extrinsic(best_block_hash, AccountTransaction::Declare(txn.clone())).unwrap();

let res = submit_extrinsic(self.pool.clone(), best_block_hash, extrinsic).await;

match res {
Ok(_val) => Ok((txn.tx_hash, txn.class_hash())),
Err(e) => Err(e),
}
}
}

/// Taken from https://github.com/paritytech/substrate/blob/master/client/rpc/src/author/mod.rs#L78
const TX_SOURCE: TransactionSource = TransactionSource::External;

#[async_trait]
impl<A, B, BE, G, C, P, H> StarknetWriteRpcApiServer for Starknet<A, B, BE, G, C, P, H>
where
Expand All @@ -290,8 +289,6 @@ where
&self,
declare_transaction: BroadcastedDeclareTransaction,
) -> RpcResult<DeclareTransactionResult> {
let best_block_hash = self.get_best_block_hash();

let opt_sierra_contract_class = if let BroadcastedDeclareTransaction::V2(ref tx) = declare_transaction {
Some(flattened_sierra_to_sierra_contract_class(tx.contract_class.clone()))
} else {
Expand All @@ -304,22 +301,8 @@ where
error!("Failed to convert BroadcastedDeclareTransaction to DeclareTransaction, error: {e}");
StarknetRpcApiError::InternalServerError
})?;
let (class_hash, tx_hash) = (transaction.class_hash(), transaction.tx_hash());

let current_block_hash = self.get_best_block_hash();
let contract_class = self
.overrides
.for_block_hash(self.client.as_ref(), current_block_hash)
.contract_class_by_class_hash(current_block_hash, class_hash);

if let Some(contract_class) = contract_class {
error!("Contract class already exists: {:?}", contract_class);
return Err(StarknetRpcApiError::ClassAlreadyDeclared.into());
}

let extrinsic = self.convert_tx_to_extrinsic(best_block_hash, AccountTransaction::Declare(transaction))?;

submit_extrinsic(self.pool.clone(), best_block_hash, extrinsic).await?;
let (tx_hash, class_hash) = self.declare_tx_common(transaction).await?;

if let Some(sierra_contract_class) = opt_sierra_contract_class {
if let Some(e) = self.backend.sierra_classes().store_sierra_class(class_hash, sierra_contract_class).err() {
Expand Down
Loading

0 comments on commit 02d92a0

Please sign in to comment.