mirror of
https://github.com/serai-dex/serai.git
synced 2025-12-08 20:29:23 +00:00
Tweak ConfirmKeyPair to alleviate database requirements of coordinator
This commit is contained in:
@@ -124,8 +124,8 @@ async fn handle_key_gen<D: Db, Pro: Processor>(
|
|||||||
.await?
|
.await?
|
||||||
.unwrap_or(BlockHash([0; 32])), // TODO: Have the processor override this
|
.unwrap_or(BlockHash([0; 32])), // TODO: Have the processor override this
|
||||||
},
|
},
|
||||||
// TODO: Check the DB for which attempt used this key pair
|
set,
|
||||||
id: KeyGenId { set, attempt: todo!() },
|
key_pair,
|
||||||
},
|
},
|
||||||
))
|
))
|
||||||
.await;
|
.await;
|
||||||
|
|||||||
@@ -9,7 +9,7 @@ use dkg::{Participant, ThresholdParams};
|
|||||||
use serai_primitives::BlockHash;
|
use serai_primitives::BlockHash;
|
||||||
use in_instructions_primitives::SignedBatch;
|
use in_instructions_primitives::SignedBatch;
|
||||||
use tokens_primitives::OutInstructionWithBalance;
|
use tokens_primitives::OutInstructionWithBalance;
|
||||||
use validator_sets_primitives::ValidatorSet;
|
use validator_sets_primitives::{ValidatorSet, KeyPair};
|
||||||
|
|
||||||
#[derive(Clone, Copy, PartialEq, Eq, Debug, Zeroize, Serialize, Deserialize)]
|
#[derive(Clone, Copy, PartialEq, Eq, Debug, Zeroize, Serialize, Deserialize)]
|
||||||
pub struct SubstrateContext {
|
pub struct SubstrateContext {
|
||||||
@@ -141,11 +141,12 @@ pub mod coordinator {
|
|||||||
pub mod substrate {
|
pub mod substrate {
|
||||||
use super::*;
|
use super::*;
|
||||||
|
|
||||||
#[derive(Clone, PartialEq, Eq, Debug, Zeroize, Serialize, Deserialize)]
|
#[derive(Clone, PartialEq, Eq, Debug, Serialize, Deserialize)]
|
||||||
pub enum CoordinatorMessage {
|
pub enum CoordinatorMessage {
|
||||||
ConfirmKeyPair {
|
ConfirmKeyPair {
|
||||||
context: SubstrateContext,
|
context: SubstrateContext,
|
||||||
id: key_gen::KeyGenId,
|
set: ValidatorSet,
|
||||||
|
key_pair: KeyPair,
|
||||||
},
|
},
|
||||||
SubstrateBlock {
|
SubstrateBlock {
|
||||||
context: SubstrateContext,
|
context: SubstrateContext,
|
||||||
|
|||||||
@@ -15,7 +15,10 @@ use frost::{
|
|||||||
|
|
||||||
use log::info;
|
use log::info;
|
||||||
|
|
||||||
use serai_client::{primitives::BlockHash, validator_sets::primitives::ValidatorSet};
|
use serai_client::{
|
||||||
|
primitives::BlockHash,
|
||||||
|
validator_sets::primitives::{ValidatorSet, KeyPair},
|
||||||
|
};
|
||||||
use messages::{SubstrateContext, key_gen::*};
|
use messages::{SubstrateContext, key_gen::*};
|
||||||
|
|
||||||
use crate::{Get, DbTxn, Db, coins::Coin};
|
use crate::{Get, DbTxn, Db, coins::Coin};
|
||||||
@@ -65,8 +68,8 @@ impl<C: Coin, D: Db> KeyGenDb<C, D> {
|
|||||||
.unwrap()
|
.unwrap()
|
||||||
}
|
}
|
||||||
|
|
||||||
fn generated_keys_key(id: &KeyGenId) -> Vec<u8> {
|
fn generated_keys_key(set: ValidatorSet, key_pair: (&[u8], &[u8])) -> Vec<u8> {
|
||||||
Self::key_gen_key(b"generated_keys", bincode::serialize(id).unwrap())
|
Self::key_gen_key(b"generated_keys", bincode::serialize(&(set, key_pair)).unwrap())
|
||||||
}
|
}
|
||||||
fn save_keys(
|
fn save_keys(
|
||||||
txn: &mut D::Transaction<'_>,
|
txn: &mut D::Transaction<'_>,
|
||||||
@@ -76,7 +79,13 @@ impl<C: Coin, D: Db> KeyGenDb<C, D> {
|
|||||||
) {
|
) {
|
||||||
let mut keys = substrate_keys.serialize();
|
let mut keys = substrate_keys.serialize();
|
||||||
keys.extend(coin_keys.serialize().iter());
|
keys.extend(coin_keys.serialize().iter());
|
||||||
txn.put(Self::generated_keys_key(id), keys);
|
txn.put(
|
||||||
|
Self::generated_keys_key(
|
||||||
|
id.set,
|
||||||
|
(substrate_keys.group_key().to_bytes().as_ref(), coin_keys.group_key().to_bytes().as_ref()),
|
||||||
|
),
|
||||||
|
keys,
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
fn keys_key(key: &<C::Curve as Ciphersuite>::G) -> Vec<u8> {
|
fn keys_key(key: &<C::Curve as Ciphersuite>::G) -> Vec<u8> {
|
||||||
@@ -96,9 +105,13 @@ impl<C: Coin, D: Db> KeyGenDb<C, D> {
|
|||||||
}
|
}
|
||||||
fn confirm_keys(
|
fn confirm_keys(
|
||||||
txn: &mut D::Transaction<'_>,
|
txn: &mut D::Transaction<'_>,
|
||||||
id: &KeyGenId,
|
set: ValidatorSet,
|
||||||
|
key_pair: KeyPair,
|
||||||
) -> (ThresholdKeys<Ristretto>, ThresholdKeys<C::Curve>) {
|
) -> (ThresholdKeys<Ristretto>, ThresholdKeys<C::Curve>) {
|
||||||
let (keys_vec, keys) = Self::read_keys(txn, &Self::generated_keys_key(id));
|
let (keys_vec, keys) = Self::read_keys(
|
||||||
|
txn,
|
||||||
|
&Self::generated_keys_key(set, (key_pair.0.as_ref(), key_pair.1.as_ref())),
|
||||||
|
);
|
||||||
txn.put(Self::keys_key(&keys.1.group_key()), keys_vec);
|
txn.put(Self::keys_key(&keys.1.group_key()), keys_vec);
|
||||||
keys
|
keys
|
||||||
}
|
}
|
||||||
@@ -352,15 +365,16 @@ impl<C: Coin, D: Db> KeyGen<C, D> {
|
|||||||
&mut self,
|
&mut self,
|
||||||
txn: &mut D::Transaction<'_>,
|
txn: &mut D::Transaction<'_>,
|
||||||
context: SubstrateContext,
|
context: SubstrateContext,
|
||||||
id: KeyGenId,
|
set: ValidatorSet,
|
||||||
|
key_pair: KeyPair,
|
||||||
) -> KeyConfirmed<C::Curve> {
|
) -> KeyConfirmed<C::Curve> {
|
||||||
let (substrate_keys, coin_keys) = KeyGenDb::<C, D>::confirm_keys(txn, &id);
|
let (substrate_keys, coin_keys) = KeyGenDb::<C, D>::confirm_keys(txn, set, key_pair);
|
||||||
|
|
||||||
info!(
|
info!(
|
||||||
"Confirmed key pair {} {} from {:?}",
|
"Confirmed key pair {} {} for set {:?}",
|
||||||
hex::encode(substrate_keys.group_key().to_bytes()),
|
hex::encode(substrate_keys.group_key().to_bytes()),
|
||||||
hex::encode(coin_keys.group_key().to_bytes()),
|
hex::encode(coin_keys.group_key().to_bytes()),
|
||||||
id
|
set,
|
||||||
);
|
);
|
||||||
|
|
||||||
KeyConfirmed {
|
KeyConfirmed {
|
||||||
|
|||||||
@@ -301,10 +301,10 @@ async fn handle_coordinator_msg<D: Db, C: Coin, Co: Coordinator>(
|
|||||||
|
|
||||||
CoordinatorMessage::Substrate(msg) => {
|
CoordinatorMessage::Substrate(msg) => {
|
||||||
match msg {
|
match msg {
|
||||||
messages::substrate::CoordinatorMessage::ConfirmKeyPair { context, id } => {
|
messages::substrate::CoordinatorMessage::ConfirmKeyPair { context, set, key_pair } => {
|
||||||
// See TributaryMutable's struct definition for why this block is safe
|
// See TributaryMutable's struct definition for why this block is safe
|
||||||
let KeyConfirmed { activation_block, substrate_keys, coin_keys } =
|
let KeyConfirmed { activation_block, substrate_keys, coin_keys } =
|
||||||
tributary_mutable.key_gen.confirm(txn, context, id).await;
|
tributary_mutable.key_gen.confirm(txn, context, set, key_pair).await;
|
||||||
tributary_mutable.substrate_signers.insert(
|
tributary_mutable.substrate_signers.insert(
|
||||||
substrate_keys.group_key().to_bytes().to_vec(),
|
substrate_keys.group_key().to_bytes().to_vec(),
|
||||||
SubstrateSigner::new(substrate_keys),
|
SubstrateSigner::new(substrate_keys),
|
||||||
|
|||||||
@@ -9,6 +9,7 @@ use frost::{Participant, ThresholdParams, tests::clone_without};
|
|||||||
|
|
||||||
use serai_db::{DbTxn, Db, MemDb};
|
use serai_db::{DbTxn, Db, MemDb};
|
||||||
|
|
||||||
|
use sp_application_crypto::sr25519;
|
||||||
use serai_client::{
|
use serai_client::{
|
||||||
primitives::{MONERO_NET_ID, BlockHash},
|
primitives::{MONERO_NET_ID, BlockHash},
|
||||||
validator_sets::primitives::{Session, ValidatorSet},
|
validator_sets::primitives::{Session, ValidatorSet},
|
||||||
@@ -124,6 +125,7 @@ pub async fn test_key_gen<C: Coin>() {
|
|||||||
}
|
}
|
||||||
txn.commit();
|
txn.commit();
|
||||||
}
|
}
|
||||||
|
let res = res.unwrap();
|
||||||
|
|
||||||
// Rebuild 1 and 4
|
// Rebuild 1 and 4
|
||||||
rebuild(&mut key_gens, &dbs, 1);
|
rebuild(&mut key_gens, &dbs, 1);
|
||||||
@@ -136,7 +138,8 @@ pub async fn test_key_gen<C: Coin>() {
|
|||||||
.confirm(
|
.confirm(
|
||||||
&mut txn,
|
&mut txn,
|
||||||
SubstrateContext { coin_latest_finalized_block: BlockHash([0x11; 32]) },
|
SubstrateContext { coin_latest_finalized_block: BlockHash([0x11; 32]) },
|
||||||
ID,
|
ID.set,
|
||||||
|
(sr25519::Public(res.0), res.1.clone().try_into().unwrap()),
|
||||||
)
|
)
|
||||||
.await;
|
.await;
|
||||||
txn.commit();
|
txn.commit();
|
||||||
@@ -147,8 +150,8 @@ pub async fn test_key_gen<C: Coin>() {
|
|||||||
assert_eq!(substrate_keys.params(), params);
|
assert_eq!(substrate_keys.params(), params);
|
||||||
assert_eq!(coin_keys.params(), params);
|
assert_eq!(coin_keys.params(), params);
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
&(substrate_keys.group_key().to_bytes(), coin_keys.group_key().to_bytes().as_ref().to_vec()),
|
(substrate_keys.group_key().to_bytes(), coin_keys.group_key().to_bytes().as_ref().to_vec()),
|
||||||
res.as_ref().unwrap()
|
res
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user