Provide a dedicated signature in Precommit of just the block hash

Greatly simplifies verifying when syncing.
This commit is contained in:
Luke Parker
2022-10-17 02:32:45 -04:00
parent 1c71e25234
commit b993ff1cc8
4 changed files with 93 additions and 35 deletions

View File

@@ -4,8 +4,17 @@ use crate::{ext::*, Round, Step, Data, Message, TendermintError};
pub(crate) struct MessageLog<N: Network> {
weights: Arc<N::Weights>,
precommitted: HashMap<N::ValidatorId, <N::Block as Block>::Id>,
log: HashMap<Round, HashMap<N::ValidatorId, HashMap<Step, Data<N::Block>>>>,
pub(crate) precommitted: HashMap<
N::ValidatorId,
(<N::Block as Block>::Id, <N::SignatureScheme as SignatureScheme>::Signature),
>,
log: HashMap<
Round,
HashMap<
N::ValidatorId,
HashMap<Step, Data<N::Block, <N::SignatureScheme as SignatureScheme>::Signature>>,
>,
>,
}
impl<N: Network> MessageLog<N> {
@@ -16,7 +25,7 @@ impl<N: Network> MessageLog<N> {
// Returns true if it's a new message
pub(crate) fn log(
&mut self,
msg: Message<N::ValidatorId, N::Block>,
msg: Message<N::ValidatorId, N::Block, <N::SignatureScheme as SignatureScheme>::Signature>,
) -> Result<bool, TendermintError<N::ValidatorId>> {
let round = self.log.entry(msg.round).or_insert_with(HashMap::new);
let msgs = round.entry(msg.sender).or_insert_with(HashMap::new);
@@ -31,13 +40,13 @@ impl<N: Network> MessageLog<N> {
}
// If they already precommitted to a distinct hash, error
if let Data::Precommit(Some(hash)) = msg.data {
if let Some(prev) = self.precommitted.get(&msg.sender) {
if hash != *prev {
if let Data::Precommit(Some((hash, sig))) = &msg.data {
if let Some((prev, _)) = self.precommitted.get(&msg.sender) {
if hash != prev {
Err(TendermintError::Malicious(msg.sender))?;
}
}
self.precommitted.insert(msg.sender, hash);
self.precommitted.insert(msg.sender, (*hash, sig.clone()));
}
msgs.insert(step, msg.data);
@@ -46,7 +55,11 @@ impl<N: Network> MessageLog<N> {
// For a given round, return the participating weight for this step, and the weight agreeing with
// the data.
pub(crate) fn message_instances(&self, round: Round, data: Data<N::Block>) -> (u64, u64) {
pub(crate) fn message_instances(
&self,
round: Round,
data: Data<N::Block, <N::SignatureScheme as SignatureScheme>::Signature>,
) -> (u64, u64) {
let mut participating = 0;
let mut weight = 0;
for (participant, msgs) in &self.log[&round] {
@@ -73,7 +86,11 @@ impl<N: Network> MessageLog<N> {
}
// Check if consensus has been reached on a specific piece of data
pub(crate) fn has_consensus(&self, round: Round, data: Data<N::Block>) -> bool {
pub(crate) fn has_consensus(
&self,
round: Round,
data: Data<N::Block, <N::SignatureScheme as SignatureScheme>::Signature>,
) -> bool {
let (_, weight) = self.message_instances(round, data);
weight >= self.weights.threshold()
}
@@ -83,7 +100,7 @@ impl<N: Network> MessageLog<N> {
round: Round,
sender: N::ValidatorId,
step: Step,
) -> Option<&Data<N::Block>> {
) -> Option<&Data<N::Block, <N::SignatureScheme as SignatureScheme>::Signature>> {
self.log.get(&round).and_then(|round| round.get(&sender).and_then(|msgs| msgs.get(&step)))
}
}