From 86fc74b2b36d4c09e80d0e3d5650678f176ba787 Mon Sep 17 00:00:00 2001 From: ZhAnGeek Date: Wed, 6 Nov 2024 16:04:30 +0800 Subject: [PATCH] feat: makes revm sp1 compatible through feature controlling --- Cargo.lock | 32 +++++---------------------- crates/precompile/Cargo.toml | 12 +++++----- crates/precompile/src/bls.rs | 2 +- crates/precompile/src/cometbft.rs | 2 +- crates/precompile/src/iavl.rs | 15 +++++++++++++ crates/precompile/src/lib.rs | 12 +++++++--- crates/precompile/src/tendermint.rs | 7 ++++++ crates/precompile/src/tm_secp256k1.rs | 11 +++++++-- 8 files changed, 54 insertions(+), 39 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index d0d004c2..0a70a6e9 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1104,7 +1104,7 @@ checksum = "4b82cf0babdbd58558212896d1a4272303a57bdb245c2bf1147185fb45640e70" [[package]] name = "cometbft" version = "0.1.0-alpha.2" -source = "git+https://github.com/bnb-chain/greenfield-cometbft-rs.git?rev=1282547#12825470f8b2df8e960e39a84ad4c9921492f512" +source = "git+https://github.com/bnb-chain/greenfield-cometbft-rs.git?rev=5582327#558232788fca6ade6ae2e16a7557af1982105c4f" dependencies = [ "bytes 1.7.1", "cometbft-proto", @@ -1112,7 +1112,6 @@ dependencies = [ "ed25519 2.2.3", "ed25519-consensus", "flex-error", - "futures", "num-traits", "once_cell", "prost 0.12.6", @@ -1132,7 +1131,7 @@ dependencies = [ [[package]] name = "cometbft-config" version = "0.1.0-alpha.2" -source = "git+https://github.com/bnb-chain/greenfield-cometbft-rs.git?rev=1282547#12825470f8b2df8e960e39a84ad4c9921492f512" +source = "git+https://github.com/bnb-chain/greenfield-cometbft-rs.git?rev=5582327#558232788fca6ade6ae2e16a7557af1982105c4f" dependencies = [ "cometbft", "flex-error", @@ -1145,7 +1144,7 @@ dependencies = [ [[package]] name = "cometbft-light-client" version = "0.1.0-alpha.2" -source = "git+https://github.com/bnb-chain/greenfield-cometbft-rs.git?rev=1282547#12825470f8b2df8e960e39a84ad4c9921492f512" +source = "git+https://github.com/bnb-chain/greenfield-cometbft-rs.git?rev=5582327#558232788fca6ade6ae2e16a7557af1982105c4f" dependencies = [ "cometbft", "cometbft-light-client-verifier", @@ -1154,7 +1153,6 @@ dependencies = [ "crossbeam-channel", "derive_more 0.99.18", "flex-error", - "futures", "regex", "serde", "serde_cbor", @@ -1162,14 +1160,13 @@ dependencies = [ "serde_json", "static_assertions", "time", - "tokio", "tracing", ] [[package]] name = "cometbft-light-client-verifier" version = "0.1.0-alpha.2" -source = "git+https://github.com/bnb-chain/greenfield-cometbft-rs.git?rev=1282547#12825470f8b2df8e960e39a84ad4c9921492f512" +source = "git+https://github.com/bnb-chain/greenfield-cometbft-rs.git?rev=5582327#558232788fca6ade6ae2e16a7557af1982105c4f" dependencies = [ "cometbft", "derive_more 0.99.18", @@ -1181,7 +1178,7 @@ dependencies = [ [[package]] name = "cometbft-proto" version = "0.1.0-alpha.2" -source = "git+https://github.com/bnb-chain/greenfield-cometbft-rs.git?rev=1282547#12825470f8b2df8e960e39a84ad4c9921492f512" +source = "git+https://github.com/bnb-chain/greenfield-cometbft-rs.git?rev=5582327#558232788fca6ade6ae2e16a7557af1982105c4f" dependencies = [ "bytes 1.7.1", "flex-error", @@ -1198,7 +1195,7 @@ dependencies = [ [[package]] name = "cometbft-rpc" version = "0.1.0-alpha.2" -source = "git+https://github.com/bnb-chain/greenfield-cometbft-rs.git?rev=1282547#12825470f8b2df8e960e39a84ad4c9921492f512" +source = "git+https://github.com/bnb-chain/greenfield-cometbft-rs.git?rev=5582327#558232788fca6ade6ae2e16a7557af1982105c4f" dependencies = [ "async-trait", "bytes 1.7.1", @@ -1206,12 +1203,10 @@ dependencies = [ "cometbft-config", "cometbft-proto", "flex-error", - "futures", "getrandom 0.2.15", "peg", "pin-project", "rand 0.8.5", - "reqwest 0.11.27", "semver 1.0.23", "serde", "serde_bytes", @@ -1220,8 +1215,6 @@ dependencies = [ "subtle-encoding", "thiserror", "time", - "tokio", - "tracing", "url", "uuid", "walkdir", @@ -3812,7 +3805,6 @@ dependencies = [ "percent-encoding", "pin-project-lite", "rustls 0.21.12", - "rustls-native-certs", "rustls-pemfile 1.0.4", "serde", "serde_json", @@ -4232,18 +4224,6 @@ dependencies = [ "zeroize", ] -[[package]] -name = "rustls-native-certs" -version = "0.6.3" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a9aace74cb666635c918e9c12bc0d348266037aa8eb599b5cba565709a8dff00" -dependencies = [ - "openssl-probe", - "rustls-pemfile 1.0.4", - "schannel", - "security-framework", -] - [[package]] name = "rustls-pemfile" version = "1.0.4" diff --git a/crates/precompile/Cargo.toml b/crates/precompile/Cargo.toml index 1b89d4f3..ed455c04 100644 --- a/crates/precompile/Cargo.toml +++ b/crates/precompile/Cargo.toml @@ -38,13 +38,13 @@ secp256k1 = { version = ">=0.28, <=0.29", default-features = false, features = [ "rand", "global-context", ], optional = true } -cometbft = { git = "https://github.com/bnb-chain/greenfield-cometbft-rs.git", rev = "1282547" } -cometbft-light-client-verifier = { git = "https://github.com/bnb-chain/greenfield-cometbft-rs.git", rev = "1282547" } -cometbft-proto = { git = "https://github.com/bnb-chain/greenfield-cometbft-rs.git", rev = "1282547" } -cometbft-light-client = { git = "https://github.com/bnb-chain/greenfield-cometbft-rs.git", rev = "1282547" } +cometbft = { git = "https://github.com/bnb-chain/greenfield-cometbft-rs.git", rev = "5582327" } +cometbft-light-client-verifier = { git = "https://github.com/bnb-chain/greenfield-cometbft-rs.git", rev = "5582327" } +cometbft-proto = { git = "https://github.com/bnb-chain/greenfield-cometbft-rs.git", rev = "5582327" } +cometbft-light-client = { git = "https://github.com/bnb-chain/greenfield-cometbft-rs.git", rev = "5582327", default-features = false } prost = { version = "0.12.6" } bls_on_arkworks = "0.3.0" -tendermint = { git = "https://github.com/bnb-chain/tendermint-rs-parlia", rev = "8c21ccbd58a174e07eed2c9343e63ccd00f0fbd5", features = ["secp256k1"] } +tendermint = { git = "https://github.com/bnb-chain/tendermint-rs-parlia", rev = "8c21ccbd58a174e07eed2c9343e63ccd00f0fbd5", optional = true, features = ["secp256k1"] } parity-bytes = { version = "0.1.2", default-features = false } # SHA2-256 and RIPEMD-160 @@ -99,7 +99,7 @@ std = [ hashbrown = ["revm-primitives/hashbrown"] asm-keccak = ["revm-primitives/asm-keccak"] -bsc = ["revm-primitives/bsc", "secp256k1", "secp256r1"] +bsc = ["revm-primitives/bsc", "secp256k1", "secp256r1", "tendermint"] optimism = ["revm-primitives/optimism", "secp256r1"] # Optimism default handler enabled Optimism handler register by default in EvmBuilder. diff --git a/crates/precompile/src/bls.rs b/crates/precompile/src/bls.rs index 6e5bebba..b68d5699 100644 --- a/crates/precompile/src/bls.rs +++ b/crates/precompile/src/bls.rs @@ -3,7 +3,7 @@ use bls_on_arkworks as bls; use revm_primitives::{Bytes, PrecompileOutput}; use std::vec::Vec; -pub(crate) const BLS_SIGNATURE_VALIDATION: PrecompileWithAddress = PrecompileWithAddress( +pub const BLS_SIGNATURE_VALIDATION: PrecompileWithAddress = PrecompileWithAddress( crate::u64_to_address(102), Precompile::Standard(bls_signature_validation_run), ); diff --git a/crates/precompile/src/cometbft.rs b/crates/precompile/src/cometbft.rs index 52f3f6ad..a76187e3 100644 --- a/crates/precompile/src/cometbft.rs +++ b/crates/precompile/src/cometbft.rs @@ -14,7 +14,7 @@ use prost::Message; use revm_primitives::{Bytes, PrecompileOutput}; use std::{borrow::ToOwned, string::String, vec::Vec}; -pub(crate) const COMETBFT_LIGHT_BLOCK_VALIDATION: PrecompileWithAddress = PrecompileWithAddress( +pub const COMETBFT_LIGHT_BLOCK_VALIDATION: PrecompileWithAddress = PrecompileWithAddress( crate::u64_to_address(103), Precompile::Standard(cometbft_light_block_validation_run), ); diff --git a/crates/precompile/src/iavl.rs b/crates/precompile/src/iavl.rs index 47593ab5..abe87821 100644 --- a/crates/precompile/src/iavl.rs +++ b/crates/precompile/src/iavl.rs @@ -1,8 +1,10 @@ #![allow(dead_code)] +#![allow(unused_imports)] use crate::{Bytes, Error, Precompile, PrecompileError, PrecompileResult, PrecompileWithAddress}; use parity_bytes::BytesRef; use revm_primitives::PrecompileOutput; +#[cfg(feature = "bsc")] use tendermint::lite::iavl_proof; /// Iavl proof validation precompile for BSC. @@ -61,6 +63,7 @@ fn iavl_proof_validation_run_plato(input: &Bytes, gas_limit: u64) -> PrecompileR } /// Run Iavl proof validation with given hardfork toggles. +#[cfg(feature = "bsc")] fn iavl_proof_validation_run_inner( input: &Bytes, gas_limit: u64, @@ -86,7 +89,19 @@ fn iavl_proof_validation_run_inner( } } +#[cfg(not(feature = "bsc"))] +fn iavl_proof_validation_run_inner( + _input: &Bytes, + _gas_limit: u64, + _is_moran: bool, + _is_planck: bool, + _is_plato: bool, +) -> PrecompileResult { + Ok(PrecompileOutput::new(0, Bytes::default())) +} + #[cfg(test)] +#[cfg(feature = "bsc")] mod tests { use super::*; use revm_primitives::hex; diff --git a/crates/precompile/src/lib.rs b/crates/precompile/src/lib.rs index ec297fdd..706ad0b6 100644 --- a/crates/precompile/src/lib.rs +++ b/crates/precompile/src/lib.rs @@ -23,8 +23,8 @@ pub mod secp256k1; pub mod secp256r1; pub mod utilities; -mod bls; -mod cometbft; +pub mod bls; +pub mod cometbft; mod double_sign; mod iavl; mod tendermint; @@ -181,7 +181,9 @@ impl Precompiles { pub fn nano() -> &'static Self { static INSTANCE: OnceBox = OnceBox::new(); INSTANCE.get_or_init(|| { + #[allow(unused_mut)] let mut precompiles = Self::istanbul().clone(); + #[cfg(feature = "bsc")] precompiles.extend([ tendermint::TENDERMINT_HEADER_VALIDATION_NANO, iavl::IAVL_PROOF_VALIDATION_NANO, @@ -195,7 +197,9 @@ impl Precompiles { pub fn moran() -> &'static Self { static INSTANCE: OnceBox = OnceBox::new(); INSTANCE.get_or_init(|| { + #[allow(unused_mut)] let mut precompiles = Self::istanbul().clone(); + #[cfg(feature = "bsc")] precompiles.extend([ tendermint::TENDERMINT_HEADER_VALIDATION, iavl::IAVL_PROOF_VALIDATION_MORAN, @@ -209,7 +213,9 @@ impl Precompiles { pub fn planck() -> &'static Self { static INSTANCE: OnceBox = OnceBox::new(); INSTANCE.get_or_init(|| { + #[allow(unused_mut)] let mut precompiles = Self::istanbul().clone(); + #[cfg(feature = "bsc")] precompiles.extend([ tendermint::TENDERMINT_HEADER_VALIDATION, iavl::IAVL_PROOF_VALIDATION_PLANCK, @@ -263,7 +269,7 @@ impl Precompiles { precompiles.extend([double_sign::DOUBLE_SIGN_EVIDENCE_VALIDATION]); // this feature is enabled with bsc - #[cfg(feature = "secp256k1")] + #[cfg(all(feature = "secp256k1", feature = "bsc"))] precompiles.extend([tm_secp256k1::TM_SECP256K1_SIGNATURE_RECOVER]); Box::new(precompiles) diff --git a/crates/precompile/src/tendermint.rs b/crates/precompile/src/tendermint.rs index 01289472..0f4b4a3a 100644 --- a/crates/precompile/src/tendermint.rs +++ b/crates/precompile/src/tendermint.rs @@ -1,26 +1,32 @@ +#![allow(unused_imports)] use crate::{Bytes, Error, Precompile, PrecompileError, PrecompileResult, PrecompileWithAddress}; use parity_bytes::BytesRef; use revm_primitives::PrecompileOutput; +#[cfg(feature = "bsc")] use tendermint::lite::light_client; /// Tendermint precompile for BSC. +#[cfg(feature = "bsc")] pub(crate) const TENDERMINT_HEADER_VALIDATION: PrecompileWithAddress = PrecompileWithAddress( crate::u64_to_address(100), Precompile::Standard(crate::tendermint::tendermint_header_validation_run), ); +#[allow(dead_code)] /// Tendermint precompile for BSC after Nano hardfork. pub(crate) const TENDERMINT_HEADER_VALIDATION_NANO: PrecompileWithAddress = PrecompileWithAddress( crate::u64_to_address(100), Precompile::Standard(crate::tendermint::tendermint_header_validation_run_nano), ); +#[allow(dead_code)] /// Run the Tendermint header validation precompile after Nano hardfork. fn tendermint_header_validation_run_nano(_input: &Bytes, _gas_limit: u64) -> PrecompileResult { Err(PrecompileError::other("suspended").into()) } /// Run the Tendermint header validation precompile. +#[cfg(feature = "bsc")] fn tendermint_header_validation_run(input: &Bytes, gas_limit: u64) -> PrecompileResult { const TENDERMINT_HEADER_VALIDATION_BASE: u64 = 3_000; @@ -41,6 +47,7 @@ fn tendermint_header_validation_run(input: &Bytes, gas_limit: u64) -> Precompile } #[cfg(test)] +#[cfg(feature = "bsc")] mod tests { use super::*; use revm_primitives::hex; diff --git a/crates/precompile/src/tm_secp256k1.rs b/crates/precompile/src/tm_secp256k1.rs index e06cfa9b..a83781c6 100644 --- a/crates/precompile/src/tm_secp256k1.rs +++ b/crates/precompile/src/tm_secp256k1.rs @@ -1,16 +1,23 @@ +#![allow(unused_imports)] + use crate::{Bytes, Error, Precompile, PrecompileError, PrecompileResult, PrecompileWithAddress}; use revm_primitives::PrecompileOutput; use secp256k1::{ecdsa, Message, PublicKey}; +#[cfg(feature = "bsc")] use tendermint::{account, public_key}; /// Tendermint SECP256K1 signature recover precompile for BSC. +#[cfg(feature = "bsc")] pub(crate) const TM_SECP256K1_SIGNATURE_RECOVER: PrecompileWithAddress = PrecompileWithAddress( crate::u64_to_address(105), Precompile::Standard(tm_secp256k1_signature_recover_run), ); +#[allow(dead_code)] const SECP256K1_PUBKEY_LENGTH: usize = 33; +#[allow(dead_code)] const SECP256K1_SIGNATURE_LENGTH: usize = 64; +#[allow(dead_code)] const SECP256K1_SIGNATURE_MSGHASH_LENGTH: usize = 32; /// Runs the Tendermint SECP256K1 signature recover precompile. @@ -20,6 +27,7 @@ const SECP256K1_SIGNATURE_MSGHASH_LENGTH: usize = 32; /// | PubKey | Signature | SignatureMsgHash | /// /// | 33 bytes | 64 bytes | 32 bytes | +#[cfg(feature = "bsc")] fn tm_secp256k1_signature_recover_run(input: &Bytes, gas_limit: u64) -> PrecompileResult { const TM_SECP256K1_SIGNATURE_RECOVER_BASE: u64 = 3_000; @@ -57,13 +65,11 @@ fn tm_secp256k1_signature_recover_run(input: &Bytes, gas_limit: u64) -> Precompi if !res { return Err(PrecompileError::other("invalid signature").into()); } - let tm_pub_key = match public_key::PublicKey::from_raw_secp256k1(&input[..SECP256K1_PUBKEY_LENGTH]) { Some(pk) => pk, None => return Err(PrecompileError::other("invalid pubkey").into()), }; - return Ok(PrecompileOutput::new( TM_SECP256K1_SIGNATURE_RECOVER_BASE, Bytes::copy_from_slice(account::Id::from(tm_pub_key).as_bytes()), @@ -71,6 +77,7 @@ fn tm_secp256k1_signature_recover_run(input: &Bytes, gas_limit: u64) -> Precompi } #[cfg(test)] +#[cfg(feature = "bsc")] mod tests { use super::*; use revm_primitives::hex;