Skip to content

Commit

Permalink
refactor: change the node address from public key to address
Browse files Browse the repository at this point in the history
  • Loading branch information
Eason committed Oct 11, 2023
1 parent a3432b4 commit 1471fcf
Show file tree
Hide file tree
Showing 10 changed files with 44 additions and 39 deletions.
10 changes: 6 additions & 4 deletions core/consensus/src/adapter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};

Expand Down Expand Up @@ -140,7 +140,7 @@ where
prevote_ratio: u64,
precommit_ratio: u64,
brake_ratio: u64,
validators: Vec<Validator>,
validators: Vec<ValidatorExtend>,
) -> ProtocolResult<()> {
self.overlord_handler
.read()
Expand Down Expand Up @@ -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,
})
Expand Down
19 changes: 11 additions & 8 deletions core/consensus/src/consensus.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::{
Expand Down Expand Up @@ -126,7 +125,7 @@ impl<Adapter: ConsensusAdapter + 'static> OverlordConsensus<Adapter> {
.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,
Expand All @@ -144,7 +143,7 @@ impl<Adapter: ConsensusAdapter + 'static> OverlordConsensus<Adapter> {
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();
Expand All @@ -164,13 +163,15 @@ impl<Adapter: ConsensusAdapter + 'static> OverlordConsensus<Adapter> {
&self,
init_height: u64,
interval: u64,
validators: Vec<Validator>,
validators: Vec<ValidatorExtend>,
timer_config: Option<DurationConfig>,
) -> 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,
})
Expand All @@ -192,12 +193,14 @@ pub fn gen_overlord_status(
prevote_ratio: u64,
precommit_ratio: u64,
brake_ratio: u64,
validators: Vec<Validator>,
validators: Vec<ValidatorExtend>,
) -> 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,
})
Expand Down
8 changes: 6 additions & 2 deletions core/consensus/src/engine.rs
Original file line number Diff line number Diff line change
Expand Up @@ -449,11 +449,13 @@ impl<Adapter: ConsensusAdapter + 'static> Engine<Proposal> for ConsensusEngine<A
.await?
};

// The address is calculated by the validator's secp256k1 uncompressed public
// key, not blst public key.
let mut old_validators = old_metadata
.verifier_list
.into_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,
})
Expand Down Expand Up @@ -784,10 +786,12 @@ pub fn generate_new_crypto_map(metadata: Metadata) -> ProtocolResult<HashMap<Byt
}

fn convert_to_overlord_authority(validators: &[ValidatorExtend]) -> Vec<Node> {
// 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,
})
Expand Down
2 changes: 1 addition & 1 deletion core/consensus/src/synchronization.rs
Original file line number Diff line number Diff line change
Expand Up @@ -518,7 +518,7 @@ impl<Adapter: SynchronizationAdapter> OverlordSynchronization<Adapter> {
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!(
Expand Down
3 changes: 2 additions & 1 deletion core/consensus/src/tests/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -145,7 +146,7 @@ impl SynchronizationAdapter for MockSyncAdapter {
prevote_ratio: u64,
precommit_ratio: u64,
brake_ratio: u64,
validators: Vec<Validator>,
validators: Vec<ValidatorExtend>,
) -> ProtocolResult<()> {
Ok(())
}
Expand Down
3 changes: 3 additions & 0 deletions core/consensus/src/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<HashMap<Bytes, BlsPublicKey>>,
Expand Down
18 changes: 5 additions & 13 deletions core/run/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};

Expand Down Expand Up @@ -204,15 +204,7 @@ async fn start<K: KeyProvider>(
metadata_handle.init_hardfork(current_block.header.number)?;

let metadata = metadata_handle.get_metadata_by_block_number(current_block.header.number)?;
let validators: Vec<Validator> = 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::<Vec<_>>();
let validators = metadata.verifier_list.clone();

// Set args in mempool
mempool.set_args(
Expand Down Expand Up @@ -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());
Expand Down Expand Up @@ -411,7 +403,7 @@ async fn get_status_agent(

fn run_overlord_consensus<M, N, S, DB>(
metadata: Metadata,
validators: Vec<Validator>,
validators: Vec<ValidatorExtend>,
current_block: Block,
overlord_consensus: Arc<OverlordConsensus<OverlordConsensusAdapter<M, N, S, DB>>>,
) where
Expand Down
12 changes: 6 additions & 6 deletions core/run/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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",
},
];

Expand Down
4 changes: 2 additions & 2 deletions protocol/src/traits/consensus.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};

Expand Down Expand Up @@ -72,7 +72,7 @@ pub trait SynchronizationAdapter: CommonConsensusAdapter + Send + Sync {
prevote_ratio: u64,
precommit_ratio: u64,
brake_ratio: u64,
validators: Vec<Validator>,
validators: Vec<ValidatorExtend>,
) -> ProtocolResult<()>;

/// Pull some blocks from other nodes from `begin` to `end`.
Expand Down
4 changes: 2 additions & 2 deletions protocol/src/types/primitive.rs
Original file line number Diff line number Diff line change
Expand Up @@ -391,8 +391,8 @@ impl Ord for ValidatorExtend {
}
}

impl From<ValidatorExtend> 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,
Expand Down

0 comments on commit 1471fcf

Please sign in to comment.