Skip to content

Commit

Permalink
chore: resolve merge conflicts
Browse files Browse the repository at this point in the history
  • Loading branch information
pythonberg1997 committed Jun 19, 2024
1 parent cd1ae53 commit 88cc933
Show file tree
Hide file tree
Showing 11 changed files with 213 additions and 188 deletions.
177 changes: 95 additions & 82 deletions Cargo.lock

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ members = [
"crates/revm",
"crates/primitives",
"crates/interpreter",
"crates/precompile",
"crates/precompile",
]
resolver = "2"
default-members = ["crates/revm"]
Expand Down
11 changes: 6 additions & 5 deletions crates/precompile/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,12 @@ rust_2018_idioms = "deny"
all = "warn"

[dependencies]
alloy-rlp = { version = "0.3.4", default-features = false, features = [
"arrayvec",
"derive",
] }
alloy-primitives = {version = "0.7.2"}

revm-primitives = { path = "../primitives", version = "4.0.0", default-features = false }
once_cell = { version = "1.19", default-features = false, features = ["alloc"] }

Expand All @@ -40,11 +46,6 @@ prost = { version = "0.12.3" }
bls_on_arkworks = "0.3.0"
tendermint = { git = "https://github.com/bnb-chain/tendermint-rs-parlia", tag = "v0.1.0-beta.1", features = ["secp256k1"] }
parity-bytes = { version = "0.1.2", default-features = false }
alloy-rlp = { version = "0.3", default-features = false, features = [
"arrayvec",
"derive",
] }
alloy-primitives = {version = "0.7.0"}

# SHA2-256 and RIPEMD-160
sha2 = { version = "0.10", default-features = false }
Expand Down
42 changes: 22 additions & 20 deletions crates/precompile/src/bls.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::{Error, Precompile, PrecompileResult, PrecompileWithAddress};
use bls_on_arkworks as bls;
use revm_primitives::Bytes;
use revm_primitives::{Bytes, PrecompileOutput};
use std::vec::Vec;

pub(crate) const BLS_SIGNATURE_VALIDATION: PrecompileWithAddress = PrecompileWithAddress(
Expand All @@ -21,15 +21,15 @@ const BLS_DST: &[u8] = bls::DST_ETHEREUM.as_bytes();
fn bls_signature_validation_run(input: &Bytes, gas_limit: u64) -> PrecompileResult {
let cost = calc_gas_cost(input);
if cost > gas_limit {
return Err(Error::OutOfGas);
return Err(Error::OutOfGas.into());
}

let msg_and_sig_length = BLS_MSG_HASH_LENGTH + BLS_SIGNATURE_LENGTH;
let input_length = input.len() as u64;
if (input_length <= msg_and_sig_length)
|| ((input_length - msg_and_sig_length) % BLS_SINGLE_PUBKEY_LENGTH != 0)
{
return Err(Error::Reverted(cost));
return Err(Error::Reverted(cost).into());
}

let msg_hash: &Vec<u8> = &input[..BLS_MSG_HASH_LENGTH as usize].to_vec();
Expand All @@ -38,7 +38,7 @@ fn bls_signature_validation_run(input: &Bytes, gas_limit: u64) -> PrecompileResu

// check signature format
if bls::signature_to_point(&signature.to_vec()).is_err() {
return Err(Error::Reverted(cost));
return Err(Error::Reverted(cost).into());
}

let pub_key_count = (input_length - msg_and_sig_length) / BLS_SINGLE_PUBKEY_LENGTH;
Expand All @@ -50,13 +50,13 @@ fn bls_signature_validation_run(input: &Bytes, gas_limit: u64) -> PrecompileResu
let pub_key = &pub_keys_data[i as usize * BLS_SINGLE_PUBKEY_LENGTH as usize
..(i + 1) as usize * BLS_SINGLE_PUBKEY_LENGTH as usize];
if !bls::key_validate(&pub_key.to_vec()) {
return Err(Error::Reverted(cost));
return Err(Error::Reverted(cost).into());
}
pub_keys.push(pub_key.to_vec());
msg_hashes.push(msg_hash.clone().to_vec());
}
if pub_keys.is_empty() {
return Err(Error::Reverted(cost));
return Err(Error::Reverted(cost).into());
}

// verify signature
Expand All @@ -67,7 +67,7 @@ fn bls_signature_validation_run(input: &Bytes, gas_limit: u64) -> PrecompileResu
output = Bytes::from(vec![0]);
}

