v2.6.5 beta (#11240)

* [CI] check evmbin build (#11096)
* Correct EIP-712 encoding (#11092)
* [client]: Fix for incorrectly dropped consensus messages (#11082) (#11086)
* Update hardcoded headers (foundation, classic, kovan, xdai, ewc, ...) (#11053)
* Add cargo-remote dir to .gitignore (?)
* Update light client headers: ropsten 6631425 foundation 8798209 (#11201)
* Update list of bootnodes for xDai chain (#11236)
* ethcore/res: add mordor testnet configuration (#11200)
* [chain specs]: activate Istanbul on mainnet (#11228)
* [builtin]: support multiple prices and activations in chain spec (#11039)
* [receipt]: add sender & receiver to RichReceipts (#11179)
* [ethcore/builtin]: do not panic in blake2pricer on short input (#11180)
* Made ecrecover implementation trait public (#11188)
* Fix docker centos build (#11226)
* Update MIX bootnodes. (#11203)
* Insert explicit warning into the panic hook (#11225)
* Use provided usd-per-eth value if an endpoint is specified (#11209)
* Cleanup stratum a bit (#11161)
* Add Constantinople EIPs to the dev (instant_seal) config (#10809) (already backported)
* util Host: fix a double Read Lock bug in fn Host::session_readable() (#11175)
* ethcore client: fix a double Read Lock bug in fn Client::logs() (#11172)
* Type annotation for next_key() matching of json filter options (#11192)
* Upgrade jsonrpc to latest (#11206)
* [dependencies]: jsonrpc 14.0.1 (#11183)
* Upgrade to jsonrpc v14 (#11151)
* Switching sccache from local to Redis (#10971)
* Snapshot restoration overhaul (#11219)
* Add new line after writing block to hex file. (#10984)
* Pause pruning while snapshotting (#11178)
* Change how RPCs eth_call and eth_estimateGas handle "Pending" (#11127)
* Fix block detail updating (#11015)
* Make InstantSeal Instant again #11186
* Filter out some bad ropsten warp snapshots (#11247)
This commit is contained in:
Talha Cross
2019-11-11 23:55:19 +01:00
committed by s3krit
parent 9838c9f447
commit bc90ba2863
193 changed files with 24263 additions and 3349 deletions

View File

@@ -16,13 +16,17 @@
//! Standard built-in contracts.
#![warn(missing_docs)]
use std::{
cmp::{max, min},
collections::BTreeMap,
convert::{TryFrom, TryInto},
io::{self, Read, Cursor},
mem::size_of,
str::FromStr
};
use bn;
use byteorder::{BigEndian, LittleEndian, ReadBytesExt};
use ethereum_types::{H256, U256};
use ethjson;
@@ -34,17 +38,18 @@ use parity_bytes::BytesRef;
use parity_crypto::digest;
use eip_152::compress;
type EthcoreError = String;
/// Native implementation of a built-in contract.
trait Implementation: Send + Sync {
pub trait Implementation: Send + Sync {
/// execute this built-in on the given input, writing to the given output.
fn execute(&self, input: &[u8], output: &mut BytesRef) -> Result<(), &'static str>;
}
/// A gas pricing scheme for built-in contracts.
// TODO: refactor this trait, see https://github.com/paritytech/parity-ethereum/issues/11014
trait Pricer: Send + Sync {
/// The gas cost of running this built-in for the given input data at block number `at`
fn cost(&self, input: &[u8], at: u64) -> U256;
fn cost(&self, input: &[u8]) -> U256;
}
/// Pricing for the Blake2 compression function (aka "F").
@@ -53,78 +58,94 @@ trait Pricer: Send + Sync {
pub type Blake2FPricer = u64;
impl Pricer for Blake2FPricer {
fn cost(&self, input: &[u8], _at: u64) -> U256 {
use std::convert::TryInto;
let (rounds_bytes, _) = input.split_at(std::mem::size_of::<u32>());
fn cost(&self, input: &[u8]) -> U256 {
const FOUR: usize = std::mem::size_of::<u32>();
// Returning zero if the conversion fails is fine because `execute()` will check the length
// and bail with the appropriate error.
let rounds = u32::from_be_bytes(rounds_bytes.try_into().unwrap_or([0u8; 4]));
if input.len() < FOUR {
return U256::zero();
}
let (rounds_bytes, _) = input.split_at(FOUR);
let rounds = u32::from_be_bytes(rounds_bytes.try_into().unwrap_or([0u8; FOUR]));
U256::from(*self as u128 * rounds as u128)
}
}
/// A linear pricing model. This computes a price using a base cost and a cost per-word.
struct Linear {
base: usize,
word: usize,
/// Pricing model
#[derive(Debug)]
enum Pricing {
AltBn128Pairing(AltBn128PairingPricer),
AltBn128ConstOperations(AltBn128ConstOperations),
Blake2F(Blake2FPricer),
Linear(Linear),
Modexp(ModexpPricer),
}
/// A special pricing model for modular exponentiation.
struct ModexpPricer {
divisor: usize,
}
impl Pricer for Linear {
fn cost(&self, input: &[u8], _at: u64) -> U256 {
U256::from(self.base) + U256::from(self.word) * U256::from((input.len() + 31) / 32)
impl Pricer for Pricing {
fn cost(&self, input: &[u8]) -> U256 {
match self {
Pricing::AltBn128Pairing(inner) => inner.cost(input),
Pricing::AltBn128ConstOperations(inner) => inner.cost(input),
Pricing::Blake2F(inner) => inner.cost(input),
Pricing::Linear(inner) => inner.cost(input),
Pricing::Modexp(inner) => inner.cost(input),
}
}
}
/// alt_bn128 constant operations (add and mul) pricing model.
struct AltBn128ConstOperations {
price: usize,
eip1108_transition_at: u64,
eip1108_transition_price: usize,
/// A linear pricing model. This computes a price using a base cost and a cost per-word.
#[derive(Debug)]
struct Linear {
base: u64,
word: u64,
}
impl Pricer for AltBn128ConstOperations {
fn cost(&self, _input: &[u8], at: u64) -> U256 {
if at >= self.eip1108_transition_at {
self.eip1108_transition_price.into()
} else {
self.price.into()
}
/// A special pricing model for modular exponentiation.
#[derive(Debug)]
struct ModexpPricer {
divisor: u64,
}
impl Pricer for Linear {
fn cost(&self, input: &[u8]) -> U256 {
U256::from(self.base) + U256::from(self.word) * U256::from((input.len() + 31) / 32)
}
}
/// alt_bn128 pairing price
#[derive(Debug, Copy, Clone)]
struct AltBn128PairingPrice {
base: usize,
pair: usize,
base: u64,
pair: u64,
}
/// alt_bn128_pairing pricing model. This computes a price using a base cost and a cost per pair.
#[derive(Debug)]
struct AltBn128PairingPricer {
price: AltBn128PairingPrice,
eip1108_transition_at: u64,
eip1108_transition_price: AltBn128PairingPrice,
}
/// Pricing for constant alt_bn128 operations (ECADD and ECMUL)
#[derive(Debug, Copy, Clone)]
pub struct AltBn128ConstOperations {
/// Fixed price.
pub price: u64,
}
impl Pricer for AltBn128ConstOperations {
fn cost(&self, _input: &[u8]) -> U256 {
self.price.into()
}
}
impl Pricer for AltBn128PairingPricer {
fn cost(&self, input: &[u8], at: u64) -> U256 {
let price = if at >= self.eip1108_transition_at {
self.eip1108_transition_price
} else {
self.price
};
U256::from(price.base) + U256::from(price.pair) * U256::from(input.len() / 192)
fn cost(&self, input: &[u8]) -> U256 {
U256::from(self.price.base) + U256::from(self.price.pair) * U256::from(input.len() / 192)
}
}
impl Pricer for ModexpPricer {
fn cost(&self, input: &[u8], _at: u64) -> U256 {
fn cost(&self, input: &[u8]) -> U256 {
let mut reader = input.chain(io::repeat(0));
let mut buf = [0; 32];
@@ -149,8 +170,10 @@ impl Pricer for ModexpPricer {
let m = max(mod_len, base_len);
// read fist 32-byte word of the exponent.
let exp_low = if base_len + 96 >= input.len() as u64 { U256::zero() } else {
let mut buf = [0; 32];
let exp_low = if base_len + 96 >= input.len() as u64 {
U256::zero()
} else {
buf.iter_mut().for_each(|b| *b = 0);
let mut reader = input[(96 + base_len as usize)..].chain(io::repeat(0));
let len = min(exp_len, 32) as usize;
reader.read_exact(&mut buf[(32 - len)..]).expect("reading from zero-extended memory cannot fail; qed");
@@ -181,7 +204,7 @@ impl ModexpPricer {
match x {
x if x <= 64 => x * x,
x if x <= 1024 => (x * x) / 4 + 96 * x - 3072,
x => (x * x) / 16 + 480 * x - 199680,
x => (x * x) / 16 + 480 * x - 199_680,
}
}
}
@@ -190,137 +213,179 @@ impl ModexpPricer {
///
/// Call `cost` to compute cost for the given input, `execute` to execute the contract
/// on the given input, and `is_active` to determine whether the contract is active.
///
/// Unless `is_active` is true,
pub struct Builtin {
pricer: Box<dyn Pricer>,
native: Box<dyn Implementation>,
activate_at: u64,
pricer: BTreeMap<u64, Pricing>,
native: EthereumBuiltin,
}
impl Builtin {
/// Simple forwarder for cost.
///
/// Return the cost of the most recently activated pricer at the current block number.
///
/// If no pricer is actived `zero` is returned
///
/// If multiple `activation_at` has the same block number the last one is used
/// (follows `BTreeMap` semantics).
#[inline]
pub fn cost(&self, input: &[u8], at: u64) -> U256 {
self.pricer.cost(input, at)
if let Some((_, pricer)) = self.pricer.range(0..=at).last() {
pricer.cost(input)
} else {
U256::zero()
}
}
/// Simple forwarder for execute.
#[inline]
pub fn execute(&self, input: &[u8], output: &mut BytesRef) -> Result<(), &'static str> {
self.native.execute(input, output)
}
/// Whether the builtin is activated at the given block number.
#[inline]
pub fn is_active(&self, at: u64) -> bool {
at >= self.activate_at
self.pricer.range(0..=at).last().is_some()
}
}
impl From<ethjson::spec::Builtin> for Builtin {
fn from(b: ethjson::spec::Builtin) -> Self {
let pricer: Box<dyn Pricer> = match b.pricing {
ethjson::spec::Pricing::Blake2F { gas_per_round } => {
Box::new(gas_per_round)
},
ethjson::spec::Pricing::Linear(linear) => {
Box::new(Linear {
impl TryFrom<ethjson::spec::builtin::Builtin> for Builtin {
type Error = EthcoreError;
fn try_from(b: ethjson::spec::builtin::Builtin) -> Result<Self, Self::Error> {
let native = EthereumBuiltin::from_str(&b.name)?;
let mut pricer = BTreeMap::new();
for (activate_at, p) in b.pricing {
pricer.insert(activate_at, p.price.into());
}
Ok(Self { pricer, native })
}
}
impl From<ethjson::spec::builtin::Pricing> for Pricing {
fn from(pricing: ethjson::spec::builtin::Pricing) -> Self {
match pricing {
ethjson::spec::builtin::Pricing::Blake2F { gas_per_round } => {
Pricing::Blake2F(gas_per_round)
}
ethjson::spec::builtin::Pricing::Linear(linear) => {
Pricing::Linear(Linear {
base: linear.base,
word: linear.word,
})
}
ethjson::spec::Pricing::Modexp(exp) => {
Box::new(ModexpPricer {
ethjson::spec::builtin::Pricing::Modexp(exp) => {
Pricing::Modexp(ModexpPricer {
divisor: if exp.divisor == 0 {
warn!(target: "builtin", "Zero modexp divisor specified. Falling back to default.");
warn!(target: "builtin", "Zero modexp divisor specified. Falling back to default: 10.");
10
} else {
exp.divisor
}
})
}
ethjson::spec::Pricing::AltBn128Pairing(pricer) => {
Box::new(AltBn128PairingPricer {
ethjson::spec::builtin::Pricing::AltBn128Pairing(pricer) => {
Pricing::AltBn128Pairing(AltBn128PairingPricer {
price: AltBn128PairingPrice {
base: pricer.base,
pair: pricer.pair,
},
eip1108_transition_at: b.eip1108_transition.map_or(u64::max_value(), Into::into),
eip1108_transition_price: AltBn128PairingPrice {
base: pricer.eip1108_transition_base,
pair: pricer.eip1108_transition_pair,
},
})
}
ethjson::spec::Pricing::AltBn128ConstOperations(pricer) => {
Box::new(AltBn128ConstOperations {
price: pricer.price,
eip1108_transition_price: pricer.eip1108_transition_price,
eip1108_transition_at: b.eip1108_transition.map_or(u64::max_value(), Into::into)
ethjson::spec::builtin::Pricing::AltBn128ConstOperations(pricer) => {
Pricing::AltBn128ConstOperations(AltBn128ConstOperations {
price: pricer.price
})
}
};
Builtin {
pricer,
native: ethereum_builtin(&b.name),
activate_at: b.activate_at.map_or(0, Into::into),
}
}
}
/// Ethereum built-in factory.
fn ethereum_builtin(name: &str) -> Box<dyn Implementation> {
match name {
"identity" => Box::new(Identity) as Box<dyn Implementation>,
"ecrecover" => Box::new(EcRecover) as Box<dyn Implementation>,
"sha256" => Box::new(Sha256) as Box<dyn Implementation>,
"ripemd160" => Box::new(Ripemd160) as Box<dyn Implementation>,
"modexp" => Box::new(Modexp) as Box<dyn Implementation>,
"alt_bn128_add" => Box::new(Bn128Add) as Box<dyn Implementation>,
"alt_bn128_mul" => Box::new(Bn128Mul) as Box<dyn Implementation>,
"alt_bn128_pairing" => Box::new(Bn128Pairing) as Box<dyn Implementation>,
"blake2_f" => Box::new(Blake2F) as Box<dyn Implementation>,
_ => panic!("invalid builtin name: {}", name),
/// Ethereum builtins:
enum EthereumBuiltin {
/// The identity function
Identity(Identity),
/// ec recovery
EcRecover(EcRecover),
/// sha256
Sha256(Sha256),
/// ripemd160
Ripemd160(Ripemd160),
/// modexp (EIP 198)
Modexp(Modexp),
/// alt_bn128_add
Bn128Add(Bn128Add),
/// alt_bn128_mul
Bn128Mul(Bn128Mul),
/// alt_bn128_pairing
Bn128Pairing(Bn128Pairing),
/// blake2_f (The Blake2 compression function F, EIP-152)
Blake2F(Blake2F)
}
impl FromStr for EthereumBuiltin {
type Err = EthcoreError;
fn from_str(name: &str) -> Result<EthereumBuiltin, Self::Err> {
match name {
"identity" => Ok(EthereumBuiltin::Identity(Identity)),
"ecrecover" => Ok(EthereumBuiltin::EcRecover(EcRecover)),
"sha256" => Ok(EthereumBuiltin::Sha256(Sha256)),
"ripemd160" => Ok(EthereumBuiltin::Ripemd160(Ripemd160)),
"modexp" => Ok(EthereumBuiltin::Modexp(Modexp)),
"alt_bn128_add" => Ok(EthereumBuiltin::Bn128Add(Bn128Add)),
"alt_bn128_mul" => Ok(EthereumBuiltin::Bn128Mul(Bn128Mul)),
"alt_bn128_pairing" => Ok(EthereumBuiltin::Bn128Pairing(Bn128Pairing)),
"blake2_f" => Ok(EthereumBuiltin::Blake2F(Blake2F)),
_ => return Err(format!("invalid builtin name: {}", name)),
}
}
}
// Ethereum builtins:
//
// - The identity function
// - ec recovery
// - sha256
// - ripemd160
// - modexp (EIP198)
// - alt_bn128_add
// - alt_bn128_mul
// - alt_bn128_pairing
// - blake2_f (The Blake2 compression function F, EIP-152)
impl Implementation for EthereumBuiltin {
fn execute(&self, input: &[u8], output: &mut BytesRef) -> Result<(), &'static str> {
match self {
EthereumBuiltin::Identity(inner) => inner.execute(input, output),
EthereumBuiltin::EcRecover(inner) => inner.execute(input, output),
EthereumBuiltin::Sha256(inner) => inner.execute(input, output),
EthereumBuiltin::Ripemd160(inner) => inner.execute(input, output),
EthereumBuiltin::Modexp(inner) => inner.execute(input, output),
EthereumBuiltin::Bn128Add(inner) => inner.execute(input, output),
EthereumBuiltin::Bn128Mul(inner) => inner.execute(input, output),
EthereumBuiltin::Bn128Pairing(inner) => inner.execute(input, output),
EthereumBuiltin::Blake2F(inner) => inner.execute(input, output),
}
}
}
#[derive(Debug)]
struct Identity;
pub struct Identity;
#[derive(Debug)]
struct EcRecover;
pub struct EcRecover;
#[derive(Debug)]
struct Sha256;
pub struct Sha256;
#[derive(Debug)]
struct Ripemd160;
pub struct Ripemd160;
#[derive(Debug)]
struct Modexp;
pub struct Modexp;
#[derive(Debug)]
struct Bn128Add;
pub struct Bn128Add;
#[derive(Debug)]
struct Bn128Mul;
pub struct Bn128Mul;
#[derive(Debug)]
struct Bn128Pairing;
pub struct Bn128Pairing;
#[derive(Debug)]
struct Blake2F;
pub struct Blake2F;
impl Implementation for Identity {
fn execute(&self, input: &[u8], output: &mut BytesRef) -> Result<(), &'static str> {
@@ -342,7 +407,7 @@ impl Implementation for EcRecover {
let s = H256::from_slice(&input[96..128]);
let bit = match v[31] {
27 | 28 if &v.0[..31] == &[0; 31] => v[31] - 27,
27 | 28 if v.0[..31] == [0; 31] => v[31] - 27,
_ => { return Ok(()); },
};
@@ -384,13 +449,13 @@ impl Implementation for Blake2F {
// state vector, h
let mut h = [0u64; 8];
for state_word in h.iter_mut() {
for state_word in &mut h {
*state_word = cursor.read_u64::<LittleEndian>().expect(PROOF);
}
// message block vector, m
let mut m = [0u64; 16];
for msg_word in m.iter_mut() {
for msg_word in &mut m {
*msg_word = cursor.read_u64::<LittleEndian>().expect(PROOF);
}
@@ -443,7 +508,7 @@ fn modexp(mut base: BigUint, exp: Vec<u8>, modulus: BigUint) -> BigUint {
let mut exp = exp.into_iter().skip_while(|d| *d == 0).peekable();
// n^0 % m
if let None = exp.peek() {
if exp.peek().is_none() {
return BigUint::one();
}
@@ -452,7 +517,7 @@ fn modexp(mut base: BigUint, exp: Vec<u8>, modulus: BigUint) -> BigUint {
return BigUint::zero();
}
base = base % &modulus;
base %= &modulus;
// Fast path for base divisible by modulus.
if base.is_zero() { return BigUint::zero() }
@@ -621,10 +686,11 @@ impl Bn128Pairing {
fn execute_with_error(&self, input: &[u8], output: &mut BytesRef) -> Result<(), &'static str> {
use bn::{AffineG1, AffineG2, Fq, Fq2, pairing_batch, G1, G2, Gt, Group};
let elements = input.len() / 192; // (a, b_a, b_b - each 64-byte affine coordinates)
let ret_val = if input.len() == 0 {
let ret_val = if input.is_empty() {
U256::one()
} else {
// (a, b_a, b_b - each 64-byte affine coordinates)
let elements = input.len() / 192;
let mut vals = Vec::new();
for idx in 0..elements {
let a_x = Fq::from_slice(&input[idx*192..idx*192+32])
@@ -679,19 +745,26 @@ impl Bn128Pairing {
#[cfg(test)]
mod tests {
use std::convert::TryFrom;
use ethereum_types::U256;
use ethjson::uint::Uint;
use ethjson::spec::builtin::{
Builtin as JsonBuiltin, Linear as JsonLinearPricing,
PricingAt, AltBn128Pairing as JsonAltBn128PairingPricing, Pricing as JsonPricing,
};
use hex_literal::hex;
use macros::map;
use num::{BigUint, Zero, One};
use parity_bytes::BytesRef;
use hex_literal::hex;
use super::{Builtin, Linear, ethereum_builtin, Pricer, ModexpPricer, modexp as me};
use super::{
BTreeMap, Builtin, EthereumBuiltin, FromStr, Implementation, Linear,
ModexpPricer, modexp as me, Pricing
};
#[test]
fn blake2f_cost() {
let f = Builtin {
pricer: Box::new(123),
native: ethereum_builtin("blake2_f"),
activate_at: 0,
pricer: map![0 => Pricing::Blake2F(123)],
native: EthereumBuiltin::from_str("blake2_f").unwrap(),
};
// 5 rounds
let input = hex!("0000000548c9bdf267e6096a3ba7ca8485ae67bb2bf894fe72f36e3cf1361d5f3af54fa5d182e6ad7f520e511f6c3e2b8c68059b6bbd41fbabd9831f79217e1319cde05b61626300000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000300000000000000000000000000000001");
@@ -701,9 +774,21 @@ mod tests {
assert_eq!(f.cost(&input[..], 0), U256::from(123*5));
}
#[test]
fn blake2f_cost_on_invalid_length() {
let f = Builtin {
pricer: map![0 => Pricing::Blake2F(123)],
native: EthereumBuiltin::from_str("blake2_f").expect("known builtin"),
};
// invalid input (too short)
let input = hex!("00");
assert_eq!(f.cost(&input[..], 0), U256::from(0));
}
#[test]
fn blake2_f_is_err_on_invalid_length() {
let blake2 = ethereum_builtin("blake2_f");
let blake2 = EthereumBuiltin::from_str("blake2_f").unwrap();
// Test vector 1 and expected output from https://github.com/ethereum/EIPs/blob/master/EIPS/eip-152.md#test-vector-1
let input = hex!("00000c48c9bdf267e6096a3ba7ca8485ae67bb2bf894fe72f36e3cf1361d5f3af54fa5d182e6ad7f520e511f6c3e2b8c68059b6bbd41fbabd9831f79217e1319cde05b61626300000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000300000000000000000000000000000001");
let mut out = [0u8; 64];
@@ -715,7 +800,7 @@ mod tests {
#[test]
fn blake2_f_is_err_on_invalid_length_2() {
let blake2 = ethereum_builtin("blake2_f");
let blake2 = EthereumBuiltin::from_str("blake2_f").unwrap();
// Test vector 2 and expected output from https://github.com/ethereum/EIPs/blob/master/EIPS/eip-152.md#test-vector-2
let input = hex!("000000000c48c9bdf267e6096a3ba7ca8485ae67bb2bf894fe72f36e3cf1361d5f3af54fa5d182e6ad7f520e511f6c3e2b8c68059b6bbd41fbabd9831f79217e1319cde05b61626300000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000300000000000000000000000000000001");
let mut out = [0u8; 64];
@@ -727,7 +812,7 @@ mod tests {
#[test]
fn blake2_f_is_err_on_bad_finalization_flag() {
let blake2 = ethereum_builtin("blake2_f");
let blake2 = EthereumBuiltin::from_str("blake2_f").unwrap();
// Test vector 3 and expected output from https://github.com/ethereum/EIPs/blob/master/EIPS/eip-152.md#test-vector-3
let input = hex!("0000000c48c9bdf267e6096a3ba7ca8485ae67bb2bf894fe72f36e3cf1361d5f3af54fa5d182e6ad7f520e511f6c3e2b8c68059b6bbd41fbabd9831f79217e1319cde05b61626300000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000300000000000000000000000000000002");
let mut out = [0u8; 64];
@@ -739,7 +824,7 @@ mod tests {
#[test]
fn blake2_f_zero_rounds_is_ok_test_vector_4() {
let blake2 = ethereum_builtin("blake2_f");
let blake2 = EthereumBuiltin::from_str("blake2_f").unwrap();
// Test vector 4 and expected output from https://github.com/ethereum/EIPs/blob/master/EIPS/eip-152.md#test-vector-4
let input = hex!("0000000048c9bdf267e6096a3ba7ca8485ae67bb2bf894fe72f36e3cf1361d5f3af54fa5d182e6ad7f520e511f6c3e2b8c68059b6bbd41fbabd9831f79217e1319cde05b61626300000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000300000000000000000000000000000001");
let expected = hex!("08c9bcf367e6096a3ba7ca8485ae67bb2bf894fe72f36e3cf1361d5f3af54fa5d282e6ad7f520e511f6c3e2b8c68059b9442be0454267ce079217e1319cde05b");
@@ -750,7 +835,7 @@ mod tests {
#[test]
fn blake2_f_test_vector_5() {
let blake2 = ethereum_builtin("blake2_f");
let blake2 = EthereumBuiltin::from_str("blake2_f").unwrap();
// Test vector 5 and expected output from https://github.com/ethereum/EIPs/blob/master/EIPS/eip-152.md#test-vector-5
let input = hex!("0000000c48c9bdf267e6096a3ba7ca8485ae67bb2bf894fe72f36e3cf1361d5f3af54fa5d182e6ad7f520e511f6c3e2b8c68059b6bbd41fbabd9831f79217e1319cde05b61626300000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000300000000000000000000000000000001");
let expected = hex!("ba80a53f981c4d0d6a2797b69f12f6e94c212f14685ac4b74b12bb6fdbffa2d17d87c5392aab792dc252d5de4533cc9518d38aa8dbf1925ab92386edd4009923");
@@ -761,7 +846,7 @@ mod tests {
#[test]
fn blake2_f_test_vector_6() {
let blake2 = ethereum_builtin("blake2_f");
let blake2 = EthereumBuiltin::from_str("blake2_f").unwrap();
// Test vector 6 and expected output from https://github.com/ethereum/EIPs/blob/master/EIPS/eip-152.md#test-vector-6
let input = hex!("0000000c48c9bdf267e6096a3ba7ca8485ae67bb2bf894fe72f36e3cf1361d5f3af54fa5d182e6ad7f520e511f6c3e2b8c68059b6bbd41fbabd9831f79217e1319cde05b61626300000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000300000000000000000000000000000000");
let expected = hex!("75ab69d3190a562c51aef8d88f1c2775876944407270c42c9844252c26d2875298743e7f6d5ea2f2d3e8d226039cd31b4e426ac4f2d3d666a610c2116fde4735");
@@ -772,7 +857,7 @@ mod tests {
#[test]
fn blake2_f_test_vector_7() {
let blake2 = ethereum_builtin("blake2_f");
let blake2 = EthereumBuiltin::from_str("blake2_f").unwrap();
// Test vector 7 and expected output from https://github.com/ethereum/EIPs/blob/master/EIPS/eip-152.md#test-vector-7
let input = hex!("0000000148c9bdf267e6096a3ba7ca8485ae67bb2bf894fe72f36e3cf1361d5f3af54fa5d182e6ad7f520e511f6c3e2b8c68059b6bbd41fbabd9831f79217e1319cde05b61626300000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000300000000000000000000000000000001");
let expected = hex!("b63a380cb2897d521994a85234ee2c181b5f844d2c624c002677e9703449d2fba551b3a8333bcdf5f2f7e08993d53923de3d64fcc68c034e717b9293fed7a421");
@@ -784,7 +869,7 @@ mod tests {
#[ignore]
#[test]
fn blake2_f_test_vector_8() {
let blake2 = ethereum_builtin("blake2_f");
let blake2 = EthereumBuiltin::from_str("blake2_f").unwrap();
// Test vector 8 and expected output from https://github.com/ethereum/EIPs/blob/master/EIPS/eip-152.md#test-vector-8
// Note this test is slow, 4294967295/0xffffffff rounds take a while.
let input = hex!("ffffffff48c9bdf267e6096a3ba7ca8485ae67bb2bf894fe72f36e3cf1361d5f3af54fa5d182e6ad7f520e511f6c3e2b8c68059b6bbd41fbabd9831f79217e1319cde05b61626300000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000300000000000000000000000000000001");
@@ -829,8 +914,7 @@ mod tests {
#[test]
fn identity() {
let f = ethereum_builtin("identity");
let f = EthereumBuiltin::from_str("identity").unwrap();
let i = [0u8, 1, 2, 3];
let mut o2 = [255u8; 2];
@@ -849,8 +933,7 @@ mod tests {
#[test]
fn sha256() {
let f = ethereum_builtin("sha256");
let f = EthereumBuiltin::from_str("sha256").unwrap();
let i = [0u8; 0];
let mut o = [255u8; 32];
@@ -872,8 +955,7 @@ mod tests {
#[test]
fn ripemd160() {
let f = ethereum_builtin("ripemd160");
let f = EthereumBuiltin::from_str("ripemd160").unwrap();
let i = [0u8; 0];
let mut o = [255u8; 32];
@@ -891,7 +973,7 @@ mod tests {
#[test]
fn ecrecover() {
let f = ethereum_builtin("ecrecover");
let f = EthereumBuiltin::from_str("ecrecover").unwrap();
let i = hex!("47173285a8d7341e5e972fc677286384f802f8ef42a5ec5f03bbfa254cb01fad000000000000000000000000000000000000000000000000000000000000001b650acf9d3f5f0a2c799776a1254355d5f4061762a237396a99a0e0e3fc2bcd6729514a0dacb2e623ac4abd157cb18163ff942280db4d5caad66ddf941ba12e03");
@@ -941,18 +1023,16 @@ mod tests {
#[test]
fn modexp() {
let f = Builtin {
pricer: Box::new(ModexpPricer { divisor: 20 }),
native: ethereum_builtin("modexp"),
activate_at: 0,
pricer: map![0 => Pricing::Modexp(ModexpPricer { divisor: 20 })],
native: EthereumBuiltin::from_str("modexp").unwrap(),
};
// test for potential gas cost multiplication overflow
{
let input = hex!("0000000000000000000000000000000000000000000000000000000000000001000000000000000000000000000000000000000000000000000000003b27bafd00000000000000000000000000000000000000000000000000000000503c8ac3");
let expected_cost = U256::max_value();
assert_eq!(f.cost(&input[..], 0), expected_cost.into());
assert_eq!(f.cost(&input[..], 0), expected_cost);
}
// test for potential exp len overflow
@@ -969,7 +1049,7 @@ mod tests {
f.execute(&input[..], &mut BytesRef::Fixed(&mut output[..])).expect("Builtin should fail");
assert_eq!(output, expected);
assert_eq!(f.cost(&input[..], 0), expected_cost.into());
assert_eq!(f.cost(&input[..], 0), expected_cost);
}
// fermat's little theorem example.
@@ -1054,9 +1134,8 @@ mod tests {
fn bn128_add() {
let f = Builtin {
pricer: Box::new(Linear { base: 0, word: 0 }),
native: ethereum_builtin("alt_bn128_add"),
activate_at: 0,
pricer: map![0 => Pricing::Linear(Linear { base: 0, word: 0 })],
native: EthereumBuiltin::from_str("alt_bn128_add").unwrap(),
};
// zero-points additions
@@ -1113,9 +1192,8 @@ mod tests {
fn bn128_mul() {
let f = Builtin {
pricer: Box::new(Linear { base: 0, word: 0 }),
native: ethereum_builtin("alt_bn128_mul"),
activate_at: 0,
pricer: map![0 => Pricing::Linear(Linear { base: 0, word: 0 })],
native: EthereumBuiltin::from_str("alt_bn128_mul").unwrap(),
};
// zero-point multiplication
@@ -1153,9 +1231,8 @@ mod tests {
fn builtin_pairing() -> Builtin {
Builtin {
pricer: Box::new(Linear { base: 0, word: 0 }),
native: ethereum_builtin("alt_bn128_pairing"),
activate_at: 0,
pricer: map![0 => Pricing::Linear(Linear { base: 0, word: 0 })],
native: EthereumBuiltin::from_str("alt_bn128_pairing").unwrap(),
}
}
@@ -1226,16 +1303,15 @@ mod tests {
#[test]
#[should_panic]
fn from_unknown_linear() {
let _ = ethereum_builtin("foo");
let _ = EthereumBuiltin::from_str("foo").unwrap();
}
#[test]
fn is_active() {
let pricer = Box::new(Linear { base: 10, word: 20} );
let pricer = Pricing::Linear(Linear { base: 10, word: 20 });
let b = Builtin {
pricer: pricer as Box<dyn Pricer>,
native: ethereum_builtin("identity"),
activate_at: 100_000,
pricer: map![100_000 => pricer],
native: EthereumBuiltin::from_str("identity").unwrap(),
};
assert!(!b.is_active(99_999));
@@ -1245,11 +1321,10 @@ mod tests {
#[test]
fn from_named_linear() {
let pricer = Box::new(Linear { base: 10, word: 20 });
let pricer = Pricing::Linear(Linear { base: 10, word: 20 });
let b = Builtin {
pricer: pricer as Box<dyn Pricer>,
native: ethereum_builtin("identity"),
activate_at: 1,
pricer: map![0 => pricer],
native: EthereumBuiltin::from_str("identity").unwrap(),
};
assert_eq!(b.cost(&[0; 0], 0), U256::from(10));
@@ -1265,15 +1340,15 @@ mod tests {
#[test]
fn from_json() {
let b = Builtin::from(ethjson::spec::Builtin {
let b = Builtin::try_from(ethjson::spec::Builtin {
name: "identity".to_owned(),
pricing: ethjson::spec::Pricing::Linear(ethjson::spec::Linear {
base: 10,
word: 20,
}),
activate_at: None,
eip1108_transition: None,
});
pricing: map![
0 => PricingAt {
info: None,
price: JsonPricing::Linear(JsonLinearPricing { base: 10, word: 20 })
}
]
}).unwrap();
assert_eq!(b.cost(&[0; 0], 0), U256::from(10));
assert_eq!(b.cost(&[0; 1], 0), U256::from(30));
@@ -1288,17 +1363,25 @@ mod tests {
#[test]
fn bn128_pairing_eip1108_transition() {
let b = Builtin::from(ethjson::spec::Builtin {
let b = Builtin::try_from(JsonBuiltin {
name: "alt_bn128_pairing".to_owned(),
pricing: ethjson::spec::Pricing::AltBn128Pairing(ethjson::spec::builtin::AltBn128Pairing {
base: 100_000,
pair: 80_000,
eip1108_transition_base: 45_000,
eip1108_transition_pair: 34_000,
}),
activate_at: Some(Uint(U256::from(10))),
eip1108_transition: Some(Uint(U256::from(20))),
});
pricing: map![
10 => PricingAt {
info: None,
price: JsonPricing::AltBn128Pairing(JsonAltBn128PairingPricing {
base: 100_000,
pair: 80_000,
}),
},
20 => PricingAt {
info: None,
price: JsonPricing::AltBn128Pairing(JsonAltBn128PairingPricing {
base: 45_000,
pair: 34_000,
}),
}
],
}).unwrap();
assert_eq!(b.cost(&[0; 192 * 3], 10), U256::from(340_000), "80 000 * 3 + 100 000 == 340 000");
assert_eq!(b.cost(&[0; 192 * 7], 20), U256::from(283_000), "34 000 * 7 + 45 000 == 283 000");
@@ -1306,15 +1389,25 @@ mod tests {
#[test]
fn bn128_add_eip1108_transition() {
let b = Builtin::from(ethjson::spec::Builtin {
let b = Builtin::try_from(JsonBuiltin {
name: "alt_bn128_add".to_owned(),
pricing: ethjson::spec::Pricing::AltBn128ConstOperations(ethjson::spec::builtin::AltBn128ConstOperations {
price: 500,
eip1108_transition_price: 150,
}),
activate_at: Some(Uint(U256::from(10))),
eip1108_transition: Some(Uint(U256::from(20))),
});
pricing: map![
10 => PricingAt {
info: None,
price: JsonPricing::Linear(JsonLinearPricing {
base: 500,
word: 0,
}),
},
20 => PricingAt {
info: None,
price: JsonPricing::Linear(JsonLinearPricing {
base: 150,
word: 0,
}),
}
],
}).unwrap();
assert_eq!(b.cost(&[0; 192], 10), U256::from(500));
assert_eq!(b.cost(&[0; 10], 20), U256::from(150), "after istanbul hardfork gas cost for add should be 150");
@@ -1322,17 +1415,99 @@ mod tests {
#[test]
fn bn128_mul_eip1108_transition() {
let b = Builtin::from(ethjson::spec::Builtin {
let b = Builtin::try_from(JsonBuiltin {
name: "alt_bn128_mul".to_owned(),
pricing: ethjson::spec::Pricing::AltBn128ConstOperations(ethjson::spec::builtin::AltBn128ConstOperations {
price: 40_000,
eip1108_transition_price: 6000,
}),
activate_at: Some(Uint(U256::from(10))),
eip1108_transition: Some(Uint(U256::from(20))),
});
pricing: map![
10 => PricingAt {
info: None,
price: JsonPricing::Linear(JsonLinearPricing {
base: 40_000,
word: 0,
}),
},
20 => PricingAt {
info: None,
price: JsonPricing::Linear(JsonLinearPricing {
base: 6_000,
word: 0,
}),
}
],
}).unwrap();
assert_eq!(b.cost(&[0; 192], 10), U256::from(40_000));
assert_eq!(b.cost(&[0; 10], 20), U256::from(6_000), "after istanbul hardfork gas cost for mul should be 6 000");
}
#[test]
fn multimap_use_most_recent_on_activate() {
let b = Builtin::try_from(JsonBuiltin {
name: "alt_bn128_mul".to_owned(),
pricing: map![
10 => PricingAt {
info: None,
price: JsonPricing::Linear(JsonLinearPricing {
base: 40_000,
word: 0,
}),
},
20 => PricingAt {
info: None,
price: JsonPricing::Linear(JsonLinearPricing {
base: 6_000,
word: 0,
})
},
100 => PricingAt {
info: None,
price: JsonPricing::Linear(JsonLinearPricing {
base: 1_337,
word: 0,
})
}
]
}).unwrap();
assert_eq!(b.cost(&[0; 2], 0), U256::zero(), "not activated yet; should be zero");
assert_eq!(b.cost(&[0; 3], 10), U256::from(40_000), "use price #1");
assert_eq!(b.cost(&[0; 4], 20), U256::from(6_000), "use price #2");
assert_eq!(b.cost(&[0; 1], 99), U256::from(6_000), "use price #2");
assert_eq!(b.cost(&[0; 1], 100), U256::from(1_337), "use price #3");
assert_eq!(b.cost(&[0; 1], u64::max_value()), U256::from(1_337), "use price #3 indefinitely");
}
#[test]
fn multimap_use_last_with_same_activate_at() {
let b = Builtin::try_from(JsonBuiltin {
name: "alt_bn128_mul".to_owned(),
pricing: map![
1 => PricingAt {
info: None,
price: JsonPricing::Linear(JsonLinearPricing {
base: 40_000,
word: 0,
}),
},
1 => PricingAt {
info: None,
price: JsonPricing::Linear(JsonLinearPricing {
base: 6_000,
word: 0,
}),
},
1 => PricingAt {
info: None,
price: JsonPricing::Linear(JsonLinearPricing {
base: 1_337,
word: 0,
}),
}
],
}).unwrap();
assert_eq!(b.cost(&[0; 1], 0), U256::from(0), "not activated yet");
assert_eq!(b.cost(&[0; 1], 1), U256::from(1_337), "use price #3");
}
}