From 344fe858141359453205add788f3022618394a57 Mon Sep 17 00:00:00 2001 From: driftluo Date: Tue, 26 Sep 2023 16:08:30 +0800 Subject: [PATCH] feat: first hardfork with contract limit --- .../metadata/contracts/metadata.sol | 3 +- common/config-parser/src/types/spec.rs | 25 +++-- .../metadata/abi/metadata_abi.json | 12 +-- .../metadata/abi/metadata_abi.rs | 20 ++-- .../src/system_contract/metadata/abi/mod.rs | 36 +++---- .../src/system_contract/metadata/mod.rs | 3 +- .../src/system_contract/metadata/store.rs | 101 +++++++++++++++--- .../src/tests/system_script/metadata.rs | 18 ++-- core/run/src/tests.rs | 12 +-- protocol/src/types/block.rs | 2 +- protocol/src/types/primitive.rs | 81 +++++++++++++- 11 files changed, 236 insertions(+), 77 deletions(-) diff --git a/builtin-contract/metadata/contracts/metadata.sol b/builtin-contract/metadata/contracts/metadata.sol index f9795bb8d..2af827253 100644 --- a/builtin-contract/metadata/contracts/metadata.sol +++ b/builtin-contract/metadata/contracts/metadata.sol @@ -41,8 +41,8 @@ contract MetadataManager { uint64 tx_num_limit; uint64 max_tx_size; uint64 gas_limit; - uint64 gas_price; uint64 interval; + uint64 max_contract_limit; } struct CkbRelatedInfo { @@ -91,7 +91,6 @@ contract MetadataManager { target.version = metadata.version; target.epoch = metadata.epoch; target.consensus_config.gas_limit = metadata.consensus_config.gas_limit; - target.consensus_config.gas_price = metadata.consensus_config.gas_price; target.consensus_config.interval = metadata.consensus_config.interval; target.consensus_config.propose_ratio = metadata .consensus_config diff --git a/common/config-parser/src/types/spec.rs b/common/config-parser/src/types/spec.rs index e304940c4..833e6ffcf 100644 --- a/common/config-parser/src/types/spec.rs +++ b/common/config-parser/src/types/spec.rs @@ -264,12 +264,18 @@ impl Genesis { // genesis extra_data: { vec![ExtraData { - inner: Into::::into(HardforkInput { - hardforks: self.hardforks.clone(), - block_number: 0, - }) - .encode() - .unwrap(), + inner: { + let mut info = 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.encode().unwrap() + }, }] }, base_fee_per_gas: self.base_fee_per_gas, @@ -309,4 +315,11 @@ impl From for HardforkInfoInner { #[derive(Clone, Debug, Serialize, Deserialize, Copy, ValueEnum, EnumIter, PartialEq, Eq, Hash)] pub enum HardforkName { None = 0b0, + Andromeda = 0b1, +} + +impl HardforkName { + pub fn all() -> u64 { + HardforkName::None as u64 | HardforkName::Andromeda as u64 + } } diff --git a/core/executor/src/system_contract/metadata/abi/metadata_abi.json b/core/executor/src/system_contract/metadata/abi/metadata_abi.json index d2b18b5a1..502e866b8 100644 --- a/core/executor/src/system_contract/metadata/abi/metadata_abi.json +++ b/core/executor/src/system_contract/metadata/abi/metadata_abi.json @@ -113,12 +113,12 @@ }, { "internalType": "uint64", - "name": "gas_price", + "name": "interval", "type": "uint64" }, { "internalType": "uint64", - "name": "interval", + "name": "max_contract_limit", "type": "uint64" } ], @@ -259,12 +259,12 @@ }, { "internalType": "uint64", - "name": "gas_price", + "name": "interval", "type": "uint64" }, { "internalType": "uint64", - "name": "interval", + "name": "max_contract_limit", "type": "uint64" } ], @@ -322,12 +322,12 @@ }, { "internalType": "uint64", - "name": "gas_price", + "name": "interval", "type": "uint64" }, { "internalType": "uint64", - "name": "interval", + "name": "max_contract_limit", "type": "uint64" } ], diff --git a/core/executor/src/system_contract/metadata/abi/metadata_abi.rs b/core/executor/src/system_contract/metadata/abi/metadata_abi.rs index 8dedf3fea..0722a4a7b 100644 --- a/core/executor/src/system_contract/metadata/abi/metadata_abi.rs +++ b/core/executor/src/system_contract/metadata/abi/metadata_abi.rs @@ -11,7 +11,7 @@ pub use metadata_contract::*; )] pub mod metadata_contract { #[rustfmt::skip] - const __ABI: &str = "[\n {\n \"inputs\": [\n {\n \"components\": [\n {\n \"components\": [\n {\n \"internalType\": \"uint64\",\n \"name\": \"start\",\n \"type\": \"uint64\"\n },\n {\n \"internalType\": \"uint64\",\n \"name\": \"end\",\n \"type\": \"uint64\"\n }\n ],\n \"internalType\": \"struct MetadataManager.MetadataVersion\",\n \"name\": \"version\",\n \"type\": \"tuple\"\n },\n {\n \"internalType\": \"uint64\",\n \"name\": \"epoch\",\n \"type\": \"uint64\"\n },\n {\n \"components\": [\n {\n \"internalType\": \"bytes\",\n \"name\": \"bls_pub_key\",\n \"type\": \"bytes\"\n },\n {\n \"internalType\": \"bytes\",\n \"name\": \"pub_key\",\n \"type\": \"bytes\"\n },\n {\n \"internalType\": \"address\",\n \"name\": \"address_\",\n \"type\": \"address\"\n },\n {\n \"internalType\": \"uint32\",\n \"name\": \"propose_weight\",\n \"type\": \"uint32\"\n },\n {\n \"internalType\": \"uint32\",\n \"name\": \"vote_weight\",\n \"type\": \"uint32\"\n }\n ],\n \"internalType\": \"struct MetadataManager.ValidatorExtend[]\",\n \"name\": \"verifier_list\",\n \"type\": \"tuple[]\"\n },\n {\n \"components\": [\n {\n \"internalType\": \"address\",\n \"name\": \"address_\",\n \"type\": \"address\"\n },\n {\n \"internalType\": \"uint64\",\n \"name\": \"count\",\n \"type\": \"uint64\"\n }\n ],\n \"internalType\": \"struct MetadataManager.ProposeCount[]\",\n \"name\": \"propose_counter\",\n \"type\": \"tuple[]\"\n },\n {\n \"components\": [\n {\n \"internalType\": \"uint64\",\n \"name\": \"propose_ratio\",\n \"type\": \"uint64\"\n },\n {\n \"internalType\": \"uint64\",\n \"name\": \"prevote_ratio\",\n \"type\": \"uint64\"\n },\n {\n \"internalType\": \"uint64\",\n \"name\": \"precommit_ratio\",\n \"type\": \"uint64\"\n },\n {\n \"internalType\": \"uint64\",\n \"name\": \"brake_ratio\",\n \"type\": \"uint64\"\n },\n {\n \"internalType\": \"uint64\",\n \"name\": \"tx_num_limit\",\n \"type\": \"uint64\"\n },\n {\n \"internalType\": \"uint64\",\n \"name\": \"max_tx_size\",\n \"type\": \"uint64\"\n },\n {\n \"internalType\": \"uint64\",\n \"name\": \"gas_limit\",\n \"type\": \"uint64\"\n },\n {\n \"internalType\": \"uint64\",\n \"name\": \"gas_price\",\n \"type\": \"uint64\"\n },\n {\n \"internalType\": \"uint64\",\n \"name\": \"interval\",\n \"type\": \"uint64\"\n }\n ],\n \"internalType\": \"struct MetadataManager.ConsensusConfig\",\n \"name\": \"consensus_config\",\n \"type\": \"tuple\"\n }\n ],\n \"internalType\": \"struct MetadataManager.Metadata\",\n \"name\": \"metadata\",\n \"type\": \"tuple\"\n }\n ],\n \"name\": \"appendMetadata\",\n \"outputs\": [],\n \"stateMutability\": \"nonpayable\",\n \"type\": \"function\"\n },\n {\n \"inputs\": [\n {\n \"internalType\": \"uint64\",\n \"name\": \"epoch\",\n \"type\": \"uint64\"\n }\n ],\n \"name\": \"getMetadata\",\n \"outputs\": [\n {\n \"components\": [\n {\n \"components\": [\n {\n \"internalType\": \"uint64\",\n \"name\": \"start\",\n \"type\": \"uint64\"\n },\n {\n \"internalType\": \"uint64\",\n \"name\": \"end\",\n \"type\": \"uint64\"\n }\n ],\n \"internalType\": \"struct MetadataManager.MetadataVersion\",\n \"name\": \"version\",\n \"type\": \"tuple\"\n },\n {\n \"internalType\": \"uint64\",\n \"name\": \"epoch\",\n \"type\": \"uint64\"\n },\n {\n \"components\": [\n {\n \"internalType\": \"bytes\",\n \"name\": \"bls_pub_key\",\n \"type\": \"bytes\"\n },\n {\n \"internalType\": \"bytes\",\n \"name\": \"pub_key\",\n \"type\": \"bytes\"\n },\n {\n \"internalType\": \"address\",\n \"name\": \"address_\",\n \"type\": \"address\"\n },\n {\n \"internalType\": \"uint32\",\n \"name\": \"propose_weight\",\n \"type\": \"uint32\"\n },\n {\n \"internalType\": \"uint32\",\n \"name\": \"vote_weight\",\n \"type\": \"uint32\"\n }\n ],\n \"internalType\": \"struct MetadataManager.ValidatorExtend[]\",\n \"name\": \"verifier_list\",\n \"type\": \"tuple[]\"\n },\n {\n \"components\": [\n {\n \"internalType\": \"address\",\n \"name\": \"address_\",\n \"type\": \"address\"\n },\n {\n \"internalType\": \"uint64\",\n \"name\": \"count\",\n \"type\": \"uint64\"\n }\n ],\n \"internalType\": \"struct MetadataManager.ProposeCount[]\",\n \"name\": \"propose_counter\",\n \"type\": \"tuple[]\"\n },\n {\n \"components\": [\n {\n \"internalType\": \"uint64\",\n \"name\": \"propose_ratio\",\n \"type\": \"uint64\"\n },\n {\n \"internalType\": \"uint64\",\n \"name\": \"prevote_ratio\",\n \"type\": \"uint64\"\n },\n {\n \"internalType\": \"uint64\",\n \"name\": \"precommit_ratio\",\n \"type\": \"uint64\"\n },\n {\n \"internalType\": \"uint64\",\n \"name\": \"brake_ratio\",\n \"type\": \"uint64\"\n },\n {\n \"internalType\": \"uint64\",\n \"name\": \"tx_num_limit\",\n \"type\": \"uint64\"\n },\n {\n \"internalType\": \"uint64\",\n \"name\": \"max_tx_size\",\n \"type\": \"uint64\"\n },\n {\n \"internalType\": \"uint64\",\n \"name\": \"gas_limit\",\n \"type\": \"uint64\"\n },\n {\n \"internalType\": \"uint64\",\n \"name\": \"gas_price\",\n \"type\": \"uint64\"\n },\n {\n \"internalType\": \"uint64\",\n \"name\": \"interval\",\n \"type\": \"uint64\"\n }\n ],\n \"internalType\": \"struct MetadataManager.ConsensusConfig\",\n \"name\": \"consensus_config\",\n \"type\": \"tuple\"\n }\n ],\n \"internalType\": \"struct MetadataManager.Metadata\",\n \"name\": \"\",\n \"type\": \"tuple\"\n }\n ],\n \"stateMutability\": \"view\",\n \"type\": \"function\"\n },\n {\n \"inputs\": [\n {\n \"components\": [\n {\n \"internalType\": \"uint64\",\n \"name\": \"propose_ratio\",\n \"type\": \"uint64\"\n },\n {\n \"internalType\": \"uint64\",\n \"name\": \"prevote_ratio\",\n \"type\": \"uint64\"\n },\n {\n \"internalType\": \"uint64\",\n \"name\": \"precommit_ratio\",\n \"type\": \"uint64\"\n },\n {\n \"internalType\": \"uint64\",\n \"name\": \"brake_ratio\",\n \"type\": \"uint64\"\n },\n {\n \"internalType\": \"uint64\",\n \"name\": \"tx_num_limit\",\n \"type\": \"uint64\"\n },\n {\n \"internalType\": \"uint64\",\n \"name\": \"max_tx_size\",\n \"type\": \"uint64\"\n },\n {\n \"internalType\": \"uint64\",\n \"name\": \"gas_limit\",\n \"type\": \"uint64\"\n },\n {\n \"internalType\": \"uint64\",\n \"name\": \"gas_price\",\n \"type\": \"uint64\"\n },\n {\n \"internalType\": \"uint64\",\n \"name\": \"interval\",\n \"type\": \"uint64\"\n }\n ],\n \"internalType\": \"struct MetadataManager.ConsensusConfig\",\n \"name\": \"config\",\n \"type\": \"tuple\"\n }\n ],\n \"name\": \"updateConsensusConfig\",\n \"outputs\": [],\n \"stateMutability\": \"view\",\n \"type\": \"function\"\n },\n {\n \"inputs\": [\n {\n \"components\": [\n {\n \"internalType\": \"bytes32\",\n \"name\": \"metadata_type_id\",\n \"type\": \"bytes32\"\n },\n {\n \"internalType\": \"bytes32\",\n \"name\": \"checkpoint_type_id\",\n \"type\": \"bytes32\"\n },\n {\n \"internalType\": \"bytes32\",\n \"name\": \"xudt_args\",\n \"type\": \"bytes32\"\n },\n {\n \"internalType\": \"bytes32\",\n \"name\": \"stake_smt_type_id\",\n \"type\": \"bytes32\"\n },\n {\n \"internalType\": \"bytes32\",\n \"name\": \"delegate_smt_type_id\",\n \"type\": \"bytes32\"\n },\n {\n \"internalType\": \"bytes32\",\n \"name\": \"reward_smt_type_id\",\n \"type\": \"bytes32\"\n }\n ],\n \"internalType\": \"struct MetadataManager.CkbRelatedInfo\",\n \"name\": \"ckb_related_info\",\n \"type\": \"tuple\"\n }\n ],\n \"name\": \"setCkbRelatedInfo\",\n \"outputs\": [],\n \"stateMutability\": \"view\",\n \"type\": \"function\"\n }\n]\n"; + const __ABI: &str = "[\n {\n \"inputs\": [\n {\n \"components\": [\n {\n \"components\": [\n {\n \"internalType\": \"uint64\",\n \"name\": \"start\",\n \"type\": \"uint64\"\n },\n {\n \"internalType\": \"uint64\",\n \"name\": \"end\",\n \"type\": \"uint64\"\n }\n ],\n \"internalType\": \"struct MetadataManager.MetadataVersion\",\n \"name\": \"version\",\n \"type\": \"tuple\"\n },\n {\n \"internalType\": \"uint64\",\n \"name\": \"epoch\",\n \"type\": \"uint64\"\n },\n {\n \"components\": [\n {\n \"internalType\": \"bytes\",\n \"name\": \"bls_pub_key\",\n \"type\": \"bytes\"\n },\n {\n \"internalType\": \"bytes\",\n \"name\": \"pub_key\",\n \"type\": \"bytes\"\n },\n {\n \"internalType\": \"address\",\n \"name\": \"address_\",\n \"type\": \"address\"\n },\n {\n \"internalType\": \"uint32\",\n \"name\": \"propose_weight\",\n \"type\": \"uint32\"\n },\n {\n \"internalType\": \"uint32\",\n \"name\": \"vote_weight\",\n \"type\": \"uint32\"\n }\n ],\n \"internalType\": \"struct MetadataManager.ValidatorExtend[]\",\n \"name\": \"verifier_list\",\n \"type\": \"tuple[]\"\n },\n {\n \"components\": [\n {\n \"internalType\": \"address\",\n \"name\": \"address_\",\n \"type\": \"address\"\n },\n {\n \"internalType\": \"uint64\",\n \"name\": \"count\",\n \"type\": \"uint64\"\n }\n ],\n \"internalType\": \"struct MetadataManager.ProposeCount[]\",\n \"name\": \"propose_counter\",\n \"type\": \"tuple[]\"\n },\n {\n \"components\": [\n {\n \"internalType\": \"uint64\",\n \"name\": \"propose_ratio\",\n \"type\": \"uint64\"\n },\n {\n \"internalType\": \"uint64\",\n \"name\": \"prevote_ratio\",\n \"type\": \"uint64\"\n },\n {\n \"internalType\": \"uint64\",\n \"name\": \"precommit_ratio\",\n \"type\": \"uint64\"\n },\n {\n \"internalType\": \"uint64\",\n \"name\": \"brake_ratio\",\n \"type\": \"uint64\"\n },\n {\n \"internalType\": \"uint64\",\n \"name\": \"tx_num_limit\",\n \"type\": \"uint64\"\n },\n {\n \"internalType\": \"uint64\",\n \"name\": \"max_tx_size\",\n \"type\": \"uint64\"\n },\n {\n \"internalType\": \"uint64\",\n \"name\": \"gas_limit\",\n \"type\": \"uint64\"\n },\n {\n \"internalType\": \"uint64\",\n \"name\": \"interval\",\n \"type\": \"uint64\"\n },\n {\n \"internalType\": \"uint64\",\n \"name\": \"max_contract_limit\",\n \"type\": \"uint64\"\n }\n ],\n \"internalType\": \"struct MetadataManager.ConsensusConfig\",\n \"name\": \"consensus_config\",\n \"type\": \"tuple\"\n }\n ],\n \"internalType\": \"struct MetadataManager.Metadata\",\n \"name\": \"metadata\",\n \"type\": \"tuple\"\n }\n ],\n \"name\": \"appendMetadata\",\n \"outputs\": [],\n \"stateMutability\": \"nonpayable\",\n \"type\": \"function\"\n },\n {\n \"inputs\": [\n {\n \"internalType\": \"uint64\",\n \"name\": \"epoch\",\n \"type\": \"uint64\"\n }\n ],\n \"name\": \"getMetadata\",\n \"outputs\": [\n {\n \"components\": [\n {\n \"components\": [\n {\n \"internalType\": \"uint64\",\n \"name\": \"start\",\n \"type\": \"uint64\"\n },\n {\n \"internalType\": \"uint64\",\n \"name\": \"end\",\n \"type\": \"uint64\"\n }\n ],\n \"internalType\": \"struct MetadataManager.MetadataVersion\",\n \"name\": \"version\",\n \"type\": \"tuple\"\n },\n {\n \"internalType\": \"uint64\",\n \"name\": \"epoch\",\n \"type\": \"uint64\"\n },\n {\n \"components\": [\n {\n \"internalType\": \"bytes\",\n \"name\": \"bls_pub_key\",\n \"type\": \"bytes\"\n },\n {\n \"internalType\": \"bytes\",\n \"name\": \"pub_key\",\n \"type\": \"bytes\"\n },\n {\n \"internalType\": \"address\",\n \"name\": \"address_\",\n \"type\": \"address\"\n },\n {\n \"internalType\": \"uint32\",\n \"name\": \"propose_weight\",\n \"type\": \"uint32\"\n },\n {\n \"internalType\": \"uint32\",\n \"name\": \"vote_weight\",\n \"type\": \"uint32\"\n }\n ],\n \"internalType\": \"struct MetadataManager.ValidatorExtend[]\",\n \"name\": \"verifier_list\",\n \"type\": \"tuple[]\"\n },\n {\n \"components\": [\n {\n \"internalType\": \"address\",\n \"name\": \"address_\",\n \"type\": \"address\"\n },\n {\n \"internalType\": \"uint64\",\n \"name\": \"count\",\n \"type\": \"uint64\"\n }\n ],\n \"internalType\": \"struct MetadataManager.ProposeCount[]\",\n \"name\": \"propose_counter\",\n \"type\": \"tuple[]\"\n },\n {\n \"components\": [\n {\n \"internalType\": \"uint64\",\n \"name\": \"propose_ratio\",\n \"type\": \"uint64\"\n },\n {\n \"internalType\": \"uint64\",\n \"name\": \"prevote_ratio\",\n \"type\": \"uint64\"\n },\n {\n \"internalType\": \"uint64\",\n \"name\": \"precommit_ratio\",\n \"type\": \"uint64\"\n },\n {\n \"internalType\": \"uint64\",\n \"name\": \"brake_ratio\",\n \"type\": \"uint64\"\n },\n {\n \"internalType\": \"uint64\",\n \"name\": \"tx_num_limit\",\n \"type\": \"uint64\"\n },\n {\n \"internalType\": \"uint64\",\n \"name\": \"max_tx_size\",\n \"type\": \"uint64\"\n },\n {\n \"internalType\": \"uint64\",\n \"name\": \"gas_limit\",\n \"type\": \"uint64\"\n },\n {\n \"internalType\": \"uint64\",\n \"name\": \"interval\",\n \"type\": \"uint64\"\n },\n {\n \"internalType\": \"uint64\",\n \"name\": \"max_contract_limit\",\n \"type\": \"uint64\"\n }\n ],\n \"internalType\": \"struct MetadataManager.ConsensusConfig\",\n \"name\": \"consensus_config\",\n \"type\": \"tuple\"\n }\n ],\n \"internalType\": \"struct MetadataManager.Metadata\",\n \"name\": \"\",\n \"type\": \"tuple\"\n }\n ],\n \"stateMutability\": \"view\",\n \"type\": \"function\"\n },\n {\n \"inputs\": [\n {\n \"components\": [\n {\n \"internalType\": \"uint64\",\n \"name\": \"propose_ratio\",\n \"type\": \"uint64\"\n },\n {\n \"internalType\": \"uint64\",\n \"name\": \"prevote_ratio\",\n \"type\": \"uint64\"\n },\n {\n \"internalType\": \"uint64\",\n \"name\": \"precommit_ratio\",\n \"type\": \"uint64\"\n },\n {\n \"internalType\": \"uint64\",\n \"name\": \"brake_ratio\",\n \"type\": \"uint64\"\n },\n {\n \"internalType\": \"uint64\",\n \"name\": \"tx_num_limit\",\n \"type\": \"uint64\"\n },\n {\n \"internalType\": \"uint64\",\n \"name\": \"max_tx_size\",\n \"type\": \"uint64\"\n },\n {\n \"internalType\": \"uint64\",\n \"name\": \"gas_limit\",\n \"type\": \"uint64\"\n },\n {\n \"internalType\": \"uint64\",\n \"name\": \"interval\",\n \"type\": \"uint64\"\n },\n {\n \"internalType\": \"uint64\",\n \"name\": \"max_contract_limit\",\n \"type\": \"uint64\"\n }\n ],\n \"internalType\": \"struct MetadataManager.ConsensusConfig\",\n \"name\": \"config\",\n \"type\": \"tuple\"\n }\n ],\n \"name\": \"updateConsensusConfig\",\n \"outputs\": [],\n \"stateMutability\": \"view\",\n \"type\": \"function\"\n },\n {\n \"inputs\": [\n {\n \"components\": [\n {\n \"internalType\": \"bytes32\",\n \"name\": \"metadata_type_id\",\n \"type\": \"bytes32\"\n },\n {\n \"internalType\": \"bytes32\",\n \"name\": \"checkpoint_type_id\",\n \"type\": \"bytes32\"\n },\n {\n \"internalType\": \"bytes32\",\n \"name\": \"xudt_args\",\n \"type\": \"bytes32\"\n },\n {\n \"internalType\": \"bytes32\",\n \"name\": \"stake_smt_type_id\",\n \"type\": \"bytes32\"\n },\n {\n \"internalType\": \"bytes32\",\n \"name\": \"delegate_smt_type_id\",\n \"type\": \"bytes32\"\n },\n {\n \"internalType\": \"bytes32\",\n \"name\": \"reward_smt_type_id\",\n \"type\": \"bytes32\"\n }\n ],\n \"internalType\": \"struct MetadataManager.CkbRelatedInfo\",\n \"name\": \"ckb_related_info\",\n \"type\": \"tuple\"\n }\n ],\n \"name\": \"setCkbRelatedInfo\",\n \"outputs\": [],\n \"stateMutability\": \"view\",\n \"type\": \"function\"\n }\n]\n"; /// The parsed JSON ABI of the contract. pub static METADATACONTRACT_ABI: ::ethers::contract::Lazy<::ethers::core::abi::Abi> = ::ethers::contract::Lazy::new(|| { @@ -305,15 +305,15 @@ pub mod metadata_contract { Hash, )] pub struct ConsensusConfig { - pub propose_ratio: u64, - pub prevote_ratio: u64, - pub precommit_ratio: u64, - pub brake_ratio: u64, - pub tx_num_limit: u64, - pub max_tx_size: u64, - pub gas_limit: u64, - pub gas_price: u64, - pub interval: u64, + pub propose_ratio: u64, + pub prevote_ratio: u64, + pub precommit_ratio: u64, + pub brake_ratio: u64, + pub tx_num_limit: u64, + pub max_tx_size: u64, + pub gas_limit: u64, + pub interval: u64, + pub max_contract_limit: u64, } /// `Metadata((uint64,uint64),uint64,(bytes,bytes,address,uint32,uint32)[], /// (address,uint64)[],(uint64,uint64,uint64,uint64,uint64,uint64,uint64, diff --git a/core/executor/src/system_contract/metadata/abi/mod.rs b/core/executor/src/system_contract/metadata/abi/mod.rs index 12bdb9806..c7f576d18 100644 --- a/core/executor/src/system_contract/metadata/abi/mod.rs +++ b/core/executor/src/system_contract/metadata/abi/mod.rs @@ -35,15 +35,15 @@ impl From for metadata_abi::Metadata { impl From for metadata_abi::ConsensusConfig { fn from(value: ConsensusConfig) -> Self { metadata_abi::ConsensusConfig { - propose_ratio: value.propose_ratio, - prevote_ratio: value.prevote_ratio, - precommit_ratio: value.precommit_ratio, - brake_ratio: value.brake_ratio, - tx_num_limit: value.tx_num_limit, - max_tx_size: value.max_tx_size, - gas_limit: value.gas_limit, - gas_price: value.gas_price, - interval: value.interval, + propose_ratio: value.propose_ratio, + prevote_ratio: value.prevote_ratio, + precommit_ratio: value.precommit_ratio, + brake_ratio: value.brake_ratio, + tx_num_limit: value.tx_num_limit, + max_tx_size: value.max_tx_size, + gas_limit: value.gas_limit, + interval: value.interval, + max_contract_limit: value.max_contract_limit, } } } @@ -51,15 +51,15 @@ impl From for metadata_abi::ConsensusConfig { impl From for ConsensusConfig { fn from(value: metadata_abi::ConsensusConfig) -> Self { ConsensusConfig { - propose_ratio: value.propose_ratio, - prevote_ratio: value.prevote_ratio, - precommit_ratio: value.precommit_ratio, - brake_ratio: value.brake_ratio, - tx_num_limit: value.tx_num_limit, - max_tx_size: value.max_tx_size, - gas_limit: value.gas_limit, - gas_price: value.gas_price, - interval: value.interval, + propose_ratio: value.propose_ratio, + prevote_ratio: value.prevote_ratio, + precommit_ratio: value.precommit_ratio, + brake_ratio: value.brake_ratio, + tx_num_limit: value.tx_num_limit, + max_tx_size: value.max_tx_size, + gas_limit: value.gas_limit, + interval: value.interval, + max_contract_limit: value.max_contract_limit, } } } diff --git a/core/executor/src/system_contract/metadata/mod.rs b/core/executor/src/system_contract/metadata/mod.rs index 9fc794ea8..b2fec8e79 100644 --- a/core/executor/src/system_contract/metadata/mod.rs +++ b/core/executor/src/system_contract/metadata/mod.rs @@ -32,6 +32,7 @@ const METADATA_CACHE_SIZE: NonZeroUsize = unsafe { NonZeroUsize::new_unchecked(1 lazy_static::lazy_static! { static ref EPOCH_SEGMENT_KEY: H256 = Hasher::digest("epoch_segment"); static ref CKB_RELATED_INFO_KEY: H256 = Hasher::digest("ckb_related_info"); + static ref CONSENSUS_CONFIG: H256 = Hasher::digest("consensus_config"); static ref HARDFORK_KEY: H256 = Hasher::digest("hardfork"); static ref HARDFORK_INFO: ArcSwap = ArcSwap::new(Arc::new(H256::zero())); static ref METADATA_CACHE: RwLock> = RwLock::new(LruCache::new(METADATA_CACHE_SIZE)); @@ -97,7 +98,7 @@ impl SystemContract } metadata_abi::MetadataContractCalls::UpdateConsensusConfig(c) => { exec_try!( - store.update_consensus_config(c.config), + store.update_consensus_config(c.config.into()), gas_limit, "[metadata] update consensus config" ); diff --git a/core/executor/src/system_contract/metadata/store.rs b/core/executor/src/system_contract/metadata/store.rs index f8dac5c32..743f60511 100644 --- a/core/executor/src/system_contract/metadata/store.rs +++ b/core/executor/src/system_contract/metadata/store.rs @@ -1,18 +1,21 @@ use std::collections::BTreeMap; use std::sync::Arc; +use common_config_parser::types::spec::HardforkName; use protocol::trie::Trie as _; -use protocol::types::{CkbRelatedInfo, HardforkInfo, HardforkInfoInner, Metadata, H160, H256}; +use protocol::types::{ + CkbRelatedInfo, ConsensusConfig, ConsensusConfigV0, HardforkInfo, HardforkInfoInner, Metadata, + MetadataInner, H160, H256, +}; use protocol::{codec::ProtocolCodec, ProtocolResult}; use crate::system_contract::metadata::{ - segment::EpochSegment, CKB_RELATED_INFO_KEY, EPOCH_SEGMENT_KEY, HARDFORK_KEY, + segment::EpochSegment, CKB_RELATED_INFO_KEY, CONSENSUS_CONFIG, EPOCH_SEGMENT_KEY, + HARDFORK_INFO, HARDFORK_KEY, }; use crate::system_contract::{error::SystemScriptError, METADATA_DB}; use crate::{adapter::RocksTrieDB, MPTTrie, CURRENT_METADATA_ROOT}; -use super::metadata_abi::ConsensusConfig; - pub struct MetadataStore { pub trie: MPTTrie, } @@ -86,13 +89,18 @@ impl MetadataStore { epoch_segment.append_endpoint(metadata.version.end)?; + let (inner, config) = metadata.into_part(); + let current_hardfork = **HARDFORK_INFO.load(); + self.trie.insert( EPOCH_SEGMENT_KEY.as_bytes().to_vec(), epoch_segment.as_bytes(), )?; + self.trie + .insert(inner.epoch.to_be_bytes().to_vec(), inner.encode()?.to_vec())?; self.trie.insert( - metadata.epoch.to_be_bytes().to_vec(), - metadata.encode()?.to_vec(), + CONSENSUS_CONFIG.as_bytes().to_vec(), + encode_consensus_config(current_hardfork, config.encode()?.to_vec()), )?; let new_root = self.trie.commit()?; CURRENT_METADATA_ROOT.with(|r| *r.borrow_mut() = new_root); @@ -105,7 +113,8 @@ impl MetadataStore { block_number: u64, proposer: &H160, ) -> ProtocolResult<()> { - let mut metadata = self.get_metadata_by_block_number(block_number)?; + let epoch = self.get_epoch_by_block_number(block_number)?; + let mut metadata = self.get_metadata_inner(epoch)?; if let Some(counter) = metadata .propose_counter .iter_mut() @@ -130,11 +139,26 @@ impl MetadataStore { } pub fn get_metadata(&self, epoch: u64) -> ProtocolResult { + let inner = self.get_metadata_inner(epoch)?; + let config = self.get_consensus_config()?; + Ok(Metadata::from_parts(inner, config)) + } + + fn get_metadata_inner(&self, epoch: u64) -> ProtocolResult { let raw = self .trie .get(&epoch.to_be_bytes())? .ok_or_else(|| SystemScriptError::MissingRecord(epoch))?; - Metadata::decode(raw) + MetadataInner::decode(raw) + } + + fn get_consensus_config(&self) -> ProtocolResult { + let raw = self + .trie + .get(CONSENSUS_CONFIG.as_bytes())? + .expect("Inner panic with can't find consensus config"); + + decode_consensus_config(raw) } pub fn get_metadata_by_block_number(&self, block_number: u64) -> ProtocolResult { @@ -151,14 +175,10 @@ impl MetadataStore { } pub fn update_consensus_config(&mut self, config: ConsensusConfig) -> ProtocolResult<()> { - let epoch_segment = self.get_epoch_segment()?; - let latest_epoch = epoch_segment.get_latest_epoch_number(); - let mut metadata = self.get_metadata(latest_epoch)?; - - metadata.consensus_config = config.into(); + let current_hardfork = **HARDFORK_INFO.load(); self.trie.insert( - metadata.epoch.to_be_bytes().to_vec(), - metadata.encode()?.to_vec(), + CONSENSUS_CONFIG.as_bytes().to_vec(), + encode_consensus_config(current_hardfork, config.encode()?.to_vec()), )?; let new_root = self.trie.commit()?; CURRENT_METADATA_ROOT.with(|r| *r.borrow_mut() = new_root); @@ -222,3 +242,54 @@ impl MetadataStore { } } } + +enum ConsensusConfigFlag { + V0 = 0b0, + V1 = 0b1, +} + +impl From for ConsensusConfigFlag { + fn from(value: u16) -> Self { + match value { + 0b0 => ConsensusConfigFlag::V0, + 0b1 => ConsensusConfigFlag::V1, + _ => unreachable!(), + } + } +} + +impl ConsensusConfigFlag { + fn new(flags: H256) -> Self { + let v1_name_flag = H256::from_low_u64_be((HardforkName::Andromeda as u64).to_be()); + let res = flags & v1_name_flag; + + if res & v1_name_flag == v1_name_flag { + ConsensusConfigFlag::V1 + } else { + ConsensusConfigFlag::V0 + } + } +} + +fn decode_consensus_config(raw: Vec) -> ProtocolResult { + let raw_flag = { + let mut a = [0u8; 2]; + a[0] = raw[0]; + a[1] = raw[1]; + a + }; + let flag = ConsensusConfigFlag::from(u16::from_be_bytes(raw_flag)); + + match flag { + ConsensusConfigFlag::V0 => ConsensusConfigV0::decode(&raw[2..]).map(Into::into), + ConsensusConfigFlag::V1 => ConsensusConfig::decode(&raw[2..]), + } +} + +fn encode_consensus_config(current_hardfork: H256, config: Vec) -> Vec { + let flag = ConsensusConfigFlag::new(current_hardfork); + + let mut res = (flag as u16).to_be_bytes().to_vec(); + res.extend(config); + res +} diff --git a/core/executor/src/tests/system_script/metadata.rs b/core/executor/src/tests/system_script/metadata.rs index d5ad4b715..4d0172c64 100644 --- a/core/executor/src/tests/system_script/metadata.rs +++ b/core/executor/src/tests/system_script/metadata.rs @@ -184,15 +184,15 @@ fn prepare_metadata() -> Metadata { verifier_list: vec![prepare_validator()], propose_counter: vec![], consensus_config: ConsensusConfig { - gas_limit: 1u64, - gas_price: 0u64, - interval: 0u64, - propose_ratio: 1u64, - prevote_ratio: 1u64, - precommit_ratio: 1u64, - brake_ratio: 1u64, - tx_num_limit: 1u64, - max_tx_size: 1u64, + gas_limit: 1u64, + interval: 0u64, + propose_ratio: 1u64, + prevote_ratio: 1u64, + precommit_ratio: 1u64, + brake_ratio: 1u64, + tx_num_limit: 1u64, + max_tx_size: 1u64, + max_contract_limit: 0x6000u64, }, } } diff --git a/core/run/src/tests.rs b/core/run/src/tests.rs index 6fb3382f4..c84e42698 100644 --- a/core/run/src/tests.rs +++ b/core/run/src/tests.rs @@ -39,8 +39,8 @@ const TESTCASES: &[TestCase] = &[ config_file: "config.toml", chain_spec_file: "specs/single_node/chain-spec.toml", key_file: "debug.key", - input_genesis_hash: "0x4e06dc4a01178db42c029f7d65f65a5763702a21082cfcb626c6c41054a7a276", - genesis_state_root: "0xb6c51706f77a788606c8b932ad4f752c3f9a54008cd82e85f417d3a7e900d5c2", + input_genesis_hash: "0x7360659a70f5713a4cc30b51e819dacd421047faf3295371cccbd34f7e5536c3", + genesis_state_root: "0xa70f71c1be81bc8dbe1303a310bfdcb69924abaab7e96775891bb5871f10af2c", genesis_receipts_root: "0x7e747618f612d08dfe54bcb67f58f13a49e8b1bafee9a8f19a3a0f7122f44d02", }, TestCase { @@ -48,8 +48,8 @@ const TESTCASES: &[TestCase] = &[ config_file: "nodes/node_1.toml", chain_spec_file: "specs/multi_nodes/chain-spec.toml", key_file: "debug.key", - input_genesis_hash: "0xf16db25ca1a0cff5339d76e9802c75c43faac35ee4a9294a51234b167c69159f", - genesis_state_root: "0x9f76f02e823115e7b9887d07678f40b8ff50f82d4d09214fa481bdad68f5192a", + input_genesis_hash: "0x6b2214dc0bd93c64c234a99d996c90ccd97b0f89a888d50292dc34897486e30a", + genesis_state_root: "0x69f3ac6ea8149daa36a6e8d5d2f4f892efcf9a7ead31a1dcb0301a27db4d71a9", genesis_receipts_root: "0x7e747618f612d08dfe54bcb67f58f13a49e8b1bafee9a8f19a3a0f7122f44d02", }, TestCase { @@ -57,8 +57,8 @@ const TESTCASES: &[TestCase] = &[ 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: "0x4e06dc4a01178db42c029f7d65f65a5763702a21082cfcb626c6c41054a7a276", - genesis_state_root: "0x9dddf9df077b2bb98f72686256dae92fb975d416fa3093956151056b913a065d", + input_genesis_hash: "0x7360659a70f5713a4cc30b51e819dacd421047faf3295371cccbd34f7e5536c3", + genesis_state_root: "0xa19f1fad30421e499af8a296f228802da8d41ee45617fac253a41731f1255ecf", genesis_receipts_root: "0x7e747618f612d08dfe54bcb67f58f13a49e8b1bafee9a8f19a3a0f7122f44d02", }, ]; diff --git a/protocol/src/types/block.rs b/protocol/src/types/block.rs index ab1236dbe..f5d029446 100644 --- a/protocol/src/types/block.rs +++ b/protocol/src/types/block.rs @@ -362,7 +362,6 @@ mod tests { }], consensus_config: ConsensusConfig { gas_limit: 4294967295, - gas_price: 1, interval: 3000, propose_ratio: 15, prevote_ratio: 10, @@ -370,6 +369,7 @@ mod tests { brake_ratio: 10, tx_num_limit: 20000, max_tx_size: 1024, + max_contract_limit: 0x6000 } }; diff --git a/protocol/src/types/primitive.rs b/protocol/src/types/primitive.rs index 9e510a126..a327f4c72 100644 --- a/protocol/src/types/primitive.rs +++ b/protocol/src/types/primitive.rs @@ -306,15 +306,45 @@ pub struct Metadata { pub consensus_config: ConsensusConfig, } +impl Metadata { + pub fn into_part(self) -> (MetadataInner, ConsensusConfig) { + ( + MetadataInner { + version: self.version, + epoch: self.epoch, + verifier_list: self.verifier_list, + propose_counter: self.propose_counter, + }, + self.consensus_config, + ) + } + + pub fn from_parts(inner: MetadataInner, config: ConsensusConfig) -> Self { + Metadata { + version: inner.version, + epoch: inner.epoch, + verifier_list: inner.verifier_list, + propose_counter: inner.propose_counter, + consensus_config: config, + } + } +} + +#[derive(RlpEncodable, RlpDecodable, Default, Clone, Debug, PartialEq, Eq)] +pub struct MetadataInner { + pub version: MetadataVersion, + pub epoch: u64, + pub verifier_list: Vec, + pub propose_counter: Vec, +} + #[derive( RlpEncodable, RlpDecodable, Serialize, Deserialize, Default, Clone, Debug, PartialEq, Eq, )] -pub struct ConsensusConfig { +pub struct ConsensusConfigV0 { #[cfg_attr(feature = "hex-serialize", serde(serialize_with = "serialize_uint"))] pub gas_limit: u64, #[cfg_attr(feature = "hex-serialize", serde(serialize_with = "serialize_uint"))] - pub gas_price: u64, - #[cfg_attr(feature = "hex-serialize", serde(serialize_with = "serialize_uint"))] pub interval: u64, #[cfg_attr(feature = "hex-serialize", serde(serialize_with = "serialize_uint"))] pub propose_ratio: u64, @@ -330,6 +360,51 @@ pub struct ConsensusConfig { pub max_tx_size: u64, } +impl From for ConsensusConfig { + fn from(value: ConsensusConfigV0) -> Self { + ConsensusConfig { + gas_limit: value.gas_limit, + interval: value.interval, + precommit_ratio: value.precommit_ratio, + propose_ratio: value.propose_ratio, + prevote_ratio: value.prevote_ratio, + brake_ratio: value.brake_ratio, + tx_num_limit: value.tx_num_limit, + max_tx_size: value.max_tx_size, + max_contract_limit: 0x6000, + } + } +} + +#[derive( + RlpEncodable, RlpDecodable, Serialize, Deserialize, Default, Clone, Debug, PartialEq, Eq, +)] +pub struct ConsensusConfig { + #[cfg_attr(feature = "hex-serialize", serde(serialize_with = "serialize_uint"))] + pub gas_limit: u64, + #[cfg_attr(feature = "hex-serialize", serde(serialize_with = "serialize_uint"))] + pub interval: u64, + #[cfg_attr(feature = "hex-serialize", serde(serialize_with = "serialize_uint"))] + pub propose_ratio: u64, + #[cfg_attr(feature = "hex-serialize", serde(serialize_with = "serialize_uint"))] + pub prevote_ratio: u64, + #[cfg_attr(feature = "hex-serialize", serde(serialize_with = "serialize_uint"))] + pub precommit_ratio: u64, + #[cfg_attr(feature = "hex-serialize", serde(serialize_with = "serialize_uint"))] + pub brake_ratio: u64, + #[cfg_attr(feature = "hex-serialize", serde(serialize_with = "serialize_uint"))] + pub tx_num_limit: u64, + #[cfg_attr(feature = "hex-serialize", serde(serialize_with = "serialize_uint"))] + pub max_tx_size: u64, + #[cfg_attr(feature = "hex-serialize", serde(serialize_with = "serialize_uint"))] + #[serde(default = "default_max_contract_limit")] + pub max_contract_limit: u64, +} + +fn default_max_contract_limit() -> u64 { + 0x6000 +} + impl From for DurationConfig { fn from(m: Metadata) -> Self { DurationConfig {