2019-01-07 11:33:07 +01:00
|
|
|
// Copyright 2015-2019 Parity Technologies (UK) Ltd.
|
|
|
|
// This file is part of Parity Ethereum.
|
2016-03-17 13:41:11 +01:00
|
|
|
|
2019-01-07 11:33:07 +01:00
|
|
|
// Parity Ethereum is free software: you can redistribute it and/or modify
|
2016-03-17 13:41:11 +01:00
|
|
|
// it under the terms of the GNU General Public License as published by
|
|
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
|
|
// (at your option) any later version.
|
|
|
|
|
2019-01-07 11:33:07 +01:00
|
|
|
// Parity Ethereum is distributed in the hope that it will be useful,
|
2016-03-17 13:41:11 +01:00
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
// GNU General Public License for more details.
|
|
|
|
|
|
|
|
// You should have received a copy of the GNU General Public License
|
2019-01-07 11:33:07 +01:00
|
|
|
// along with Parity Ethereum. If not, see <http://www.gnu.org/licenses/>.
|
2016-03-17 13:41:11 +01:00
|
|
|
|
|
|
|
//! Spec builtin deserialization.
|
|
|
|
|
2017-03-21 17:36:38 +01:00
|
|
|
use uint::Uint;
|
2019-11-11 21:57:38 +01:00
|
|
|
use std::collections::BTreeMap;
|
2019-09-12 18:43:53 +02:00
|
|
|
|
2016-03-18 19:08:57 +01:00
|
|
|
/// Linear pricing.
|
2016-04-09 19:20:35 +02:00
|
|
|
#[derive(Debug, PartialEq, Deserialize, Clone)]
|
2018-11-27 23:21:31 +01:00
|
|
|
#[serde(deny_unknown_fields)]
|
2016-03-17 13:41:11 +01:00
|
|
|
pub struct Linear {
|
2016-04-09 19:20:35 +02:00
|
|
|
/// Base price.
|
2019-11-11 21:57:38 +01:00
|
|
|
pub base: u64,
|
2016-04-09 19:20:35 +02:00
|
|
|
/// Price for word.
|
2019-11-11 21:57:38 +01:00
|
|
|
pub word: u64,
|
2016-03-17 13:41:11 +01:00
|
|
|
}
|
|
|
|
|
2017-03-21 17:36:38 +01:00
|
|
|
/// Pricing for modular exponentiation.
|
|
|
|
#[derive(Debug, PartialEq, Deserialize, Clone)]
|
2018-11-27 23:21:31 +01:00
|
|
|
#[serde(deny_unknown_fields)]
|
2017-03-21 17:36:38 +01:00
|
|
|
pub struct Modexp {
|
|
|
|
/// Price divisor.
|
2019-11-11 21:57:38 +01:00
|
|
|
pub divisor: u64,
|
2017-03-21 17:36:38 +01:00
|
|
|
}
|
|
|
|
|
2019-09-12 18:43:53 +02:00
|
|
|
/// Pricing for constant alt_bn128 operations (ECADD and ECMUL)
|
|
|
|
#[derive(Debug, PartialEq, Deserialize, Clone)]
|
|
|
|
#[serde(deny_unknown_fields)]
|
|
|
|
pub struct AltBn128ConstOperations {
|
|
|
|
/// price
|
2019-11-11 21:57:38 +01:00
|
|
|
pub price: u64,
|
2019-09-12 18:43:53 +02:00
|
|
|
}
|
|
|
|
|
2017-09-15 21:07:54 +02:00
|
|
|
/// Pricing for alt_bn128_pairing.
|
|
|
|
#[derive(Debug, PartialEq, Deserialize, Clone)]
|
2018-11-27 23:21:31 +01:00
|
|
|
#[serde(deny_unknown_fields)]
|
2017-09-15 21:07:54 +02:00
|
|
|
pub struct AltBn128Pairing {
|
|
|
|
/// Base price.
|
2019-11-11 21:57:38 +01:00
|
|
|
pub base: u64,
|
2017-09-15 21:07:54 +02:00
|
|
|
/// Price per point pair.
|
2019-11-11 21:57:38 +01:00
|
|
|
pub pair: u64,
|
2017-09-15 21:07:54 +02:00
|
|
|
}
|
|
|
|
|
2016-03-18 19:08:57 +01:00
|
|
|
/// Pricing variants.
|
2016-04-09 19:20:35 +02:00
|
|
|
#[derive(Debug, PartialEq, Deserialize, Clone)]
|
2018-11-27 23:21:31 +01:00
|
|
|
#[serde(deny_unknown_fields)]
|
2018-10-29 16:49:04 +01:00
|
|
|
#[serde(rename_all = "snake_case")]
|
2016-03-18 19:08:57 +01:00
|
|
|
pub enum Pricing {
|
2019-09-12 18:43:53 +02:00
|
|
|
/// Pricing for Blake2 compression function: each call costs the same amount per round.
|
2019-09-26 11:03:39 +02:00
|
|
|
Blake2F {
|
|
|
|
/// Price per round of Blake2 compression function.
|
|
|
|
gas_per_round: u64,
|
|
|
|
},
|
2016-03-18 19:08:57 +01:00
|
|
|
/// Linear pricing.
|
|
|
|
Linear(Linear),
|
2017-03-21 17:36:38 +01:00
|
|
|
/// Pricing for modular exponentiation.
|
|
|
|
Modexp(Modexp),
|
2017-09-15 21:07:54 +02:00
|
|
|
/// Pricing for alt_bn128_pairing exponentiation.
|
|
|
|
AltBn128Pairing(AltBn128Pairing),
|
2019-09-12 18:43:53 +02:00
|
|
|
/// Pricing for constant alt_bn128 operations
|
|
|
|
AltBn128ConstOperations(AltBn128ConstOperations),
|
2016-03-18 19:08:57 +01:00
|
|
|
}
|
|
|
|
|
2019-11-11 21:57:38 +01:00
|
|
|
/// Builtin compability layer
|
2016-04-09 19:20:35 +02:00
|
|
|
#[derive(Debug, PartialEq, Deserialize, Clone)]
|
2018-11-27 23:21:31 +01:00
|
|
|
#[serde(deny_unknown_fields)]
|
2019-11-11 21:57:38 +01:00
|
|
|
pub struct BuiltinCompat {
|
|
|
|
/// Builtin name.
|
|
|
|
name: String,
|
|
|
|
/// Builtin pricing.
|
|
|
|
pricing: PricingCompat,
|
|
|
|
/// Activation block.
|
|
|
|
activate_at: Option<Uint>,
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Spec builtin.
|
|
|
|
#[derive(Debug, PartialEq, Clone)]
|
2016-03-17 13:41:11 +01:00
|
|
|
pub struct Builtin {
|
2016-04-09 19:20:35 +02:00
|
|
|
/// Builtin name.
|
|
|
|
pub name: String,
|
|
|
|
/// Builtin pricing.
|
2019-11-11 21:57:38 +01:00
|
|
|
pub pricing: BTreeMap<u64, PricingAt>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl From<BuiltinCompat> for Builtin {
|
|
|
|
fn from(legacy: BuiltinCompat) -> Self {
|
|
|
|
let pricing = match legacy.pricing {
|
|
|
|
PricingCompat::Single(pricing) => {
|
|
|
|
let mut map = BTreeMap::new();
|
|
|
|
let activate_at: u64 = legacy.activate_at.map_or(0, Into::into);
|
|
|
|
map.insert(activate_at, PricingAt { info: None, price: pricing });
|
|
|
|
map
|
|
|
|
}
|
|
|
|
PricingCompat::Multi(pricings) => {
|
|
|
|
pricings.into_iter().map(|(a, p)| (a.into(), p)).collect()
|
|
|
|
}
|
|
|
|
};
|
|
|
|
Self { name: legacy.name, pricing }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Compability layer for different pricings
|
|
|
|
#[derive(Debug, PartialEq, Deserialize, Clone)]
|
|
|
|
#[serde(rename_all = "snake_case")]
|
|
|
|
#[serde(deny_unknown_fields)]
|
|
|
|
#[serde(untagged)]
|
|
|
|
enum PricingCompat {
|
|
|
|
/// Single builtin
|
|
|
|
Single(Pricing),
|
|
|
|
/// Multiple builtins
|
|
|
|
Multi(BTreeMap<Uint, PricingAt>),
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Price for a builtin, with the block number to activate it on
|
|
|
|
#[derive(Debug, PartialEq, Deserialize, Clone)]
|
|
|
|
#[serde(deny_unknown_fields)]
|
|
|
|
pub struct PricingAt {
|
|
|
|
/// Description of the activation, e.g. "PunyPony HF, March 12, 2025".
|
|
|
|
pub info: Option<String>,
|
|
|
|
/// Builtin pricing.
|
|
|
|
pub price: Pricing,
|
2016-03-17 13:41:11 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
use serde_json;
|
2017-03-21 17:36:38 +01:00
|
|
|
use uint::Uint;
|
2019-11-11 21:57:38 +01:00
|
|
|
use super::{Builtin, BuiltinCompat, BTreeMap, Pricing, PricingAt, Linear, Modexp, AltBn128ConstOperations};
|
|
|
|
use macros::map;
|
2016-03-17 13:41:11 +01:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn builtin_deserialization() {
|
|
|
|
let s = r#"{
|
|
|
|
"name": "ecrecover",
|
2016-03-18 22:54:36 +01:00
|
|
|
"pricing": { "linear": { "base": 3000, "word": 0 } }
|
2016-03-17 13:41:11 +01:00
|
|
|
}"#;
|
2019-11-11 21:57:38 +01:00
|
|
|
let builtin: Builtin = serde_json::from_str::<BuiltinCompat>(s).unwrap().into();
|
|
|
|
assert_eq!(builtin.name, "ecrecover");
|
|
|
|
assert_eq!(builtin.pricing, map![
|
|
|
|
0 => PricingAt {
|
|
|
|
info: None,
|
|
|
|
price: Pricing::Linear(Linear { base: 3000, word: 0 })
|
|
|
|
}
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn deserialize_multiple_pricings() {
|
|
|
|
let s = r#"{
|
|
|
|
"name": "ecrecover",
|
|
|
|
"pricing": {
|
|
|
|
"0": {
|
|
|
|
"price": {"linear": { "base": 3000, "word": 0 }}
|
|
|
|
},
|
|
|
|
"500": {
|
|
|
|
"info": "enable fake EIP at block 500",
|
|
|
|
"price": {"linear": { "base": 10, "word": 0 }}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}"#;
|
|
|
|
let builtin: Builtin = serde_json::from_str::<BuiltinCompat>(s).unwrap().into();
|
|
|
|
assert_eq!(builtin.name, "ecrecover");
|
|
|
|
assert_eq!(builtin.pricing, map![
|
|
|
|
0 => PricingAt {
|
|
|
|
info: None,
|
|
|
|
price: Pricing::Linear(Linear { base: 3000, word: 0 })
|
|
|
|
},
|
|
|
|
500 => PricingAt {
|
|
|
|
info: Some(String::from("enable fake EIP at block 500")),
|
|
|
|
price: Pricing::Linear(Linear { base: 10, word: 0 })
|
|
|
|
}
|
|
|
|
]);
|
2017-03-21 17:36:38 +01:00
|
|
|
}
|
|
|
|
|
2019-09-12 18:43:53 +02:00
|
|
|
#[test]
|
|
|
|
fn deserialization_blake2_f_builtin() {
|
|
|
|
let s = r#"{
|
|
|
|
"name": "blake2_f",
|
|
|
|
"activate_at": "0xffffff",
|
2019-09-26 11:03:39 +02:00
|
|
|
"pricing": { "blake2_f": { "gas_per_round": 123 } }
|
2019-09-12 18:43:53 +02:00
|
|
|
}"#;
|
2019-11-11 21:57:38 +01:00
|
|
|
let builtin: Builtin = serde_json::from_str::<BuiltinCompat>(s).unwrap().into();
|
|
|
|
assert_eq!(builtin.name, "blake2_f");
|
|
|
|
assert_eq!(builtin.pricing, map![
|
|
|
|
0xffffff => PricingAt {
|
|
|
|
info: None,
|
|
|
|
price: Pricing::Blake2F { gas_per_round: 123 }
|
|
|
|
}
|
|
|
|
]);
|
2019-09-12 18:43:53 +02:00
|
|
|
}
|
|
|
|
|
2017-03-21 17:36:38 +01:00
|
|
|
#[test]
|
|
|
|
fn activate_at() {
|
|
|
|
let s = r#"{
|
|
|
|
"name": "late_start",
|
|
|
|
"activate_at": 100000,
|
|
|
|
"pricing": { "modexp": { "divisor": 5 } }
|
|
|
|
}"#;
|
|
|
|
|
2019-11-11 21:57:38 +01:00
|
|
|
let builtin: Builtin = serde_json::from_str::<BuiltinCompat>(s).unwrap().into();
|
|
|
|
assert_eq!(builtin.name, "late_start");
|
|
|
|
assert_eq!(builtin.pricing, map![
|
|
|
|
100_000 => PricingAt {
|
|
|
|
info: None,
|
|
|
|
price: Pricing::Modexp(Modexp { divisor: 5 })
|
|
|
|
}
|
|
|
|
]);
|
2016-03-17 13:41:11 +01:00
|
|
|
}
|
|
|
|
}
|