diff --git a/coordinator/src/substrate/mod.rs b/coordinator/src/substrate/mod.rs index fa699305..19d76ff9 100644 --- a/coordinator/src/substrate/mod.rs +++ b/coordinator/src/substrate/mod.rs @@ -23,7 +23,11 @@ use processor_messages::{SubstrateContext, key_gen::KeyGenId, CoordinatorMessage use tokio::time::sleep; -use crate::{Db, processors::Processors, tributary::TributarySpec}; +use crate::{ + Db, + processors::Processors, + tributary::{TributarySpec, TributaryDb}, +}; mod db; pub use db::*; @@ -298,6 +302,12 @@ async fn handle_block< if !SubstrateDb::::handled_event(&db.0, hash, event_id) { log::info!("found fresh key gen event {:?}", key_gen); if let ValidatorSetsEvent::KeyGen { set, key_pair } = key_gen { + // Immediately ensure this key pair is accessible to the tributary, before we fire any + // events off of it + let mut txn = db.0.txn(); + TributaryDb::::set_key_pair(&mut txn, set, &key_pair); + txn.commit(); + handle_key_gen(key, processors, serai, &block, set, key_pair).await?; } else { panic!("KeyGen event wasn't KeyGen: {key_gen:?}"); diff --git a/coordinator/src/tributary/db.rs b/coordinator/src/tributary/db.rs index 04c42748..6b423b03 100644 --- a/coordinator/src/tributary/db.rs +++ b/coordinator/src/tributary/db.rs @@ -4,7 +4,7 @@ use scale::{Encode, Decode}; use ciphersuite::{group::GroupEncoding, Ciphersuite, Ristretto}; -use serai_client::validator_sets::primitives::KeyPair; +use serai_client::validator_sets::primitives::{ValidatorSet, KeyPair}; pub use serai_db::*; @@ -87,6 +87,16 @@ impl TributaryDb { .map(|bytes| KeyPair::decode(&mut bytes.as_slice()).unwrap()) } + pub fn key_pair_key(set: ValidatorSet) -> Vec { + Self::tributary_key(b"key_pair", set.encode()) + } + pub fn set_key_pair(txn: &mut D::Transaction<'_>, set: ValidatorSet, key_pair: &KeyPair) { + txn.put(Self::key_pair_key(set), key_pair.encode()); + } + pub fn key_pair(getter: &G, set: ValidatorSet) -> Option { + Some(KeyPair::decode(&mut getter.get(Self::key_pair_key(set))?.as_slice()).unwrap()) + } + fn recognized_id_key(label: &'static str, genesis: [u8; 32], id: [u8; 32]) -> Vec { Self::tributary_key(b"recognized", [label.as_bytes(), genesis.as_ref(), id.as_ref()].concat()) } diff --git a/coordinator/src/tributary/handle.rs b/coordinator/src/tributary/handle.rs index 657a0b50..37fd4108 100644 --- a/coordinator/src/tributary/handle.rs +++ b/coordinator/src/tributary/handle.rs @@ -522,6 +522,7 @@ pub async fn handle_application_tx< } Transaction::SignPreprocess(data) => { + let key_pair = TributaryDb::::key_pair(txn, spec.set()); match handle( txn, Zone::Sign, @@ -537,7 +538,14 @@ pub async fn handle_application_tx< .send( spec.set().network, CoordinatorMessage::Sign(sign::CoordinatorMessage::Preprocesses { - id: SignId { key: todo!(), id: data.plan, attempt: data.attempt }, + id: SignId { + key: key_pair + .expect("completed SignPreprocess despite not setting the key pair") + .1 + .into(), + id: data.plan, + attempt: data.attempt, + }, preprocesses, }), ) @@ -548,6 +556,7 @@ pub async fn handle_application_tx< } } Transaction::SignShare(data) => { + let key_pair = TributaryDb::::key_pair(txn, spec.set()); match handle( txn, Zone::Sign, @@ -563,7 +572,14 @@ pub async fn handle_application_tx< .send( spec.set().network, CoordinatorMessage::Sign(sign::CoordinatorMessage::Shares { - id: SignId { key: todo!(), id: data.plan, attempt: data.attempt }, + id: SignId { + key: key_pair + .expect("completed SignShares despite not setting the key pair") + .1 + .into(), + id: data.plan, + attempt: data.attempt, + }, shares, }), ) @@ -573,6 +589,8 @@ pub async fn handle_application_tx< None => {} } } - Transaction::SignCompleted(_, _, _) => todo!(), + Transaction::SignCompleted(_, _, _) => { + // TODO + } } }