diff --git a/core/consensus/src/adapter.rs b/core/consensus/src/adapter.rs index 452682824..26cb47e7f 100644 --- a/core/consensus/src/adapter.rs +++ b/core/consensus/src/adapter.rs @@ -16,8 +16,8 @@ use protocol::traits::{ }; use protocol::types::{ BatchSignedTxs, Block, BlockNumber, BlockVersion, Bytes, ExecResp, Hash, Header, Hex, - MerkleRoot, Metadata, PackedTxHashes, Proof, Proposal, Receipt, SignedTransaction, Validator, - U256, + MerkleRoot, Metadata, PackedTxHashes, Proof, Proposal, Receipt, SignedTransaction, + ValidatorExtend, U256, }; use protocol::{async_trait, tokio::task, trie, ProtocolResult}; @@ -140,7 +140,7 @@ where prevote_ratio: u64, precommit_ratio: u64, brake_ratio: u64, - validators: Vec, + validators: Vec, ) -> ProtocolResult<()> { self.overlord_handler .read() @@ -502,11 +502,13 @@ where .into()); } + // The address is calculated by the validator's secp256k1 uncompressed public + // key, not blst public key. let mut authority_list = metadata .verifier_list .iter() .map(|v| Node { - address: v.pub_key.as_bytes(), + address: v.address.as_bytes().to_vec().into(), propose_weight: v.propose_weight, vote_weight: v.vote_weight, }) diff --git a/core/consensus/src/consensus.rs b/core/consensus/src/consensus.rs index a9803b6d7..463b15d1b 100644 --- a/core/consensus/src/consensus.rs +++ b/core/consensus/src/consensus.rs @@ -6,14 +6,13 @@ use overlord::types::{ use overlord::{DurationConfig, Overlord, OverlordHandler}; use protocol::traits::{Consensus, ConsensusAdapter, Context, NodeInfo}; -use protocol::types::{Proposal, Validator}; +use protocol::types::{Proposal, ValidatorExtend}; use protocol::{ async_trait, codec::ProtocolCodec, tokio::sync::Mutex as AsyncMutex, ProtocolResult, }; use common_apm::tracing::{AxonTracer, Tag}; use common_apm_derive::trace_span; -use common_crypto::PublicKey as _; use crate::wal::{ConsensusWal, SignedTxsWAL}; use crate::{ @@ -126,7 +125,7 @@ impl OverlordConsensus { .unwrap(); let overlord = Overlord::new( - node_info.self_pub_key.to_bytes(), + node_info.self_address.as_slice().to_vec().into(), Arc::clone(&engine), crypto, engine, @@ -144,7 +143,7 @@ impl OverlordConsensus { metadata.consensus_config.prevote_ratio, metadata.consensus_config.precommit_ratio, metadata.consensus_config.brake_ratio, - metadata.verifier_list.into_iter().map(Into::into).collect(), + metadata.verifier_list, )), ) .unwrap(); @@ -164,13 +163,15 @@ impl OverlordConsensus { &self, init_height: u64, interval: u64, - validators: Vec, + validators: Vec, timer_config: Option, ) -> ProtocolResult<()> { + // The address is calculated by the validator's secp256k1 uncompressed public + // key, not blst public key. let authority_list = validators .into_iter() .map(|v| Node { - address: v.pub_key, + address: v.address.as_bytes().to_vec().into(), propose_weight: v.propose_weight, vote_weight: v.vote_weight, }) @@ -192,12 +193,14 @@ pub fn gen_overlord_status( prevote_ratio: u64, precommit_ratio: u64, brake_ratio: u64, - validators: Vec, + validators: Vec, ) -> Status { + // The address is calculated by the validator's secp256k1 uncompressed public + // key, not blst public key. let mut authority_list = validators .into_iter() .map(|v| Node { - address: v.pub_key, + address: v.address.as_bytes().to_vec().into(), propose_weight: v.propose_weight, vote_weight: v.vote_weight, }) diff --git a/core/consensus/src/engine.rs b/core/consensus/src/engine.rs index e5dc4ce66..0db9b4fad 100644 --- a/core/consensus/src/engine.rs +++ b/core/consensus/src/engine.rs @@ -449,11 +449,13 @@ impl Engine for ConsensusEngine ProtocolResult Vec { + // The address is calculated by the validator's secp256k1 uncompressed public + // key, not blst public key. let mut authority = validators .iter() .map(|v| Node { - address: v.pub_key.as_bytes(), + address: v.address.as_bytes().to_vec().into(), propose_weight: v.propose_weight, vote_weight: v.vote_weight, }) diff --git a/core/consensus/src/synchronization.rs b/core/consensus/src/synchronization.rs index 84f50a42f..d82088a10 100644 --- a/core/consensus/src/synchronization.rs +++ b/core/consensus/src/synchronization.rs @@ -518,7 +518,7 @@ impl OverlordSynchronization { metadata.consensus_config.prevote_ratio, metadata.consensus_config.precommit_ratio, metadata.consensus_config.brake_ratio, - metadata.verifier_list.into_iter().map(Into::into).collect(), + metadata.verifier_list, )?; log::info!( diff --git a/core/consensus/src/tests/mod.rs b/core/consensus/src/tests/mod.rs index 8d9c9efdc..8d433d8ad 100644 --- a/core/consensus/src/tests/mod.rs +++ b/core/consensus/src/tests/mod.rs @@ -6,6 +6,7 @@ pub mod synchronization; use std::{collections::HashMap, str::FromStr}; use protocol::rand::random; +use protocol::types::ValidatorExtend; use protocol::{ async_trait, codec::hex_decode, @@ -145,7 +146,7 @@ impl SynchronizationAdapter for MockSyncAdapter { prevote_ratio: u64, precommit_ratio: u64, brake_ratio: u64, - validators: Vec, + validators: Vec, ) -> ProtocolResult<()> { Ok(()) } diff --git a/core/consensus/src/util.rs b/core/consensus/src/util.rs index 9364b2d0c..e89e31751 100644 --- a/core/consensus/src/util.rs +++ b/core/consensus/src/util.rs @@ -31,6 +31,9 @@ pub fn time_now() -> u64 { .as_secs() } +/// The `addr_pubkey` field is the map of address with the blst public key. To +/// be notice that the address is the calculate from node's secp256k1 +/// uncompressed public key pub struct OverlordCrypto { private_key: BlsPrivateKey, addr_pubkey: RwLock>, diff --git a/core/run/src/lib.rs b/core/run/src/lib.rs index b60dff2f2..bbfea2908 100644 --- a/core/run/src/lib.rs +++ b/core/run/src/lib.rs @@ -16,7 +16,7 @@ use protocol::traits::{ }; use protocol::types::{ Block, Bloom, BloomInput, ExecResp, HardforkInfoInner, Header, Metadata, Proposal, RichBlock, - SignedTransaction, Validator, ValidatorExtend, H256, + SignedTransaction, ValidatorExtend, H256, }; use protocol::{lazy::CHAIN_ID, trie::DB as TrieDB, ProtocolResult}; @@ -204,15 +204,7 @@ async fn start( metadata_handle.init_hardfork(current_block.header.number)?; let metadata = metadata_handle.get_metadata_by_block_number(current_block.header.number)?; - let validators: Vec = metadata - .verifier_list - .iter() - .map(|v| Validator { - pub_key: v.pub_key.as_bytes(), - propose_weight: v.propose_weight, - vote_weight: v.vote_weight, - }) - .collect::>(); + let validators = metadata.verifier_list.clone(); // Set args in mempool mempool.set_args( @@ -377,10 +369,10 @@ fn init_crypto( let mut bls_pub_keys = HashMap::new(); for validator_extend in validators.iter() { - let address = validator_extend.pub_key.as_bytes(); + let address = validator_extend.address.as_bytes().to_vec(); let hex_pubkey = validator_extend.bls_pub_key.as_bytes(); let pub_key = BlsPublicKey::try_from(hex_pubkey.as_ref()).map_err(MainError::Crypto)?; - bls_pub_keys.insert(address, pub_key); + bls_pub_keys.insert(address.into(), pub_key); } let crypto = OverlordCrypto::new(bls_priv_key, bls_pub_keys, String::new()); @@ -411,7 +403,7 @@ async fn get_status_agent( fn run_overlord_consensus( metadata: Metadata, - validators: Vec, + validators: Vec, current_block: Block, overlord_consensus: Arc>>, ) where diff --git a/core/run/src/tests.rs b/core/run/src/tests.rs index 746b158e5..5e109876d 100644 --- a/core/run/src/tests.rs +++ b/core/run/src/tests.rs @@ -48,24 +48,24 @@ const TESTCASES: &[TestCase] = &[ config_file: "config.toml", chain_spec_file: "specs/single_node/chain-spec.toml", key_file: "debug.key", - input_genesis_hash: "0xe3a40f0115fbf101520ceea1ce7103a73cb46554187ac7ed67f3522103e06d99", - genesis_state_root: "0x2f1e8e50d5ab97af96fdb5d6de8e691e5bb80f46f2c98c4133d265bd8b60de61", + input_genesis_hash: "0x274c0c52500c3978776d8836b8afe0999a946a010166c12a85a1c45b9cd2c5a2", + genesis_state_root: "0x940458498b6ac368ab17e9ede64d0cc1d321bc4ec835e09a333a4151c7785ea1", }, TestCase { chain_name: "multi_nodes", config_file: "nodes/node_1.toml", chain_spec_file: "specs/multi_nodes/chain-spec.toml", key_file: "debug.key", - input_genesis_hash: "0x1b4cf78373961dabcba5d4a9402c924fc4fecdd9ce367239f02c8971a052f3b5", - genesis_state_root: "0xf684cbec490eb5b8a07b80f369f3bf87f05ec73494b869111010a6ad6fa89894", + input_genesis_hash: "0x70cc025ae586f054157f6d8a6558c39c359cde0eb4b9acbdf3f31a8e14a6a6fc", + genesis_state_root: "0x9976026c069e8d931d55f93637663e494caae772c2c274ad636de9bc7baf5191", }, TestCase { chain_name: "multi_nodes_short_epoch_len", 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: "0xd930632a7565acfc149c1d896d79910608768de5b936fdb34cc47c9b2296dd2a", - genesis_state_root: "0xa5e1e7ac3e03f7dc26cc93ab69c0ec49e591cbdaa7694c75682745c40bfca468", + input_genesis_hash: "0x4213963522f2d72fa8b33ab4a8b33d79f0d387999f97f38d5c93d9b047baa743", + genesis_state_root: "0x33a4f19a7d1bca010f6c3f17904e23f099dd2a022e1f1401fbffed27a1919370", }, ]; diff --git a/protocol/src/traits/consensus.rs b/protocol/src/traits/consensus.rs index 688e88260..c7d59dcc9 100644 --- a/protocol/src/traits/consensus.rs +++ b/protocol/src/traits/consensus.rs @@ -7,7 +7,7 @@ use common_crypto::Secp256k1PublicKey; use crate::types::{ Address, Block, BlockNumber, Bytes, ExecResp, HardforkInfoInner, Hash, Header, Hex, MerkleRoot, - Metadata, PackedTxHashes, Proof, Proposal, Receipt, SignedTransaction, Validator, U256, + Metadata, PackedTxHashes, Proof, Proposal, Receipt, SignedTransaction, ValidatorExtend, U256, }; use crate::{async_trait, traits::Context, ProtocolResult}; @@ -72,7 +72,7 @@ pub trait SynchronizationAdapter: CommonConsensusAdapter + Send + Sync { prevote_ratio: u64, precommit_ratio: u64, brake_ratio: u64, - validators: Vec, + validators: Vec, ) -> ProtocolResult<()>; /// Pull some blocks from other nodes from `begin` to `end`. diff --git a/protocol/src/types/primitive.rs b/protocol/src/types/primitive.rs index e0c2df4d0..17d4b796b 100644 --- a/protocol/src/types/primitive.rs +++ b/protocol/src/types/primitive.rs @@ -391,8 +391,8 @@ impl Ord for ValidatorExtend { } } -impl From for Validator { - fn from(ve: ValidatorExtend) -> Self { +impl From<&ValidatorExtend> for Validator { + fn from(ve: &ValidatorExtend) -> Self { Validator { pub_key: ve.pub_key.as_bytes(), propose_weight: ve.propose_weight,