Cleanup DB handling a bit in key-gen/attempt-manager

This commit is contained in:
Luke Parker
2024-08-19 00:41:18 -04:00
parent 1e8a9ec5bd
commit 2f3bd7a02a
6 changed files with 77 additions and 27 deletions

View File

@@ -36,10 +36,10 @@ pub(crate) struct Participations {
}
create_db!(
KeyGenDb {
ParamsDb: (session: &Session) -> RawParams,
ParticipationsDb: (session: &Session) -> Participations,
KeySharesDb: (session: &Session) -> Vec<u8>,
KeyGen {
Params: (session: &Session) -> RawParams,
Participations: (session: &Session) -> Participations,
KeyShares: (session: &Session) -> Vec<u8>,
}
);
@@ -48,7 +48,7 @@ impl<P: KeyGenParams> KeyGenDb<P> {
pub(crate) fn set_params(txn: &mut impl DbTxn, session: Session, params: Params<P>) {
assert_eq!(params.substrate_evrf_public_keys.len(), params.network_evrf_public_keys.len());
ParamsDb::set(
Params::set(
txn,
&session,
&RawParams {
@@ -68,7 +68,7 @@ impl<P: KeyGenParams> KeyGenDb<P> {
}
pub(crate) fn params(getter: &impl Get, session: Session) -> Option<Params<P>> {
ParamsDb::get(getter, &session).map(|params| Params {
Params::get(getter, &session).map(|params| Params {
t: params.t,
n: params
.network_evrf_public_keys
@@ -101,12 +101,13 @@ impl<P: KeyGenParams> KeyGenDb<P> {
session: Session,
participations: &Participations,
) {
ParticipationsDb::set(txn, &session, participations)
Participations::set(txn, &session, participations)
}
pub(crate) fn participations(getter: &impl Get, session: Session) -> Option<Participations> {
ParticipationsDb::get(getter, &session)
Participations::get(getter, &session)
}
// Set the key shares for a session.
pub(crate) fn set_key_shares(
txn: &mut impl DbTxn,
session: Session,
@@ -120,7 +121,7 @@ impl<P: KeyGenParams> KeyGenDb<P> {
keys.extend(substrate_keys.serialize().as_slice());
keys.extend(network_keys.serialize().as_slice());
}
KeySharesDb::set(txn, &session, &keys);
KeyShares::set(txn, &session, &keys);
}
#[allow(clippy::type_complexity)]
@@ -128,7 +129,7 @@ impl<P: KeyGenParams> KeyGenDb<P> {
getter: &impl Get,
session: Session,
) -> Option<(Vec<ThresholdKeys<Ristretto>>, Vec<ThresholdKeys<P::ExternalNetworkCurve>>)> {
let keys = KeySharesDb::get(getter, &session)?;
let keys = KeyShares::get(getter, &session)?;
let mut keys: &[u8] = keys.as_ref();
let mut substrate_keys = vec![];

View File

@@ -182,7 +182,7 @@ impl<P: KeyGenParams, D: Db> KeyGen<P, D> {
match msg {
CoordinatorMessage::GenerateKey { session, threshold, evrf_public_keys } => {
log::info!("Generating new key. Session: {session:?}");
log::info!("generating new key, session: {session:?}");
// Unzip the vector of eVRF keys
let substrate_evrf_public_keys =
@@ -258,7 +258,7 @@ impl<P: KeyGenParams, D: Db> KeyGen<P, D> {
}
CoordinatorMessage::Participation { session, participant, participation } => {
log::info!("received participation from {:?} for {:?}", participant, session);
log::debug!("received participation from {:?} for {:?}", participant, session);
let Params { t: threshold, n, substrate_evrf_public_keys, network_evrf_public_keys } =
KeyGenDb::<P>::params(txn, session).unwrap();
@@ -293,7 +293,7 @@ impl<P: KeyGenParams, D: Db> KeyGen<P, D> {
// participations and continue. We solely have to verify them, as to identify malicious
// participants and prevent DoSs, before returning
if self.key_shares(session).is_some() {
log::info!("already finished generating a key for {:?}", session);
log::debug!("already finished generating a key for {:?}", session);
match EvrfDkg::<Ristretto>::verify(
&mut OsRng,
@@ -511,6 +511,8 @@ impl<P: KeyGenParams, D: Db> KeyGen<P, D> {
}
KeyGenDb::<P>::set_key_shares(txn, session, &substrate_keys, &network_keys);
log::info!("generated key, session: {session:?}");
// Since no one we verified was invalid, and we had the threshold, yield the new keys
vec![ProcessorMessage::GeneratedKeyPair {
session,