Skip to content
This repository has been archived by the owner on Dec 2, 2022. It is now read-only.

Commit

Permalink
Remove Ethash, switch mainnet chainspec to Beacon
Browse files Browse the repository at this point in the history
  • Loading branch information
vorot93 committed Sep 15, 2022
1 parent 196fc1d commit a5ba9d1
Show file tree
Hide file tree
Showing 10 changed files with 16 additions and 609 deletions.
15 changes: 0 additions & 15 deletions Cargo.lock

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

1 change: 0 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@ enr = { version = "0.6", default-features = false, features = [
] }
enum-primitive-derive = "0.2"
expanded-pathbuf = "0.1"
ethash = { git = "https://github.com/rust-ethereum/ethash" }
ethereum-forkid = "0.10"
ethereum-interfaces = { git = "https://github.com/ledgerwatch/interfaces", features = [
"remotekv",
Expand Down
174 changes: 6 additions & 168 deletions bin/consensus-tests.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,6 @@
#![feature(let_else)]
#![allow(clippy::suspicious_else_formatting)]
use akula::{
consensus::{
difficulty::{canonical_difficulty, BlockDifficultyBombData},
*,
},
crypto::keccak256,
models::*,
res::chainspec::*,
*,
};
use akula::{consensus::*, crypto::keccak256, models::*, res::chainspec::*, *};
use anyhow::{bail, ensure, format_err};
use bytes::Bytes;
use clap::Parser;
Expand All @@ -18,7 +9,7 @@ use expanded_pathbuf::ExpandedPathBuf;
use fastrlp::*;
use maplit::*;
use once_cell::sync::Lazy;
use serde::{de, Deserialize};
use serde::Deserialize;
use serde_json::{Map, Value};
use std::{
collections::{HashMap, HashSet},
Expand Down Expand Up @@ -161,22 +152,11 @@ impl FromStr for Network {
}
}

fn testconfig(
name: Network,
upgrades: Upgrades,
dao_block: Option<BlockNumber>,
bomb_delay: BlockNumber,
) -> ChainSpec {
fn testconfig(name: Network, upgrades: Upgrades, dao_block: Option<BlockNumber>) -> ChainSpec {
let mut spec = MAINNET.clone();
spec.name = format!("{:?}", name);
spec.consensus.eip1559_block = upgrades.london;
let SealVerificationParams::Ethash { block_reward, difficulty_bomb, skip_pow_verification, homestead_formula, byzantium_formula,.. } = &mut spec.consensus.seal_verification else { unreachable!() };
*difficulty_bomb = Some(DifficultyBomb {
delays: btreemap! { BlockNumber(0) => bomb_delay },
});
*skip_pow_verification = true;
*homestead_formula = upgrades.homestead;
*byzantium_formula = upgrades.byzantium;
let SealVerificationParams::Beacon { block_reward, .. } = &mut spec.consensus.seal_verification else { unreachable!() };
spec.upgrades = upgrades;

block_reward.clear();
Expand All @@ -200,15 +180,14 @@ fn testconfig(

static NETWORK_CONFIG: Lazy<HashMap<Network, ChainSpec>> = Lazy::new(|| {
vec![
(Network::Frontier, Upgrades::default(), None, 0),
(Network::Frontier, Upgrades::default(), None),
(
Network::Homestead,
Upgrades {
homestead: Some(0.into()),
..Default::default()
},
None,
0,
),
(
Network::EIP150,
Expand All @@ -218,7 +197,6 @@ static NETWORK_CONFIG: Lazy<HashMap<Network, ChainSpec>> = Lazy::new(|| {
..Default::default()
},
None,
0,
),
(
Network::EIP158,
Expand All @@ -229,7 +207,6 @@ static NETWORK_CONFIG: Lazy<HashMap<Network, ChainSpec>> = Lazy::new(|| {
..Default::default()
},
None,
0,
),
(
Network::Byzantium,
Expand All @@ -241,7 +218,6 @@ static NETWORK_CONFIG: Lazy<HashMap<Network, ChainSpec>> = Lazy::new(|| {
..Default::default()
},
None,
3000000,
),
(
Network::Constantinople,
Expand All @@ -254,7 +230,6 @@ static NETWORK_CONFIG: Lazy<HashMap<Network, ChainSpec>> = Lazy::new(|| {
..Default::default()
},
None,
5000000,
),
(
Network::ConstantinopleFix,
Expand All @@ -268,7 +243,6 @@ static NETWORK_CONFIG: Lazy<HashMap<Network, ChainSpec>> = Lazy::new(|| {
..Default::default()
},
None,
5000000,
),
(
Network::Istanbul,
Expand All @@ -283,7 +257,6 @@ static NETWORK_CONFIG: Lazy<HashMap<Network, ChainSpec>> = Lazy::new(|| {
..Default::default()
},
None,
9000000,
),
(
Network::Berlin,
Expand All @@ -299,7 +272,6 @@ static NETWORK_CONFIG: Lazy<HashMap<Network, ChainSpec>> = Lazy::new(|| {
..Default::default()
},
None,
9000000,
),
(
Network::London,
Expand All @@ -316,7 +288,6 @@ static NETWORK_CONFIG: Lazy<HashMap<Network, ChainSpec>> = Lazy::new(|| {
..Default::default()
},
None,
9700000,
),
(
Network::FrontierToHomesteadAt5,
Expand All @@ -325,7 +296,6 @@ static NETWORK_CONFIG: Lazy<HashMap<Network, ChainSpec>> = Lazy::new(|| {
..Default::default()
},
None,
0,
),
(
Network::HomesteadToEIP150At5,
Expand All @@ -335,7 +305,6 @@ static NETWORK_CONFIG: Lazy<HashMap<Network, ChainSpec>> = Lazy::new(|| {
..Default::default()
},
None,
0,
),
(
Network::HomesteadToDaoAt5,
Expand All @@ -344,7 +313,6 @@ static NETWORK_CONFIG: Lazy<HashMap<Network, ChainSpec>> = Lazy::new(|| {
..Default::default()
},
Some(5.into()),
0,
),
(
Network::EIP158ToByzantiumAt5,
Expand All @@ -356,7 +324,6 @@ static NETWORK_CONFIG: Lazy<HashMap<Network, ChainSpec>> = Lazy::new(|| {
..Default::default()
},
None,
3000000,
),
(
Network::ByzantiumToConstantinopleFixAt5,
Expand All @@ -370,7 +337,6 @@ static NETWORK_CONFIG: Lazy<HashMap<Network, ChainSpec>> = Lazy::new(|| {
..Default::default()
},
None,
5000000,
),
(
Network::BerlinToLondonAt5,
Expand All @@ -387,7 +353,6 @@ static NETWORK_CONFIG: Lazy<HashMap<Network, ChainSpec>> = Lazy::new(|| {
..Default::default()
},
None,
9700000,
),
(
Network::EIP2384,
Expand All @@ -402,7 +367,6 @@ static NETWORK_CONFIG: Lazy<HashMap<Network, ChainSpec>> = Lazy::new(|| {
..Default::default()
},
None,
9000000,
),
(
Network::ArrowGlacier,
Expand All @@ -419,7 +383,6 @@ static NETWORK_CONFIG: Lazy<HashMap<Network, ChainSpec>> = Lazy::new(|| {
..Default::default()
},
None,
10700000,
),
(
Network::Merge,
Expand All @@ -436,16 +399,10 @@ static NETWORK_CONFIG: Lazy<HashMap<Network, ChainSpec>> = Lazy::new(|| {
paris: Some(0.into()),
},
None,
11_200_000,
),
]
.into_iter()
.map(|(network, upgrades, dao_block, bomb_delay)| {
(
network,
testconfig(network, upgrades, dao_block, bomb_delay.into()),
)
})
.map(|(network, upgrades, dao_block)| (network, testconfig(network, upgrades, dao_block)))
.collect()
});

Expand All @@ -460,55 +417,8 @@ pub struct AccountState {
pub storage: HashMap<U256, U256>,
}

fn deserialize_str_as_blocknumber<'de, D>(deserializer: D) -> Result<BlockNumber, D::Error>
where
D: de::Deserializer<'de>,
{
deserialize_hexstr_as_u64(deserializer).map(BlockNumber)
}

fn deserialize_str_as_u128<'de, D>(deserializer: D) -> Result<u128, D::Error>
where
D: de::Deserializer<'de>,
{
let s = String::deserialize(deserializer)?;

let d = if let Some(stripped) = s.strip_prefix("0x") {
u128::from_str_radix(stripped, 16)
} else {
s.parse()
}
.map_err(|e| format!("{}/{}", e, s))
.unwrap();

Ok(d)
}

#[derive(Debug, Deserialize)]
#[serde(rename_all = "camelCase")]
struct DifficultyTest {
/// Timestamp of a previous block
#[serde(deserialize_with = "deserialize_hexstr_as_u64")]
parent_timestamp: u64,
/// Difficulty of a previous block
#[serde(deserialize_with = "deserialize_str_as_u128")]
parent_difficulty: u128,
/// Timestamp of a current block
#[serde(deserialize_with = "deserialize_hexstr_as_u64")]
current_timestamp: u64,
/// Number of a current block (previous block number = currentBlockNumber - 1)
#[serde(deserialize_with = "deserialize_str_as_blocknumber")]
current_block_number: BlockNumber,
/// Difficulty of a current block
#[serde(deserialize_with = "deserialize_str_as_u128")]
current_difficulty: u128,
#[serde(default)]
parent_uncles: U256,
}

#[derive(Debug, Deserialize)]
enum SealEngine {
Ethash,
NoProof,
}

Expand Down Expand Up @@ -813,57 +723,6 @@ fn transaction_test(testdata: TransactionTest) -> anyhow::Result<()> {
Ok(())
}

type NetworkDifficultyTests = HashMap<String, DifficultyTest>;

#[instrument(skip(testdata))]
fn difficulty_test(testdata: HashMap<String, Value>) -> anyhow::Result<()> {
for (network, testdata) in testdata {
if network == "_info" {
continue;
}

let network =
Network::from_str(&network).map_err(|_| format_err!("Unknown network: {}", network))?;
let testdata = serde_json::from_value::<NetworkDifficultyTests>(testdata)?;

for (_, testdata) in testdata {
let parent_has_uncles = if testdata.parent_uncles == 0 {
false
} else if testdata.parent_uncles == 1 {
true
} else {
bail!("Invalid parentUncles: {}", testdata.parent_uncles);
};

let config = NETWORK_CONFIG[&network].clone();
let SealVerificationParams::Ethash { homestead_formula, byzantium_formula, difficulty_bomb, .. } = config.consensus.seal_verification else {unreachable!()};

let calculated_difficulty = canonical_difficulty(
testdata.current_block_number,
testdata.current_timestamp,
testdata.parent_difficulty.into(),
testdata.parent_timestamp,
parent_has_uncles,
switch_is_active(byzantium_formula, testdata.current_block_number),
switch_is_active(homestead_formula, testdata.current_block_number),
difficulty_bomb.map(|b| BlockDifficultyBombData {
delay_to: b.get_delay_to(testdata.current_block_number),
}),
);

ensure!(
calculated_difficulty.as_u128() == testdata.current_difficulty,
"Difficulty mismatch for block {}\n{} != {}",
testdata.current_block_number,
calculated_difficulty,
testdata.current_difficulty
);
}
}

Ok(())
}

#[instrument(skip(f))]
fn run_test_file<Test>(
path: &Path,
Expand Down Expand Up @@ -968,27 +827,6 @@ async fn run() {
let mut res = RunResults::default();

let mut skipped = 0;
for entry in walkdir::WalkDir::new(root_dir.join(&*DIFFICULTY_DIR))
.into_iter()
.filter_entry(|e| {
if exclude_test(e.path(), &root_dir) {
skipped += 1;
return false;
}

true
})
{
let e = entry.unwrap();

if e.file_type().is_file() {
let p = e.into_path();
let test_names = Arc::clone(&test_names);
tasks.push(tokio::spawn(async move {
run_test_file(p.as_path(), &test_names, difficulty_test)
}));
}
}

for entry in walkdir::WalkDir::new(root_dir.join(&*BLOCKCHAIN_DIR))
.into_iter()
Expand Down
Loading

0 comments on commit a5ba9d1

Please sign in to comment.