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: add nonce to auth msg #9

Open
wants to merge 3 commits into
base: cl/eip-3074
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
3 changes: 3 additions & 0 deletions crates/interpreter/src/host.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@ pub trait Host {
/// Get code hash of `address` and if the account is cold.
fn code_hash(&mut self, address: Address) -> Option<(B256, bool)>;

/// Get nonce of `address`.
fn nonce(&mut self, address: Address) -> Option<u64>;

/// Get storage value of `address` at `index` and if the account is cold.
fn sload(&mut self, address: Address, index: U256) -> Option<(U256, bool)>;

Expand Down
5 changes: 5 additions & 0 deletions crates/interpreter/src/host/dummy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,11 @@ impl Host for DummyHost {
Some((KECCAK_EMPTY, false))
}

#[inline]
fn nonce(&mut self, _address: Address) -> Option<u64> {
Some(0)
}

#[inline]
fn sload(&mut self, __address: Address, index: U256) -> Option<(U256, bool)> {
match self.storage.entry(index) {
Expand Down
11 changes: 8 additions & 3 deletions crates/interpreter/src/instructions/host.rs
Original file line number Diff line number Diff line change
Expand Up @@ -405,15 +405,20 @@ pub fn auth<H: Host, SPEC: Spec>(interpreter: &mut Interpreter<'_>, host: &mut H
});

// Build the original auth message and compute the hash.
let mut message = [0u8; 97];
let mut message = [0u8; 129];
message[0] = 0x04; // AUTH_MAGIC - add constant?
message[1..33].copy_from_slice(
U256::from(host.env().cfg.chain_id)
.to_be_bytes::<32>()
.as_ref(),
);
message[33..65].copy_from_slice(interpreter.contract().address.into_word().as_ref());
message[65..97].copy_from_slice(commit.unwrap_or_default().as_ref());
message[33..65].copy_from_slice(
U256::from(host.nonce(authority).unwrap())
.to_be_bytes::<32>()
.as_ref(),
);
message[65..97].copy_from_slice(interpreter.contract().address.into_word().as_ref());
message[97..129].copy_from_slice(commit.unwrap_or_default().as_ref());
let message_hash = revm_primitives::keccak256(&message);

// Verify the signature
Expand Down
9 changes: 9 additions & 0 deletions crates/revm/src/evm_context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,15 @@ impl<'a, DB: Database> EVMData<'a, DB> {
Some((acc.info.code_hash, is_cold))
}

/// Return account nonce.
pub fn nonce(&mut self, address: Address) -> Option<u64> {
self.journaled_state
.load_account(address, &mut self.db)
.map_err(|e| self.error = Some(e))
.ok()
.map(|(acc, _)| acc.info.nonce)
}

/// Load storage slot, if storage is not present inside the account then it will be loaded from database.
pub fn sload(&mut self, address: Address, index: U256) -> Option<(U256, bool)> {
// account is always warm. reference on that statement https://eips.ethereum.org/EIPS/eip-2929 see `Note 2:`
Expand Down
4 changes: 4 additions & 0 deletions crates/revm/src/evm_impl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -840,6 +840,10 @@ impl<'a, GSPEC: Spec + 'static, DB: Database> Host for EVMImpl<'a, GSPEC, DB> {
self.data.code_hash(address)
}

fn nonce(&mut self, address: Address) -> Option<u64> {
self.data.nonce(address)
}

fn sload(&mut self, address: Address, index: U256) -> Option<(U256, bool)> {
self.data.sload(address, index)
}
Expand Down