2023-04-20 05:05:17 -04:00
|
|
|
use core::ops::Deref;
|
2023-04-24 00:53:15 -04:00
|
|
|
use std::collections::HashMap;
|
2023-04-20 05:05:17 -04:00
|
|
|
|
|
|
|
|
use zeroize::Zeroizing;
|
|
|
|
|
|
|
|
|
|
use ciphersuite::{Ciphersuite, Ristretto};
|
|
|
|
|
|
2023-05-08 22:20:51 -04:00
|
|
|
use tokio::sync::mpsc::UnboundedSender;
|
|
|
|
|
|
2023-04-24 06:50:40 -04:00
|
|
|
use tributary::{Signed, Block, TributaryReader};
|
2023-04-20 05:05:17 -04:00
|
|
|
|
|
|
|
|
use processor_messages::{
|
|
|
|
|
key_gen::{self, KeyGenId},
|
2023-04-20 06:27:00 -04:00
|
|
|
sign::{self, SignId},
|
|
|
|
|
coordinator, CoordinatorMessage,
|
2023-04-20 05:05:17 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
use serai_db::DbTxn;
|
|
|
|
|
|
|
|
|
|
use crate::{
|
|
|
|
|
Db,
|
2023-05-09 23:44:41 -04:00
|
|
|
processors::Processors,
|
2023-04-20 05:05:17 -04:00
|
|
|
tributary::{TributaryDb, TributarySpec, Transaction},
|
|
|
|
|
};
|
|
|
|
|
|
2023-05-08 22:20:51 -04:00
|
|
|
#[derive(Clone, Copy, PartialEq, Eq, Debug)]
|
|
|
|
|
pub enum RecognizedIdType {
|
|
|
|
|
Block,
|
|
|
|
|
Plan,
|
|
|
|
|
}
|
|
|
|
|
|
2023-04-20 05:05:17 -04:00
|
|
|
// Handle a specific Tributary block
|
2023-08-01 02:49:31 -04:00
|
|
|
#[allow(clippy::needless_pass_by_ref_mut)] // False positive?
|
2023-05-09 23:44:41 -04:00
|
|
|
async fn handle_block<D: Db, Pro: Processors>(
|
2023-04-20 05:05:17 -04:00
|
|
|
db: &mut TributaryDb<D>,
|
|
|
|
|
key: &Zeroizing<<Ristretto as Ciphersuite>::F>,
|
2023-05-08 22:20:51 -04:00
|
|
|
recognized_id: &UnboundedSender<([u8; 32], RecognizedIdType, [u8; 32])>,
|
2023-05-09 23:44:41 -04:00
|
|
|
processors: &Pro,
|
2023-04-20 05:05:17 -04:00
|
|
|
spec: &TributarySpec,
|
|
|
|
|
block: Block<Transaction>,
|
|
|
|
|
) {
|
2023-08-08 15:12:47 -04:00
|
|
|
log::info!("found block for Tributary {:?}", spec.set());
|
|
|
|
|
|
2023-04-24 06:50:40 -04:00
|
|
|
let genesis = spec.genesis();
|
2023-04-20 05:05:17 -04:00
|
|
|
let hash = block.hash();
|
|
|
|
|
|
|
|
|
|
let mut event_id = 0;
|
2023-04-20 14:24:49 -04:00
|
|
|
#[allow(clippy::explicit_counter_loop)] // event_id isn't TX index. It just currently lines up
|
2023-04-20 05:05:17 -04:00
|
|
|
for tx in block.transactions {
|
|
|
|
|
if !TributaryDb::<D>::handled_event(&db.0, hash, event_id) {
|
|
|
|
|
let mut txn = db.0.txn();
|
|
|
|
|
|
2023-04-20 14:24:49 -04:00
|
|
|
// Used to determine if an ID is acceptable
|
|
|
|
|
#[derive(Clone, Copy, PartialEq, Eq, Debug)]
|
|
|
|
|
enum Zone {
|
|
|
|
|
Dkg,
|
|
|
|
|
Batch,
|
|
|
|
|
Sign,
|
|
|
|
|
}
|
|
|
|
|
|
2023-04-20 14:51:33 -04:00
|
|
|
impl Zone {
|
|
|
|
|
fn label(&self) -> &'static str {
|
|
|
|
|
match self {
|
|
|
|
|
Zone::Dkg => {
|
|
|
|
|
panic!("getting the label for dkg despite dkg code paths not needing a label")
|
|
|
|
|
}
|
2023-04-20 14:24:49 -04:00
|
|
|
Zone::Batch => "batch",
|
|
|
|
|
Zone::Sign => "sign",
|
2023-04-20 14:51:33 -04:00
|
|
|
}
|
2023-04-20 14:24:49 -04:00
|
|
|
}
|
2023-04-20 14:51:33 -04:00
|
|
|
}
|
2023-04-20 14:24:49 -04:00
|
|
|
|
2023-04-24 06:50:40 -04:00
|
|
|
let mut handle = |zone: Zone,
|
|
|
|
|
label,
|
|
|
|
|
needed,
|
|
|
|
|
id,
|
|
|
|
|
attempt,
|
|
|
|
|
mut bytes: Vec<u8>,
|
|
|
|
|
signed: Signed| {
|
|
|
|
|
if zone == Zone::Dkg {
|
|
|
|
|
// Since Dkg doesn't have an ID, solely attempts, this should just be [0; 32]
|
|
|
|
|
assert_eq!(id, [0; 32], "DKG, which shouldn't have IDs, had a non-0 ID");
|
|
|
|
|
} else if !TributaryDb::<D>::recognized_id(&txn, zone.label(), genesis, id) {
|
|
|
|
|
// TODO: Full slash
|
|
|
|
|
todo!();
|
|
|
|
|
}
|
2023-04-20 05:05:17 -04:00
|
|
|
|
2023-04-24 06:50:40 -04:00
|
|
|
// If they've already published a TX for this attempt, slash
|
|
|
|
|
if let Some(data) = TributaryDb::<D>::data(label, &txn, genesis, id, attempt, signed.signer)
|
|
|
|
|
{
|
|
|
|
|
if data != bytes {
|
2023-04-20 14:51:33 -04:00
|
|
|
// TODO: Full slash
|
|
|
|
|
todo!();
|
2023-04-20 05:05:17 -04:00
|
|
|
}
|
|
|
|
|
|
2023-04-24 06:50:40 -04:00
|
|
|
// TODO: Slash
|
|
|
|
|
return None;
|
|
|
|
|
}
|
2023-04-20 14:51:33 -04:00
|
|
|
|
2023-04-24 06:50:40 -04:00
|
|
|
// If the attempt is lesser than the blockchain's, slash
|
|
|
|
|
let curr_attempt = TributaryDb::<D>::attempt(&txn, genesis, id);
|
|
|
|
|
if attempt < curr_attempt {
|
|
|
|
|
// TODO: Slash for being late
|
|
|
|
|
return None;
|
|
|
|
|
}
|
|
|
|
|
if attempt > curr_attempt {
|
|
|
|
|
// TODO: Full slash
|
|
|
|
|
todo!();
|
|
|
|
|
}
|
2023-04-20 14:51:33 -04:00
|
|
|
|
2023-04-24 06:50:40 -04:00
|
|
|
// TODO: We can also full slash if shares before all commitments, or share before the
|
|
|
|
|
// necessary preprocesses
|
|
|
|
|
|
2023-04-25 15:05:58 -04:00
|
|
|
// TODO: If this is shares, we need to check they are part of the selected signing set
|
|
|
|
|
|
2023-04-24 06:50:40 -04:00
|
|
|
// Store this data
|
|
|
|
|
let received =
|
|
|
|
|
TributaryDb::<D>::set_data(label, &mut txn, genesis, id, attempt, signed.signer, &bytes);
|
|
|
|
|
|
|
|
|
|
// If we have all the needed commitments/preprocesses/shares, tell the processor
|
|
|
|
|
// TODO: This needs to be coded by weight, not by validator count
|
|
|
|
|
if received == needed {
|
|
|
|
|
let mut data = HashMap::new();
|
|
|
|
|
for validator in spec.validators().iter().map(|validator| validator.0) {
|
|
|
|
|
data.insert(
|
|
|
|
|
spec.i(validator).unwrap(),
|
|
|
|
|
if validator == signed.signer {
|
|
|
|
|
bytes.split_off(0)
|
|
|
|
|
} else if let Some(data) =
|
|
|
|
|
TributaryDb::<D>::data(label, &txn, genesis, id, attempt, validator)
|
|
|
|
|
{
|
|
|
|
|
data
|
|
|
|
|
} else {
|
|
|
|
|
continue;
|
|
|
|
|
},
|
|
|
|
|
);
|
2023-04-20 14:51:33 -04:00
|
|
|
}
|
2023-04-24 06:50:40 -04:00
|
|
|
assert_eq!(data.len(), usize::from(needed));
|
|
|
|
|
|
2023-08-13 02:21:56 -04:00
|
|
|
// Remove our own piece of data
|
|
|
|
|
assert!(data
|
|
|
|
|
.remove(
|
|
|
|
|
&spec
|
|
|
|
|
.i(Ristretto::generator() * key.deref())
|
|
|
|
|
.expect("handling a message for a Tributary we aren't part of")
|
|
|
|
|
)
|
|
|
|
|
.is_some());
|
|
|
|
|
|
2023-04-24 06:50:40 -04:00
|
|
|
return Some(data);
|
|
|
|
|
}
|
|
|
|
|
None
|
|
|
|
|
};
|
2023-04-20 05:05:17 -04:00
|
|
|
|
|
|
|
|
match tx {
|
|
|
|
|
Transaction::DkgCommitments(attempt, bytes, signed) => {
|
2023-04-20 06:27:00 -04:00
|
|
|
if let Some(commitments) =
|
2023-04-20 14:24:49 -04:00
|
|
|
handle(Zone::Dkg, b"dkg_commitments", spec.n(), [0; 32], attempt, bytes, signed)
|
2023-04-20 06:27:00 -04:00
|
|
|
{
|
2023-08-13 02:21:56 -04:00
|
|
|
log::info!("got all DkgCommitments for {}", hex::encode(genesis));
|
2023-05-09 23:44:41 -04:00
|
|
|
processors
|
|
|
|
|
.send(
|
|
|
|
|
spec.set().network,
|
|
|
|
|
CoordinatorMessage::KeyGen(key_gen::CoordinatorMessage::Commitments {
|
|
|
|
|
id: KeyGenId { set: spec.set(), attempt },
|
|
|
|
|
commitments,
|
|
|
|
|
}),
|
|
|
|
|
)
|
2023-04-20 05:05:17 -04:00
|
|
|
.await;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-08-13 02:21:56 -04:00
|
|
|
Transaction::DkgShares(attempt, sender_i, mut shares, signed) => {
|
|
|
|
|
if sender_i !=
|
|
|
|
|
spec
|
|
|
|
|
.i(signed.signer)
|
|
|
|
|
.expect("transaction added to tributary by signer who isn't a participant")
|
|
|
|
|
{
|
2023-04-20 05:05:17 -04:00
|
|
|
// TODO: Full slash
|
2023-04-20 14:24:49 -04:00
|
|
|
todo!();
|
2023-04-20 05:05:17 -04:00
|
|
|
}
|
|
|
|
|
|
2023-08-13 02:21:56 -04:00
|
|
|
if shares.len() != (usize::from(spec.n()) - 1) {
|
|
|
|
|
// TODO: Full slash
|
|
|
|
|
todo!();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Only save our share's bytes
|
|
|
|
|
let our_i = spec
|
|
|
|
|
.i(Ristretto::generator() * key.deref())
|
|
|
|
|
.expect("in a tributary we're not a validator for");
|
|
|
|
|
// This unwrap is safe since the length of shares is checked, the the only missing key
|
|
|
|
|
// within the valid range will be the sender's i
|
|
|
|
|
let bytes = if sender_i == our_i { vec![] } else { shares.remove(&our_i).unwrap() };
|
2023-04-20 05:05:17 -04:00
|
|
|
|
2023-04-20 14:24:49 -04:00
|
|
|
if let Some(shares) =
|
|
|
|
|
handle(Zone::Dkg, b"dkg_shares", spec.n(), [0; 32], attempt, bytes, signed)
|
|
|
|
|
{
|
2023-08-13 02:21:56 -04:00
|
|
|
log::info!("got all DkgShares for {}", hex::encode(genesis));
|
2023-05-09 23:44:41 -04:00
|
|
|
processors
|
|
|
|
|
.send(
|
|
|
|
|
spec.set().network,
|
|
|
|
|
CoordinatorMessage::KeyGen(key_gen::CoordinatorMessage::Shares {
|
|
|
|
|
id: KeyGenId { set: spec.set(), attempt },
|
|
|
|
|
shares,
|
|
|
|
|
}),
|
|
|
|
|
)
|
2023-04-20 05:05:17 -04:00
|
|
|
.await;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-04-20 14:51:33 -04:00
|
|
|
Transaction::ExternalBlock(block) => {
|
|
|
|
|
// Because this external block has been finalized, its batch ID should be authorized
|
2023-05-08 22:20:51 -04:00
|
|
|
TributaryDb::<D>::recognize_id(&mut txn, Zone::Batch.label(), genesis, block);
|
|
|
|
|
recognized_id
|
|
|
|
|
.send((genesis, RecognizedIdType::Block, block))
|
|
|
|
|
.expect("recognized_id_recv was dropped. are we shutting down?");
|
2023-04-20 14:51:33 -04:00
|
|
|
}
|
2023-04-20 15:37:22 -04:00
|
|
|
|
|
|
|
|
Transaction::SubstrateBlock(block) => {
|
2023-04-24 06:50:40 -04:00
|
|
|
let plan_ids = TributaryDb::<D>::plan_ids(&txn, genesis, block).expect(
|
2023-04-20 15:37:22 -04:00
|
|
|
"synced a tributary block finalizing a substrate block in a provided transaction \
|
|
|
|
|
despite us not providing that transaction",
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
for id in plan_ids {
|
2023-04-24 06:50:40 -04:00
|
|
|
TributaryDb::<D>::recognize_id(&mut txn, Zone::Sign.label(), genesis, id);
|
2023-05-08 22:20:51 -04:00
|
|
|
recognized_id
|
|
|
|
|
.send((genesis, RecognizedIdType::Plan, id))
|
|
|
|
|
.expect("recognized_id_recv was dropped. are we shutting down?");
|
2023-04-20 15:37:22 -04:00
|
|
|
}
|
|
|
|
|
}
|
2023-04-20 05:05:17 -04:00
|
|
|
|
2023-04-20 06:27:00 -04:00
|
|
|
Transaction::BatchPreprocess(data) => {
|
2023-04-20 14:24:49 -04:00
|
|
|
if let Some(preprocesses) = handle(
|
|
|
|
|
Zone::Batch,
|
|
|
|
|
b"batch_preprocess",
|
|
|
|
|
spec.t(),
|
|
|
|
|
data.plan,
|
|
|
|
|
data.attempt,
|
|
|
|
|
data.data,
|
|
|
|
|
data.signed,
|
|
|
|
|
) {
|
2023-05-09 23:44:41 -04:00
|
|
|
processors
|
|
|
|
|
.send(
|
|
|
|
|
spec.set().network,
|
|
|
|
|
CoordinatorMessage::Coordinator(
|
|
|
|
|
coordinator::CoordinatorMessage::BatchPreprocesses {
|
|
|
|
|
id: SignId { key: todo!(), id: data.plan, attempt: data.attempt },
|
|
|
|
|
preprocesses,
|
|
|
|
|
},
|
|
|
|
|
),
|
|
|
|
|
)
|
2023-04-20 06:27:00 -04:00
|
|
|
.await;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
Transaction::BatchShare(data) => {
|
2023-04-20 14:24:49 -04:00
|
|
|
if let Some(shares) = handle(
|
|
|
|
|
Zone::Batch,
|
|
|
|
|
b"batch_share",
|
|
|
|
|
spec.t(),
|
|
|
|
|
data.plan,
|
|
|
|
|
data.attempt,
|
|
|
|
|
data.data,
|
|
|
|
|
data.signed,
|
|
|
|
|
) {
|
2023-05-09 23:44:41 -04:00
|
|
|
processors
|
|
|
|
|
.send(
|
|
|
|
|
spec.set().network,
|
|
|
|
|
CoordinatorMessage::Coordinator(coordinator::CoordinatorMessage::BatchShares {
|
|
|
|
|
id: SignId { key: todo!(), id: data.plan, attempt: data.attempt },
|
|
|
|
|
shares: shares
|
|
|
|
|
.drain()
|
|
|
|
|
.map(|(validator, share)| (validator, share.try_into().unwrap()))
|
|
|
|
|
.collect(),
|
|
|
|
|
}),
|
|
|
|
|
)
|
2023-04-20 06:27:00 -04:00
|
|
|
.await;
|
2023-04-20 06:59:42 -04:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Transaction::SignPreprocess(data) => {
|
2023-04-20 14:24:49 -04:00
|
|
|
if let Some(preprocesses) = handle(
|
|
|
|
|
Zone::Sign,
|
|
|
|
|
b"sign_preprocess",
|
|
|
|
|
spec.t(),
|
|
|
|
|
data.plan,
|
|
|
|
|
data.attempt,
|
|
|
|
|
data.data,
|
|
|
|
|
data.signed,
|
|
|
|
|
) {
|
2023-05-09 23:44:41 -04:00
|
|
|
processors
|
|
|
|
|
.send(
|
|
|
|
|
spec.set().network,
|
|
|
|
|
CoordinatorMessage::Sign(sign::CoordinatorMessage::Preprocesses {
|
|
|
|
|
id: SignId { key: todo!(), id: data.plan, attempt: data.attempt },
|
|
|
|
|
preprocesses,
|
|
|
|
|
}),
|
|
|
|
|
)
|
2023-04-20 06:59:42 -04:00
|
|
|
.await;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
Transaction::SignShare(data) => {
|
2023-04-20 14:24:49 -04:00
|
|
|
if let Some(shares) = handle(
|
|
|
|
|
Zone::Sign,
|
|
|
|
|
b"sign_share",
|
|
|
|
|
spec.t(),
|
|
|
|
|
data.plan,
|
|
|
|
|
data.attempt,
|
|
|
|
|
data.data,
|
|
|
|
|
data.signed,
|
|
|
|
|
) {
|
2023-05-09 23:44:41 -04:00
|
|
|
processors
|
|
|
|
|
.send(
|
|
|
|
|
spec.set().network,
|
|
|
|
|
CoordinatorMessage::Sign(sign::CoordinatorMessage::Shares {
|
|
|
|
|
id: SignId { key: todo!(), id: data.plan, attempt: data.attempt },
|
|
|
|
|
shares,
|
|
|
|
|
}),
|
|
|
|
|
)
|
2023-04-20 06:59:42 -04:00
|
|
|
.await;
|
2023-04-20 06:27:00 -04:00
|
|
|
}
|
|
|
|
|
}
|
2023-07-14 14:05:12 -04:00
|
|
|
Transaction::SignCompleted(_, _, _) => todo!(),
|
2023-04-20 05:05:17 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
TributaryDb::<D>::handle_event(&mut txn, hash, event_id);
|
|
|
|
|
txn.commit();
|
|
|
|
|
}
|
|
|
|
|
event_id += 1;
|
|
|
|
|
}
|
2023-04-20 06:27:00 -04:00
|
|
|
|
|
|
|
|
// TODO: Trigger any necessary re-attempts
|
2023-04-20 05:05:17 -04:00
|
|
|
}
|
|
|
|
|
|
2023-05-09 23:44:41 -04:00
|
|
|
pub async fn handle_new_blocks<D: Db, Pro: Processors>(
|
2023-04-20 05:05:17 -04:00
|
|
|
db: &mut TributaryDb<D>,
|
|
|
|
|
key: &Zeroizing<<Ristretto as Ciphersuite>::F>,
|
2023-05-08 22:20:51 -04:00
|
|
|
recognized_id: &UnboundedSender<([u8; 32], RecognizedIdType, [u8; 32])>,
|
2023-05-09 23:44:41 -04:00
|
|
|
processors: &Pro,
|
2023-04-20 05:05:17 -04:00
|
|
|
spec: &TributarySpec,
|
2023-04-24 06:50:40 -04:00
|
|
|
tributary: &TributaryReader<D, Transaction>,
|
2023-04-20 05:05:17 -04:00
|
|
|
) {
|
2023-04-24 06:50:40 -04:00
|
|
|
let genesis = tributary.genesis();
|
|
|
|
|
let mut last_block = db.last_block(genesis);
|
2023-04-24 00:53:15 -04:00
|
|
|
while let Some(next) = tributary.block_after(&last_block) {
|
|
|
|
|
let block = tributary.block(&next).unwrap();
|
2023-05-09 23:44:41 -04:00
|
|
|
handle_block(db, key, recognized_id, processors, spec, block).await;
|
2023-04-24 00:53:15 -04:00
|
|
|
last_block = next;
|
2023-04-24 06:50:40 -04:00
|
|
|
db.set_last_block(genesis, next);
|
2023-04-20 05:05:17 -04:00
|
|
|
}
|
|
|
|
|
}
|