Skip to content
This repository has been archived by the owner on Sep 13, 2022. It is now read-only.

Commit

Permalink
refactor(consensus)!: replace Validator address bytes with pubkey byt…
Browse files Browse the repository at this point in the history
…es (#354)

* change(consensus): replace Validator address bytes with pubkey bytes

BREAKING CHANGE:
- replace Validator address bytes with pubkey bytes

* change(consensus): log validator address instead of its public key

Block proposer is address instead public key

* fix: compilation failed
  • Loading branch information
zeroqn authored Jul 15, 2020
1 parent 5907e13 commit e4433d7
Show file tree
Hide file tree
Showing 34 changed files with 338 additions and 307 deletions.
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ rand = "0.7"
cita_trie = "2.0"
core-network = { path = "./core/network", features = ["diagnostic"] }
tokio = { version = "0.2", features = ["full"] }
bs58 = "0.3"

[workspace]
members = [
Expand Down
3 changes: 2 additions & 1 deletion built-in-services/metadata/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,11 @@ serde = { version = "1.0", features = ["derive"] }
serde_json = "1.0"
rlp = "0.4"
bytes = "0.5"
derive_more = "0.15"
derive_more = "0.99"
byteorder = "1.3"

[dev-dependencies]
hex = "0.4"
cita_trie = "2.0"
async-trait = "0.1"
framework = { path = "../../framework" }
5 changes: 3 additions & 2 deletions built-in-services/metadata/src/tests/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,9 @@ fn mock_metadata() -> Metadata {
cycles_price: 1,
interval: 3000,
verifier_list: [ValidatorExtend {
bls_pub_key: Hex::from_string("0x04188ef9488c19458a963cc57b567adde7db8f8b6bec392d5cb7b67b0abc1ed6cd966edc451f6ac2ef38079460eb965e890d1f576e4039a20467820237cda753f07a8b8febae1ec052190973a1bcf00690ea8fc0168b3fbbccd1c4e402eda5ef22".to_owned()).unwrap(),
address: Address::from_hex("0xCAB8EEA4799C21379C20EF5BAA2CC8AF1BEC475B").unwrap(),
bls_pub_key: Hex::from_string("0x0401139331589f32220ec5f41f6faa0f5c3f4d36af011ab014cefd9d8f36b53b04a2031f681d1c9648a2a5d534d742931b0a5a4132da9ee752c1144d6396bed6cfc635c9687258cec9b60b387d35cf9e13f29091e11ae88024d74ca904c0ea3fb3".to_owned()).unwrap(),
pub_key: Hex::from_string("0x026c184a9016f6f71a234c86b141621f38b68c78602ab06768db4d83682c616004".to_owned()).unwrap(),
address: Address::from_hex("0x76961e339fe2f1f931d84c425754806fb4174c34").unwrap(),
propose_weight: 1,
vote_weight: 1,
}]
Expand Down
4 changes: 2 additions & 2 deletions core/api/src/schema/block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ pub struct Proof {
#[derive(juniper::GraphQLObject, Clone)]
#[graphql(description = "Validator address set")]
pub struct Validator {
pub address: Address,
pub pubkey: Bytes,
pub propose_weight: i32,
pub vote_weight: i32,
}
Expand Down Expand Up @@ -142,7 +142,7 @@ impl From<protocol::types::Proof> for Proof {
impl From<protocol::types::Validator> for Validator {
fn from(validator: protocol::types::Validator) -> Self {
Validator {
address: Address::from(validator.address),
pubkey: Bytes::from(validator.pub_key),
propose_weight: validator.vote_weight as i32,
vote_weight: validator.vote_weight as i32,
}
Expand Down
1 change: 0 additions & 1 deletion core/consensus/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@ lazy_static = "1.4"
num-traits = "0.2"
rand = "0.7"


[features]
default = []
random_leader = ["overlord/random_leader"]
35 changes: 20 additions & 15 deletions core/consensus/src/adapter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -548,11 +548,11 @@ where

let authority_map = previous_metadata
.verifier_list
.into_iter()
.iter()
.map(|v| {
let address = v.address.as_bytes();
let address = v.pub_key.decode();
let node = Node {
address: v.address.as_bytes(),
address: v.pub_key.decode(),
propose_weight: v.propose_weight,
vote_weight: v.vote_weight,
};
Expand All @@ -563,7 +563,10 @@ where
// TODO: useless check
// check proposer
if block.header.height != 0
&& !authority_map.contains_key(&block.header.proposer.as_bytes())
&& !previous_metadata
.verifier_list
.iter()
.any(|v| v.address == block.header.proposer)
{
log::error!(
"[consensus] verify_block_header, block.header.proposer: {:?}, authority_map: {:?}",
Expand All @@ -575,10 +578,12 @@ where

// check validators
for validator in block.header.validators.iter() {
if !authority_map.contains_key(&validator.address.as_bytes()) {
let validator_address = Address::from_pubkey_bytes(validator.pub_key.clone());

if !authority_map.contains_key(&validator.pub_key) {
log::error!(
"[consensus] verify_block_header, validator.address: {:?}, authority_map: {:?}",
validator.address,
validator_address,
authority_map
);
return Err(ConsensusError::VerifyBlockHeader(
Expand All @@ -587,14 +592,14 @@ where
)
.into());
} else {
let node = authority_map.get(&validator.address.as_bytes()).unwrap();
let node = authority_map.get(&validator.pub_key).unwrap();

if node.vote_weight != validator.vote_weight
|| node.propose_weight != validator.vote_weight
{
log::error!(
"[consensus] verify_block_header, validator.address: {:?}, authority_map: {:?}",
validator.address,
validator_address,
authority_map
);
return Err(ConsensusError::VerifyBlockHeader(
Expand Down Expand Up @@ -664,7 +669,7 @@ where
.verifier_list
.iter()
.map(|v| Node {
address: v.address.as_bytes(),
address: v.pub_key.decode(),
propose_weight: v.propose_weight,
vote_weight: v.vote_weight,
})
Expand All @@ -686,7 +691,7 @@ where
.iter()
.map(|node| (node.address.clone(), node.vote_weight))
.collect::<HashMap<overlord::types::Address, u32>>();
self.verity_proof_weight(
self.verify_proof_weight(
ctx.clone(),
block.header.height,
weight_map,
Expand All @@ -698,7 +703,7 @@ where
.verifier_list
.iter()
.filter_map(|v| {
if signed_voters.contains(&v.address.as_bytes()) {
if signed_voters.contains(&v.pub_key.decode()) {
Some(v.bls_pub_key.clone())
} else {
None
Expand Down Expand Up @@ -748,7 +753,7 @@ where
}

#[muta_apm::derive::tracing_span(kind = "consensus.adapter")]
fn verity_proof_weight(
fn verify_proof_weight(
&self,
ctx: Context,
block_height: u64,
Expand All @@ -765,15 +770,15 @@ where
.ok_or(ConsensusError::VerifyProof(block_height, WeightNotFound))
.map_err(|e| {
log::error!(
"[consensus] verity_proof_weight,signed_voter_address: {:?}",
"[consensus] verify_proof_weight,signed_voter_address: {:?}",
signed_voter_address
);
e
})?;
accumulator += u64::from(*(weight));
} else {
log::error!(
"[consensus] verity_proof_weight, weight not found, signed_voter_address: {:?}",
"[consensus] verify_proof_weight, weight not found, signed_voter_address: {:?}",
signed_voter_address
);
return Err(
Expand All @@ -784,7 +789,7 @@ where

if 3 * accumulator <= 2 * total_validator_weight {
log::error!(
"[consensus] verity_proof_weight, accumulator: {}, total: {}",
"[consensus] verify_proof_weight, accumulator: {}, total: {}",
accumulator,
total_validator_weight
);
Expand Down
14 changes: 4 additions & 10 deletions core/consensus/src/consensus.rs
Original file line number Diff line number Diff line change
Expand Up @@ -119,12 +119,7 @@ impl<Adapter: ConsensusAdapter + 'static> OverlordConsensus<Adapter> {
lock,
));

let overlord = Overlord::new(
node_info.self_address.as_bytes(),
Arc::clone(&engine),
crypto,
engine,
);
let overlord = Overlord::new(node_info.self_pub_key, Arc::clone(&engine), crypto, engine);
let overlord_handler = overlord.get_handler();
let status = status_agent.to_inner();

Expand Down Expand Up @@ -182,7 +177,7 @@ pub fn gen_overlord_status(
let mut authority_list = validators
.into_iter()
.map(|v| Node {
address: v.address.as_bytes(),
address: v.pub_key.clone(),
propose_weight: v.propose_weight,
vote_weight: v.vote_weight,
})
Expand Down Expand Up @@ -216,7 +211,7 @@ impl<T: overlord::Codec> OverlordMsgExt for OverlordMsg<T> {
OverlordMsg::AggregatedVote(av) => av.get_height().to_string(),
OverlordMsg::RichStatus(s) => s.height.to_string(),
OverlordMsg::SignedChoke(sc) => sc.choke.height.to_string(),
OverlordMsg::Stop => "".to_owned(),
_ => "".to_owned(),
}
}

Expand All @@ -225,9 +220,8 @@ impl<T: overlord::Codec> OverlordMsgExt for OverlordMsg<T> {
OverlordMsg::SignedProposal(sp) => sp.proposal.round.to_string(),
OverlordMsg::SignedVote(sv) => sv.get_round().to_string(),
OverlordMsg::AggregatedVote(av) => av.get_round().to_string(),
OverlordMsg::RichStatus(_) => "".to_owned(),
OverlordMsg::SignedChoke(sc) => sc.choke.round.to_string(),
OverlordMsg::Stop => "".to_owned(),
_ => "".to_owned(),
}
}
}
Expand Down
17 changes: 9 additions & 8 deletions core/consensus/src/engine.rs
Original file line number Diff line number Diff line change
Expand Up @@ -454,15 +454,16 @@ impl<Adapter: ConsensusAdapter + 'static> Engine<FixedPill> for ConsensusEngine<
/// Only signed vote will be transmit to the relayer.
#[muta_apm::derive::tracing_span(
kind = "consensus.engine",
logs = "{'address':
'Address::from_bytes(addr.clone()).unwrap().as_hex()'}"
logs = "{'pub_key': 'hex::encode(pub_key.clone())'}"
)]
async fn transmit_to_relayer(
&self,
ctx: Context,
addr: Bytes,
pub_key: Bytes,
msg: OverlordMsg<FixedPill>,
) -> Result<(), Box<dyn Error + Send>> {
let address = Address::from_pubkey_bytes(pub_key)?;

match msg {
OverlordMsg::SignedVote(sv) => {
let msg = sv.rlp_bytes();
Expand All @@ -471,7 +472,7 @@ impl<Adapter: ConsensusAdapter + 'static> Engine<FixedPill> for ConsensusEngine<
ctx,
msg,
END_GOSSIP_SIGNED_VOTE,
MessageTarget::Specified(Address::from_bytes(addr)?),
MessageTarget::Specified(address),
)
.await?;
}
Expand All @@ -482,7 +483,7 @@ impl<Adapter: ConsensusAdapter + 'static> Engine<FixedPill> for ConsensusEngine<
ctx,
msg,
END_GOSSIP_AGGREGATED_VOTE,
MessageTarget::Specified(Address::from_bytes(addr)?),
MessageTarget::Specified(address),
)
.await?;
}
Expand Down Expand Up @@ -521,7 +522,7 @@ impl<Adapter: ConsensusAdapter + 'static> Engine<FixedPill> for ConsensusEngine<
.verifier_list
.into_iter()
.map(|v| Node {
address: v.address.as_bytes(),
address: v.pub_key.decode(),
propose_weight: v.propose_weight,
vote_weight: v.vote_weight,
})
Expand Down Expand Up @@ -760,7 +761,7 @@ impl<Adapter: ConsensusAdapter + 'static> ConsensusEngine<Adapter> {
pub fn generate_new_crypto_map(metadata: Metadata) -> ProtocolResult<HashMap<Bytes, BlsPublicKey>> {
let mut new_addr_pubkey_map = HashMap::new();
for validator in metadata.verifier_list.into_iter() {
let addr = validator.address.as_bytes();
let addr = validator.pub_key.decode();
let hex_pubkey = hex::decode(validator.bls_pub_key.as_string_trim0x()).map_err(|err| {
ConsensusError::Other(format!("hex decode metadata bls pubkey error {:?}", err))
})?;
Expand All @@ -775,7 +776,7 @@ fn covert_to_overlord_authority(validators: &[Validator]) -> Vec<Node> {
let mut authority = validators
.iter()
.map(|v| Node {
address: v.address.as_bytes(),
address: v.pub_key.clone(),
propose_weight: v.propose_weight,
vote_weight: v.vote_weight,
})
Expand Down
2 changes: 1 addition & 1 deletion core/consensus/src/status.rs
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ impl CurrentConsensusStatus {
.verifier_list
.iter()
.map(|v| Validator {
address: v.address.clone(),
pub_key: v.pub_key.decode(),
propose_weight: v.propose_weight,
vote_weight: v.vote_weight,
})
Expand Down
21 changes: 4 additions & 17 deletions core/consensus/src/synchronization.rs
Original file line number Diff line number Diff line change
Expand Up @@ -156,8 +156,7 @@ impl<Adapter: SynchronizationAdapter> OverlordSynchronization<Adapter> {

#[muta_apm::derive::tracing_span(
kind = "consensus.sync",
logs = "{'current_height': 'current_height', 'remote_height':
'remote_height'}"
logs = "{'current_height': 'current_height', 'remote_height': 'remote_height'}"
)]
async fn start_sync(
&self,
Expand Down Expand Up @@ -364,11 +363,7 @@ impl<Adapter: SynchronizationAdapter> OverlordSynchronization<Adapter> {
Ok(())
}

#[muta_apm::derive::tracing_span(
kind = "consensus.sync",
logs = "{'height':
'height'}"
)]
#[muta_apm::derive::tracing_span(kind = "consensus.sync", logs = "{'height': 'height'}")]
async fn get_rich_block_from_remote(
&self,
ctx: Context,
Expand All @@ -390,22 +385,14 @@ impl<Adapter: SynchronizationAdapter> OverlordSynchronization<Adapter> {
Ok(RichBlock { block, txs })
}

#[muta_apm::derive::tracing_span(
kind = "consensus.sync",
logs = "{'height':
'height'}"
)]
#[muta_apm::derive::tracing_span(kind = "consensus.sync", logs = "{'height': 'height'}")]
async fn get_block_from_remote(&self, ctx: Context, height: u64) -> ProtocolResult<Block> {
self.adapter
.get_block_from_remote(ctx.clone(), height)
.await
}

#[muta_apm::derive::tracing_span(
kind = "consensus.sync",
logs = "{'txs_len':
'txs.len()'}"
)]
#[muta_apm::derive::tracing_span(kind = "consensus.sync", logs = "{'txs_len': 'txs.len()'}")]
async fn save_chain_data(
&self,
ctx: Context,
Expand Down
10 changes: 9 additions & 1 deletion core/consensus/src/tests/status.rs
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,7 @@ fn mock_validators_extend(len: usize) -> Vec<ValidatorExtend> {
"0xd654c7a6747fc2e34808c1ebb1510bfb19b443d639f2fab6dc41fce9f634de37".to_string(),
)
.unwrap(),
pub_key: mock_pub_key(),
address: mock_address(),
propose_weight: random::<u32>(),
vote_weight: random::<u32>(),
Expand Down Expand Up @@ -201,7 +202,7 @@ fn mock_validators(len: usize) -> Vec<Validator> {

fn mock_validator() -> Validator {
Validator {
address: mock_address(),
pub_key: mock_pub_key().decode(),
propose_weight: random::<u32>(),
vote_weight: random::<u32>(),
}
Expand Down Expand Up @@ -230,6 +231,13 @@ fn mock_address() -> Address {
Address::from_hash(hash).unwrap()
}

fn mock_pub_key() -> Hex {
Hex::from_string(
"0x026c184a9016f6f71a234c86b141621f38b68c78602ab06768db4d83682c616004".to_owned(),
)
.unwrap()
}

fn get_random_bytes(len: usize) -> Bytes {
let vec: Vec<u8> = (0..len).map(|_| random::<u8>()).collect();
Bytes::from(vec)
Expand Down
Loading

0 comments on commit e4433d7

Please sign in to comment.