diff --git a/common/config-parser/src/types/spec.rs b/common/config-parser/src/types/spec.rs index 17e4f0422..9d129e148 100644 --- a/common/config-parser/src/types/spec.rs +++ b/common/config-parser/src/types/spec.rs @@ -9,10 +9,9 @@ use strum_macros::EnumIter; use common_crypto::Secp256k1RecoverablePrivateKey; use protocol::{ - codec::{decode_256bits_key, deserialize_address, ProtocolCodec}, + codec::{decode_256bits_key, deserialize_address}, types::{ - ExtraData, HardforkInfoInner, Header, Key256Bits, Metadata, H160, H256, RLP_EMPTY_LIST, - RLP_NULL, U256, + HardforkInfoInner, Header, Key256Bits, Metadata, H160, H256, RLP_EMPTY_LIST, RLP_NULL, U256, }, }; @@ -216,16 +215,10 @@ impl Genesis { } pub fn generate_hardfork_info(&self) -> HardforkInfoInner { - let mut info = Into::::into(HardforkInput { + Into::::into(HardforkInput { hardforks: self.hardforks.clone(), block_number: 0, - }); - - if info.flags.is_zero() { - info.flags = H256::from_low_u64_be(HardforkName::all().to_be()); - } - - info + }) } } @@ -243,12 +236,24 @@ pub struct HardforkInput { impl From for HardforkInfoInner { fn from(value: HardforkInput) -> Self { - let flags = { - let r = value.hardforks.into_iter().fold(0, |acc, s| acc | s as u64); + let convert_fn = |hardforks: Vec| -> H256 { + let r = hardforks.into_iter().fold(0, |acc, s| acc | s as u64); H256::from_low_u64_be(r.to_be()) }; + let flags = if value.hardforks.is_empty() { + H256::from_low_u64_be(HardforkName::all().to_be()) + } else if value.hardforks.len() == 1 { + if value.hardforks[0] == HardforkName::None { + H256::zero() + } else { + convert_fn(value.hardforks) + } + } else { + convert_fn(value.hardforks) + }; + HardforkInfoInner { block_number: value.block_number, flags, @@ -264,6 +269,6 @@ pub enum HardforkName { impl HardforkName { pub fn all() -> u64 { - HardforkName::None as u64 | HardforkName::Andromeda as u64 + HardforkName::Andromeda as u64 } } diff --git a/core/executor/src/system_contract/metadata/store.rs b/core/executor/src/system_contract/metadata/store.rs index d3d61c182..f8cbaef4b 100644 --- a/core/executor/src/system_contract/metadata/store.rs +++ b/core/executor/src/system_contract/metadata/store.rs @@ -18,14 +18,15 @@ use crate::{adapter::RocksTrieDB, MPTTrie, CURRENT_METADATA_ROOT}; /// The metadata store does not follow the storage layout of EVM smart contract. /// It use MPT called Metadata MPT with the following layout: -/// | key | value | -/// | -------------------- | ------------------------ | -/// | EPOCH_SEGMENT_KEY | `EpochSegment.encode()` | -/// | CKB_RELATED_INFO_KEY | `CkbRelatedInfo.encode()`| -/// | HARDFORK_KEY | `HardforkInfo.encode()` | -/// | epoch_0.be_bytes() | `Metadata.encode()` | -/// | epoch_1.be_bytes() | `Metadata.encode()` | -/// | ... | ... | +/// | key | value | +/// | -------------------- | ------------------------------------ | +/// | EPOCH_SEGMENT_KEY | `EpochSegment.encode()` | +/// | CKB_RELATED_INFO_KEY | `CkbRelatedInfo.encode()` | +/// | HARDFORK_KEY | `HardforkInfo.encode()` | +/// | epoch_0.be_bytes() | `Metadata.encode()` | +/// | epoch_1.be_bytes() | `Metadata.encode()` | +/// | CONSENSUS_CONFIG | `version + ConsensesConfig.encode()` | +/// | ... | ... | /// /// All these data are stored in a the `c9` column family of RocksDB, and the /// root of the Metadata MPT is stored in the storage MPT of the metadata diff --git a/core/executor/src/tests/system_script/metadata.rs b/core/executor/src/tests/system_script/metadata.rs index fe4f2f408..91d9ba91b 100644 --- a/core/executor/src/tests/system_script/metadata.rs +++ b/core/executor/src/tests/system_script/metadata.rs @@ -206,3 +206,61 @@ fn prepare_validator() -> ValidatorExtend { vote_weight: 1u32, } } + +// #[tokio::test] +// async fn update_consensus_config() { +// let config: +// crate::system_contract::metadata::metadata_abi::ConsensusConfig = +// ConsensusConfig { gas_limit: 0x3e7fffffc18, +// interval: 0xbb8, +// propose_ratio: 0xf, +// prevote_ratio: 0xa, +// precommit_ratio: 0xa, +// brake_ratio: 0xa, +// tx_num_limit: 0x4e20, +// max_tx_size: 0x186a0000, +// max_contract_limit: 0x8000u64, +// } +// .into(); + +// let tx_data = +// crate::system_contract::metadata::metadata_abi::UpdateConsensusConfigCall { config } +// .encode(); + +// send_eth_tx("http://127.0.0.1:8000", tx_data, METADATA_CONTRACT_ADDRESS).await +// } +// use ethers::prelude::*; +// use ethers::signers::{LocalWallet, Signer}; +// use ethers::types::transaction::eip2718::TypedTransaction::Legacy; +// use ethers::types::{Address, TransactionRequest}; + +// const ADDRESS: &str = "0x8ab0CF264DF99D83525e9E11c7e4db01558AE1b1"; +// const PRIVATE_KEY: &str = +// "37aa0f893d05914a4def0460c0a984d3611546cfb26924d7a7ca6e0db9950a2d"; pub async +// fn send_eth_tx(axon_url: &str, data: Vec, to: Address) { let provider +// = Provider::::try_from(axon_url).unwrap(); + +// let from: Address = ADDRESS.parse().unwrap(); +// let nonce = provider.get_transaction_count(from, None).await.unwrap(); + +// let transaction_request = TransactionRequest::new() +// .chain_id(0x41786f6e) +// .to(to) +// .data(data) +// .from(from) +// .gas_price(1) +// .gas(21000) +// .nonce(nonce); + +// let wallet = LocalWallet::from_str(PRIVATE_KEY).expect("failed to create +// wallet"); let tx = Legacy(transaction_request); +// let signature: Signature = wallet.sign_transaction(&tx).await.unwrap(); + +// provider +// .send_raw_transaction(tx.rlp_signed(&signature)) +// .await +// .unwrap() +// .await +// .unwrap() +// .expect("failed to send eth tx"); +// } diff --git a/core/run/src/tests.rs b/core/run/src/tests.rs index 6901bd866..1447d0485 100644 --- a/core/run/src/tests.rs +++ b/core/run/src/tests.rs @@ -27,13 +27,10 @@ use protocol::{ codec::{hex_decode, ProtocolCodec as _}, tokio, trie::{MemoryDB, PatriciaTrie, Trie as _}, -<<<<<<< HEAD types::{ - Bloom, BloomInput, Header, Metadata, Proposal, RichBlock, H256, RLP_EMPTY_LIST, RLP_NULL, + Bloom, BloomInput, HardforkInfo, HardforkInfoInner, Header, Metadata, Proposal, RichBlock, + H256, RLP_EMPTY_LIST, RLP_NULL, }, -======= - types::{HardforkInfo, HardforkInfoInner, Header, Metadata, Proposal, RichBlock, H256}, ->>>>>>> 80c12a3 (feat: first hardfork with contract limit) }; use crate::{components::chain_spec::ChainSpecExt as _, execute_genesis, DatabaseGroup}; @@ -55,24 +52,24 @@ const TESTCASES: &[TestCase] = &[ config_file: "config.toml", chain_spec_file: "specs/single_node/chain-spec.toml", key_file: "debug.key", - input_genesis_hash: "0xe3a40f0115fbf101520ceea1ce7103a73cb46554187ac7ed67f3522103e06d99", - genesis_state_root: "0x2f1e8e50d5ab97af96fdb5d6de8e691e5bb80f46f2c98c4133d265bd8b60de61", + input_genesis_hash: "0x21e10c85388a1606ef0b8d258d95d4403faefc4c9672623c524b5070fae92ca5", + genesis_state_root: "0xf939bf06a1d818aee1555a11f06b20c215003c056e26403ecb1cb4b15958f6ac", }, TestCase { chain_name: "multi_nodes", config_file: "nodes/node_1.toml", chain_spec_file: "specs/multi_nodes/chain-spec.toml", key_file: "debug.key", - input_genesis_hash: "0x1b4cf78373961dabcba5d4a9402c924fc4fecdd9ce367239f02c8971a052f3b5", - genesis_state_root: "0xf684cbec490eb5b8a07b80f369f3bf87f05ec73494b869111010a6ad6fa89894", + input_genesis_hash: "0x12da11f848f789ff8c7b6d75faf0740899588d17f0f3c3b9ffa53df30f7d22ce", + genesis_state_root: "0xe911923f522acff78d3f08b851aca86870bda9f24f1722f1d97518ff3b8697c6", }, 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", key_file: "debug.key", - input_genesis_hash: "0xd930632a7565acfc149c1d896d79910608768de5b936fdb34cc47c9b2296dd2a", - genesis_state_root: "0xa5e1e7ac3e03f7dc26cc93ab69c0ec49e591cbdaa7694c75682745c40bfca468", + input_genesis_hash: "0x6519f88a4a02d6a04806f837133bab7a44d64e78efff5f599d07a23d9a2cf9a2", + genesis_state_root: "0x1fc51ffe40ba5f03523370e8a1baba3046574db9abf769f21e42458f50047a2c", }, ];