Skip to content

Commit

Permalink
EVM: Fix Sequencer Nonce (#1014)
Browse files Browse the repository at this point in the history
* fix sequencer nonce

* switch to atomic
  • Loading branch information
orkunkilic authored Oct 10, 2023
1 parent 52c33ae commit 22f51a7
Showing 1 changed file with 9 additions and 12 deletions.
21 changes: 9 additions & 12 deletions full-node/sov-ethereum/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,15 @@ pub use sov_evm::DevSigner;
#[cfg(feature = "experimental")]
pub mod experimental {
use std::array::TryFromSliceError;
use std::collections::HashMap;
use std::sync::atomic::{AtomicU64, Ordering};
use std::sync::{Arc, Mutex};

use borsh::ser::BorshSerialize;
use demo_stf::runtime::{DefaultContext, Runtime};
use ethers::types::{Bytes, H256};
use jsonrpsee::types::ErrorObjectOwned;
use jsonrpsee::RpcModule;
use reth_primitives::{
Address as RethAddress, TransactionSignedNoHash as RethTransactionSignedNoHash, U128, U256,
};
use reth_primitives::{TransactionSignedNoHash as RethTransactionSignedNoHash, U128, U256};
use reth_rpc_types::{CallRequest, TransactionRequest, TypedTransactionRequest};
use sov_evm::{CallMessage, Evm, RlpEvmTransaction};
use sov_modules_api::default_signature::private_key::DefaultPrivateKey;
Expand Down Expand Up @@ -58,7 +56,7 @@ pub mod experimental {
}

pub struct Ethereum<C: sov_modules_api::Context, Da: DaService> {
nonces: Mutex<HashMap<RethAddress, u64>>,
nonce: AtomicU64,
da_service: Da,
batch_builder: Arc<Mutex<EthBatchBuilder>>,
eth_rpc_config: EthRpcConfig,
Expand All @@ -67,14 +65,14 @@ pub mod experimental {

impl<C: sov_modules_api::Context, Da: DaService> Ethereum<C, Da> {
fn new(
nonces: Mutex<HashMap<RethAddress, u64>>,
nonce: AtomicU64,
da_service: Da,
batch_builder: Arc<Mutex<EthBatchBuilder>>,
eth_rpc_config: EthRpcConfig,
storage: C::Storage,
) -> Self {
Self {
nonces,
nonce,
da_service,
batch_builder,
eth_rpc_config,
Expand All @@ -91,12 +89,8 @@ pub mod experimental {
let signed_transaction: RethTransactionSignedNoHash = raw_tx.clone().try_into()?;

let tx_hash = signed_transaction.hash();
let sender = signed_transaction
.recover_signer()
.ok_or(sov_evm::EthApiError::InvalidTransactionSignature)?;

let mut nonces = self.nonces.lock().unwrap();
let nonce = *nonces.entry(sender).and_modify(|n| *n += 1).or_insert(0);
let nonce = self.nonce.load(Ordering::SeqCst);

let tx = CallMessage { tx: raw_tx };
let message = <Runtime<DefaultContext, Da::Spec> as EncodeCall<
Expand All @@ -108,6 +102,9 @@ pub mod experimental {
message,
nonce,
);

self.nonce.store(nonce + 1, Ordering::SeqCst);

Ok((H256::from(tx_hash), tx.try_to_vec()?))
}

Expand Down

0 comments on commit 22f51a7

Please sign in to comment.