From 7ec2f5c4b718c7ff149bcafa47116d98e7d9c685 Mon Sep 17 00:00:00 2001 From: Eason Date: Wed, 11 Oct 2023 17:23:02 +0800 Subject: [PATCH] fix unit test --- core/consensus/src/adapter.rs | 1 + core/consensus/src/consensus.rs | 4 +++- core/consensus/src/engine.rs | 2 ++ core/consensus/src/synchronization.rs | 2 +- core/consensus/src/util.rs | 5 +++++ core/run/src/lib.rs | 10 +--------- core/run/src/tests.rs | 12 ++++++------ protocol/src/types/primitive.rs | 4 ++-- 8 files changed, 21 insertions(+), 19 deletions(-) diff --git a/core/consensus/src/adapter.rs b/core/consensus/src/adapter.rs index 452682824..8aae32ec8 100644 --- a/core/consensus/src/adapter.rs +++ b/core/consensus/src/adapter.rs @@ -502,6 +502,7 @@ where .into()); } + // The address field of Node struct should use the node's secp256k1 public key let mut authority_list = metadata .verifier_list .iter() diff --git a/core/consensus/src/consensus.rs b/core/consensus/src/consensus.rs index a9803b6d7..78997bddf 100644 --- a/core/consensus/src/consensus.rs +++ b/core/consensus/src/consensus.rs @@ -144,7 +144,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.iter().map(Into::into).collect(), )), ) .unwrap(); @@ -167,6 +167,7 @@ impl OverlordConsensus { validators: Vec, timer_config: Option, ) -> ProtocolResult<()> { + // The address field of Node struct should use the node's secp256k1 public key let authority_list = validators .into_iter() .map(|v| Node { @@ -194,6 +195,7 @@ pub fn gen_overlord_status( brake_ratio: u64, validators: Vec, ) -> Status { + // The address field of Node struct should use the node's secp256k1 public key let mut authority_list = validators .into_iter() .map(|v| Node { diff --git a/core/consensus/src/engine.rs b/core/consensus/src/engine.rs index e5dc4ce66..5c2401b80 100644 --- a/core/consensus/src/engine.rs +++ b/core/consensus/src/engine.rs @@ -449,6 +449,7 @@ impl Engine for ConsensusEngine ProtocolResult Vec { + // The address field of Node struct should use the node's secp256k1 public key let mut authority = validators .iter() .map(|v| Node { diff --git a/core/consensus/src/synchronization.rs b/core/consensus/src/synchronization.rs index 84f50a42f..c92523094 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.iter().map(Into::into).collect(), )?; log::info!( diff --git a/core/consensus/src/util.rs b/core/consensus/src/util.rs index 9364b2d0c..97313aefe 100644 --- a/core/consensus/src/util.rs +++ b/core/consensus/src/util.rs @@ -31,6 +31,11 @@ pub fn time_now() -> u64 { .as_secs() } +/// The `private_key` is the blst private key of the node. The `addr_pubkey` is +/// a map to get the blst public key by the address. To be notice that the +/// address uses secp256k1 **public key** which is same as the `address` field +/// in `Node` struct. Use secp256k1 public key instead of address can reduce the +/// `keccak256` hash calculation at the end of each height. 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..462def7f2 100644 --- a/core/run/src/lib.rs +++ b/core/run/src/lib.rs @@ -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: Vec = metadata.verifier_list.iter().map(Into::into).collect(); // Set args in mempool mempool.set_args( 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/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,