Ok((cost, output))
Ok(PrecompileOutput::new(cost, output))
}

fn calc_gas_cost(input: &Bytes) -> u64 {
Expand Down Expand Up @@ -104,7 +104,7 @@ mod tests {

let excepted_output = Bytes::from(vec![1]);
let result = match bls_signature_validation_run(&Bytes::from(input.clone()), 100_000_000) {
Ok((_, output)) => output,
Ok(o) => o.bytes,
Err(e) => panic!("BLS signature validation failed, {:?}", e),
};
assert_eq!(result, excepted_output);
Expand All @@ -120,7 +120,7 @@ mod tests {

let excepted_output = Bytes::from(vec![0]);
let result = match bls_signature_validation_run(&Bytes::from(input.clone()), 100_000_000) {
Ok((_, output)) => output,
Ok(o) => o.bytes,
Err(e) => panic!("BLS signature validation failed, {:?}", e),
};
assert_eq!(result, excepted_output);
Expand All @@ -136,7 +136,7 @@ mod tests {

match bls_signature_validation_run(&Bytes::from(input.clone()), 100_000_000) {
Ok(_) => panic!("BLS signature validation failed, expect error"),
Err(e) => assert_eq!(e, Error::Reverted(4500)),
Err(e) => assert_eq!(e, Error::Reverted(4500).into()),
}

// wrong pubkey
Expand All @@ -150,7 +150,7 @@ mod tests {

match bls_signature_validation_run(&Bytes::from(input.clone()), 100_000_000) {
Ok(_) => panic!("BLS signature validation failed, expect error"),
Err(e) => assert_eq!(e, Error::Reverted(4500)),
Err(e) => assert_eq!(e, Error::Reverted(4500).into()),
}
}

Expand All @@ -170,7 +170,7 @@ mod tests {

let excepted_output = Bytes::from(vec![1]);
let result = match bls_signature_validation_run(&Bytes::from(input.clone()), 100_000_000) {
Ok((_, output)) => output,
Ok(o) => o.bytes,
Err(e) => panic!("BLS signature validation failed, {:?}", e),
};
assert_eq!(result, excepted_output);
Expand All @@ -188,10 +188,11 @@ mod tests {
input.extend_from_slice(&pub_key2);
input.extend_from_slice(&pub_key3);
let excepted_output = Bytes::from(vec![0]);
match bls_signature_validation_run(&Bytes::from(input.clone()), 100_000_000) {
Ok((_, output)) => assert_eq!(output, excepted_output),
let result = match bls_signature_validation_run(&Bytes::from(input.clone()), 100_000_000) {
Ok(o) => o.bytes,
Err(e) => panic!("BLS signature validation failed, {:?}", e),
}
};
assert_eq!(result, excepted_output);

// wrong signature
let msg_hash = hex!("1377c7e66081cb65e473c1b95db5195a27d04a7108b468890224bedbe1a8a6eb");
Expand All @@ -208,7 +209,7 @@ mod tests {

match bls_signature_validation_run(&Bytes::from(input.clone()), 100_000_000) {
Ok(_) => panic!("BLS signature validation failed, expect error"),
Err(e) => assert_eq!(e, Error::Reverted(11500)),
Err(e) => assert_eq!(e, Error::Reverted(11500).into()),
}

// invalid pubkey
Expand All @@ -226,7 +227,7 @@ mod tests {

match bls_signature_validation_run(&Bytes::from(input.clone()), 100_000_000) {
Ok(_) => panic!("BLS signature validation failed, expect error"),
Err(e) => assert_eq!(e, Error::Reverted(11500)),
Err(e) => assert_eq!(e, Error::Reverted(11500).into()),
}

// duplicate pubkey
Expand All @@ -242,9 +243,10 @@ mod tests {
input.extend_from_slice(&pub_key2);
input.extend_from_slice(&pub_key3);
let excepted_output = Bytes::from(vec![0]);
match bls_signature_validation_run(&Bytes::from(input.clone()), 100_000_000) {
Ok((_, output)) => assert_eq!(output, excepted_output),
let result = match bls_signature_validation_run(&Bytes::from(input.clone()), 100_000_000) {
Ok(o) => o.bytes,
Err(e) => panic!("BLS signature validation failed, {:?}", e),
}
};
assert_eq!(result, excepted_output);
}
}
32 changes: 16 additions & 16 deletions crates/precompile/src/cometbft.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use cometbft_light_client_verifier::{
};
use cometbft_proto::types::v1::LightBlock as TmLightBlock;
use prost::Message;
use revm_primitives::Bytes;
use revm_primitives::{Bytes, PrecompileOutput};
use std::{borrow::ToOwned, string::String, vec::Vec};

pub(crate) const COMETBFT_LIGHT_BLOCK_VALIDATION: PrecompileWithAddress = PrecompileWithAddress(
Expand Down Expand Up @@ -66,33 +66,33 @@ fn cometbft_light_block_validation_run_inner(
const COMETBFT_LIGHT_BLOCK_VALIDATION_BASE: u64 = 3_000;

if COMETBFT_LIGHT_BLOCK_VALIDATION_BASE > gas_limit {
return Err(Error::OutOfGas);
return Err(Error::OutOfGas.into());
}

let (mut consensus_state, tm_light_block) = match decode_light_block_validation_input(input) {
Ok(result) => result,
Err(e) => return Err(e),
Err(e) => return Err(e.into()),
};

let light_block = match convert_light_block_from_proto(&tm_light_block) {
Ok(lb) => lb,
Err(e) => return Err(e),
Err(e) => return Err(e.into()),
};

let mut validator_set_changed = match consensus_state.apply_light_block(&light_block) {
Ok(validator_set_changed) => validator_set_changed,
Err(e) => return Err(e),
Err(e) => return Err(e.into()),
};
if !is_hertz {
validator_set_changed = false;
}

let consensus_state_bytes = match consensus_state.encode() {
Ok(cs) => cs,
Err(e) => return Err(e),
Err(e) => return Err(e.into()),
};

Ok((
Ok(PrecompileOutput::new(
COMETBFT_LIGHT_BLOCK_VALIDATION_BASE,
encode_light_block_validation_result(validator_set_changed, consensus_state_bytes),
))
Expand Down Expand Up @@ -429,12 +429,12 @@ mod tests {
));

let result = cometbft_light_block_validation_run(&input, 100_000);
let (gas_used, output) = match result {
Ok(result) => result,
let PrecompileOutput { gas_used, bytes } = match result {
Ok(output) => output,
Err(_) => panic!("cometbft_light_block_validation_run failed"),
};
assert_eq!(gas_used, 3_000);
assert_eq!(output, except_output);
assert_eq!(bytes, except_output);
}
// apply light block failed
{
Expand All @@ -443,7 +443,7 @@ mod tests {
));

let result = cometbft_light_block_validation_run(&input, 100_000);
let expected = Err(Error::CometBftApplyBlockFailed);
let expected = Err(Error::CometBftApplyBlockFailed.into());
assert_eq!(result, expected);
}
// consensus height >= light block height
Expand All @@ -453,7 +453,7 @@ mod tests {
));

let result = cometbft_light_block_validation_run(&input, 100_000);
let expected = Err(Error::CometBftInvalidInput);
let expected = Err(Error::CometBftInvalidInput.into());
assert_eq!(result, expected);
}
// chain id mismatch
Expand All @@ -463,7 +463,7 @@ mod tests {
));

let result = cometbft_light_block_validation_run(&input, 100_000);
let expected = Err(Error::CometBftInvalidInput);
let expected = Err(Error::CometBftInvalidInput.into());
assert_eq!(result, expected);
}
}
Expand Down Expand Up @@ -732,11 +732,11 @@ mod tests {
));

let result = cometbft_light_block_validation_run_before_hertz(&input, 100_000);
let (gas_used, output) = match result {
Ok(result) => result,
let PrecompileOutput { gas_used, bytes } = match result {
Ok(output) => output,
Err(_) => panic!("cometbft_light_block_validation_run failed"),
};
assert_eq!(gas_used, 3_000);
assert_eq!(output, except_output_after_hertz);
assert_eq!(bytes, except_output_after_hertz);
}
}
Loading

0 comments on commit 88cc933

Please sign in to comment.