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: makes revm sp1 compatible through feature controlling #84

Open
wants to merge 5 commits into
base: main
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
32 changes: 6 additions & 26 deletions Cargo.lock

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

16 changes: 8 additions & 8 deletions crates/precompile/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -38,14 +38,14 @@ 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 = "ccf58da" }
cometbft-light-client-verifier = { git = "https://github.com/bnb-chain/greenfield-cometbft-rs.git", rev = "ccf58da" }
cometbft-proto = { git = "https://github.com/bnb-chain/greenfield-cometbft-rs.git", rev = "ccf58da" }
cometbft-light-client = { git = "https://github.com/bnb-chain/greenfield-cometbft-rs.git", rev = "ccf58da", 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"] }
parity-bytes = { version = "0.1.2", default-features = false }
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, optional = true }

# SHA2-256 and RIPEMD-160
sha2 = { version = "0.10", default-features = false }
Expand Down Expand Up @@ -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", "parity-bytes"]

optimism = ["revm-primitives/optimism", "secp256r1"]
# Optimism default handler enabled Optimism handler register by default in EvmBuilder.
Expand All @@ -126,7 +126,7 @@ portable = ["revm-primitives/portable", "c-kzg?/portable"]
# Use `secp256k1` as a faster alternative to `k256`.
# The problem that `secp256k1` has is it fails to build for `wasm` target on Windows and Mac as it is c lib.
# In Linux it passes. If you don't require to build wasm on win/mac, it is safe to use it and it is enabled by default.
secp256k1 = ["dep:secp256k1"]
secp256k1 = ["dep:secp256k1", "tendermint"]

# Enables the BLS12-381 precompiles.
blst = ["dep:blst"]
Expand Down
2 changes: 1 addition & 1 deletion crates/precompile/src/bls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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),
);
Expand Down
4 changes: 2 additions & 2 deletions crates/precompile/src/cometbft.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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),
);
Expand Down Expand Up @@ -408,7 +408,7 @@ fn encode_light_block_validation_result(
let mut output =
vec![0; (VALIDATE_RESULT_METADATA_LENGTH + consensus_state_bytes.len() as u64) as usize];
output[0] = if validator_set_changed { 1 } else { 0 };
output[24..32].copy_from_slice(consensus_state_bytes.len().to_be_bytes().as_ref());
output[24..32].copy_from_slice((consensus_state_bytes.len() as u64).to_be_bytes().as_ref());
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Compatibility for 32-bit system, risv32 running panic for this line due to mismatch len

output[32..].copy_from_slice(consensus_state_bytes.as_ref());
Bytes::from(output)
}
Expand Down
65 changes: 45 additions & 20 deletions crates/precompile/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,12 @@ pub mod secp256k1;
pub mod secp256r1;
pub mod utilities;

mod bls;
mod cometbft;
pub mod bls;
pub mod cometbft;
mod double_sign;
#[cfg(feature = "bsc")]
mod iavl;
#[cfg(feature = "bsc")]
mod tendermint;
#[cfg(feature = "secp256k1")]
mod tm_secp256k1;
Expand Down Expand Up @@ -181,11 +183,17 @@ impl Precompiles {
pub fn nano() -> &'static Self {
static INSTANCE: OnceBox<Precompiles> = OnceBox::new();
INSTANCE.get_or_init(|| {
let mut precompiles = Self::istanbul().clone();
precompiles.extend([
tendermint::TENDERMINT_HEADER_VALIDATION_NANO,
iavl::IAVL_PROOF_VALIDATION_NANO,
]);
let precompiles = Self::istanbul().clone();

#[cfg(feature = "bsc")]
let precompiles = {
let mut precompiles = precompiles;
precompiles.extend([
tendermint::TENDERMINT_HEADER_VALIDATION_NANO,
iavl::IAVL_PROOF_VALIDATION_NANO,
]);
precompiles
};

Box::new(precompiles)
})
Expand All @@ -195,11 +203,17 @@ impl Precompiles {
pub fn moran() -> &'static Self {
static INSTANCE: OnceBox<Precompiles> = OnceBox::new();
INSTANCE.get_or_init(|| {
let mut precompiles = Self::istanbul().clone();
precompiles.extend([
tendermint::TENDERMINT_HEADER_VALIDATION,
iavl::IAVL_PROOF_VALIDATION_MORAN,
]);
let precompiles = Self::istanbul().clone();

#[cfg(feature = "bsc")]
let precompiles = {
let mut precompiles = precompiles;
precompiles.extend([
tendermint::TENDERMINT_HEADER_VALIDATION,
iavl::IAVL_PROOF_VALIDATION_MORAN,
]);
precompiles
};

Box::new(precompiles)
})
Expand All @@ -209,11 +223,17 @@ impl Precompiles {
pub fn planck() -> &'static Self {
static INSTANCE: OnceBox<Precompiles> = OnceBox::new();
INSTANCE.get_or_init(|| {
let mut precompiles = Self::istanbul().clone();
precompiles.extend([
tendermint::TENDERMINT_HEADER_VALIDATION,
iavl::IAVL_PROOF_VALIDATION_PLANCK,
]);
let precompiles = Self::istanbul().clone();

#[cfg(feature = "bsc")]
let precompiles = {
let mut precompiles = precompiles;
precompiles.extend([
tendermint::TENDERMINT_HEADER_VALIDATION,
iavl::IAVL_PROOF_VALIDATION_PLANCK,
]);
precompiles
};

Box::new(precompiles)
})
Expand All @@ -237,8 +257,13 @@ impl Precompiles {
pub fn plato() -> &'static Self {
static INSTANCE: OnceBox<Precompiles> = OnceBox::new();
INSTANCE.get_or_init(|| {
let mut precompiles = Self::luban().clone();
precompiles.extend([iavl::IAVL_PROOF_VALIDATION_PLATO]);
let precompiles = Self::luban().clone();
#[cfg(feature = "bsc")]
let precompiles = {
let mut precompiles = precompiles;
precompiles.extend([iavl::IAVL_PROOF_VALIDATION_PLATO]);
precompiles
};

Box::new(precompiles)
})
Expand All @@ -263,7 +288,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)
Expand Down
4 changes: 2 additions & 2 deletions crates/precompile/src/tendermint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,13 @@ use tendermint::lite::light_client;
/// Tendermint precompile for BSC.
pub(crate) const TENDERMINT_HEADER_VALIDATION: PrecompileWithAddress = PrecompileWithAddress(
crate::u64_to_address(100),
Precompile::Standard(crate::tendermint::tendermint_header_validation_run),
Precompile::Standard(tendermint_header_validation_run),
);

/// 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),
Precompile::Standard(tendermint_header_validation_run_nano),
);

/// Run the Tendermint header validation precompile after Nano hardfork.
Expand Down
3 changes: 3 additions & 0 deletions crates/precompile/src/tm_secp256k1.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
#![allow(dead_code)]
#![cfg_attr(feature = "bsc", warn(dead_code))]

use crate::{Bytes, Error, Precompile, PrecompileError, PrecompileResult, PrecompileWithAddress};
use revm_primitives::PrecompileOutput;
use secp256k1::{ecdsa, Message, PublicKey};
Expand Down
Loading