Skip to content

Commit

Permalink
feat: change serialization method for extra_data
Browse files Browse the repository at this point in the history
  • Loading branch information
driftluo committed Sep 20, 2023
1 parent 8e5d567 commit a615c94
Show file tree
Hide file tree
Showing 10 changed files with 68 additions and 57 deletions.
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
36 changes: 20 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,19 @@ 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) {
if let Ok(data) = HardforkInfoInner::decode(&t.inner) {
if !self
.node_info
.hardfork_proposals
.read()
.unwrap()
.as_ref()
.map(|v| &data == v)
.unwrap_or_default()
{
return Err(ProtocolError::from(ConsensusError::HardforkDontMatch).into());
}
}
}

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: "0x3efe365ab7a903557a846646bdc4abdb2eed9d7292ed2ccce1f2e94e1c9e7ad0",
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: "0xd138005096cc7e43531e34e33afd8964e9bde49afcb37ce0c8192650e18fa8ee",
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: "0x3efe365ab7a903557a846646bdc4abdb2eed9d7292ed2ccce1f2e94e1c9e7ad0",
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
23 changes: 13 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,7 @@ 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,
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 All @@ -246,6 +238,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

0 comments on commit a615c94

Please sign in to comment.