diff --git a/Cargo.lock b/Cargo.lock index f181224ab..b55071e4c 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1589,6 +1589,7 @@ dependencies = [ "log 0.4.8 (registry+https://github.com/rust-lang/crates.io-index)", "parking_lot 0.9.0 (registry+https://github.com/rust-lang/crates.io-index)", "protocol 0.1.0", + "rlp 0.4.2 (registry+https://github.com/rust-lang/crates.io-index)", "runtime 0.3.0-alpha.7 (registry+https://github.com/rust-lang/crates.io-index)", "runtime-tokio 0.3.0-alpha.6 (registry+https://github.com/rust-lang/crates.io-index)", "serde 1.0.101 (registry+https://github.com/rust-lang/crates.io-index)", diff --git a/Cargo.toml b/Cargo.toml index 05f5d8d42..061fdde61 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -26,6 +26,7 @@ log = "0.4" clap = "2.33" bytes = "0.4" hex = "0.3" +rlp = "0.4" [workspace] members = [ diff --git a/core/consensus/src/consensus.rs b/core/consensus/src/consensus.rs index 564d00ae2..c8ccf4748 100644 --- a/core/consensus/src/consensus.rs +++ b/core/consensus/src/consensus.rs @@ -135,7 +135,7 @@ fn gen_overlord_status(epoch_id: u64, interval: u64, validators: Vec) authority_list.sort(); Status { - epoch_id: epoch_id + 1, + epoch_id, interval: Some(interval), authority_list, } diff --git a/core/consensus/src/engine.rs b/core/consensus/src/engine.rs index 393778edf..0b3875733 100644 --- a/core/consensus/src/engine.rs +++ b/core/consensus/src/engine.rs @@ -45,32 +45,31 @@ impl Engine ) -> Result<(FixedPill, Bytes), Box> { let current_consensus_status = { self.current_consensus_status.read().clone() }; - let tmp = epoch_id; let (ordered_tx_hashes, propose_hashes) = self .adapter .get_txs_from_mempool(ctx, epoch_id, current_consensus_status.cycles_limit) .await? .clap(); - if current_consensus_status.epoch_id == epoch_id { + if current_consensus_status.epoch_id != epoch_id { return Err(ProtocolError::from(ConsensusError::MissingEpochHeader(epoch_id)).into()); } let header = EpochHeader { - chain_id: self.node_info.chain_id.clone(), - pre_hash: current_consensus_status.prev_hash, - epoch_id: tmp, - timestamp: time_now(), - logs_bloom: current_consensus_status.logs_bloom, - order_root: current_consensus_status.order_root.clone(), - confirm_root: current_consensus_status.confirm_root.clone(), - state_root: current_consensus_status.state_root.clone(), - receipt_root: current_consensus_status.receipt_root.clone(), - cycles_used: current_consensus_status.cycles_used, - proposer: self.node_info.self_address.clone(), - proof: current_consensus_status.proof.clone(), + chain_id: self.node_info.chain_id.clone(), + pre_hash: current_consensus_status.prev_hash, + epoch_id, + timestamp: time_now(), + logs_bloom: current_consensus_status.logs_bloom, + order_root: current_consensus_status.order_root.clone(), + confirm_root: current_consensus_status.confirm_root.clone(), + state_root: current_consensus_status.state_root.clone(), + receipt_root: current_consensus_status.receipt_root.clone(), + cycles_used: current_consensus_status.cycles_used, + proposer: self.node_info.self_address.clone(), + proof: current_consensus_status.proof.clone(), validator_version: 0u64, - validators: current_consensus_status.validators.clone(), + validators: current_consensus_status.validators.clone(), }; let epoch = Epoch { header, @@ -136,15 +135,14 @@ impl Engine } let pill = commit.content.inner; - let tmp = commit.proof; // Sorage save the lastest proof. let proof = Proof { - epoch_id: tmp.epoch_id, - round: tmp.round, - epoch_hash: Hash::from_bytes(tmp.epoch_hash)?, - signature: tmp.signature.signature, - bitmap: tmp.signature.address_bitmap, + epoch_id: commit.proof.epoch_id, + round: commit.proof.round, + epoch_hash: Hash::from_bytes(commit.proof.epoch_hash)?, + signature: commit.proof.signature.signature, + bitmap: commit.proof.signature.address_bitmap, }; self.adapter.save_proof(ctx.clone(), proof.clone()).await?; @@ -181,7 +179,7 @@ impl Engine }; let status = Status { - epoch_id: current_consensus_status.epoch_id, + epoch_id: epoch_id + 1, interval: Some(current_consensus_status.consensus_interval), authority_list: covert_to_overlord_authority(¤t_consensus_status.validators), }; diff --git a/core/consensus/src/lib.rs b/core/consensus/src/lib.rs index 448f86e2d..39a9238f3 100644 --- a/core/consensus/src/lib.rs +++ b/core/consensus/src/lib.rs @@ -1,9 +1,9 @@ mod engine; -mod fixed_types; mod util; pub mod adapter; pub mod consensus; +pub mod fixed_types; pub mod message; pub use overlord::DurationConfig; diff --git a/src/main.rs b/src/main.rs index 5199de81e..a9c1c6af3 100644 --- a/src/main.rs +++ b/src/main.rs @@ -14,6 +14,7 @@ use core_api::adapter::DefaultAPIAdapter; use core_api::config::GraphQLConfig; use core_consensus::adapter::OverlordConsensusAdapter; use core_consensus::consensus::OverlordConsensus; +use core_consensus::fixed_types::FixedPill; use core_consensus::message::{ ProposalMessageHandler, QCMessageHandler, VoteMessageHandler, END_GOSSIP_AGGREGATED_VOTE, END_GOSSIP_SIGNED_PROPOSAL, END_GOSSIP_SIGNED_VOTE, @@ -27,7 +28,8 @@ use core_storage::{adapter::rocks::RocksAdapter, ImplStorage}; use protocol::traits::executor::ExecutorFactory; use protocol::traits::{CurrentConsensusStatus, NodeInfo, Storage}; use protocol::types::{ - Address, Bloom, Epoch, EpochHeader, Genesis, Hash, MerkleRoot, Proof, UserAddress, + Address, Bloom, Epoch, EpochHeader, Genesis, Hash, MerkleRoot, Pill, Proof, UserAddress, + Validator, }; use protocol::ProtocolResult; @@ -214,12 +216,18 @@ async fn start(cfg: &Config) -> ProtocolResult<()> { self_address: my_address.clone(), }; let current_header = ¤t_epoch.header; - // TODO: pre hash. + + let prevhash = Hash::digest(Bytes::from(rlp::encode(&FixedPill { + inner: Pill { + epoch: current_epoch.clone(), + propose_hashes: vec![], + }, + }))); let current_consensus_status = CurrentConsensusStatus { cycles_price: cfg.consensus.cycles_price, cycles_limit: cfg.consensus.cycles_limit, - epoch_id: current_epoch.header.epoch_id, - prev_hash: Hash::from_empty(), + epoch_id: current_epoch.header.epoch_id + 1, + prev_hash: prevhash, logs_bloom: current_header.logs_bloom, order_root: current_header.order_root.clone(), confirm_root: current_header.confirm_root.clone(), @@ -227,7 +235,16 @@ async fn start(cfg: &Config) -> ProtocolResult<()> { receipt_root: current_header.receipt_root.clone(), cycles_used: current_header.cycles_used, proof: current_header.proof.clone(), - validators: current_header.validators.clone(), + validators: cfg + .consensus + .verifier_list + .iter() + .map(|v| Validator { + address: UserAddress::from_hex(v).unwrap(), + propose_weight: 1, + vote_weight: 1, + }) + .collect(), consensus_interval: cfg.consensus.interval, };