Skip to content

Commit

Permalink
refactor: change config to single struct
Browse files Browse the repository at this point in the history
  • Loading branch information
driftluo committed Aug 16, 2023
1 parent f29d8b0 commit bc2507a
Show file tree
Hide file tree
Showing 9 changed files with 91 additions and 78 deletions.
10 changes: 5 additions & 5 deletions core/consensus/src/consensus.rs
Original file line number Diff line number Diff line change
Expand Up @@ -133,11 +133,11 @@ impl<Adapter: ConsensusAdapter + 'static> OverlordConsensus<Adapter> {
Context::new(),
OverlordMsg::RichStatus(gen_overlord_status(
status.last_number + 1,
metadata.interval,
metadata.propose_ratio,
metadata.prevote_ratio,
metadata.precommit_ratio,
metadata.brake_ratio,
metadata.consensus_config.interval,
metadata.consensus_config.propose_ratio,
metadata.consensus_config.prevote_ratio,
metadata.consensus_config.precommit_ratio,
metadata.consensus_config.brake_ratio,
metadata.verifier_list.into_iter().map(Into::into).collect(),
)),
)
Expand Down
4 changes: 2 additions & 2 deletions core/consensus/src/engine.rs
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,7 @@ impl<Adapter: ConsensusAdapter + 'static> Engine<Proposal> for ConsensusEngine<A
if current_number == status.last_number {
return Ok(Status {
height: current_number + 1,
interval: Some(metadata.interval),
interval: Some(metadata.consensus_config.interval),
authority_list: convert_to_overlord_authority(&metadata.verifier_list),
timer_config: Some(metadata.into()),
});
Expand Down Expand Up @@ -288,7 +288,7 @@ impl<Adapter: ConsensusAdapter + 'static> Engine<Proposal> for ConsensusEngine<A
let epoch = metadata.epoch;
let status = Status {
height: next_block_number,
interval: Some(metadata.interval),
interval: Some(metadata.consensus_config.interval),
authority_list: convert_to_overlord_authority(&metadata.verifier_list),
timer_config: Some(metadata.into()),
};
Expand Down
15 changes: 8 additions & 7 deletions core/consensus/src/synchronization.rs
Original file line number Diff line number Diff line change
Expand Up @@ -350,8 +350,8 @@ impl<Adapter: SynchronizationAdapter> OverlordSynchronization<Adapter> {
prev_hash: block.hash(),
last_number: block.header.number,
last_state_root: resp.state_root,
tx_num_limit: metadata.tx_num_limit,
max_tx_size: metadata.max_tx_size.into(),
tx_num_limit: metadata.consensus_config.tx_num_limit,
max_tx_size: metadata.consensus_config.max_tx_size.into(),
proof: proof.clone(),
};

Expand Down Expand Up @@ -484,6 +484,7 @@ impl<Adapter: SynchronizationAdapter> OverlordSynchronization<Adapter> {
self.adapter
.get_metadata_by_block_number(current_number)
.await?
.consensus_config
.interval,
))
.await;
Expand Down Expand Up @@ -512,11 +513,11 @@ impl<Adapter: SynchronizationAdapter> OverlordSynchronization<Adapter> {
self.adapter.update_status(
ctx,
sync_status.last_number,
metadata.interval,
metadata.propose_ratio,
metadata.prevote_ratio,
metadata.precommit_ratio,
metadata.brake_ratio,
metadata.consensus_config.interval,
metadata.consensus_config.propose_ratio,
metadata.consensus_config.prevote_ratio,
metadata.consensus_config.precommit_ratio,
metadata.consensus_config.brake_ratio,
metadata.verifier_list.into_iter().map(Into::into).collect(),
)?;

Expand Down
49 changes: 26 additions & 23 deletions core/executor/src/system_contract/metadata/abi/mod.rs
Original file line number Diff line number Diff line change
@@ -1,28 +1,31 @@
pub mod metadata_abi;

use protocol::types::{
CkbRelatedInfo, Hex, Metadata, MetadataVersion, ProposeCount, ValidatorExtend, H256,
CkbRelatedInfo, ConsensusConfig, Hex, Metadata, MetadataVersion, ProposeCount, ValidatorExtend,
H256,
};

impl From<metadata_abi::Metadata> for Metadata {
fn from(value: metadata_abi::Metadata) -> Metadata {
Metadata {
version: MetadataVersion {
version: MetadataVersion {
start: value.version.start,
end: value.version.end,
},
epoch: value.epoch,
gas_limit: value.gas_limit,
gas_price: value.gas_price,
interval: value.interval,
verifier_list: value.verifier_list.into_iter().map(Into::into).collect(),
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,
propose_counter: value.propose_counter.into_iter().map(Into::into).collect(),
epoch: value.epoch,
verifier_list: value.verifier_list.into_iter().map(Into::into).collect(),
propose_counter: value.propose_counter.into_iter().map(Into::into).collect(),
consensus_config: ConsensusConfig {
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,
},
}
}
}
Expand All @@ -32,16 +35,16 @@ impl From<Metadata> for metadata_abi::Metadata {
metadata_abi::Metadata {
version: value.version.into(),
epoch: value.epoch,
gas_limit: value.gas_limit,
gas_price: value.gas_price,
interval: value.interval,
gas_limit: value.consensus_config.gas_limit,
gas_price: value.consensus_config.gas_price,
interval: value.consensus_config.interval,
verifier_list: value.verifier_list.into_iter().map(Into::into).collect(),
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,
propose_ratio: value.consensus_config.propose_ratio,
prevote_ratio: value.consensus_config.prevote_ratio,
precommit_ratio: value.consensus_config.precommit_ratio,
brake_ratio: value.consensus_config.brake_ratio,
tx_num_limit: value.consensus_config.tx_num_limit,
max_tx_size: value.consensus_config.max_tx_size,
propose_counter: value.propose_counter.into_iter().map(Into::into).collect(),
}
}
Expand Down
18 changes: 9 additions & 9 deletions core/run/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -335,8 +335,8 @@ impl Axon {
mempool.set_args(
Context::new(),
current_block.header.state_root,
metadata.gas_limit,
metadata.max_tx_size,
metadata.consensus_config.gas_limit,
metadata.consensus_config.max_tx_size,
);

// Init overlord consensus and synchronization
Expand Down Expand Up @@ -582,8 +582,8 @@ impl Axon {
let current_consensus_status = CurrentStatus {
prev_hash: block.hash(),
last_number: header.number,
max_tx_size: metadata.max_tx_size.into(),
tx_num_limit: metadata.tx_num_limit,
max_tx_size: metadata.consensus_config.max_tx_size.into(),
tx_num_limit: metadata.consensus_config.tx_num_limit,
proof: latest_proof,
last_state_root: if header.number == 0 {
self.state_root
Expand Down Expand Up @@ -808,17 +808,17 @@ impl Axon {
DB: TrieDB,
{
let timer_config = DurationConfig {
propose_ratio: metadata.propose_ratio,
prevote_ratio: metadata.prevote_ratio,
precommit_ratio: metadata.precommit_ratio,
brake_ratio: metadata.brake_ratio,
propose_ratio: metadata.consensus_config.propose_ratio,
prevote_ratio: metadata.consensus_config.prevote_ratio,
precommit_ratio: metadata.consensus_config.precommit_ratio,
brake_ratio: metadata.consensus_config.brake_ratio,
};

tokio::spawn(async move {
if let Err(e) = overlord_consensus
.run(
current_block.header.number,
metadata.interval,
metadata.consensus_config.interval,
validators,
Some(timer_config),
)
Expand Down
2 changes: 1 addition & 1 deletion core/run/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ const CONFIG_FILE: &str = "config.toml";

const GENESIS_HASH: &str = "0x2cc987996d5d26d18cb76dceb85d9b46e4f05f11ff331247225d983ec7a7b78f";
const GENESIS_STATE_ROOT: &str =
"0x65f57a6a666e656de33ed68957e04b35b3fe1b35a90f6eafb6f283c907dc3d77";
"0x47bd35cdb8bd43da1d5ce85ad77fd68de4d654fc2677ac36bd3e18cfda0ff136";
const GENESIS_RECEIPTS_ROOT: &str =
"0x8544b530238201f1620b139861a6841040b37f78f8bdae8736ef5cec474e979b";

Expand Down
22 changes: 11 additions & 11 deletions devtools/genesis-generator/metadata.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,6 @@
"end": 100000000
},
"epoch": 0,
"common_ref": "0x6c747758636859487038",
"timeout_gap": 1000,
"gas_limit": 4294967295000,
"gas_price": 1,
"interval": 3000,
"verifier_list": [
{
"bls_pub_key": "0xac85bbb40347b6e06ac2dc2da1f75eece029cdc0ed2d456c457d27e288bfbfbcd4c5c19716e9b250134a0e76ce50fa22",
Expand All @@ -18,10 +13,15 @@
"vote_weight": 1
}
],
"propose_ratio": 15,
"prevote_ratio": 10,
"precommit_ratio": 10,
"brake_ratio": 10,
"tx_num_limit": 20000,
"max_tx_size": 409600000
"consensus_config": {
"propose_ratio": 15,
"prevote_ratio": 10,
"precommit_ratio": 10,
"brake_ratio": 10,
"tx_num_limit": 20000,
"max_tx_size": 409600000,
"gas_limit": 4294967295000,
"gas_price": 1,
"interval": 3000
}
}
24 changes: 13 additions & 11 deletions protocol/src/types/block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -278,8 +278,8 @@ impl RichBlock {
#[cfg(test)]
mod tests {
use crate::types::{
Block, BlockVersion, Header, Hex, Metadata, MetadataVersion, ProposeCount, RichBlock,
ValidatorExtend, H160,
Block, BlockVersion, ConsensusConfig, Header, Hex, Metadata, MetadataVersion, ProposeCount,
RichBlock, ValidatorExtend, H160,
};
use std::time::{SystemTime, UNIX_EPOCH};

Expand Down Expand Up @@ -339,15 +339,6 @@ mod tests {
let metadata = Metadata {
version: MetadataVersion::new(0, 1000000000),
epoch: 0,
gas_limit: 4294967295,
gas_price: 1,
interval: 3000,
propose_ratio: 15,
prevote_ratio: 10,
precommit_ratio: 10,
brake_ratio: 10,
tx_num_limit: 20000,
max_tx_size: 1024,
verifier_list: vec![ValidatorExtend {
bls_pub_key: Hex::from_string("0x04102947214862a503c73904deb5818298a186d68c7907bb609583192a7de6331493835e5b8281f4d9ee705537c0e765580e06f86ddce5867812fceb42eecefd209f0eddd0389d6b7b0100f00fb119ef9ab23826c6ea09aadcc76fa6cea6a32724".to_string()).unwrap(),
pub_key: Hex::from_string("0x02ef0cb0d7bc6c18b4bea1f5908d9106522b35ab3c399369605d4242525bda7e60".to_string()).unwrap(),
Expand All @@ -359,6 +350,17 @@ mod tests {
address: H160::default(),
count: 0,
}],
consensus_config: ConsensusConfig {
gas_limit: 4294967295,
gas_price: 1,
interval: 3000,
propose_ratio: 15,
prevote_ratio: 10,
precommit_ratio: 10,
brake_ratio: 10,
tx_num_limit: 20000,
max_tx_size: 1024,
}
};

println!("{}", serde_json::to_string(&metadata).unwrap());
Expand Down
25 changes: 16 additions & 9 deletions protocol/src/types/primitive.rs
Original file line number Diff line number Diff line change
Expand Up @@ -320,16 +320,25 @@ impl MetadataVersion {
RlpEncodable, RlpDecodable, Serialize, Deserialize, Default, Clone, Debug, PartialEq, Eq,
)]
pub struct Metadata {
pub version: MetadataVersion,
pub version: MetadataVersion,
#[cfg_attr(feature = "hex-serialize", serde(serialize_with = "serialize_uint"))]
pub epoch: u64,
pub epoch: u64,
pub verifier_list: Vec<ValidatorExtend>,
#[serde(skip_deserializing)]
pub propose_counter: Vec<ProposeCount>,
pub consensus_config: ConsensusConfig,
}

#[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 gas_price: u64,
#[cfg_attr(feature = "hex-serialize", serde(serialize_with = "serialize_uint"))]
pub interval: u64,
pub verifier_list: Vec<ValidatorExtend>,
#[cfg_attr(feature = "hex-serialize", serde(serialize_with = "serialize_uint"))]
pub propose_ratio: u64,
#[cfg_attr(feature = "hex-serialize", serde(serialize_with = "serialize_uint"))]
Expand All @@ -342,17 +351,15 @@ pub struct Metadata {
pub tx_num_limit: u64,
#[cfg_attr(feature = "hex-serialize", serde(serialize_with = "serialize_uint"))]
pub max_tx_size: u64,
#[serde(skip_deserializing)]
pub propose_counter: Vec<ProposeCount>,
}

impl From<Metadata> for DurationConfig {
fn from(m: Metadata) -> Self {
DurationConfig {
propose_ratio: m.propose_ratio,
prevote_ratio: m.prevote_ratio,
precommit_ratio: m.precommit_ratio,
brake_ratio: m.brake_ratio,
propose_ratio: m.consensus_config.propose_ratio,
prevote_ratio: m.consensus_config.prevote_ratio,
precommit_ratio: m.consensus_config.precommit_ratio,
brake_ratio: m.consensus_config.brake_ratio,
}
}
}
Expand Down

0 comments on commit bc2507a

Please sign in to comment.