Route KeyPair so Tributary can construct SignId as needed

This commit is contained in:
Luke Parker 2023-08-25 18:37:22 -04:00
parent 3745f8b6af
commit f249e20028
No known key found for this signature in database
3 changed files with 43 additions and 5 deletions

View file

@ -23,7 +23,11 @@ use processor_messages::{SubstrateContext, key_gen::KeyGenId, CoordinatorMessage
use tokio::time::sleep; use tokio::time::sleep;
use crate::{Db, processors::Processors, tributary::TributarySpec}; use crate::{
Db,
processors::Processors,
tributary::{TributarySpec, TributaryDb},
};
mod db; mod db;
pub use db::*; pub use db::*;
@ -298,6 +302,12 @@ async fn handle_block<
if !SubstrateDb::<D>::handled_event(&db.0, hash, event_id) { if !SubstrateDb::<D>::handled_event(&db.0, hash, event_id) {
log::info!("found fresh key gen event {:?}", key_gen); log::info!("found fresh key gen event {:?}", key_gen);
if let ValidatorSetsEvent::KeyGen { set, key_pair } = 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::<D>::set_key_pair(&mut txn, set, &key_pair);
txn.commit();
handle_key_gen(key, processors, serai, &block, set, key_pair).await?; handle_key_gen(key, processors, serai, &block, set, key_pair).await?;
} else { } else {
panic!("KeyGen event wasn't KeyGen: {key_gen:?}"); panic!("KeyGen event wasn't KeyGen: {key_gen:?}");

View file

@ -4,7 +4,7 @@ use scale::{Encode, Decode};
use ciphersuite::{group::GroupEncoding, Ciphersuite, Ristretto}; 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::*; pub use serai_db::*;
@ -87,6 +87,16 @@ impl<D: Db> TributaryDb<D> {
.map(|bytes| KeyPair::decode(&mut bytes.as_slice()).unwrap()) .map(|bytes| KeyPair::decode(&mut bytes.as_slice()).unwrap())
} }
pub fn key_pair_key(set: ValidatorSet) -> Vec<u8> {
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<G: Get>(getter: &G, set: ValidatorSet) -> Option<KeyPair> {
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<u8> { fn recognized_id_key(label: &'static str, genesis: [u8; 32], id: [u8; 32]) -> Vec<u8> {
Self::tributary_key(b"recognized", [label.as_bytes(), genesis.as_ref(), id.as_ref()].concat()) Self::tributary_key(b"recognized", [label.as_bytes(), genesis.as_ref(), id.as_ref()].concat())
} }

View file

@ -522,6 +522,7 @@ pub async fn handle_application_tx<
} }
Transaction::SignPreprocess(data) => { Transaction::SignPreprocess(data) => {
let key_pair = TributaryDb::<D>::key_pair(txn, spec.set());
match handle( match handle(
txn, txn,
Zone::Sign, Zone::Sign,
@ -537,7 +538,14 @@ pub async fn handle_application_tx<
.send( .send(
spec.set().network, spec.set().network,
CoordinatorMessage::Sign(sign::CoordinatorMessage::Preprocesses { 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, preprocesses,
}), }),
) )
@ -548,6 +556,7 @@ pub async fn handle_application_tx<
} }
} }
Transaction::SignShare(data) => { Transaction::SignShare(data) => {
let key_pair = TributaryDb::<D>::key_pair(txn, spec.set());
match handle( match handle(
txn, txn,
Zone::Sign, Zone::Sign,
@ -563,7 +572,14 @@ pub async fn handle_application_tx<
.send( .send(
spec.set().network, spec.set().network,
CoordinatorMessage::Sign(sign::CoordinatorMessage::Shares { 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, shares,
}), }),
) )
@ -573,6 +589,8 @@ pub async fn handle_application_tx<
None => {} None => {}
} }
} }
Transaction::SignCompleted(_, _, _) => todo!(), Transaction::SignCompleted(_, _, _) => {
// TODO
}
} }
} }