Skip to content

Commit

Permalink
feat: set the chain id at rpc launch (#898)
Browse files Browse the repository at this point in the history
set the chain id at rpc launch
  • Loading branch information
greged93 authored Mar 27, 2024
1 parent 0ba8694 commit c71507a
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 17 deletions.
10 changes: 4 additions & 6 deletions src/eth_provider/provider.rs
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,7 @@ pub trait EthereumProvider {
pub struct EthDataProvider<SP: starknet::providers::Provider> {
database: Database,
starknet_provider: SP,
chain_id: u64,
}

#[async_trait]
Expand Down Expand Up @@ -179,11 +180,8 @@ where
}
}

// TODO cache chain id
async fn chain_id(&self) -> EthProviderResult<Option<U64>> {
let chain_id = self.starknet_provider.chain_id().await.map_err(KakarotError::from)?;
let chain_id: Option<u64> = chain_id.try_into().ok();
Ok(chain_id.map(U64::from))
Ok(Some(U64::from(self.chain_id)))
}

async fn block_by_hash(&self, hash: B256, full: bool) -> EthProviderResult<Option<RichBlock>> {
Expand Down Expand Up @@ -634,8 +632,8 @@ impl<SP> EthDataProvider<SP>
where
SP: starknet::providers::Provider + Send + Sync,
{
pub const fn new(database: Database, starknet_provider: SP) -> Self {
Self { database, starknet_provider }
pub const fn new(database: Database, starknet_provider: SP, chain_id: u64) -> Self {
Self { database, starknet_provider, chain_id }
}

#[cfg(feature = "testing")]
Expand Down
18 changes: 9 additions & 9 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use kakarot_rpc::eth_rpc::rpc::KakarotRpcModuleBuilder;
use kakarot_rpc::eth_rpc::run_server;
use mongodb::options::{DatabaseOptions, ReadConcern, WriteConcern};
use starknet::providers::jsonrpc::HttpTransport;
use starknet::providers::{JsonRpcClient, SequencerGatewayProvider};
use starknet::providers::{JsonRpcClient, Provider as _, SequencerGatewayProvider};
use tracing_subscriber::util::SubscriberInitExt;

enum StarknetProvider {
Expand Down Expand Up @@ -50,18 +50,18 @@ async fn main() -> Result<()> {
DatabaseOptions::builder().read_concern(ReadConcern::MAJORITY).write_concern(WriteConcern::MAJORITY).build(),
));

let provider =
JsonRpcClient::new(HttpTransport::new(starknet_config.network.provider_url().expect("Incorrect provider URL")));
let chain_id = provider.chain_id().await?;
let chain_id: u64 = chain_id.try_into()?;

// Get the deployer nonce and set the value in the DEPLOY_WALLET_NONCE
#[cfg(feature = "hive")]
{
use kakarot_rpc::eth_provider::constant::{CHAIN_ID, DEPLOY_WALLET, DEPLOY_WALLET_NONCE};
use starknet::accounts::ConnectedAccount;
use starknet::providers::Provider;
let provider = JsonRpcClient::new(HttpTransport::new(
starknet_config.network.provider_url().expect("Incorrect provider URL"),
));

let chain_id = provider.chain_id().await?;
CHAIN_ID.set(chain_id).expect("Failed to set chain id");
CHAIN_ID.set(chain_id.into()).expect("Failed to set chain id");

let deployer_nonce = DEPLOY_WALLET.get_nonce().await?;
let mut nonce = DEPLOY_WALLET_NONCE.lock().await;
Expand All @@ -71,12 +71,12 @@ async fn main() -> Result<()> {
let kakarot_rpc_module = match starknet_provider {
StarknetProvider::JsonRpcClient(starknet_provider) => {
let starknet_provider = Arc::new(starknet_provider);
let eth_provider = EthDataProvider::new(db, starknet_provider);
let eth_provider = EthDataProvider::new(db, starknet_provider, chain_id);
KakarotRpcModuleBuilder::new(eth_provider).rpc_module()
}
StarknetProvider::SequencerGatewayProvider(starknet_provider) => {
let starknet_provider = Arc::new(starknet_provider);
let eth_provider = EthDataProvider::new(db, starknet_provider);
let eth_provider = EthDataProvider::new(db, starknet_provider, chain_id);
KakarotRpcModuleBuilder::new(eth_provider).rpc_module()
}
}?;
Expand Down
6 changes: 4 additions & 2 deletions src/test_utils/katana/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use katana_primitives::genesis::json::GenesisJson;
use katana_primitives::genesis::Genesis;
use reth_primitives::B256;
use starknet::providers::jsonrpc::HttpTransport;
use starknet::providers::JsonRpcClient;
use starknet::providers::{JsonRpcClient, Provider};

use crate::eth_provider::provider::EthDataProvider;
use crate::test_utils::eoa::KakarotEOA;
Expand Down Expand Up @@ -68,7 +68,9 @@ impl Katana {

// Create a Kakarot client
let database = mock_database().await;
let eth_provider = Arc::new(EthDataProvider::new(database, starknet_provider));
let chain_id = starknet_provider.chain_id().await.unwrap_or_default();
let chain_id: u64 = chain_id.try_into().unwrap_or_default();
let eth_provider = Arc::new(EthDataProvider::new(database, starknet_provider, chain_id));

let eoa = KakarotEOA::new(pk, eth_provider);

Expand Down

0 comments on commit c71507a

Please sign in to comment.