2023-09-29 03:51:01 -04:00
|
|
|
use blake2::{
|
|
|
|
|
digest::{consts::U32, Digest},
|
|
|
|
|
Blake2b,
|
|
|
|
|
};
|
|
|
|
|
|
2023-12-07 18:30:11 +04:00
|
|
|
use scale::Encode;
|
2023-09-29 03:51:01 -04:00
|
|
|
use serai_client::{
|
|
|
|
|
primitives::NetworkId,
|
2023-10-13 12:14:59 -04:00
|
|
|
validator_sets::primitives::{Session, ValidatorSet},
|
2023-09-29 03:51:01 -04:00
|
|
|
in_instructions::primitives::{Batch, SignedBatch},
|
|
|
|
|
};
|
2023-08-24 19:06:22 -04:00
|
|
|
|
2023-04-23 04:31:00 -04:00
|
|
|
pub use serai_db::*;
|
|
|
|
|
|
2023-09-27 00:44:31 -04:00
|
|
|
use ::tributary::ReadWrite;
|
2023-10-14 21:58:10 -04:00
|
|
|
use crate::tributary::{TributarySpec, Transaction, scanner::RecognizedIdType};
|
2023-04-23 04:31:00 -04:00
|
|
|
|
2023-12-07 18:30:11 +04:00
|
|
|
create_db!(
|
|
|
|
|
MainDb {
|
|
|
|
|
HandledMessageDb: (network: NetworkId) -> u64,
|
|
|
|
|
ActiveTributaryDb: () -> Vec<u8>,
|
|
|
|
|
RetiredTributaryDb: (set: ValidatorSet) -> (),
|
|
|
|
|
SignedTransactionDb: (order: &[u8], nonce: u32) -> Vec<u8>,
|
|
|
|
|
FirstPreprocessDb: (
|
|
|
|
|
network: NetworkId,
|
|
|
|
|
id_type: RecognizedIdType,
|
|
|
|
|
id: &[u8]
|
|
|
|
|
) -> Vec<Vec<u8>>,
|
|
|
|
|
LastReceivedBatchDb: (network: NetworkId) -> u32,
|
|
|
|
|
ExpectedBatchDb: (network: NetworkId, id: u32) -> [u8; 32],
|
|
|
|
|
BatchDb: (network: NetworkId, id: u32) -> SignedBatch,
|
|
|
|
|
LastVerifiedBatchDb: (network: NetworkId) -> u32,
|
|
|
|
|
HandoverBatchDb: (set: ValidatorSet) -> u32,
|
|
|
|
|
LookupHandoverBatchDb: (network: NetworkId, batch: u32) -> Session,
|
|
|
|
|
QueuedBatchesDb: (set: ValidatorSet) -> Vec<u8>
|
|
|
|
|
}
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
impl ActiveTributaryDb {
|
2023-09-27 00:00:31 -04:00
|
|
|
pub fn active_tributaries<G: Get>(getter: &G) -> (Vec<u8>, Vec<TributarySpec>) {
|
2023-12-07 18:30:11 +04:00
|
|
|
let bytes = Self::get(getter).unwrap_or_default();
|
2023-04-23 04:31:00 -04:00
|
|
|
let mut bytes_ref: &[u8] = bytes.as_ref();
|
|
|
|
|
|
|
|
|
|
let mut tributaries = vec![];
|
|
|
|
|
while !bytes_ref.is_empty() {
|
|
|
|
|
tributaries.push(TributarySpec::read(&mut bytes_ref).unwrap());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
(bytes, tributaries)
|
|
|
|
|
}
|
2023-12-07 18:30:11 +04:00
|
|
|
|
|
|
|
|
pub fn add_participating_in_tributary(txn: &mut impl DbTxn, spec: &TributarySpec) {
|
|
|
|
|
let (mut existing_bytes, existing) = ActiveTributaryDb::active_tributaries(txn);
|
2023-04-23 04:31:00 -04:00
|
|
|
for tributary in &existing {
|
|
|
|
|
if tributary == spec {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
spec.write(&mut existing_bytes).unwrap();
|
2023-12-07 18:30:11 +04:00
|
|
|
ActiveTributaryDb::set(txn, &existing_bytes);
|
2023-04-23 04:31:00 -04:00
|
|
|
}
|
2023-12-07 18:30:11 +04:00
|
|
|
|
|
|
|
|
pub fn retire_tributary(txn: &mut impl DbTxn, set: ValidatorSet) {
|
2023-10-14 16:47:25 -04:00
|
|
|
let mut active = Self::active_tributaries(txn).1;
|
|
|
|
|
for i in 0 .. active.len() {
|
|
|
|
|
if active[i].set() == set {
|
|
|
|
|
active.remove(i);
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
let mut bytes = vec![];
|
|
|
|
|
for active in active {
|
|
|
|
|
active.write(&mut bytes).unwrap();
|
|
|
|
|
}
|
2023-12-07 18:30:11 +04:00
|
|
|
Self::set(txn, &bytes);
|
|
|
|
|
RetiredTributaryDb::set(txn, set, &());
|
2023-10-14 16:47:25 -04:00
|
|
|
}
|
2023-12-07 18:30:11 +04:00
|
|
|
}
|
2023-05-08 22:20:51 -04:00
|
|
|
|
2023-12-07 18:30:11 +04:00
|
|
|
impl SignedTransactionDb {
|
|
|
|
|
pub fn take_signed_transaction(
|
|
|
|
|
txn: &mut impl DbTxn,
|
|
|
|
|
order: &[u8],
|
|
|
|
|
nonce: u32,
|
|
|
|
|
) -> Option<Transaction> {
|
|
|
|
|
let res = SignedTransactionDb::get(txn, order, nonce)
|
|
|
|
|
.map(|bytes| Transaction::read(&mut bytes.as_slice()).unwrap());
|
2023-09-27 00:44:31 -04:00
|
|
|
if res.is_some() {
|
2023-12-07 18:30:11 +04:00
|
|
|
Self::del(txn, order, nonce);
|
2023-09-27 00:44:31 -04:00
|
|
|
}
|
|
|
|
|
res
|
|
|
|
|
}
|
2023-12-07 18:30:11 +04:00
|
|
|
}
|
2023-09-27 00:44:31 -04:00
|
|
|
|
2023-12-07 18:30:11 +04:00
|
|
|
impl FirstPreprocessDb {
|
2023-09-27 13:00:04 -04:00
|
|
|
pub fn save_first_preprocess(
|
2023-12-07 18:30:11 +04:00
|
|
|
txn: &mut impl DbTxn,
|
2023-09-27 13:00:04 -04:00
|
|
|
network: NetworkId,
|
2023-10-14 21:58:10 -04:00
|
|
|
id_type: RecognizedIdType,
|
2023-11-06 19:50:32 -05:00
|
|
|
id: &[u8],
|
Support multiple key shares per validator (#416)
* Update the coordinator to give key shares based on weight, not based on existence
Participants are now identified by their starting index. While this compiles,
the following is unimplemented:
1) A conversion for DKG `i` values. It assumes the threshold `i` values used
will be identical for the MuSig signature used to confirm the DKG.
2) Expansion from compressed values to full values before forwarding to the
processor.
* Add a fn to the DkgConfirmer to convert `i` values as needed
Also removes TODOs regarding Serai ensuring validator key uniqueness +
validity. The current infra achieves both.
* Have the Tributary DB track participation by shares, not by count
* Prevent a node from obtaining 34% of the maximum amount of key shares
This is actually mainly intended to set a bound on message sizes in the
coordinator. Message sizes are amplified by the amount of key shares held, so
setting an upper bound on said amount lets it determine constants. While that
upper bound could be 150, that'd be unreasonable and increase the potential for
DoS attacks.
* Correct the mechanism to detect if sufficient accumulation has occured
It used to check if the latest accumulation hit the required threshold. Now,
accumulations may jump past the required threshold. The required mechanism is
to check the threshold wasn't prior met and is now met.
* Finish updating the coordinator to handle a multiple key share per validator environment
* Adjust stategy re: preventing noce reuse in DKG Confirmer
* Add TODOs regarding dropped transactions, add possible TODO fix
* Update tests/coordinator
This doesn't add new multi-key-share tests, it solely updates the existing
single key-share tests to compile and run, with the necessary fixes to the
coordinator.
* Update processor key_gen to handle generating multiple key shares at once
* Update SubstrateSigner
* Update signer, clippy
* Update processor tests
* Update processor docker tests
2023-11-04 19:26:13 -04:00
|
|
|
preprocess: Vec<Vec<u8>>,
|
2023-09-27 13:00:04 -04:00
|
|
|
) {
|
2023-12-07 18:30:11 +04:00
|
|
|
if let Some(existing) = FirstPreprocessDb::get(txn, network, id_type, id) {
|
2023-05-08 22:20:51 -04:00
|
|
|
assert_eq!(existing, preprocess, "saved a distinct first preprocess");
|
|
|
|
|
return;
|
|
|
|
|
}
|
2023-12-07 18:30:11 +04:00
|
|
|
FirstPreprocessDb::set(txn, network, id_type, id, &preprocess);
|
2023-05-08 22:20:51 -04:00
|
|
|
}
|
2023-12-07 18:30:11 +04:00
|
|
|
}
|
2023-08-31 22:09:29 -04:00
|
|
|
|
2023-12-07 18:30:11 +04:00
|
|
|
impl ExpectedBatchDb {
|
|
|
|
|
pub fn save_expected_batch(txn: &mut impl DbTxn, batch: &Batch) {
|
|
|
|
|
LastReceivedBatchDb::set(txn, batch.network, &batch.id);
|
|
|
|
|
Self::set(
|
|
|
|
|
txn,
|
|
|
|
|
batch.network,
|
|
|
|
|
batch.id,
|
|
|
|
|
&Blake2b::<U32>::digest(batch.instructions.encode()).into(),
|
2023-09-29 03:51:01 -04:00
|
|
|
);
|
|
|
|
|
}
|
2023-12-07 18:30:11 +04:00
|
|
|
}
|
2023-09-29 03:51:01 -04:00
|
|
|
|
2023-12-07 18:30:11 +04:00
|
|
|
impl HandoverBatchDb {
|
|
|
|
|
pub fn set_handover_batch(txn: &mut impl DbTxn, set: ValidatorSet, batch: u32) {
|
|
|
|
|
Self::set(txn, set, &batch);
|
|
|
|
|
LookupHandoverBatchDb::set(txn, set.network, batch, &set.session);
|
2023-09-29 03:51:01 -04:00
|
|
|
}
|
2023-12-07 18:30:11 +04:00
|
|
|
}
|
|
|
|
|
impl QueuedBatchesDb {
|
|
|
|
|
pub fn queue(txn: &mut impl DbTxn, set: ValidatorSet, batch: Transaction) {
|
|
|
|
|
let mut batches = Self::get(txn, set).unwrap_or_default();
|
|
|
|
|
batch.write(&mut batches).unwrap();
|
|
|
|
|
Self::set(txn, set, &batches);
|
2023-09-29 03:51:01 -04:00
|
|
|
}
|
2023-10-13 03:36:59 -04:00
|
|
|
|
2023-12-07 18:30:11 +04:00
|
|
|
pub fn take(txn: &mut impl DbTxn, set: ValidatorSet) -> Vec<Transaction> {
|
|
|
|
|
let batches_vec = Self::get(txn, set).unwrap_or_default();
|
|
|
|
|
txn.del(&Self::key(set));
|
2023-10-13 12:14:59 -04:00
|
|
|
|
|
|
|
|
let mut batches: &[u8] = &batches_vec;
|
|
|
|
|
let mut res = vec![];
|
|
|
|
|
while !batches.is_empty() {
|
|
|
|
|
res.push(Transaction::read(&mut batches).unwrap());
|
|
|
|
|
}
|
|
|
|
|
res
|
2023-10-13 03:36:59 -04:00
|
|
|
}
|
2023-04-23 04:31:00 -04:00
|
|
|
}
|