2023-11-22 13:17:51 +04:00
|
|
|
use std::collections::HashMap;
|
2023-08-14 06:08:55 -04:00
|
|
|
|
Coordinator Cleanup (#481)
* Move logic for evaluating if a cosign should occur to its own file
Cleans it up and makes it more robust.
* Have expected_next_batch return an error instead of retrying
While convenient to offer an error-free implementation, it potentially caused
very long lived lock acquisitions in handle_processor_message.
* Unify and clean DkgConfirmer and DkgRemoval
Does so via adding a new file for the common code, SigningProtocol.
Modifies from_cache to return the preprocess with the machine, as there's no
reason not to. Also removes an unused Result around the type.
Clarifies the security around deterministic nonces, removing them for
saved-to-disk cached preprocesses. The cached preprocesses are encrypted as the
DB is not a proper secret store.
Moves arguments always present in the protocol from function arguments into the
struct itself.
Removes the horribly ugly code in DkgRemoval, fixing multiple issues present
with it which would cause it to fail on use.
* Set SeraiBlockNumber in cosign.rs as it's used by the cosigning protocol
* Remove unnecessary Clone from lambdas in coordinator
* Remove the EventDb from Tributary scanner
We used per-Transaction DB TXNs so on error, we don't have to rescan the entire
block yet only the rest of it. We prevented scanning multiple transactions by
tracking which we already had.
This is over-engineered and not worth it.
* Implement borsh for HasEvents, removing the manual encoding
* Merge DkgConfirmer and DkgRemoval into signing_protocol.rs
Fixes a bug in DkgConfirmer which would cause it to improperly handle indexes
if any validator had multiple key shares.
* Strictly type DataSpecification's Label
* Correct threshold_i_map_to_keys_and_musig_i_map
It didn't include the participant's own index and accordingly was offset.
* Create TributaryBlockHandler
This struct contains all variables prior passed to handle_block and stops them
from being passed around again and again.
This also ensures fatal_slash is only called while handling a block, as needed
as it expects to operate under perfect consensus.
* Inline accumulate, store confirmation nonces with shares
Inlining accumulate makes sense due to the amount of data accumulate needed to
be passed.
Storing confirmation nonces with shares ensures that both are available or
neither. Prior, one could be yet the other may not have been (requiring an
assert in runtime to ensure we didn't bungle it somehow).
* Create helper functions for handling DkgRemoval/SubstrateSign/Sign Tributary TXs
* Move Label into SignData
All of our transactions which use SignData end up with the same common usage
pattern for Label, justifying this.
Removes 3 transactions, explicitly de-duplicating their handlers.
* Remove CurrentlyCompletingKeyPair for the non-contextual DkgKeyPair
* Remove the manual read/write for TributarySpec for borsh
This struct doesn't have any optimizations booned by the manual impl. Using
borsh reduces our scope.
* Use temporary variables to further minimize LoC in tributary handler
* Remove usage of tuples for non-trivial Tributary transactions
* Remove serde from dkg
serde could be used to deserialize intenrally inconsistent objects which could
lead to panics or faults.
The BorshDeserialize derives have been replaced with a manual implementation
which won't produce inconsistent objects.
* Abstract Future generics using new trait definitions in coordinator
* Move published_signed_transaction to tributary/mod.rs to reduce the size of main.rs
* Split coordinator/src/tributary/mod.rs into spec.rs and transaction.rs
2023-12-10 20:21:44 -05:00
|
|
|
use scale::Encode;
|
2023-12-12 12:28:53 -05:00
|
|
|
use borsh::{BorshSerialize, BorshDeserialize};
|
Coordinator Cleanup (#481)
* Move logic for evaluating if a cosign should occur to its own file
Cleans it up and makes it more robust.
* Have expected_next_batch return an error instead of retrying
While convenient to offer an error-free implementation, it potentially caused
very long lived lock acquisitions in handle_processor_message.
* Unify and clean DkgConfirmer and DkgRemoval
Does so via adding a new file for the common code, SigningProtocol.
Modifies from_cache to return the preprocess with the machine, as there's no
reason not to. Also removes an unused Result around the type.
Clarifies the security around deterministic nonces, removing them for
saved-to-disk cached preprocesses. The cached preprocesses are encrypted as the
DB is not a proper secret store.
Moves arguments always present in the protocol from function arguments into the
struct itself.
Removes the horribly ugly code in DkgRemoval, fixing multiple issues present
with it which would cause it to fail on use.
* Set SeraiBlockNumber in cosign.rs as it's used by the cosigning protocol
* Remove unnecessary Clone from lambdas in coordinator
* Remove the EventDb from Tributary scanner
We used per-Transaction DB TXNs so on error, we don't have to rescan the entire
block yet only the rest of it. We prevented scanning multiple transactions by
tracking which we already had.
This is over-engineered and not worth it.
* Implement borsh for HasEvents, removing the manual encoding
* Merge DkgConfirmer and DkgRemoval into signing_protocol.rs
Fixes a bug in DkgConfirmer which would cause it to improperly handle indexes
if any validator had multiple key shares.
* Strictly type DataSpecification's Label
* Correct threshold_i_map_to_keys_and_musig_i_map
It didn't include the participant's own index and accordingly was offset.
* Create TributaryBlockHandler
This struct contains all variables prior passed to handle_block and stops them
from being passed around again and again.
This also ensures fatal_slash is only called while handling a block, as needed
as it expects to operate under perfect consensus.
* Inline accumulate, store confirmation nonces with shares
Inlining accumulate makes sense due to the amount of data accumulate needed to
be passed.
Storing confirmation nonces with shares ensures that both are available or
neither. Prior, one could be yet the other may not have been (requiring an
assert in runtime to ensure we didn't bungle it somehow).
* Create helper functions for handling DkgRemoval/SubstrateSign/Sign Tributary TXs
* Move Label into SignData
All of our transactions which use SignData end up with the same common usage
pattern for Label, justifying this.
Removes 3 transactions, explicitly de-duplicating their handlers.
* Remove CurrentlyCompletingKeyPair for the non-contextual DkgKeyPair
* Remove the manual read/write for TributarySpec for borsh
This struct doesn't have any optimizations booned by the manual impl. Using
borsh reduces our scope.
* Use temporary variables to further minimize LoC in tributary handler
* Remove usage of tuples for non-trivial Tributary transactions
* Remove serde from dkg
serde could be used to deserialize intenrally inconsistent objects which could
lead to panics or faults.
The BorshDeserialize derives have been replaced with a manual implementation
which won't produce inconsistent objects.
* Abstract Future generics using new trait definitions in coordinator
* Move published_signed_transaction to tributary/mod.rs to reduce the size of main.rs
* Split coordinator/src/tributary/mod.rs into spec.rs and transaction.rs
2023-12-10 20:21:44 -05:00
|
|
|
|
2025-01-02 09:11:04 -05:00
|
|
|
use serai_client::{primitives::SeraiAddress, validator_sets::primitives::ValidatorSet};
|
2023-04-20 05:05:17 -04:00
|
|
|
|
2025-01-03 06:57:28 -05:00
|
|
|
use messages::sign::{VariantSignId, SignId};
|
2023-08-14 06:08:55 -04:00
|
|
|
|
2025-01-02 09:11:04 -05:00
|
|
|
use serai_db::*;
|
Add a cosigning protocol to ensure finalizations are unique (#433)
* Add a function to deterministically decide which Serai blocks should be co-signed
Has a 5 minute latency between co-signs, also used as the maximal latency
before a co-sign is started.
* Get all active tributaries we're in at a specific block
* Add and route CosignSubstrateBlock, a new provided TX
* Split queued cosigns per network
* Rename BatchSignId to SubstrateSignId
* Add SubstrateSignableId, a meta-type for either Batch or Block, and modularize around it
* Handle the CosignSubstrateBlock provided TX
* Revert substrate_signer.rs to develop (and patch to still work)
Due to SubstrateSigner moving when the prior multisig closes, yet cosigning
occurring with the most recent key, a single SubstrateSigner can be reused.
We could manage multiple SubstrateSigners, yet considering the much lower
specifications for cosigning, I'd rather treat it distinctly.
* Route cosigning through the processor
* Add note to rename SubstrateSigner post-PR
I don't want to do so now in order to preserve the diff's clarity.
* Implement cosign evaluation into the coordinator
* Get tests to compile
* Bug fixes, mark blocks without cosigners available as cosigned
* Correct the ID Batch preprocesses are saved under, add log statements
* Create a dedicated function to handle cosigns
* Correct the flow around Batch verification/queueing
Verifying `Batch`s could stall when a `Batch` was signed before its
predecessors/before the block it's contained in was cosigned (the latter being
inevitable as we can't sign a block containing a signed batch before signing
the batch).
Now, Batch verification happens on a distinct async task in order to not block
the handling of processor messages. This task is the sole caller of verify in
order to ensure last_verified_batch isn't unexpectedly mutated.
When the processor message handler needs to access it, or needs to queue a
Batch, it associates the DB TXN with a lock preventing the other task from
doing so.
This lock, as currently implemented, is a poor and inefficient design. It
should be modified to the pattern used for cosign management. Additionally, a
new primitive of a DB-backed channel may be immensely valuable.
Fixes a standing potential deadlock and a deadlock introduced with the
cosigning protocol.
* Working full-stack tests
After the last commit, this only required extending a timeout.
* Replace "co-sign" with "cosign" to make finding text easier
* Update the coordinator tests to support cosigning
* Inline prior_batch calculation to prevent panic on rotation
Noticed when doing a final review of the branch.
2023-11-15 16:57:21 -05:00
|
|
|
|
2025-01-12 05:52:33 -05:00
|
|
|
use serai_cosign::CosignIntent;
|
|
|
|
|
|
2025-01-11 04:14:21 -05:00
|
|
|
use crate::transaction::SigningProtocolRound;
|
2023-10-24 03:00:37 -04:00
|
|
|
|
2025-01-02 09:11:04 -05:00
|
|
|
/// A topic within the database which the group participates in
|
2023-12-12 12:28:53 -05:00
|
|
|
#[derive(Clone, Copy, PartialEq, Eq, Debug, Encode, BorshSerialize, BorshDeserialize)]
|
2025-01-15 07:01:24 -05:00
|
|
|
pub enum Topic {
|
2025-01-02 09:11:04 -05:00
|
|
|
/// Vote to remove a participant
|
2025-01-15 07:01:24 -05:00
|
|
|
RemoveParticipant {
|
|
|
|
|
/// The participant to remove
|
|
|
|
|
participant: SeraiAddress,
|
|
|
|
|
},
|
2023-09-01 00:59:10 -04:00
|
|
|
|
2025-01-02 09:11:04 -05:00
|
|
|
// DkgParticipation isn't represented here as participations are immediately sent to the
|
|
|
|
|
// processor, not accumulated within this databse
|
|
|
|
|
/// Participation in the signing protocol to confirm the DKG results on Substrate
|
2025-01-15 07:01:24 -05:00
|
|
|
DkgConfirmation {
|
|
|
|
|
/// The attempt number this is for
|
|
|
|
|
attempt: u32,
|
|
|
|
|
/// The round of the signing protocol
|
|
|
|
|
round: SigningProtocolRound,
|
|
|
|
|
},
|
2023-09-01 00:59:10 -04:00
|
|
|
|
2025-01-02 09:11:04 -05:00
|
|
|
/// The local view of the SlashReport, to be aggregated into the final SlashReport
|
|
|
|
|
SlashReport,
|
2023-04-20 05:05:17 -04:00
|
|
|
|
2025-01-02 09:11:04 -05:00
|
|
|
/// Participation in a signing protocol
|
2025-01-15 07:01:24 -05:00
|
|
|
Sign {
|
|
|
|
|
/// The ID of the signing protocol
|
|
|
|
|
id: VariantSignId,
|
|
|
|
|
/// The attempt number this is for
|
|
|
|
|
attempt: u32,
|
|
|
|
|
/// The round of the signing protocol
|
|
|
|
|
round: SigningProtocolRound,
|
|
|
|
|
},
|
2023-11-22 13:17:51 +04:00
|
|
|
}
|
2023-10-15 00:32:51 -04:00
|
|
|
|
2025-01-02 09:11:04 -05:00
|
|
|
enum Participating {
|
|
|
|
|
Participated,
|
|
|
|
|
Everyone,
|
|
|
|
|
}
|
2023-12-18 09:13:09 -05:00
|
|
|
|
2025-01-02 09:11:04 -05:00
|
|
|
impl Topic {
|
|
|
|
|
// The topic used by the next attempt of this protocol
|
|
|
|
|
fn next_attempt_topic(self) -> Option<Topic> {
|
|
|
|
|
#[allow(clippy::match_same_arms)]
|
|
|
|
|
match self {
|
|
|
|
|
Topic::RemoveParticipant { .. } => None,
|
2025-01-03 06:57:28 -05:00
|
|
|
Topic::DkgConfirmation { attempt, round: _ } => Some(Topic::DkgConfirmation {
|
2025-01-02 09:11:04 -05:00
|
|
|
attempt: attempt + 1,
|
2025-01-03 06:57:28 -05:00
|
|
|
round: SigningProtocolRound::Preprocess,
|
2025-01-02 09:11:04 -05:00
|
|
|
}),
|
|
|
|
|
Topic::SlashReport { .. } => None,
|
2025-01-03 06:57:28 -05:00
|
|
|
Topic::Sign { id, attempt, round: _ } => {
|
|
|
|
|
Some(Topic::Sign { id, attempt: attempt + 1, round: SigningProtocolRound::Preprocess })
|
2025-01-02 09:11:04 -05:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2023-12-18 09:13:09 -05:00
|
|
|
|
2025-01-02 09:11:04 -05:00
|
|
|
// The topic for the re-attempt to schedule
|
|
|
|
|
fn reattempt_topic(self) -> Option<(u32, Topic)> {
|
|
|
|
|
#[allow(clippy::match_same_arms)]
|
|
|
|
|
match self {
|
|
|
|
|
Topic::RemoveParticipant { .. } => None,
|
2025-01-03 06:57:28 -05:00
|
|
|
Topic::DkgConfirmation { attempt, round } => match round {
|
2025-01-02 09:11:04 -05:00
|
|
|
SigningProtocolRound::Preprocess => {
|
|
|
|
|
let attempt = attempt + 1;
|
|
|
|
|
Some((
|
|
|
|
|
attempt,
|
2025-01-03 06:57:28 -05:00
|
|
|
Topic::DkgConfirmation { attempt, round: SigningProtocolRound::Preprocess },
|
2025-01-02 09:11:04 -05:00
|
|
|
))
|
|
|
|
|
}
|
|
|
|
|
SigningProtocolRound::Share => None,
|
|
|
|
|
},
|
|
|
|
|
Topic::SlashReport { .. } => None,
|
2025-01-03 06:57:28 -05:00
|
|
|
Topic::Sign { id, attempt, round } => match round {
|
2025-01-02 09:11:04 -05:00
|
|
|
SigningProtocolRound::Preprocess => {
|
|
|
|
|
let attempt = attempt + 1;
|
2025-01-03 06:57:28 -05:00
|
|
|
Some((attempt, Topic::Sign { id, attempt, round: SigningProtocolRound::Preprocess }))
|
2025-01-02 09:11:04 -05:00
|
|
|
}
|
|
|
|
|
SigningProtocolRound::Share => None,
|
|
|
|
|
},
|
|
|
|
|
}
|
|
|
|
|
}
|
Coordinator Cleanup (#481)
* Move logic for evaluating if a cosign should occur to its own file
Cleans it up and makes it more robust.
* Have expected_next_batch return an error instead of retrying
While convenient to offer an error-free implementation, it potentially caused
very long lived lock acquisitions in handle_processor_message.
* Unify and clean DkgConfirmer and DkgRemoval
Does so via adding a new file for the common code, SigningProtocol.
Modifies from_cache to return the preprocess with the machine, as there's no
reason not to. Also removes an unused Result around the type.
Clarifies the security around deterministic nonces, removing them for
saved-to-disk cached preprocesses. The cached preprocesses are encrypted as the
DB is not a proper secret store.
Moves arguments always present in the protocol from function arguments into the
struct itself.
Removes the horribly ugly code in DkgRemoval, fixing multiple issues present
with it which would cause it to fail on use.
* Set SeraiBlockNumber in cosign.rs as it's used by the cosigning protocol
* Remove unnecessary Clone from lambdas in coordinator
* Remove the EventDb from Tributary scanner
We used per-Transaction DB TXNs so on error, we don't have to rescan the entire
block yet only the rest of it. We prevented scanning multiple transactions by
tracking which we already had.
This is over-engineered and not worth it.
* Implement borsh for HasEvents, removing the manual encoding
* Merge DkgConfirmer and DkgRemoval into signing_protocol.rs
Fixes a bug in DkgConfirmer which would cause it to improperly handle indexes
if any validator had multiple key shares.
* Strictly type DataSpecification's Label
* Correct threshold_i_map_to_keys_and_musig_i_map
It didn't include the participant's own index and accordingly was offset.
* Create TributaryBlockHandler
This struct contains all variables prior passed to handle_block and stops them
from being passed around again and again.
This also ensures fatal_slash is only called while handling a block, as needed
as it expects to operate under perfect consensus.
* Inline accumulate, store confirmation nonces with shares
Inlining accumulate makes sense due to the amount of data accumulate needed to
be passed.
Storing confirmation nonces with shares ensures that both are available or
neither. Prior, one could be yet the other may not have been (requiring an
assert in runtime to ensure we didn't bungle it somehow).
* Create helper functions for handling DkgRemoval/SubstrateSign/Sign Tributary TXs
* Move Label into SignData
All of our transactions which use SignData end up with the same common usage
pattern for Label, justifying this.
Removes 3 transactions, explicitly de-duplicating their handlers.
* Remove CurrentlyCompletingKeyPair for the non-contextual DkgKeyPair
* Remove the manual read/write for TributarySpec for borsh
This struct doesn't have any optimizations booned by the manual impl. Using
borsh reduces our scope.
* Use temporary variables to further minimize LoC in tributary handler
* Remove usage of tuples for non-trivial Tributary transactions
* Remove serde from dkg
serde could be used to deserialize intenrally inconsistent objects which could
lead to panics or faults.
The BorshDeserialize derives have been replaced with a manual implementation
which won't produce inconsistent objects.
* Abstract Future generics using new trait definitions in coordinator
* Move published_signed_transaction to tributary/mod.rs to reduce the size of main.rs
* Split coordinator/src/tributary/mod.rs into spec.rs and transaction.rs
2023-12-10 20:21:44 -05:00
|
|
|
|
2025-01-03 06:57:28 -05:00
|
|
|
// The SignId for this topic
|
|
|
|
|
//
|
|
|
|
|
// Returns None if Topic isn't Topic::Sign
|
|
|
|
|
pub(crate) fn sign_id(self, set: ValidatorSet) -> Option<messages::sign::SignId> {
|
|
|
|
|
#[allow(clippy::match_same_arms)]
|
|
|
|
|
match self {
|
|
|
|
|
Topic::RemoveParticipant { .. } => None,
|
|
|
|
|
Topic::DkgConfirmation { .. } => None,
|
|
|
|
|
Topic::SlashReport { .. } => None,
|
|
|
|
|
Topic::Sign { id, attempt, round: _ } => Some(SignId { session: set.session, id, attempt }),
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2025-01-02 09:11:04 -05:00
|
|
|
/// The topic which precedes this topic as a prerequisite
|
|
|
|
|
///
|
|
|
|
|
/// The preceding topic must define this topic as succeeding
|
|
|
|
|
fn preceding_topic(self) -> Option<Topic> {
|
|
|
|
|
#[allow(clippy::match_same_arms)]
|
|
|
|
|
match self {
|
|
|
|
|
Topic::RemoveParticipant { .. } => None,
|
2025-01-03 06:57:28 -05:00
|
|
|
Topic::DkgConfirmation { attempt, round } => match round {
|
2025-01-02 09:11:04 -05:00
|
|
|
SigningProtocolRound::Preprocess => None,
|
|
|
|
|
SigningProtocolRound::Share => {
|
2025-01-03 06:57:28 -05:00
|
|
|
Some(Topic::DkgConfirmation { attempt, round: SigningProtocolRound::Preprocess })
|
2025-01-02 09:11:04 -05:00
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
Topic::SlashReport { .. } => None,
|
2025-01-03 06:57:28 -05:00
|
|
|
Topic::Sign { id, attempt, round } => match round {
|
2025-01-02 09:11:04 -05:00
|
|
|
SigningProtocolRound::Preprocess => None,
|
|
|
|
|
SigningProtocolRound::Share => {
|
2025-01-03 06:57:28 -05:00
|
|
|
Some(Topic::Sign { id, attempt, round: SigningProtocolRound::Preprocess })
|
2025-01-02 09:11:04 -05:00
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
}
|
|
|
|
|
}
|
2023-12-18 09:13:09 -05:00
|
|
|
|
2025-01-02 09:11:04 -05:00
|
|
|
/// The topic which succeeds this topic, with this topic as a prerequisite
|
|
|
|
|
///
|
|
|
|
|
/// The succeeding topic must define this topic as preceding
|
|
|
|
|
fn succeeding_topic(self) -> Option<Topic> {
|
|
|
|
|
#[allow(clippy::match_same_arms)]
|
|
|
|
|
match self {
|
|
|
|
|
Topic::RemoveParticipant { .. } => None,
|
2025-01-03 06:57:28 -05:00
|
|
|
Topic::DkgConfirmation { attempt, round } => match round {
|
2025-01-02 09:11:04 -05:00
|
|
|
SigningProtocolRound::Preprocess => {
|
2025-01-03 06:57:28 -05:00
|
|
|
Some(Topic::DkgConfirmation { attempt, round: SigningProtocolRound::Share })
|
2025-01-02 09:11:04 -05:00
|
|
|
}
|
|
|
|
|
SigningProtocolRound::Share => None,
|
|
|
|
|
},
|
|
|
|
|
Topic::SlashReport { .. } => None,
|
2025-01-03 06:57:28 -05:00
|
|
|
Topic::Sign { id, attempt, round } => match round {
|
2025-01-02 09:11:04 -05:00
|
|
|
SigningProtocolRound::Preprocess => {
|
2025-01-03 06:57:28 -05:00
|
|
|
Some(Topic::Sign { id, attempt, round: SigningProtocolRound::Share })
|
2025-01-02 09:11:04 -05:00
|
|
|
}
|
|
|
|
|
SigningProtocolRound::Share => None,
|
|
|
|
|
},
|
|
|
|
|
}
|
|
|
|
|
}
|
2023-12-18 09:13:09 -05:00
|
|
|
|
2025-01-15 07:01:24 -05:00
|
|
|
/// If this topic requires recognition before entries are permitted for it.
|
|
|
|
|
pub fn requires_recognition(&self) -> bool {
|
2025-01-02 09:11:04 -05:00
|
|
|
#[allow(clippy::match_same_arms)]
|
|
|
|
|
match self {
|
2025-01-15 07:01:24 -05:00
|
|
|
// We don't require recognition to remove a participant
|
2025-01-02 09:11:04 -05:00
|
|
|
Topic::RemoveParticipant { .. } => false,
|
2025-01-15 07:01:24 -05:00
|
|
|
// We don't require recognition for the first attempt, solely the re-attempts
|
2025-01-02 09:11:04 -05:00
|
|
|
Topic::DkgConfirmation { attempt, .. } => *attempt != 0,
|
2025-01-15 07:01:24 -05:00
|
|
|
// We don't require recognition for the slash report
|
2025-01-02 09:11:04 -05:00
|
|
|
Topic::SlashReport { .. } => false,
|
2025-01-15 07:01:24 -05:00
|
|
|
// We do require recognition for every sign protocol
|
2025-01-02 09:11:04 -05:00
|
|
|
Topic::Sign { .. } => true,
|
|
|
|
|
}
|
|
|
|
|
}
|
2024-01-29 03:48:53 -05:00
|
|
|
|
2025-01-15 14:24:51 -05:00
|
|
|
fn required_participation(&self, n: u16) -> u16 {
|
2025-01-02 09:11:04 -05:00
|
|
|
let _ = self;
|
|
|
|
|
// All of our topics require 2/3rds participation
|
|
|
|
|
((2 * n) / 3) + 1
|
2023-11-22 13:17:51 +04:00
|
|
|
}
|
|
|
|
|
|
2025-01-02 09:11:04 -05:00
|
|
|
fn participating(&self) -> Participating {
|
|
|
|
|
#[allow(clippy::match_same_arms)]
|
|
|
|
|
match self {
|
|
|
|
|
Topic::RemoveParticipant { .. } => Participating::Everyone,
|
|
|
|
|
Topic::DkgConfirmation { .. } => Participating::Participated,
|
|
|
|
|
Topic::SlashReport { .. } => Participating::Everyone,
|
|
|
|
|
Topic::Sign { .. } => Participating::Participated,
|
|
|
|
|
}
|
2023-12-18 09:13:09 -05:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2025-01-11 04:14:21 -05:00
|
|
|
pub(crate) trait Borshy: BorshSerialize + BorshDeserialize {}
|
|
|
|
|
impl<T: BorshSerialize + BorshDeserialize> Borshy for T {}
|
|
|
|
|
|
2025-01-02 09:11:04 -05:00
|
|
|
/// The resulting data set from an accumulation
|
2025-01-03 06:57:28 -05:00
|
|
|
pub(crate) enum DataSet<D: Borshy> {
|
2025-01-02 09:11:04 -05:00
|
|
|
/// Accumulating this did not produce a data set to act on
|
|
|
|
|
/// (non-existent, not ready, prior handled, not participating, etc.)
|
|
|
|
|
None,
|
|
|
|
|
/// The data set was ready and we are participating in this event
|
|
|
|
|
Participating(HashMap<SeraiAddress, D>),
|
|
|
|
|
}
|
Slash malevolent validators (#294)
* add slash tx
* ignore unsigned tx replays
* verify that provided evidence is valid
* fix clippy + fmt
* move application tx handling to another module
* partially handle the tendermint txs
* fix pr comments
* support unsigned app txs
* add slash target to the votes
* enforce provided, unsigned, signed tx ordering within a block
* bug fixes
* add unit test for tendermint txs
* bug fixes
* update tests for tendermint txs
* add tx ordering test
* tidy up tx ordering test
* cargo +nightly fmt
* Misc fixes from rebasing
* Finish resolving clippy
* Remove sha3 from tendermint-machine
* Resolve a DoS in SlashEvidence's read
Also moves Evidence from Vec<Message> to (Message, Option<Message>). That
should meet all requirements while being a bit safer.
* Make lazy_static a dev-depend for tributary
* Various small tweaks
One use of sort was inefficient, sorting unsigned || signed when unsigned was
already properly sorted. Given how the unsigned TXs were given a nonce of 0, an
unstable sort may swap places with an unsigned TX and a signed TX with a nonce
of 0 (leading to a faulty block).
The extra protection added here sorts signed, then concats.
* Fix Tributary tests I broke, start review on tendermint/tx.rs
* Finish reviewing everything outside tests and empty_signature
* Remove empty_signature
empty_signature led to corrupted local state histories. Unfortunately, the API
is only sane with a signature.
We now use the actual signature, which risks creating a signature over a
malicious message if we have ever have an invariant producing malicious
messages. Prior, we only signed the message after the local machine confirmed
it was okay per the local view of consensus.
This is tolerated/preferred over a corrupt state history since production of
such messages is already an invariant. TODOs are added to make handling of this
theoretical invariant further robust.
* Remove async_sequential for tokio::test
There was no competition for resources forcing them to be run sequentially.
* Modify block order test to be statistically significant without multiple runs
* Clean tests
---------
Co-authored-by: Luke Parker <lukeparker5132@gmail.com>
2023-08-21 07:28:23 +03:00
|
|
|
|
2025-01-02 09:11:04 -05:00
|
|
|
create_db!(
|
|
|
|
|
CoordinatorTributary {
|
|
|
|
|
// The last handled tributary block's (number, hash)
|
|
|
|
|
LastHandledTributaryBlock: (set: ValidatorSet) -> (u64, [u8; 32]),
|
|
|
|
|
|
2025-01-11 06:51:55 -05:00
|
|
|
// The slash points a validator has accrued, with u32::MAX representing a fatal slash.
|
|
|
|
|
SlashPoints: (set: ValidatorSet, validator: SeraiAddress) -> u32,
|
2025-01-02 09:11:04 -05:00
|
|
|
|
2025-01-12 05:52:33 -05:00
|
|
|
// The cosign intent for a Substrate block
|
|
|
|
|
CosignIntents: (set: ValidatorSet, substrate_block_hash: [u8; 32]) -> CosignIntent,
|
2025-01-02 09:11:04 -05:00
|
|
|
// The latest Substrate block to cosign.
|
|
|
|
|
LatestSubstrateBlockToCosign: (set: ValidatorSet) -> [u8; 32],
|
2025-01-11 01:31:28 -05:00
|
|
|
// The hash of the block we're actively cosigning.
|
|
|
|
|
ActivelyCosigning: (set: ValidatorSet) -> [u8; 32],
|
|
|
|
|
// If this block has already been cosigned.
|
|
|
|
|
Cosigned: (set: ValidatorSet, substrate_block_hash: [u8; 32]) -> (),
|
2025-01-02 09:11:04 -05:00
|
|
|
|
2025-01-15 07:01:24 -05:00
|
|
|
// The plans to recognize upon a `Transaction::SubstrateBlock` being included on-chain.
|
2025-01-12 07:32:45 -05:00
|
|
|
SubstrateBlockPlans: (set: ValidatorSet, substrate_block_hash: [u8; 32]) -> Vec<[u8; 32]>,
|
|
|
|
|
|
2025-01-02 09:11:04 -05:00
|
|
|
// The weight accumulated for a topic.
|
2025-01-15 14:24:51 -05:00
|
|
|
AccumulatedWeight: (set: ValidatorSet, topic: Topic) -> u16,
|
2025-01-02 09:11:04 -05:00
|
|
|
// The entries accumulated for a topic, by validator.
|
|
|
|
|
Accumulated: <D: Borshy>(set: ValidatorSet, topic: Topic, validator: SeraiAddress) -> D,
|
|
|
|
|
|
|
|
|
|
// Topics to be recognized as of a certain block number due to the reattempt protocol.
|
|
|
|
|
Reattempt: (set: ValidatorSet, block_number: u64) -> Vec<Topic>,
|
2023-10-24 03:00:37 -04:00
|
|
|
}
|
2025-01-02 09:11:04 -05:00
|
|
|
);
|
2023-10-24 03:00:37 -04:00
|
|
|
|
2025-01-03 06:57:28 -05:00
|
|
|
db_channel!(
|
|
|
|
|
CoordinatorTributary {
|
|
|
|
|
ProcessorMessages: (set: ValidatorSet) -> messages::CoordinatorMessage,
|
2025-01-15 07:01:24 -05:00
|
|
|
RecognizedTopics: (set: ValidatorSet) -> Topic,
|
2025-01-03 06:57:28 -05:00
|
|
|
}
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
pub(crate) struct TributaryDb;
|
2025-01-02 09:11:04 -05:00
|
|
|
impl TributaryDb {
|
2025-01-03 06:57:28 -05:00
|
|
|
pub(crate) fn last_handled_tributary_block(
|
2025-01-02 09:11:04 -05:00
|
|
|
getter: &impl Get,
|
|
|
|
|
set: ValidatorSet,
|
|
|
|
|
) -> Option<(u64, [u8; 32])> {
|
|
|
|
|
LastHandledTributaryBlock::get(getter, set)
|
|
|
|
|
}
|
2025-01-03 06:57:28 -05:00
|
|
|
pub(crate) fn set_last_handled_tributary_block(
|
2025-01-02 09:11:04 -05:00
|
|
|
txn: &mut impl DbTxn,
|
|
|
|
|
set: ValidatorSet,
|
|
|
|
|
block_number: u64,
|
|
|
|
|
block_hash: [u8; 32],
|
|
|
|
|
) {
|
|
|
|
|
LastHandledTributaryBlock::set(txn, set, &(block_number, block_hash));
|
2023-08-14 06:08:55 -04:00
|
|
|
}
|
|
|
|
|
|
2025-01-03 06:57:28 -05:00
|
|
|
pub(crate) fn latest_substrate_block_to_cosign(
|
|
|
|
|
getter: &impl Get,
|
|
|
|
|
set: ValidatorSet,
|
|
|
|
|
) -> Option<[u8; 32]> {
|
|
|
|
|
LatestSubstrateBlockToCosign::get(getter, set)
|
|
|
|
|
}
|
|
|
|
|
pub(crate) fn set_latest_substrate_block_to_cosign(
|
|
|
|
|
txn: &mut impl DbTxn,
|
|
|
|
|
set: ValidatorSet,
|
|
|
|
|
substrate_block_hash: [u8; 32],
|
|
|
|
|
) {
|
|
|
|
|
LatestSubstrateBlockToCosign::set(txn, set, &substrate_block_hash);
|
|
|
|
|
}
|
2025-01-11 01:31:28 -05:00
|
|
|
pub(crate) fn actively_cosigning(txn: &mut impl DbTxn, set: ValidatorSet) -> Option<[u8; 32]> {
|
|
|
|
|
ActivelyCosigning::get(txn, set)
|
2025-01-03 10:30:39 -05:00
|
|
|
}
|
|
|
|
|
pub(crate) fn start_cosigning(
|
|
|
|
|
txn: &mut impl DbTxn,
|
|
|
|
|
set: ValidatorSet,
|
2025-01-11 01:31:28 -05:00
|
|
|
substrate_block_hash: [u8; 32],
|
2025-01-03 10:30:39 -05:00
|
|
|
substrate_block_number: u64,
|
|
|
|
|
) {
|
|
|
|
|
assert!(
|
|
|
|
|
ActivelyCosigning::get(txn, set).is_none(),
|
|
|
|
|
"starting cosigning while already cosigning"
|
|
|
|
|
);
|
2025-01-11 01:31:28 -05:00
|
|
|
ActivelyCosigning::set(txn, set, &substrate_block_hash);
|
2025-01-03 10:30:39 -05:00
|
|
|
|
2025-01-15 07:01:24 -05:00
|
|
|
Self::recognize_topic(
|
2025-01-03 10:30:39 -05:00
|
|
|
txn,
|
|
|
|
|
set,
|
|
|
|
|
Topic::Sign {
|
|
|
|
|
id: VariantSignId::Cosign(substrate_block_number),
|
|
|
|
|
attempt: 0,
|
|
|
|
|
round: SigningProtocolRound::Preprocess,
|
|
|
|
|
},
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
pub(crate) fn finish_cosigning(txn: &mut impl DbTxn, set: ValidatorSet) {
|
|
|
|
|
assert!(ActivelyCosigning::take(txn, set).is_some(), "finished cosigning but not cosigning");
|
|
|
|
|
}
|
2025-01-11 01:31:28 -05:00
|
|
|
pub(crate) fn mark_cosigned(
|
|
|
|
|
txn: &mut impl DbTxn,
|
|
|
|
|
set: ValidatorSet,
|
|
|
|
|
substrate_block_hash: [u8; 32],
|
|
|
|
|
) {
|
|
|
|
|
Cosigned::set(txn, set, substrate_block_hash, &());
|
|
|
|
|
}
|
|
|
|
|
pub(crate) fn cosigned(
|
|
|
|
|
txn: &mut impl DbTxn,
|
|
|
|
|
set: ValidatorSet,
|
|
|
|
|
substrate_block_hash: [u8; 32],
|
|
|
|
|
) -> bool {
|
|
|
|
|
Cosigned::get(txn, set, substrate_block_hash).is_some()
|
|
|
|
|
}
|
2025-01-03 06:57:28 -05:00
|
|
|
|
|
|
|
|
pub(crate) fn recognize_topic(txn: &mut impl DbTxn, set: ValidatorSet, topic: Topic) {
|
2025-01-02 09:11:04 -05:00
|
|
|
AccumulatedWeight::set(txn, set, topic, &0);
|
2025-01-15 07:01:24 -05:00
|
|
|
RecognizedTopics::send(txn, set, &topic);
|
|
|
|
|
}
|
|
|
|
|
pub(crate) fn recognized(getter: &impl Get, set: ValidatorSet, topic: Topic) -> bool {
|
|
|
|
|
AccumulatedWeight::get(getter, set, topic).is_some()
|
2023-12-12 12:28:53 -05:00
|
|
|
}
|
|
|
|
|
|
2025-01-03 06:57:28 -05:00
|
|
|
pub(crate) fn start_of_block(txn: &mut impl DbTxn, set: ValidatorSet, block_number: u64) {
|
2025-01-02 09:11:04 -05:00
|
|
|
for topic in Reattempt::take(txn, set, block_number).unwrap_or(vec![]) {
|
2025-01-09 19:50:20 -05:00
|
|
|
/*
|
|
|
|
|
TODO: Slash all people who preprocessed but didn't share, and add a delay to their
|
|
|
|
|
participations in future protocols. When we call accumulate, if the participant has no
|
|
|
|
|
delay, their accumulation occurs immediately. Else, the accumulation occurs after the
|
|
|
|
|
specified delay.
|
|
|
|
|
|
|
|
|
|
This means even if faulty validators are first to preprocess, they won't be selected for
|
|
|
|
|
the signing set unless there's a lack of less faulty validators available.
|
|
|
|
|
|
|
|
|
|
We need to decrease this delay upon successful partipations, and set it to the maximum upon
|
|
|
|
|
`f + 1` validators voting to fatally slash the validator in question. This won't issue the
|
|
|
|
|
fatal slash but should still be effective.
|
|
|
|
|
*/
|
2025-01-02 09:11:04 -05:00
|
|
|
Self::recognize_topic(txn, set, topic);
|
2025-01-03 06:57:28 -05:00
|
|
|
if let Some(id) = topic.sign_id(set) {
|
|
|
|
|
Self::send_message(txn, set, messages::sign::CoordinatorMessage::Reattempt { id });
|
|
|
|
|
}
|
2023-09-01 01:21:15 -04:00
|
|
|
}
|
2023-04-20 05:05:17 -04:00
|
|
|
}
|
|
|
|
|
|
2025-01-03 06:57:28 -05:00
|
|
|
pub(crate) fn fatal_slash(
|
2023-12-12 12:28:53 -05:00
|
|
|
txn: &mut impl DbTxn,
|
2025-01-02 09:11:04 -05:00
|
|
|
set: ValidatorSet,
|
|
|
|
|
validator: SeraiAddress,
|
|
|
|
|
reason: &str,
|
2023-12-12 12:28:53 -05:00
|
|
|
) {
|
2025-01-02 09:11:04 -05:00
|
|
|
log::warn!("{validator} fatally slashed: {reason}");
|
2025-01-11 06:51:55 -05:00
|
|
|
SlashPoints::set(txn, set, validator, &u32::MAX);
|
2023-12-12 12:28:53 -05:00
|
|
|
}
|
|
|
|
|
|
2025-01-03 06:57:28 -05:00
|
|
|
pub(crate) fn is_fatally_slashed(
|
|
|
|
|
getter: &impl Get,
|
|
|
|
|
set: ValidatorSet,
|
|
|
|
|
validator: SeraiAddress,
|
|
|
|
|
) -> bool {
|
2025-01-11 06:51:55 -05:00
|
|
|
SlashPoints::get(getter, set, validator).unwrap_or(0) == u32::MAX
|
2023-12-12 12:28:53 -05:00
|
|
|
}
|
|
|
|
|
|
2025-01-02 09:11:04 -05:00
|
|
|
#[allow(clippy::too_many_arguments)]
|
2025-01-03 06:57:28 -05:00
|
|
|
pub(crate) fn accumulate<D: Borshy>(
|
2023-11-22 13:17:51 +04:00
|
|
|
txn: &mut impl DbTxn,
|
2025-01-02 09:11:04 -05:00
|
|
|
set: ValidatorSet,
|
|
|
|
|
validators: &[SeraiAddress],
|
2025-01-15 14:24:51 -05:00
|
|
|
total_weight: u16,
|
2025-01-02 09:11:04 -05:00
|
|
|
block_number: u64,
|
|
|
|
|
topic: Topic,
|
|
|
|
|
validator: SeraiAddress,
|
2025-01-15 14:24:51 -05:00
|
|
|
validator_weight: u16,
|
2025-01-02 09:11:04 -05:00
|
|
|
data: &D,
|
|
|
|
|
) -> DataSet<D> {
|
|
|
|
|
// This function will only be called once for a (validator, topic) tuple due to how we handle
|
|
|
|
|
// nonces on transactions (deterministically to the topic)
|
|
|
|
|
|
|
|
|
|
let accumulated_weight = AccumulatedWeight::get(txn, set, topic);
|
2025-01-15 07:01:24 -05:00
|
|
|
if topic.requires_recognition() && accumulated_weight.is_none() {
|
|
|
|
|
Self::fatal_slash(
|
|
|
|
|
txn,
|
|
|
|
|
set,
|
|
|
|
|
validator,
|
|
|
|
|
"participated in unrecognized topic which requires recognition",
|
|
|
|
|
);
|
2025-01-02 09:11:04 -05:00
|
|
|
return DataSet::None;
|
|
|
|
|
}
|
|
|
|
|
let mut accumulated_weight = accumulated_weight.unwrap_or(0);
|
|
|
|
|
|
|
|
|
|
// Check if there's a preceding topic, this validator participated
|
|
|
|
|
let preceding_topic = topic.preceding_topic();
|
|
|
|
|
if let Some(preceding_topic) = preceding_topic {
|
|
|
|
|
if Accumulated::<D>::get(txn, set, preceding_topic, validator).is_none() {
|
|
|
|
|
Self::fatal_slash(
|
|
|
|
|
txn,
|
|
|
|
|
set,
|
|
|
|
|
validator,
|
|
|
|
|
"participated in topic without participating in prior",
|
|
|
|
|
);
|
|
|
|
|
return DataSet::None;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// The complete lack of validation on the data by these NOPs opens the potential for spam here
|
|
|
|
|
|
|
|
|
|
// If we've already accumulated past the threshold, NOP
|
|
|
|
|
if accumulated_weight >= topic.required_participation(total_weight) {
|
|
|
|
|
return DataSet::None;
|
|
|
|
|
}
|
|
|
|
|
// If this is for an old attempt, NOP
|
|
|
|
|
if let Some(next_attempt_topic) = topic.next_attempt_topic() {
|
|
|
|
|
if AccumulatedWeight::get(txn, set, next_attempt_topic).is_some() {
|
|
|
|
|
return DataSet::None;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Accumulate the data
|
|
|
|
|
accumulated_weight += validator_weight;
|
|
|
|
|
AccumulatedWeight::set(txn, set, topic, &accumulated_weight);
|
|
|
|
|
Accumulated::set(txn, set, topic, validator, data);
|
|
|
|
|
|
|
|
|
|
// Check if we now cross the weight threshold
|
|
|
|
|
if accumulated_weight >= topic.required_participation(total_weight) {
|
|
|
|
|
// Queue this for re-attempt after enough time passes
|
2025-01-03 06:57:28 -05:00
|
|
|
let reattempt_topic = topic.reattempt_topic();
|
|
|
|
|
if let Some((attempt, reattempt_topic)) = reattempt_topic {
|
2025-01-02 09:11:04 -05:00
|
|
|
// 5 minutes
|
|
|
|
|
#[cfg(not(feature = "longer-reattempts"))]
|
|
|
|
|
const BASE_REATTEMPT_DELAY: u32 =
|
2025-01-11 04:14:21 -05:00
|
|
|
(5u32 * 60 * 1000).div_ceil(tributary_sdk::tendermint::TARGET_BLOCK_TIME);
|
2025-01-02 09:11:04 -05:00
|
|
|
|
|
|
|
|
// 10 minutes, intended for latent environments like the GitHub CI
|
|
|
|
|
#[cfg(feature = "longer-reattempts")]
|
|
|
|
|
const BASE_REATTEMPT_DELAY: u32 =
|
2025-01-11 04:14:21 -05:00
|
|
|
(10u32 * 60 * 1000).div_ceil(tributary_sdk::tendermint::TARGET_BLOCK_TIME);
|
2025-01-02 09:11:04 -05:00
|
|
|
|
|
|
|
|
// Linearly scale the time for the protocol with the attempt number
|
|
|
|
|
let blocks_till_reattempt = u64::from(attempt * BASE_REATTEMPT_DELAY);
|
|
|
|
|
|
|
|
|
|
let recognize_at = block_number + blocks_till_reattempt;
|
|
|
|
|
let mut queued = Reattempt::get(txn, set, recognize_at).unwrap_or(Vec::with_capacity(1));
|
|
|
|
|
queued.push(reattempt_topic);
|
|
|
|
|
Reattempt::set(txn, set, recognize_at, &queued);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Register the succeeding topic
|
|
|
|
|
let succeeding_topic = topic.succeeding_topic();
|
|
|
|
|
if let Some(succeeding_topic) = succeeding_topic {
|
|
|
|
|
Self::recognize_topic(txn, set, succeeding_topic);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Fetch and return all participations
|
|
|
|
|
let mut data_set = HashMap::with_capacity(validators.len());
|
|
|
|
|
for validator in validators {
|
|
|
|
|
if let Some(data) = Accumulated::<D>::get(txn, set, topic, *validator) {
|
2025-01-03 06:57:28 -05:00
|
|
|
// Clean this data up if there's not a re-attempt topic
|
|
|
|
|
// If there is a re-attempt topic, we clean it up upon re-attempt
|
|
|
|
|
if reattempt_topic.is_none() {
|
2025-01-02 09:11:04 -05:00
|
|
|
Accumulated::<D>::del(txn, set, topic, *validator);
|
|
|
|
|
}
|
|
|
|
|
data_set.insert(*validator, data);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
let participated = data_set.contains_key(&validator);
|
|
|
|
|
match topic.participating() {
|
|
|
|
|
Participating::Participated => {
|
|
|
|
|
if participated {
|
|
|
|
|
DataSet::Participating(data_set)
|
|
|
|
|
} else {
|
|
|
|
|
DataSet::None
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
Participating::Everyone => DataSet::Participating(data_set),
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
DataSet::None
|
2023-10-24 03:00:37 -04:00
|
|
|
}
|
2023-11-22 13:17:51 +04:00
|
|
|
}
|
2025-01-03 06:57:28 -05:00
|
|
|
|
|
|
|
|
pub(crate) fn send_message(
|
|
|
|
|
txn: &mut impl DbTxn,
|
|
|
|
|
set: ValidatorSet,
|
|
|
|
|
message: impl Into<messages::CoordinatorMessage>,
|
|
|
|
|
) {
|
|
|
|
|
ProcessorMessages::send(txn, set, &message.into());
|
|
|
|
|
}
|
2023-11-22 13:17:51 +04:00
|
|
|
}
|