Skip to content

Commit

Permalink
Merge branch 'develop' into develop_eip1559
Browse files Browse the repository at this point in the history
  • Loading branch information
HarryLiIsMe authored Dec 17, 2022
2 parents aca8d38 + 9c727a6 commit 69ad210
Show file tree
Hide file tree
Showing 8 changed files with 37 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@ use evm_precompile_utils::{EvmDataReader, EvmDataWriter, EvmResult, Gasometer};
use module_evm::precompile::{FinState, Precompile, PrecompileId, PrecompileResult};
use tracing::debug;

const GAS_EXECUTION: u64 = 3437;

/// The Verifier precompile.
pub struct EthPairing;

Expand Down Expand Up @@ -50,20 +48,35 @@ impl Precompile for EthPairing {
}

impl EthPairing {
fn call_public_api_on_vector(data: &[u8]) -> Result<Vec<u8>, ApiError> {
fn get_operation(data: &[u8]) -> Result<(OperationType, &[u8]), ApiError> {
if data.is_empty() {
return Err(ApiError::InputError("input is zero length".to_owned()));
}
let op = OperationType::from_u8(data[0]).ok_or(ApiError::MissingValue)?;
perform_operation(op, &data[0..])
Ok((op, &data[1..]))
}

fn call_public_api_on_vector(data: &[u8]) -> Result<Vec<u8>, ApiError> {
let (op, input) = Self::get_operation(data)?;
perform_operation(op, input)
}

fn calculate_gas_for_operation(data: &[u8]) -> Result<u64, ApiError> {
let (op, input) = Self::get_operation(data)?;
eth_pairings::gas_meter::meter_operation(op, input)
}

fn execute_operation(
mut input: EvmDataReader,
target_gas: Option<u64>,
) -> EvmResult<PrecompileOutput> {
// Calculate and record required gas for operation
let mut gasometer = Gasometer::new(target_gas);
gasometer.record_cost(GAS_EXECUTION)?;
let gas = match Self::calculate_gas_for_operation(input.get_slice()) {
Ok(res) => res,
Err(api_err) => return Err(ExitError::Other(api_err.to_string().into())),
};
gasometer.record_cost(gas)?;

debug!(target: "evm", "EthPairing#executingOp");
let result = match Self::call_public_api_on_vector(input.get_slice()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@ use fp_mocks::*;

// Test from eth-pairings (eip1962) repository https://github.com/FindoraNetwork/eip1962
#[test]
fn test_g1_mul() {
fn test_bls12_pairing() {
use hex;
let hex_string = "02820d8080001026e1318f230000000000000080be6dc885e544bee65620747e191023316695af6989f85b230000000000044eccc6886286fbaee7561d483d50d89e9e9e95af2989f85b230000000000044eccc688628631";
let hex_string = "07202912811758d871b77a9c3635c28570dc020576f9fc2719d8d0494439162b2b89000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000011603e65409b693c8a08aeb3478d10aa3732a6672ba06d12912811758d871b77a9c3635c28570dc020576f9fc2719d8d0494439162b2b84000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000010206059efde42f3701020127ccb2831f0011c1d547c491b9dffbd4cfdb87598a4b370f5873ea62094a2f201faa6cb7f6fcca66de3f308a25776dac3f61edb792948fbe53e4d18a3d8aefbe011a7e9f75f3fdc77b83a97f7acd58326a0545f8aa37b69bfb32c52dc195763e8c17176e0ad4ee94d9c720e922d42688127c4b812cd7c2f8cf6126acd4c3d7568121e48b3fefe66c279f2ec71f0d6f8156a3343d1cfa54b808d747cd02419278290ad2d7d03f5de1e7b3c97732f53dbe1dfd42e51f9571f7fee3d9c1785d5a1ed6010b4f7f211a0a5f4425728e2df580196d3e3b85ef148ed769acd23e9be6e8440726cb40655787f48eaf46154cb740e2a58db5b96fa02d83fb9d0f94320da1471e0104ece4c46ac4f05a7c28ecda84292f15999747bb77c530c65448f1f837a47dd70e972c4065d0b39d40b5d550a55901516afa7f02b395963d1535fcba1705e31a117cb4beab1dc582198c4ab0c02e96a22f7bd10dde3bbbdbc9182a9596cb0ed32121616b692e8036437efb4c3816f018f11e643c6e0a049da431986a3a722b06";
let data = hex::decode(hex_string).unwrap();

assert!(EthPairing::execute(
let output = EthPairing::execute(
&EvmDataWriter::new()
.write_selector(Call::ExecuteOperation)
.write_raw_bytes(&data)
Expand All @@ -21,6 +21,9 @@ fn test_g1_mul() {
apparent_value: From::from(0),
},
&BASE_APP.lock().unwrap().deliver_state,
)
.is_ok());
);

assert!(output.is_ok());
assert_eq!(output.as_ref().unwrap().cost, 164986);
assert_eq!(output.unwrap().output, vec![0x1]);
}
11 changes: 6 additions & 5 deletions src/components/contracts/primitives/types/src/crypto.rs
Original file line number Diff line number Diff line change
Expand Up @@ -450,11 +450,12 @@ impl Verify for XfrSignature {
pub fn secp256k1_ecdsa_recover(sig: &[u8; 65], msg: &[u8; 32]) -> ruc::Result<[u8; 64]> {
let rs = libsecp256k1::Signature::parse_standard_slice(&sig[0..64])
.map_err(|_| eg!("Ecdsa signature verify error: bad RS"))?;
let v =
libsecp256k1::RecoveryId::parse(
if sig[64] > 26 { sig[64] - 27 } else { sig[64] },
)
.map_err(|_| eg!("Ecdsa signature verify error: bad V"))?;
let v = libsecp256k1::RecoveryId::parse(if sig[64] > 26 {
sig[64] - 27
} else {
sig[64]
})
.map_err(|_| eg!("Ecdsa signature verify error: bad V"))?;
let pubkey = libsecp256k1::recover(&libsecp256k1::Message::parse(msg), &rs, &v)
.map_err(|_| eg!("Ecdsa signature verify error: bad signature"))?;
let mut res = [0u8; 64];
Expand Down
2 changes: 1 addition & 1 deletion src/components/contracts/rpc/src/eth.rs
Original file line number Diff line number Diff line change
Expand Up @@ -681,7 +681,7 @@ impl EthApi for EthApiImpl {

let task = spawn_blocking(move || -> Result<Option<RichBlock>> {
if let Some(h) = height {
if 0 < h && h < *EVM_FIRST_BLOCK_HEIGHT {
if 0 < h && h < CFG.checkpoint.evm_first_block_height as u64 {
return Ok(Some(dummy_block(h, full)));
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/components/finutils/src/txn_builder/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1871,7 +1871,7 @@ impl AnonTransferOperationBuilder {
};
}

let remainder = sum_input - sum_output - (fees);
let remainder = sum_input - sum_output - fees;
if remainder > 0 {
let oabar_money_back = OpenAnonAssetRecordBuilder::new()
.amount(remainder)
Expand Down
4 changes: 1 addition & 3 deletions src/ledger/src/store/api_cache.rs
Original file line number Diff line number Diff line change
Expand Up @@ -462,9 +462,7 @@ pub fn check_lost_data(ledger: &mut LedgerState) -> Result<()> {
}

// update the last txo sid
api_cache
.last_sid
.insert("last_txo_sid".to_string(), index);
api_cache.last_sid.insert("last_txo_sid".to_string(), index);
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/ledger/src/store/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1474,7 +1474,7 @@ impl LedgerStatus {
);
if seq_id > self.block_commit_count {
return Err(eg!(("Transaction seq_id ahead of block_count")));
} else if seq_id + (TRANSACTION_WINDOW_WIDTH) < self.block_commit_count {
} else if seq_id + TRANSACTION_WINDOW_WIDTH < self.block_commit_count {
return Err(eg!(("Transaction seq_id too far behind block_count")));
} else {
// Check to see that this nrpt has not been seen before
Expand Down
2 changes: 1 addition & 1 deletion src/libs/bitmap/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -566,7 +566,7 @@ fn count_byte(mask: usize) -> u8 {
let mut result = 0;

for i in 0..8 {
result += u8::from(mask & (1 << i) != 0);
result += (mask & (1 << i) != 0) as u8;
}

result
Expand Down

0 comments on commit 69ad210

Please sign in to comment.