Skip to content

Commit

Permalink
chore: bump alloy-eip7702 (#1829)
Browse files Browse the repository at this point in the history
* chore: bump alloy-eip7702

* clippy

* bump tests and fix auth chain_id check bug

* remove prints
  • Loading branch information
klkvr authored Oct 23, 2024
1 parent bafe64b commit e682da7
Show file tree
Hide file tree
Showing 97 changed files with 900 additions and 845 deletions.
12 changes: 7 additions & 5 deletions Cargo.lock

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

2 changes: 1 addition & 1 deletion crates/primitives/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ all = "warn"
[dependencies]
# alloy
alloy-eip2930 = { version = "0.1", default-features = false }
alloy-eip7702 = { version = "0.2", default-features = false, features = [
alloy-eip7702 = { version = "0.3", default-features = false, features = [
"k256",
] }
alloy-primitives = { version = "0.8.8", default-features = false, features = [
Expand Down
12 changes: 1 addition & 11 deletions crates/primitives/src/eip7702.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
pub mod authorization_list;
pub mod bytecode;

use crate::U256;
pub use authorization_list::{
Authorization, AuthorizationList, InvalidAuthorization, RecoveredAuthorization, Signature,
Authorization, AuthorizationList, RecoveredAuthority, RecoveredAuthorization, Signature,
SignedAuthorization,
};
pub use bytecode::{
Expand All @@ -15,12 +14,3 @@ pub const PER_AUTH_BASE_COST: u64 = 12500;

/// Cost of creating authorized account that was previously empty.
pub const PER_EMPTY_ACCOUNT_COST: u64 = 25000;

/// The order of the secp256k1 curve, divided by two. Signatures that should be checked according
/// to EIP-2 should have an S value less than or equal to this.
///
/// `57896044618658097711785492504343953926418782139537452191302581570759080747168`
pub const SECP256K1N_HALF: U256 = U256::from_be_bytes([
0x7F, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
0x5D, 0x57, 0x6E, 0x73, 0x57, 0xA4, 0x50, 0x1D, 0xDF, 0xE9, 0x2F, 0x46, 0x68, 0x1B, 0x20, 0xA0,
]);
104 changes: 3 additions & 101 deletions crates/primitives/src/eip7702/authorization_list.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
pub use alloy_eip7702::{Authorization, SignedAuthorization};
pub use alloy_eip7702::{
Authorization, RecoveredAuthority, RecoveredAuthorization, SignedAuthorization,
};
pub use alloy_primitives::{Parity, Signature};

use crate::Address;
use core::{fmt, ops::Deref};
use std::{boxed::Box, vec::Vec};

use super::SECP256K1N_HALF;

/// Authorization list for EIP-7702 transaction type.
#[derive(Clone, Debug, Eq, PartialEq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
Expand Down Expand Up @@ -36,29 +34,6 @@ impl AuthorizationList {
}
}

/// Validate the authorization list.
pub fn is_valid(&self) -> Result<(), InvalidAuthorization> {
let validate = |auth: &SignedAuthorization| -> Result<(), InvalidAuthorization> {
// Check y_parity
if let Parity::Eip155(parity) = auth.signature().v() {
if parity > u8::MAX as u64 {
return Err(InvalidAuthorization::InvalidYParity);
}
}
Ok(())
};

match self {
Self::Signed(signed) => signed.iter().try_for_each(validate)?,
Self::Recovered(recovered) => recovered
.iter()
.map(|recovered| &recovered.inner)
.try_for_each(validate)?,
};

Ok(())
}

/// Return empty authorization list.
pub fn empty() -> Self {
Self::Recovered(Vec::new())
Expand All @@ -85,76 +60,3 @@ impl AuthorizationList {
Self::Recovered(signed.into_iter().map(|signed| signed.into()).collect())
}
}

/// A recovered authorization.
#[derive(Debug, Clone, Hash, Eq, PartialEq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct RecoveredAuthorization {
#[cfg_attr(feature = "serde", serde(flatten))]
inner: SignedAuthorization,
authority: Option<Address>,
}

impl RecoveredAuthorization {
/// Instantiate without performing recovery. This should be used carefully.
pub const fn new_unchecked(inner: SignedAuthorization, authority: Option<Address>) -> Self {
Self { inner, authority }
}

/// Get the `authority` for the authorization.
///
/// If this is `None`, then the authority could not be recovered.
pub fn authority(&self) -> Option<Address> {
let signature = self.inner.signature();

// Check s-value
if signature.s() > SECP256K1N_HALF {
return None;
}

// Check y_parity, Parity::Parity means that it was 0 or 1.
if !matches!(signature.v(), Parity::Parity(_)) {
return None;
}
self.authority
}

/// Splits the authorization into parts.
pub const fn into_parts(self) -> (SignedAuthorization, Option<Address>) {
(self.inner, self.authority)
}
}

impl From<SignedAuthorization> for RecoveredAuthorization {
fn from(signed_auth: SignedAuthorization) -> Self {
let authority = signed_auth.recover_authority().ok();
Self::new_unchecked(signed_auth, authority)
}
}

impl Deref for RecoveredAuthorization {
type Target = Authorization;

fn deref(&self) -> &Self::Target {
&self.inner
}
}

#[derive(Debug, Clone, PartialEq, Eq, Hash)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub enum InvalidAuthorization {
InvalidChainId,
InvalidYParity,
Eip2InvalidSValue,
}

impl fmt::Display for InvalidAuthorization {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
let s = match self {
Self::InvalidChainId => "Invalid chain_id, Expect chain's ID or zero",
Self::InvalidYParity => "Invalid y_parity, Expect 0 or 1.",
Self::Eip2InvalidSValue => "Invalid signature s-value.",
};
f.write_str(s)
}
}
3 changes: 0 additions & 3 deletions crates/primitives/src/env.rs
Original file line number Diff line number Diff line change
Expand Up @@ -202,9 +202,6 @@ impl Env {
return Err(InvalidTransaction::EmptyAuthorizationList);
}

// Validate the authorization item signature `v` to be less than u8::MAX.
auth_list.is_valid()?;

// Check if other fields are unset.
if self.tx.max_fee_per_blob_gas.is_some() || !self.tx.blob_hashes.is_empty() {
return Err(InvalidTransaction::AuthorizationListInvalidFields);
Expand Down
2 changes: 1 addition & 1 deletion crates/primitives/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ pub use bitvec;
pub use bytecode::*;
pub use constants::*;
pub use eip7702::{
Authorization, AuthorizationList, Eip7702Bytecode, Eip7702DecodeError, InvalidAuthorization,
Authorization, AuthorizationList, Eip7702Bytecode, Eip7702DecodeError, RecoveredAuthority,
RecoveredAuthorization, Signature, SignedAuthorization, EIP7702_MAGIC, EIP7702_MAGIC_BYTES,
};
pub use env::*;
Expand Down
13 changes: 1 addition & 12 deletions crates/primitives/src/result.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
use crate::{
eip7702::authorization_list::InvalidAuthorization, Address, Bytes, EvmState, Log, U256,
};
use crate::{Address, Bytes, EvmState, Log, U256};
use core::fmt;
use std::{boxed::Box, string::String, vec::Vec};

Expand Down Expand Up @@ -315,19 +313,11 @@ pub enum InvalidTransaction {
AuthorizationListInvalidFields,
/// Empty Authorization List is not allowed.
EmptyAuthorizationList,
/// Invalid EIP-7702 Authorization List
InvalidAuthorizationList(InvalidAuthorization),
/// Optimism-specific transaction validation error.
#[cfg(feature = "optimism")]
OptimismError(OptimismInvalidTransaction),
}

impl From<InvalidAuthorization> for InvalidTransaction {
fn from(value: InvalidAuthorization) -> Self {
Self::InvalidAuthorizationList(value)
}
}

#[cfg(feature = "std")]
impl std::error::Error for InvalidTransaction {}

Expand Down Expand Up @@ -406,7 +396,6 @@ impl fmt::Display for InvalidTransaction {
write!(f, "authorization list tx has invalid fields")
}
Self::EmptyAuthorizationList => write!(f, "empty authorization list"),
Self::InvalidAuthorizationList(i) => fmt::Display::fmt(i, f),
#[cfg(feature = "optimism")]
Self::OptimismError(op_error) => op_error.fmt(f),
}
Expand Down
2 changes: 1 addition & 1 deletion crates/revm/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ ethers-core = { version = "2.0", optional = true }

# alloydb
alloy-provider = { version = "0.3", optional = true, default-features = false }
alloy-eips = { version = "0.3", optional = true, default-features = false }
alloy-eips = { version = "0.3.1", optional = true, default-features = false }
alloy-transport = { version = "0.3", optional = true, default-features = false }

[dev-dependencies]
Expand Down
9 changes: 5 additions & 4 deletions crates/revm/src/evm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -397,7 +397,9 @@ mod tests {
use crate::{
db::BenchmarkDB,
interpreter::opcode::{PUSH1, SSTORE},
primitives::{address, Authorization, Bytecode, RecoveredAuthorization, Signature, U256},
primitives::{
address, Authorization, Bytecode, RecoveredAuthority, RecoveredAuthorization, U256,
},
};

#[test]
Expand All @@ -418,9 +420,8 @@ mod tests {
chain_id: 1,
address: delegate,
nonce: 0,
}
.into_signed(Signature::test_signature()),
Some(auth),
},
RecoveredAuthority::Valid(auth),
)]
.into(),
);
Expand Down
5 changes: 2 additions & 3 deletions crates/revm/src/handler/mainnet/pre_execution.rs
Original file line number Diff line number Diff line change
Expand Up @@ -115,9 +115,8 @@ pub fn apply_eip7702_auth_list<SPEC: Spec, EXT, DB: Database>(
let mut refunded_accounts = 0;
for authorization in authorization_list.recovered_iter() {
// 1. Verify the chain id is either 0 or the chain's current ID.
if !authorization.chain_id() == 0
&& authorization.chain_id() != context.evm.inner.env.cfg.chain_id
{
let chain_id = authorization.chain_id();
if chain_id != 0 && chain_id != context.evm.inner.env.cfg.chain_id {
continue;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@
"comment": "`execution-spec-tests` generated test",
"filling-transition-tool": "ethereumjs t8n v1",
"description": "Test function documentation:\n\n Test the BLS12_G1ADD precompile using different call types.",
"url": "https://github.com/ethereum/execution-spec-tests/blob/pectra-devnet-4@v1.0.0/tests/prague/eip2537_bls_12_381_precompiles/test_bls12_g1add.py#L204",
"url": "https://github.com/ethereum/execution-spec-tests/blob/pectra-devnet-4@v1.0.1/tests/prague/eip2537_bls_12_381_precompiles/test_bls12_g1add.py#L204",
"reference-spec": "https://github.com/ethereum/EIPs/blob/master/EIPS/eip-2537.md",
"reference-spec-version": "cd0f016ad0c4c68b8b1f5c502ef61ab9353b6e5e"
}
Expand Down Expand Up @@ -124,7 +124,7 @@
"comment": "`execution-spec-tests` generated test",
"filling-transition-tool": "ethereumjs t8n v1",
"description": "Test function documentation:\n\n Test the BLS12_G1ADD precompile using different call types.",
"url": "https://github.com/ethereum/execution-spec-tests/blob/pectra-devnet-4@v1.0.0/tests/prague/eip2537_bls_12_381_precompiles/test_bls12_g1add.py#L204",
"url": "https://github.com/ethereum/execution-spec-tests/blob/pectra-devnet-4@v1.0.1/tests/prague/eip2537_bls_12_381_precompiles/test_bls12_g1add.py#L204",
"reference-spec": "https://github.com/ethereum/EIPs/blob/master/EIPS/eip-2537.md",
"reference-spec-version": "cd0f016ad0c4c68b8b1f5c502ef61ab9353b6e5e"
}
Expand Down Expand Up @@ -189,7 +189,7 @@
"comment": "`execution-spec-tests` generated test",
"filling-transition-tool": "ethereumjs t8n v1",
"description": "Test function documentation:\n\n Test the BLS12_G1ADD precompile using different call types.",
"url": "https://github.com/ethereum/execution-spec-tests/blob/pectra-devnet-4@v1.0.0/tests/prague/eip2537_bls_12_381_precompiles/test_bls12_g1add.py#L204",
"url": "https://github.com/ethereum/execution-spec-tests/blob/pectra-devnet-4@v1.0.1/tests/prague/eip2537_bls_12_381_precompiles/test_bls12_g1add.py#L204",
"reference-spec": "https://github.com/ethereum/EIPs/blob/master/EIPS/eip-2537.md",
"reference-spec-version": "cd0f016ad0c4c68b8b1f5c502ef61ab9353b6e5e"
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@
"comment": "`execution-spec-tests` generated test",
"filling-transition-tool": "ethereumjs t8n v1",
"description": "Test function documentation:\n\n Test the BLS12_G1ADD precompile gas requirements.",
"url": "https://github.com/ethereum/execution-spec-tests/blob/pectra-devnet-4@v1.0.0/tests/prague/eip2537_bls_12_381_precompiles/test_bls12_g1add.py#L170",
"url": "https://github.com/ethereum/execution-spec-tests/blob/pectra-devnet-4@v1.0.1/tests/prague/eip2537_bls_12_381_precompiles/test_bls12_g1add.py#L170",
"reference-spec": "https://github.com/ethereum/EIPs/blob/master/EIPS/eip-2537.md",
"reference-spec-version": "cd0f016ad0c4c68b8b1f5c502ef61ab9353b6e5e"
}
Expand Down Expand Up @@ -124,7 +124,7 @@
"comment": "`execution-spec-tests` generated test",
"filling-transition-tool": "ethereumjs t8n v1",
"description": "Test function documentation:\n\n Test the BLS12_G1ADD precompile gas requirements.",
"url": "https://github.com/ethereum/execution-spec-tests/blob/pectra-devnet-4@v1.0.0/tests/prague/eip2537_bls_12_381_precompiles/test_bls12_g1add.py#L170",
"url": "https://github.com/ethereum/execution-spec-tests/blob/pectra-devnet-4@v1.0.1/tests/prague/eip2537_bls_12_381_precompiles/test_bls12_g1add.py#L170",
"reference-spec": "https://github.com/ethereum/EIPs/blob/master/EIPS/eip-2537.md",
"reference-spec-version": "cd0f016ad0c4c68b8b1f5c502ef61ab9353b6e5e"
}
Expand Down
Loading

0 comments on commit e682da7

Please sign in to comment.