Skip to content
This repository was archived by the owner on Nov 11, 2022. It is now read-only.

Milestone 1 #144

Merged
merged 13 commits into from
Apr 14, 2021
21 changes: 14 additions & 7 deletions beefy-gadget/src/worker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,7 @@ where
finality_notifications: FinalityNotifications<B>,
gossip_engine: Arc<Mutex<GossipEngine<B>>>,
signed_commitment_sender: notification::BeefySignedCommitmentSender<B, P::Signature>,
best_finalized_block: NumberFor<B>,
best_finalized_block: Option<NumberFor<B>>,
best_block_voted_on: NumberFor<B>,
client: Arc<C>,
metrics: Option<Metrics>,
Expand Down Expand Up @@ -236,7 +236,7 @@ where
finality_notifications: client.finality_notification_stream(),
gossip_engine: Arc::new(Mutex::new(gossip_engine)),
signed_commitment_sender,
best_finalized_block: Zero::zero(),
best_finalized_block: None,
best_block_voted_on: Zero::zero(),
client,
metrics,
Expand All @@ -247,7 +247,8 @@ where
}

fn init_validator_set(&mut self) -> Result<(), error::Lifecycle> {
let at = BlockId::hash(self.client.info().best_hash);
let info = self.client.info();
let at = BlockId::hash(info.finalized_hash);

let validator_set = self
.client
Expand Down Expand Up @@ -278,7 +279,7 @@ where
// we are actually interested in the best finalized block with the BEEFY pallet
// being available on-chain. That is why we set `best_finalized_block` here and
// not as part of `new()` already.
self.best_finalized_block = self.client.info().finalized_number;
self.best_finalized_block = Some(info.finalized_number);

debug!(target: "beefy", "🥩 Validator set with id {} initialized", validator_set.id);

Expand All @@ -304,7 +305,13 @@ where
return false;
}

let diff = self.best_finalized_block.saturating_sub(self.best_block_voted_on);
let best_finalized = if let Some(block) = self.best_finalized_block {
block
} else {
return false;
};

let diff = best_finalized.saturating_sub(self.best_block_voted_on);
let diff = diff.saturated_into::<u32>();
let next_power_of_two = (diff / 2).next_power_of_two();
let next_block_to_vote_on = self.best_block_voted_on + self.min_interval.max(next_power_of_two).into();
Expand Down Expand Up @@ -349,7 +356,7 @@ where
// NOTE: currently we act as if this block has been finalized by BEEFY as we perform
// the validator set changes instantly (insecure). Once proper validator set changes
// are implemented this should be removed
self.best_finalized_block = *notification.header.number();
self.best_finalized_block = Some(*notification.header.number());
};

if self.should_vote_on(*notification.header.number()) {
Expand Down Expand Up @@ -426,7 +433,7 @@ where
info!(target: "beefy", "🥩 Round #{} concluded, committed: {:?}.", round.1, signed_commitment);

self.signed_commitment_sender.notify(signed_commitment);
self.best_finalized_block = round.1;
self.best_finalized_block = Some(round.1);
}
}
}
Expand Down