Skip to content

Commit

Permalink
feat: support set hardfork from command line
Browse files Browse the repository at this point in the history
  • Loading branch information
driftluo committed Sep 13, 2023
1 parent 6161651 commit 131563a
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 21 deletions.
36 changes: 24 additions & 12 deletions common/config-parser/src/types/spec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ pub struct ChainSpec {
#[derive(Clone, Debug, Deserialize)]
pub struct Genesis {
pub timestamp: u64,
pub extra_data: HarforkInput,
pub extra_data: HardforkInput,
pub base_fee_per_gas: U256,
pub chain_id: u64,

Expand Down Expand Up @@ -261,24 +261,36 @@ impl Genesis {
timestamp: self.timestamp,
// 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(),
extra_data: if self.extra_data.block_number != 0 {
Default::default()
} else {
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>,
use clap::ValueEnum;

#[derive(Clone, Debug, Deserialize, Args)]
pub struct HardforkInput {
#[arg(
long = "hardfork-start-number",
required = false,
requires = "hardforks"
)]
pub block_number: u64,
#[arg(long, requires = "block_number")]
pub hardforks: Vec<HardforkType>,
}

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

Expand All @@ -292,7 +304,7 @@ impl From<HarforkInput> for HardforkInfoInner {
}
}

#[derive(Clone, Debug, Deserialize, Copy)]
enum HardforkType {
#[derive(Clone, Debug, Deserialize, Copy, ValueEnum)]
pub enum HardforkType {
None = 0b0,
}
10 changes: 7 additions & 3 deletions core/cli/src/args/run.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use clap::Parser;

use common_config_parser::types::Config;
use common_config_parser::types::{spec::HardforkInput, Config};
use common_version::Version;
use core_run::KeyProvider;

Expand All @@ -19,6 +19,9 @@ pub struct RunArgs {
help = "File path of client configurations."
)]
pub config: Config,

#[command(flatten)]
hardforks: Option<HardforkInput>,
}

impl RunArgs {
Expand All @@ -28,7 +31,7 @@ impl RunArgs {
kernel_version: Version,
key_provider: Option<K>,
) -> Result<()> {
let Self { config } = self;
let Self { config, hardforks } = self;

utils::check_version(
&config.data_path_for_version(),
Expand All @@ -38,6 +41,7 @@ impl RunArgs {
utils::register_log(&config);

let version = application_version.to_string();
core_run::run(version, config, key_provider).map_err(Error::Running)
core_run::run(version, config, key_provider, hardforks.map(Into::into))
.map_err(Error::Running)
}
}
26 changes: 22 additions & 4 deletions core/run/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ use protocol::traits::{
Context, Executor, Gossip, MemPool, Network, NodeInfo, PeerTrust, ReadOnlyStorage, Rpc, Storage,
};
use protocol::types::{
Block, ExecResp, Header, Metadata, Proposal, RichBlock, SignedTransaction, Validator,
ValidatorExtend,
Block, ExecResp, HardforkInfoInner, Header, Metadata, Proposal, RichBlock, SignedTransaction,
Validator, ValidatorExtend,
};
use protocol::{lazy::CHAIN_ID, trie::DB as TrieDB, ProtocolResult};

Expand Down Expand Up @@ -87,6 +87,7 @@ pub fn run<K: KeyProvider>(
version: String,
config: Config,
key_provider: Option<K>,
hardfork_info: Option<HardforkInfoInner>,
) -> ProtocolResult<()> {
let path_rocksdb = config.data_path_for_rocksdb();
if !path_rocksdb.exists() {
Expand All @@ -110,7 +111,7 @@ pub fn run<K: KeyProvider>(
config.executor.triedb_cache_size,
)?;
log::info!("Start all services.");
start(version, config, key_provider, &db_group).await
start(version, config, key_provider, &db_group, hardfork_info).await
})?;
rt.shutdown_timeout(std::time::Duration::from_secs(1));

Expand All @@ -122,6 +123,7 @@ async fn start<K: KeyProvider>(
config: Config,
key_provider: Option<K>,
db_group: &DatabaseGroup,
hardfork_info: Option<HardforkInfoInner>,
) -> ProtocolResult<()> {
let storage = db_group.storage();
let trie_db = db_group.trie_db();
Expand All @@ -145,6 +147,16 @@ async fn start<K: KeyProvider>(
let current_block = storage.get_latest_block(Context::new()).await?;
let current_state_root = current_block.header.state_root;

if let Some(hardfork_start_number) = hardfork_info.as_ref().map(|v| v.block_number) {
if current_block.header.number >= hardfork_start_number {
return Err(MainError::Other(format!(
"Hardfork start block number {} less than current number {}",
hardfork_start_number, current_block.header.number
))
.into());
}
}

log::info!("At block number {}", current_block.header.number + 1);

// Init network
Expand Down Expand Up @@ -235,7 +247,13 @@ async fn start<K: KeyProvider>(
let overlord_consensus = {
let consensus_wal_path = config.data_path_for_consensus_wal();
let node_info = Secp256k1PrivateKey::try_from(config.privkey.as_ref())
.map(|privkey| NodeInfo::new(current_block.header.chain_id, privkey.pub_key()))
.map(|privkey| {
NodeInfo::new(
current_block.header.chain_id,
privkey.pub_key(),
hardfork_info,
)
})
.map_err(MainError::Crypto)?;
let overlord_consensus = OverlordConsensus::new(
status_agent.clone(),
Expand Down
8 changes: 6 additions & 2 deletions protocol/src/traits/consensus.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,17 @@ pub struct NodeInfo {
}

impl NodeInfo {
pub fn new(chain_id: u64, pubkey: Secp256k1PublicKey) -> Self {
pub fn new(
chain_id: u64,
pubkey: Secp256k1PublicKey,
hardfork_info: Option<HardforkInfoInner>,
) -> Self {
let address = Address::from_pubkey(&pubkey);
Self {
chain_id,
self_pub_key: pubkey,
self_address: address,
hardfork_proposals: Default::default(),
hardfork_proposals: Arc::new(RwLock::new(hardfork_info)),
}
}
}
Expand Down

0 comments on commit 131563a

Please sign in to comment.