Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: change serialization method for extra_data #1442

Merged
merged 3 commits into from
Sep 21, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 11 additions & 8 deletions common/config-parser/src/types/spec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ use common_crypto::Secp256k1RecoverablePrivateKey;
use protocol::{
codec::{decode_256bits_key, deserialize_address, ProtocolCodec},
types::{
Block, HardforkInfoInner, Header, Key256Bits, Metadata, RichBlock, SignedTransaction, H160,
H256, U256,
Block, ExtraData, HardforkInfoInner, Header, Key256Bits, Metadata, RichBlock,
SignedTransaction, H160, H256, U256,
},
};

Expand All @@ -40,7 +40,7 @@ pub struct ChainSpec {
#[derive(Clone, Debug, Deserialize)]
pub struct Genesis {
pub timestamp: u64,
pub extra_data: HardforkInput,
pub hardforks: Vec<HardforkName>,
pub base_fee_per_gas: U256,
pub chain_id: u64,

Expand Down Expand Up @@ -261,12 +261,15 @@ impl Genesis {
timestamp: self.timestamp,
// todo: if Hardforkinput is empty, it must change to latest hardfork info to init
// genesis
extra_data: if self.extra_data.block_number != 0 {
Default::default()
} else {
Into::<HardforkInfoInner>::into(self.extra_data.clone())
extra_data: {
vec![ExtraData {
inner: Into::<HardforkInfoInner>::into(HardforkInput {
hardforks: self.hardforks.clone(),
block_number: 0,
})
.encode()
.unwrap()
.unwrap(),
}]
},
base_fee_per_gas: self.base_fee_per_gas,
chain_id: self.chain_id,
Expand Down
17 changes: 13 additions & 4 deletions core/api/src/jsonrpc/web3_types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -300,7 +300,7 @@ pub struct Web3Block {
pub number: U256,
pub gas_used: U256,
pub gas_limit: U256,
pub extra_data: Hex,
pub extra_data: Vec<Hex>,
pub logs_bloom: Option<Bloom>,
pub timestamp: U256,
pub difficulty: U256,
Expand Down Expand Up @@ -331,7 +331,12 @@ impl From<Block> for Web3Block {
total_difficulty: Some(b.header.number.into()),
seal_fields: vec![],
base_fee_per_gas: b.header.base_fee_per_gas,
extra_data: Hex::encode(&b.header.extra_data),
extra_data: b
.header
.extra_data
.iter()
.map(|i| Hex::encode(&i.inner.encode().unwrap()))
.collect(),
size: Some(b.header.size().into()),
gas_limit: b.header.gas_limit,
gas_used: b.header.gas_used,
Expand Down Expand Up @@ -821,7 +826,7 @@ pub struct FeeHistoryEmpty {
#[serde(rename_all = "camelCase")]
pub struct Web3Header {
pub difficulty: U256,
pub extra_data: Hex,
pub extra_data: Vec<Hex>,
pub gas_limit: U256,
pub gas_used: U256,
pub logs_bloom: Option<Bloom>,
Expand Down Expand Up @@ -849,7 +854,11 @@ impl From<Header> for Web3Header {
receipts_root: h.receipts_root,
miner: h.proposer,
difficulty: U256::one(),
extra_data: Hex::encode(&h.extra_data),
extra_data: h
.extra_data
.into_iter()
.map(|i| Hex::encode(i.inner.encode().unwrap()))
.collect(),
gas_limit: h.gas_limit,
gas_used: h.gas_used,
timestamp: h.timestamp.into(),
Expand Down
47 changes: 31 additions & 16 deletions core/consensus/src/engine.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ use common_merkle::TrieMerkle;
use core_executor::MetadataHandle;
use protocol::traits::{ConsensusAdapter, Context, MessageTarget, NodeInfo};
use protocol::types::{
Block, BlockVersion, Bytes, ExecResp, Hash, Hex, Metadata, Proof, Proposal, SignedTransaction,
ValidatorExtend, BASE_FEE_PER_GAS, MAX_BLOCK_GAS_LIMIT, RLP_NULL,
Block, BlockVersion, Bytes, ExecResp, ExtraData, Hash, Hex, Metadata, Proof, Proposal,
SignedTransaction, ValidatorExtend, BASE_FEE_PER_GAS, MAX_BLOCK_GAS_LIMIT, RLP_NULL,
};
use protocol::{
async_trait, codec::ProtocolCodec, tokio::sync::Mutex as AsyncMutex, types::HardforkInfoInner,
Expand Down Expand Up @@ -91,12 +91,14 @@ impl<Adapter: ConsensusAdapter + 'static> Engine<Proposal> for ConsensusEngine<A
if v.block_number <= next_number {
hardfork.take();
remove = true;
Default::default()
Vec::new()
} else {
v.encode().unwrap()
vec![ExtraData {
inner: v.encode().unwrap(),
}]
}
}
None => Default::default(),
None => Vec::new(),
}
};

Expand Down Expand Up @@ -161,17 +163,30 @@ impl<Adapter: ConsensusAdapter + 'static> Engine<Proposal> for ConsensusEngine<A
.into());
}

if let Ok(data) = HardforkInfoInner::decode(&proposal.extra_data) {
if !self
.node_info
.hardfork_proposals
.read()
.unwrap()
.as_ref()
.map(|v| &data == v)
.unwrap_or_default()
{
return Err(ProtocolError::from(ConsensusError::HardforkDontMatch).into());
if let Some(t) = proposal.extra_data.get(0) {
match HardforkInfoInner::decode(&t.inner) {
Ok(data) => {
if !self
.node_info
.hardfork_proposals
.read()
.unwrap()
.as_ref()
.map(|v| &data == v)
.unwrap_or_default()
{
return Err(ProtocolError::from(ConsensusError::Hardfork(
"hardfork proposal doesn't match".to_string(),
))
.into());
}
}
Err(_) => {
return Err(ProtocolError::from(ConsensusError::Hardfork(
"hardfork proposal can't decode".to_string(),
))
.into())
}
}
}

Expand Down
4 changes: 2 additions & 2 deletions core/consensus/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -184,8 +184,8 @@ pub enum ConsensusError {
#[display(fmt = "Build trie merkle tree error {}", _0)]
BuildMerkle(String),

#[display(fmt = "Proposal hardfork info don't match")]
HardforkDontMatch,
#[display(fmt = "Proposal hardfork info error {}", _0)]
Hardfork(String),
}

#[derive(Debug, Display)]
Expand Down
10 changes: 6 additions & 4 deletions core/executor/src/system_contract/metadata/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -120,10 +120,12 @@ impl<Adapter: ExecutorAdapter + ApplyBackend> SystemContract<Adapter>

let mut store = MetadataStore::new(root).unwrap();

if let Ok(data) = HardforkInfoInner::decode(adapter.get_ctx().extra_data) {
store
.set_hardfork_info(data.block_number, data.flags)
.expect("set new hardfork info fail")
if let Some(t) = adapter.get_ctx().extra_data.get(0) {
if let Ok(data) = HardforkInfoInner::decode(&t.inner) {
store
.set_hardfork_info(data.block_number, data.flags)
.expect("set new hardfork info fail")
}
}

let hardfork = store.hardfork_info(block_number.as_u64()).unwrap();
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 @@ -39,7 +39,7 @@ const TESTCASES: &[TestCase] = &[
config_file: "config.toml",
chain_spec_file: "specs/single_node/chain-spec.toml",
key_file: "debug.key",
input_genesis_hash: "0x5a4918d8cf9440b11f40047e184e4e2e0231cc449bd473d7dca1275b2ef938e1",
input_genesis_hash: "0x4e06dc4a01178db42c029f7d65f65a5763702a21082cfcb626c6c41054a7a276",
genesis_state_root: "0x601bd874d41eb9adb32021ee3ab934e0481065c58abfe7e757e33fb01be18dd5",
genesis_receipts_root: "0x8544b530238201f1620b139861a6841040b37f78f8bdae8736ef5cec474e979b",
},
Expand All @@ -48,7 +48,7 @@ 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: "0xf22e8b2cdcae7c833623c6a56ec072e130f1ac7dda93c615351e6598bdef61a1",
input_genesis_hash: "0xf16db25ca1a0cff5339d76e9802c75c43faac35ee4a9294a51234b167c69159f",
genesis_state_root: "0xc36f75519a047fec6a34c7be5dfca783a40eafa0d7418ad7b3ba99ad9c2dc655",
genesis_receipts_root: "0x8544b530238201f1620b139861a6841040b37f78f8bdae8736ef5cec474e979b",
},
Expand All @@ -57,7 +57,7 @@ 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: "0x5a4918d8cf9440b11f40047e184e4e2e0231cc449bd473d7dca1275b2ef938e1",
input_genesis_hash: "0x4e06dc4a01178db42c029f7d65f65a5763702a21082cfcb626c6c41054a7a276",
genesis_state_root: "0x42886558baab8a3c310d5a8313398e5f353cc4f8192838b578c857a329e9bb65",
genesis_receipts_root: "0x8544b530238201f1620b139861a6841040b37f78f8bdae8736ef5cec474e979b",
},
Expand Down
3 changes: 0 additions & 3 deletions devtools/chain/specs/multi_nodes/chain-spec.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,6 @@ base_fee_per_gas = "0x539"
chain_id = 0x41786f6e
# A JSON file which includes all transactions in the genesis block.
transactions = "genesis_transactions.json"

[genesis.extra_data]
block_number = 0
hardforks = []

#
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,6 @@ base_fee_per_gas = "0x539"
chain_id = 0x41786f6e
# A JSON file which includes all transactions in the genesis block.
transactions = "genesis_transactions.json"

[genesis.extra_data]
block_number = 0
hardforks = []

#
Expand Down
3 changes: 0 additions & 3 deletions devtools/chain/specs/single_node/chain-spec.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,6 @@ base_fee_per_gas = "0x539"
chain_id = 0x41786f6e
# A JSON file which includes all transactions in the genesis block.
transactions = "genesis_transactions.json"

[genesis.extra_data]
block_number = 0
hardforks = []

#
Expand Down
26 changes: 16 additions & 10 deletions protocol/src/types/block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,11 +58,7 @@ pub struct Proposal {
#[cfg_attr(feature = "hex-serialize", serde(serialize_with = "serialize_uint"))]
pub number: BlockNumber,
pub gas_limit: U256,
#[cfg_attr(
feature = "hex-serialize",
serde(serialize_with = "withpfx_lowercase::serialize")
)]
pub extra_data: Bytes,
pub extra_data: Vec<ExtraData>,
pub base_fee_per_gas: U256,
pub proof: Proof,
#[cfg_attr(feature = "hex-serialize", serde(serialize_with = "serialize_uint"))]
Expand Down Expand Up @@ -223,11 +219,10 @@ pub struct Header {
pub number: BlockNumber,
pub gas_used: U256,
pub gas_limit: U256,
#[cfg_attr(
feature = "hex-serialize",
serde(serialize_with = "withpfx_lowercase::serialize")
)]
pub extra_data: Bytes,
/// Extra data for the block header
/// The first index of extra_data is used to store hardfork information:
/// `HardforkInfoInner`
pub extra_data: Vec<ExtraData>,
driftluo marked this conversation as resolved.
Show resolved Hide resolved
pub base_fee_per_gas: U256,
pub proof: Proof,
#[cfg_attr(feature = "hex-serialize", serde(serialize_with = "serialize_uint"))]
Expand All @@ -246,6 +241,17 @@ impl Header {
}
}

#[derive(
RlpEncodable, RlpDecodable, Serialize, Deserialize, Default, Clone, Debug, PartialEq, Eq,
)]
pub struct ExtraData {
#[cfg_attr(
feature = "hex-serialize",
serde(serialize_with = "withpfx_lowercase::serialize")
)]
pub inner: Bytes,
}

#[derive(
RlpEncodable, RlpDecodable, Serialize, Deserialize, Default, Clone, Debug, PartialEq, Eq,
)]
Expand Down
5 changes: 2 additions & 3 deletions protocol/src/types/executor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,7 @@ pub use hasher::HasherKeccak;

use rlp_derive::{RlpDecodable, RlpEncodable};

use crate::types::{Bloom, Hash, Hasher, Header, MerkleRoot, Proposal, H160, U256};
use bytes::Bytes;
use crate::types::{Bloom, ExtraData, Hash, Hasher, Header, MerkleRoot, Proposal, H160, U256};

const BLOOM_BYTE_LENGTH: usize = 256;

Expand Down Expand Up @@ -54,7 +53,7 @@ pub struct ExecutorContext {
pub gas_price: U256,
pub block_gas_limit: U256,
pub block_base_fee_per_gas: U256,
pub extra_data: Bytes,
pub extra_data: Vec<ExtraData>,
}

impl From<Proposal> for ExecutorContext {
Expand Down
Loading