Skip to content

Commit

Permalink
EVM: Implement SendTransaction and Storage access on RPC (#902)
Browse files Browse the repository at this point in the history
* feature gate DevSigner

* ethers signer on devsigner

* update branch

* access storage in sov-ethereum rpc

* remove unnecessary imports

* cargo fmt

* fix compilation problems

* fix mutable working set

* Add debug printlns

* add more debugs

* temporary fix into transaction method

* lint: eth_sendTransaction

* fix reviews

* remove unnecessary result

* error on conversions

* enable local with experimental on evm

---------

Co-authored-by: bkolad <blazejkolad@gmail.com>
  • Loading branch information
orkunkilic and bkolad authored Sep 22, 2023
1 parent 9cf890f commit 241c417
Show file tree
Hide file tree
Showing 7 changed files with 266 additions and 49 deletions.
4 changes: 2 additions & 2 deletions Cargo.lock

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

3 changes: 2 additions & 1 deletion examples/demo-rollup/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -73,12 +73,13 @@ revm = { workspace = true }

[features]
default = ["native"] # Deviate from convention by making the "native" feature active by default. This aligns with how this package is meant to be used (as a binary first, library second).
experimental = ["sov-ethereum/experimental", "reth-primitives", "secp256k1", "demo-stf/experimental"]
experimental = ["sov-ethereum/experimental", "reth-primitives", "secp256k1", "demo-stf/experimental", "local"]
native = ["anyhow", "jsonrpsee", "serde", "serde_json", "tracing", "tokio", "tracing-subscriber",
"demo-stf/native", "sov-modules-stf-template/native", "sov-risc0-adapter/native", "sov-modules-api/native",
"sov-state/native", "sov-cli", "clap", "sov-celestia-adapter/native", "sov-db", "sov-sequencer", "sov-stf-runner/native",
"sov-modules-api/native"]
bench = ["native", "async-trait", "borsh", "hex"]
local = ["sov-ethereum/local"]

[[bench]]
name = "rollup_bench"
Expand Down
5 changes: 3 additions & 2 deletions examples/demo-rollup/src/register_rpc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,12 +42,13 @@ pub fn register_ledger(

#[cfg(feature = "experimental")]
/// register ethereum methods.
pub fn register_ethereum<Da: DaService>(
pub fn register_ethereum<C: sov_modules_api::Context, Da: DaService>(
da_service: Da,
eth_rpc_config: EthRpcConfig,
storage: C::Storage,
methods: &mut jsonrpsee::RpcModule<()>,
) -> Result<(), anyhow::Error> {
let ethereum_rpc = sov_ethereum::get_ethereum_rpc(da_service, eth_rpc_config);
let ethereum_rpc = sov_ethereum::get_ethereum_rpc::<C, Da>(da_service, eth_rpc_config, storage);

methods
.merge(ethereum_rpc)
Expand Down
11 changes: 9 additions & 2 deletions examples/demo-rollup/src/rollup.rs
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,7 @@ pub async fn new_rollup_with_celestia_da<Vm: ZkvmHost>(
eth_rpc_config: EthRpcConfig {
min_blob_size: Some(1),
sov_tx_signer_priv_key: read_sov_tx_signer_priv_key()?,
#[cfg(feature = "local")]
eth_signer,
},
prover,
Expand Down Expand Up @@ -185,6 +186,7 @@ pub fn new_rollup_with_mock_da_from_config<Vm: ZkvmHost>(
eth_rpc_config: EthRpcConfig {
min_blob_size: Some(1),
sov_tx_signer_priv_key: read_sov_tx_signer_priv_key()?,
#[cfg(feature = "local")]
eth_signer,
},
prover,
Expand Down Expand Up @@ -224,14 +226,19 @@ impl<Vm: ZkvmHost, Da: DaService<Error = anyhow::Error> + Clone> Rollup<Vm, Da>
channel: Option<oneshot::Sender<SocketAddr>>,
) -> Result<(), anyhow::Error> {
let storage = self.app.get_storage();
let mut methods = get_rpc_methods::<DefaultContext, Da::Spec>(storage);
let mut methods = get_rpc_methods::<DefaultContext, Da::Spec>(storage.clone());

// register rpc methods
{
register_ledger(self.ledger_db.clone(), &mut methods)?;
register_sequencer(self.da_service.clone(), &mut self.app, &mut methods)?;
#[cfg(feature = "experimental")]
register_ethereum(self.da_service.clone(), self.eth_rpc_config, &mut methods)?;
register_ethereum::<DefaultContext, Da>(
self.da_service.clone(),
self.eth_rpc_config,
storage,
&mut methods,
)?;
}

let storage = self.app.get_storage();
Expand Down
57 changes: 55 additions & 2 deletions examples/demo-rollup/tests/evm/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,28 @@ impl TestClient {
Ok(receipt_req)
}

async fn set_value_unsigned(
&self,
contract_address: H160,
set_arg: u32,
) -> PendingTransaction<'_, Http> {
let nonce = self.eth_get_transaction_count(self.from_addr).await;

let req = Eip1559TransactionRequest::new()
.from(self.from_addr)
.to(contract_address)
.chain_id(self.chain_id)
.nonce(nonce)
.data(self.contract.set_call_data(set_arg))
.max_priority_fee_per_gas(10u64)
.max_fee_per_gas(MAX_FEE_PER_GAS)
.gas(900000u64);

let typed_transaction = TypedTransaction::Eip1559(req);

self.eth_send_transaction(typed_transaction).await
}

async fn set_value(
&self,
contract_address: H160,
Expand Down Expand Up @@ -131,13 +153,23 @@ impl TestClient {
Ok(ethereum_types::U256::from(resp_array))
}

#[cfg(feature = "local")]
async fn eth_accounts(&self) -> Vec<Address> {
self.http_client
.request("eth_accounts", rpc_params![])
.await
.unwrap()
}

#[cfg(feature = "local")]
async fn eth_send_transaction(&self, tx: TypedTransaction) -> PendingTransaction<'_, Http> {
self.client
.provider()
.send_transaction(tx, None)
.await
.unwrap()
}

async fn eth_chain_id(&self) -> u64 {
let chain_id: ethereum_types::U64 = self
.http_client
Expand Down Expand Up @@ -239,6 +271,24 @@ impl TestClient {
assert_eq!(102, get_arg.as_u32());
}

#[cfg(feature = "local")]
{
let value = 103;

let tx_hash = {
let set_value_req = self.set_value_unsigned(contract_address, value).await;
self.send_publish_batch_request().await;
set_value_req.await.unwrap().unwrap().transaction_hash
};

let latest_block = self.eth_get_block_by_number(None).await;
assert_eq!(latest_block.transactions.len(), 1);
assert_eq!(latest_block.transactions[0], tx_hash);

let get_arg = self.query_contract(contract_address).await?;
assert_eq!(value, get_arg.as_u32());
}

Ok(())
}
}
Expand All @@ -256,8 +306,11 @@ async fn send_tx_test_to_eth(rpc_address: SocketAddr) -> Result<(), Box<dyn std:

let test_client = TestClient::new(chain_id, key, from_addr, contract, rpc_address).await;

let etc_accounts = test_client.eth_accounts().await;
assert_eq!(vec![from_addr], etc_accounts);
#[cfg(feature = "local")]
{
let etc_accounts = test_client.eth_accounts().await;
assert_eq!(vec![from_addr], etc_accounts);
}

let eth_chain_id = test_client.eth_chain_id().await;
assert_eq!(chain_id, eth_chain_id);
Expand Down
3 changes: 2 additions & 1 deletion full-node/sov-ethereum/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -35,5 +35,6 @@ tokio = { workspace = true }

[features]
default = []
experimental = ["demo-stf/experimental", "sov-evm/experimental"]
local = []
experimental = ["demo-stf/experimental", "sov-evm/experimental", "local"]
native = ["demo-stf/native", "sov-evm/native"]
Loading

0 comments on commit 241c417

Please sign in to comment.