From 47eb793ce9062fc3f7f4af429488ba4b63e2925e Mon Sep 17 00:00:00 2001 From: Luke Parker Date: Thu, 9 Jan 2025 06:58:00 -0500 Subject: [PATCH] Slash upon Tendermint evidence Decoding slash evidence requires specifying the instantiated generic `TendermintNetwork`. While irrelevant, that generic includes a type satisfying `tributary::P2p`. It was only possible to route now that we've redone the P2P API. --- coordinator/src/main.rs | 2 +- coordinator/src/tributary/scan.rs | 47 ++++++++++++++++++++----------- 2 files changed, 32 insertions(+), 17 deletions(-) diff --git a/coordinator/src/main.rs b/coordinator/src/main.rs index 9d555b71..8af0f824 100644 --- a/coordinator/src/main.rs +++ b/coordinator/src/main.rs @@ -1,7 +1,7 @@ mod tributary; mod p2p { - use serai_coordinator_p2p::*; + pub use serai_coordinator_p2p::*; pub use serai_coordinator_libp2p_p2p::Libp2p; } diff --git a/coordinator/src/tributary/scan.rs b/coordinator/src/tributary/scan.rs index bfc9760b..2c39cd13 100644 --- a/coordinator/src/tributary/scan.rs +++ b/coordinator/src/tributary/scan.rs @@ -1,4 +1,4 @@ -use core::future::Future; +use core::{marker::PhantomData, future::Future}; use std::collections::HashMap; use ciphersuite::group::GroupEncoding; @@ -22,20 +22,27 @@ use serai_task::ContinuallyRan; use messages::sign::VariantSignId; -use crate::tributary::{ - db::*, - transaction::{SigningProtocolRound, Signed, Transaction}, +use serai_cosign::Cosigning; + +use crate::{ + p2p::P2p, + tributary::{ + db::*, + transaction::{SigningProtocolRound, Signed, Transaction}, + }, }; -struct ScanBlock<'a, D: DbTxn, TD: Db> { - txn: &'a mut D, +struct ScanBlock<'a, D: Db, DT: DbTxn, TD: Db, P: P2p> { + _db: PhantomData, + _p2p: PhantomData

, + txn: &'a mut DT, set: ValidatorSet, validators: &'a [SeraiAddress], total_weight: u64, validator_weights: &'a HashMap, tributary: &'a TributaryReader, } -impl<'a, D: DbTxn, TD: Db> ScanBlock<'a, D, TD> { +impl<'a, D: Db, DT: DbTxn, TD: Db, P: P2p> ScanBlock<'a, D, DT, TD, P> { fn potentially_start_cosign(&mut self) { // Don't start a new cosigning instance if we're actively running one if TributaryDb::actively_cosigning(self.txn, self.set) { @@ -49,7 +56,11 @@ impl<'a, D: DbTxn, TD: Db> ScanBlock<'a, D, TD> { return; }; - let substrate_block_number = todo!("TODO"); + let Some(substrate_block_number) = + Cosigning::::finalized_block_number(self.txn, latest_substrate_block_to_cosign) + else { + panic!("cosigning a block our cosigner didn't index") + }; // Mark us as actively cosigning TributaryDb::start_cosigning(self.txn, self.set, substrate_block_number); @@ -320,12 +331,11 @@ impl<'a, D: DbTxn, TD: Db> ScanBlock<'a, D, TD> { Evidence::ConflictingMessages(first, second) => (first, Some(second)), Evidence::InvalidPrecommit(first) | Evidence::InvalidValidRound(first) => (first, None), }; - /* TODO let msgs = ( - decode_signed_message::>(&data.0).unwrap(), + decode_signed_message::>(&data.0).unwrap(), if data.1.is_some() { Some( - decode_signed_message::>(&data.1.unwrap()) + decode_signed_message::>(&data.1.unwrap()) .unwrap(), ) } else { @@ -336,9 +346,11 @@ impl<'a, D: DbTxn, TD: Db> ScanBlock<'a, D, TD> { // Since anything with evidence is fundamentally faulty behavior, not just temporal // errors, mark the node as fatally slashed TributaryDb::fatal_slash( - self.txn, msgs.0.msg.sender, &format!("invalid tendermint messages: {msgs:?}")); - */ - todo!("TODO") + self.txn, + self.set, + SeraiAddress(msgs.0.msg.sender), + &format!("invalid tendermint messages: {msgs:?}"), + ); } TributaryTransaction::Application(tx) => { self.handle_application_tx(block_number, tx); @@ -348,15 +360,16 @@ impl<'a, D: DbTxn, TD: Db> ScanBlock<'a, D, TD> { } } -struct ScanTributaryTask { +struct ScanTributaryTask { db: D, set: ValidatorSet, validators: Vec, total_weight: u64, validator_weights: HashMap, tributary: TributaryReader, + _p2p: PhantomData

, } -impl ContinuallyRan for ScanTributaryTask { +impl ContinuallyRan for ScanTributaryTask { fn run_iteration(&mut self) -> impl Send + Future> { async move { let (mut last_block_number, mut last_block_hash) = @@ -386,6 +399,8 @@ impl ContinuallyRan for ScanTributaryTask { let mut txn = self.db.txn(); (ScanBlock { + _db: PhantomData::, + _p2p: PhantomData::

, txn: &mut txn, set: self.set, validators: &self.validators,