Skip to content

Commit

Permalink
feat: allow users to configure hardfork information in chain spec, th…
Browse files Browse the repository at this point in the history
…e default is the latest version
  • Loading branch information
driftluo committed Sep 5, 2023
1 parent 9dc5a7c commit 5ea80d6
Show file tree
Hide file tree
Showing 6 changed files with 44 additions and 14 deletions.
38 changes: 34 additions & 4 deletions common/config-parser/src/types/spec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ use clap::builder::{StringValueParser, TypedValueParser, ValueParserFactory};
use serde::Deserialize;

use protocol::{
codec::deserialize_address,
types::{Block, Bytes, Header, RichBlock, SignedTransaction, H160, U256},
codec::{deserialize_address, ProtocolCodec},
types::{Block, HardforkInfoInner, Header, RichBlock, SignedTransaction, H160, H256, U256},
};

use crate::parse_file;
Expand All @@ -27,7 +27,7 @@ pub struct ChainSpec {
#[derive(Clone, Debug, Deserialize)]
pub struct Genesis {
pub timestamp: u64,
pub extra_data: Bytes,
pub extra_data: HarforkInput,
pub base_fee_per_gas: U256,
pub chain_id: u64,

Expand Down Expand Up @@ -128,10 +128,40 @@ impl Genesis {
pub fn build_header(&self) -> Header {
Header {
timestamp: self.timestamp,
extra_data: self.extra_data.clone(),
// todo: if Hardforkinput is empty, it must change to latest hardfork info to init
// genesis
extra_data: Into::<HardforkInfoInner>::into(self.extra_data.clone())
.encode()
.unwrap(),
base_fee_per_gas: self.base_fee_per_gas,
chain_id: self.chain_id,
..Default::default()
}
}
}

#[derive(Clone, Debug, Deserialize)]
pub struct HarforkInput {
block_number: u64,
hardforks: Vec<HardforkType>,
}

impl From<HarforkInput> for HardforkInfoInner {
fn from(value: HarforkInput) -> Self {
let flags = {
let r = value.hardforks.into_iter().fold(0, |acc, s| acc | s as u64);

H256::from_low_u64_be(r.to_be())
};

HardforkInfoInner {
block_number: value.block_number,
flags,
}
}
}

#[derive(Clone, Debug, Deserialize, Copy)]
enum HardforkType {
None = 0b0,
}
8 changes: 4 additions & 4 deletions core/executor/src/system_contract/metadata/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -116,10 +116,6 @@ impl<Adapter: ExecutorAdapter + ApplyBackend> SystemContract<Adapter>

fn after_block_hook(&self, adapter: &mut Adapter) {
let block_number = adapter.block_number();
if block_number.is_zero() {
return;
}

let root = CURRENT_METADATA_ROOT.with(|r| *r.borrow());

let mut store = MetadataStore::new(root).unwrap();
Expand All @@ -134,6 +130,10 @@ impl<Adapter: ExecutorAdapter + ApplyBackend> SystemContract<Adapter>

HARDFORK_INFO.swap(Arc::new(hardfork));

if block_number.is_zero() {
return;
}

if let Err(e) = store.update_propose_count(block_number.as_u64(), &adapter.origin()) {
panic!("Update propose count at {:?} failed: {:?}", block_number, e)
}
Expand Down
6 changes: 3 additions & 3 deletions core/run/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,23 +36,23 @@ const TESTCASES: &[TestCase] = &[
chain_name: "single_node",
config_file: "config.toml",
chain_spec_file: "specs/single_node/chain-spec.toml",
input_genesis_hash: "0x2043f690fc6e086c6940a083072a82dee16c18a4c4afaf6f4e1c7a585fae2543",
input_genesis_hash: "0x800456ba35af2363e015d780521421ba263767d9089be95816f6f077aa48547a",
genesis_state_root: "0x601bd874d41eb9adb32021ee3ab934e0481065c58abfe7e757e33fb01be18dd5",
genesis_receipts_root: "0x8544b530238201f1620b139861a6841040b37f78f8bdae8736ef5cec474e979b",
},
TestCase {
chain_name: "multi_nodes",
config_file: "nodes/node_1.toml",
chain_spec_file: "specs/multi_nodes/chain-spec.toml",
input_genesis_hash: "0x5e5c47725bb1face59965a326b1d69e1ada1892da2e2f53c4520ed5da3d88d59",
input_genesis_hash: "0x9ef66e89ca4a36f98ea7ddc177dc37aae5b795c27c2e3e1fdf0a52ee1f439dc1",
genesis_state_root: "0xc36f75519a047fec6a34c7be5dfca783a40eafa0d7418ad7b3ba99ad9c2dc655",
genesis_receipts_root: "0x8544b530238201f1620b139861a6841040b37f78f8bdae8736ef5cec474e979b",
},
TestCase {
chain_name: "multi_nodes_short_epoch_len",
config_file: "nodes/node_1.toml",
chain_spec_file: "specs/multi_nodes_short_epoch_len/chain-spec.toml",
input_genesis_hash: "0x2043f690fc6e086c6940a083072a82dee16c18a4c4afaf6f4e1c7a585fae2543",
input_genesis_hash: "0x800456ba35af2363e015d780521421ba263767d9089be95816f6f077aa48547a",
genesis_state_root: "0x42886558baab8a3c310d5a8313398e5f353cc4f8192838b578c857a329e9bb65",
genesis_receipts_root: "0x8544b530238201f1620b139861a6841040b37f78f8bdae8736ef5cec474e979b",
},
Expand Down
2 changes: 1 addition & 1 deletion devtools/chain/specs/multi_nodes/chain-spec.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

[genesis]
timestamp = 1680249207
extra_data = []
extra_data = { block_number = 0, hardforks = [] }
base_fee_per_gas = "0x539"
chain_id = 2022
# A JSON file which includes all transactions in the genesis block.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

[genesis]
timestamp = 1679656015
extra_data = []
extra_data = { block_number = 0, hardforks = [] }
base_fee_per_gas = "0x539"
chain_id = 2022
# A JSON file which includes all transactions in the genesis block.
Expand Down
2 changes: 1 addition & 1 deletion devtools/chain/specs/single_node/chain-spec.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

[genesis]
timestamp = 1679656015
extra_data = []
extra_data = { block_number = 0, hardforks = [] }
base_fee_per_gas = "0x539"
chain_id = 2022
# A JSON file which includes all transactions in the genesis block.
Expand Down

0 comments on commit 5ea80d6

Please sign in to comment.