Skip to content

Commit 20f32ea

Browse files
committed
Historic PublicKeys are now stored in memory.
Key Generation is not triggered anymore, if a older block gets verified.
1 parent 9297192 commit 20f32ea

File tree

1 file changed

+29
-44
lines changed

1 file changed

+29
-44
lines changed

crates/ethcore/src/engines/hbbft/hbbft_state.rs

Lines changed: 29 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ pub(crate) struct HbbftState {
4242
network_info: Option<NetworkInfo<NodeId>>,
4343
honey_badger: Option<HoneyBadger>,
4444
public_master_key: Option<PublicKey>,
45+
historic_public_keys: BTreeMap<u64, PublicKey>,
4546
current_posdao_epoch: u64,
4647
current_posdao_epoch_start_block: u64,
4748
last_fork_start_block: Option<u64>,
@@ -56,6 +57,7 @@ impl HbbftState {
5657
network_info: None,
5758
honey_badger: None,
5859
public_master_key: None,
60+
historic_public_keys: BTreeMap::new(),
5961
current_posdao_epoch: 0,
6062
current_posdao_epoch_start_block: 0,
6163
last_posdao_epoch_start_block: None,
@@ -131,6 +133,9 @@ impl HbbftState {
131133
self.public_master_key = Some(network_info.public_key_set().public_key());
132134
self.honey_badger = Some(self.new_honey_badger(network_info.clone())?);
133135

136+
self.historic_public_keys
137+
.insert(target_posdao_epoch, self.public_master_key.unwrap().clone());
138+
134139
for x in network_info.validator_set().all_ids() {
135140
info!(target: "engine", "Validator: {:?}", x);
136141
}
@@ -173,6 +178,8 @@ impl HbbftState {
173178

174179
let (pks, sks) = synckeygen.generate().ok()?;
175180
self.public_master_key = Some(pks.public_key());
181+
self.historic_public_keys
182+
.insert(target_posdao_epoch, pks.public_key());
176183
// Clear network info and honey badger instance, since we may not be in this POSDAO epoch any more.
177184
info!(target: "engine", "public master key: {:?}", pks.public_key());
178185

@@ -540,6 +547,8 @@ impl HbbftState {
540547
signature: &Signature,
541548
header: &Header,
542549
) -> bool {
550+
// maybe add the option: "not ready yet ?!"
551+
543552
self.skip_to_current_epoch(client.clone(), signer);
544553

545554
// Check if posdao epoch fits the parent block of the header seal to verify.
@@ -552,56 +561,32 @@ impl HbbftState {
552561
return false;
553562
}
554563
};
555-
if self.current_posdao_epoch != target_posdao_epoch {
564+
565+
if self.current_posdao_epoch > target_posdao_epoch {
556566
trace!(target: "consensus", "verify_seal - hbbft state epoch does not match epoch at the header's parent, attempting to reconstruct the appropriate public key share from scratch.");
557-
// If the requested block nr is already imported we try to generate the public master key from scratch.
558-
let posdao_epoch_start = match get_posdao_epoch_start(
559-
&*client,
560-
BlockId::Number(parent_block_nr),
561-
) {
562-
Ok(epoch_start) => epoch_start,
563-
Err(e) => {
564-
error!(target: "consensus", "Querying epoch start block failed with error: {:?}", e);
565-
return false;
566-
}
567-
};
568567

569-
let synckeygen = match initialize_synckeygen(
570-
&*client,
571-
&Arc::new(RwLock::new(Option::None)),
572-
BlockId::Number(posdao_epoch_start.low_u64()),
573-
ValidatorType::Current,
574-
) {
575-
Ok(synckeygen) => synckeygen,
576-
Err(e) => {
577-
let diff = parent_block_nr - posdao_epoch_start.low_u64();
578-
error!(target: "consensus", "Error: Synckeygen failed. parent block: {} epoch_start: {} diff {} with error: {:?}. current posdao: {:?} target epoch {:?}", parent_block_nr, posdao_epoch_start, diff, e, self.current_posdao_epoch, target_posdao_epoch);
568+
match self.historic_public_keys.get(&target_posdao_epoch) {
569+
Some(key) => {
570+
if key.verify(signature, header.bare_hash()) {
571+
return true;
572+
} else {
573+
error!(target: "consensus", "Failed to verify seal - historic public key verification failed!");
574+
return false;
575+
}
576+
}
577+
None => {
578+
warn!(target: "consensus", "unable to verifiy seal for historic block, public key not available.");
579579
return false;
580580
}
581-
};
582-
583-
if !synckeygen.is_ready() {
584-
error!(target: "consensus", "Synckeygen not ready when it sohuld be!");
585-
return false;
586581
}
587-
588-
let pks = match synckeygen.generate() {
589-
Ok((pks, _)) => pks,
590-
Err(e) => {
591-
error!(target: "consensus", "Generating of public key share failed with error: {:?}", e);
592-
return false;
582+
} else {
583+
// not a historic block, we can use the current public key.
584+
match self.public_master_key {
585+
Some(key) => key.verify(signature, header.bare_hash()),
586+
None => {
587+
error!(target: "consensus", "Failed to verify seal - public master key not available!");
588+
false
593589
}
594-
};
595-
596-
trace!(target: "consensus", "verify_seal - successfully reconstructed public key share of past posdao epoch.");
597-
return pks.public_key().verify(signature, header.bare_hash());
598-
}
599-
600-
match self.public_master_key {
601-
Some(key) => key.verify(signature, header.bare_hash()),
602-
None => {
603-
error!(target: "consensus", "Failed to verify seal - public master key not available!");
604-
false
605590
}
606591
}
607592
}

0 commit comments

Comments
 (0)