Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: include transaction propagation feature #1375

Open
wants to merge 9 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 40 additions & 0 deletions Cargo.lock

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

2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,7 @@ katana-primitives = { git = 'https://github.com/dojoengine/dojo', rev = "7ff6593
], optional = true }
mockall = { version = "0.13.0", default-features = false, optional = true }
clap = { version = "4.5.17", optional = true }
mockito = { version = "1.5.0", default-features = false, optional = true }


starknet_api = { version = "0.13.0-rc.0", optional = true }
Expand Down Expand Up @@ -211,6 +212,7 @@ testing = [
]
binaries = ["clap"]
hive = []
rpc_forwarding = []
arbitrary = ["rand", "dep:arbitrary"]

[[bin]]
Expand Down
22 changes: 20 additions & 2 deletions src/eth_rpc/servers/eth_rpc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@ use crate::{
client::{EthClient, KakarotTransactions, TransactionHashProvider},
eth_rpc::api::eth_api::EthApiServer,
providers::eth_provider::{
constant::MAX_PRIORITY_FEE_PER_GAS, error::EthApiError, BlockProvider, ChainProvider, GasProvider, LogProvider,
ReceiptProvider, StateProvider, TransactionProvider,
constant::MAX_PRIORITY_FEE_PER_GAS,
error::{EthApiError, TransactionError},
BlockProvider, ChainProvider, GasProvider, LogProvider, ReceiptProvider, StateProvider, TransactionProvider,
},
};
use jsonrpsee::core::{async_trait, RpcResult as Result};
Expand All @@ -18,6 +19,12 @@ use reth_rpc_types::{
use serde_json::Value;
use starknet::providers::Provider;
use std::sync::Arc;
#[cfg(feature = "rpc_forwarding")]
use {
crate::providers::eth_provider::constant::MAIN_RPC_URL,
alloy_provider::{Provider as provider_alloy, ProviderBuilder},
url::Url,
};

/// The RPC module for the Ethereum protocol required by Kakarot.
#[derive(Debug)]
Expand Down Expand Up @@ -255,6 +262,17 @@ where
#[tracing::instrument(skip_all, ret, err)]
async fn send_raw_transaction(&self, bytes: Bytes) -> Result<B256> {
tracing::info!("Serving eth_sendRawTransaction");
#[cfg(feature = "rpc_forwarding")]
{
let provider = ProviderBuilder::new().on_http(Url::parse(MAIN_RPC_URL.as_ref()).unwrap());
let tx_hash = provider
.send_raw_transaction(&bytes)
.await
.map_err(|e| EthApiError::Transaction(TransactionError::Broadcast(e.into())))?;

return Ok(*tx_hash.tx_hash());
}
#[cfg(not(feature = "rpc_forwarding"))]
Ok(self.eth_client.send_raw_transaction(bytes).await?)
}

Expand Down
6 changes: 6 additions & 0 deletions src/providers/eth_provider/constant.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use std::{
str::FromStr,
sync::{LazyLock, OnceLock},
};
use url::Url;

/// Maximum priority fee per gas
pub static MAX_PRIORITY_FEE_PER_GAS: LazyLock<u64> = LazyLock::new(|| 0);
Expand Down Expand Up @@ -78,3 +79,8 @@ pub mod hive {
});
pub static DEPLOY_WALLET_NONCE: LazyLock<Arc<Mutex<Felt>>> = LazyLock::new(|| Arc::new(Mutex::new(Felt::ZERO)));
}

pub static MAIN_RPC_URL: LazyLock<Url> = LazyLock::new(|| {
Url::parse(&std::env::var("MAIN_RPC_URL").expect("Missing MAIN_RPC_URL environment variable"))
.expect("Invalid MAIN_RPC_URL environment variable")
});
42 changes: 41 additions & 1 deletion tests/tests/kakarot_api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,51 @@ use kakarot_rpc::{
rpc::{start_kakarot_rpc_server, RawRpcParamsBuilder},
},
};
use reth_primitives::B256;
use reth_primitives::{sign_message, Address, Bytes, Transaction, TransactionSigned, TxEip1559, TxKind, B256, U256};
use rstest::*;
use serde_json::Value;
use std::str::FromStr;

#[cfg(feature = "rpc_forwarding")]
#[rstest]
#[awt]
#[tokio::test(flavor = "multi_thread")]
async fn test_send_raw_transaction_rpc_forwarding(#[future] katana: Katana, _setup: ()) {
use mockito::Server;
use std::env;
let mut server = Server::new();
let mock_server = server
.mock("POST", "/")
.with_status(200)
.with_header("content-type", "application/json")
.with_body(
r#"{"jsonrpc":"2.0","id":1,"result":"0x1234567890abcdef1234567890abcdef1234567890abcdef1234567890abcdef"}"#,
)
.create();

let (_, _) = start_kakarot_rpc_server(&katana).await.expect("Error setting up Kakarot RPC server");

// Set the MAIN_RPC_URL environment variable
env::set_var("MAIN_RPC_URL", server.url());
drop(server);

let eth_client = katana.eth_client();

// Create a sample raw transaction
let raw_tx = Bytes::from(vec![1, 2, 3, 4]);

// Call the function
let result = eth_client.send_raw_transaction(raw_tx).await;

// Assert the result
assert!(result.is_ok());
let tx_hash = result.unwrap();
assert_eq!(tx_hash, B256::from_str("1234567890abcdef1234567890abcdef1234567890abcdef1234567890abcdef").unwrap());

// Verify that the mock was called
mock_server.assert();
}

#[rstest]
#[awt]
#[tokio::test(flavor = "multi_thread")]
Expand Down
Loading