mirror of
https://github.com/serai-dex/serai.git
synced 2025-12-08 12:19:24 +00:00
Implement SeraiAddress <-> Participant mapping and add RemoveParticipant transactions
This commit is contained in:
@@ -4,7 +4,7 @@ use std::sync::Arc;
|
||||
use futures::stream::{StreamExt, FuturesOrdered};
|
||||
|
||||
use serai_client::{
|
||||
primitives::{PublicKey, NetworkId, EmbeddedEllipticCurve},
|
||||
primitives::{NetworkId, SeraiAddress, EmbeddedEllipticCurve},
|
||||
validator_sets::primitives::MAX_KEY_SHARES_PER_SET,
|
||||
Serai,
|
||||
};
|
||||
@@ -26,14 +26,14 @@ create_db!(
|
||||
pub struct EphemeralEventStream<D: Db> {
|
||||
db: D,
|
||||
serai: Arc<Serai>,
|
||||
validator: PublicKey,
|
||||
validator: SeraiAddress,
|
||||
}
|
||||
|
||||
impl<D: Db> EphemeralEventStream<D> {
|
||||
/// Create a new ephemeral event stream.
|
||||
///
|
||||
/// Only one of these may exist over the provided database.
|
||||
pub fn new(db: D, serai: Arc<Serai>, validator: PublicKey) -> Self {
|
||||
pub fn new(db: D, serai: Arc<Serai>, validator: SeraiAddress) -> Self {
|
||||
Self { db, serai, validator }
|
||||
}
|
||||
}
|
||||
@@ -145,6 +145,10 @@ impl<D: Db> ContinuallyRan for EphemeralEventStream<D> {
|
||||
"block #{block_number} declared a new set but didn't have the participants"
|
||||
))?
|
||||
};
|
||||
let validators = validators
|
||||
.into_iter()
|
||||
.map(|(validator, weight)| (SeraiAddress::from(validator), weight))
|
||||
.collect::<Vec<_>>();
|
||||
let in_set = validators.iter().any(|(validator, _)| *validator == self.validator);
|
||||
if in_set {
|
||||
if u16::try_from(validators.len()).is_err() {
|
||||
@@ -177,14 +181,16 @@ impl<D: Db> ContinuallyRan for EphemeralEventStream<D> {
|
||||
embedded_elliptic_curve_keys.push_back(async move {
|
||||
tokio::try_join!(
|
||||
// One future to fetch the substrate embedded key
|
||||
serai
|
||||
.embedded_elliptic_curve_key(validator, EmbeddedEllipticCurve::Embedwards25519),
|
||||
serai.embedded_elliptic_curve_key(
|
||||
validator.into(),
|
||||
EmbeddedEllipticCurve::Embedwards25519
|
||||
),
|
||||
// One future to fetch the external embedded key, if there is a distinct curve
|
||||
async {
|
||||
// `embedded_elliptic_curves` is documented to have the second entry be the
|
||||
// network-specific curve (if it exists and is distinct from Embedwards25519)
|
||||
if let Some(curve) = set.network.embedded_elliptic_curves().get(1) {
|
||||
serai.embedded_elliptic_curve_key(validator, *curve).await.map(Some)
|
||||
serai.embedded_elliptic_curve_key(validator.into(), *curve).await.map(Some)
|
||||
} else {
|
||||
Ok(None)
|
||||
}
|
||||
@@ -215,19 +221,22 @@ impl<D: Db> ContinuallyRan for EphemeralEventStream<D> {
|
||||
}
|
||||
}
|
||||
|
||||
crate::NewSet::send(
|
||||
&mut txn,
|
||||
&NewSetInformation {
|
||||
set: *set,
|
||||
serai_block: block.block_hash,
|
||||
declaration_time: block.time,
|
||||
// TODO: Why do we have this as an explicit field here?
|
||||
// Shouldn't thiis be inlined into the Processor's key gen code, where it's used?
|
||||
threshold: ((total_weight * 2) / 3) + 1,
|
||||
validators,
|
||||
evrf_public_keys,
|
||||
},
|
||||
);
|
||||
let mut new_set = NewSetInformation {
|
||||
set: *set,
|
||||
serai_block: block.block_hash,
|
||||
declaration_time: block.time,
|
||||
// TODO: Why do we have this as an explicit field here?
|
||||
// Shouldn't this be inlined into the Processor's key gen code, where it's used?
|
||||
threshold: ((total_weight * 2) / 3) + 1,
|
||||
validators,
|
||||
evrf_public_keys,
|
||||
participant_indexes: Default::default(),
|
||||
participant_indexes_reverse_lookup: Default::default(),
|
||||
};
|
||||
// These aren't serialized, and we immediately serialize and drop this, so this isn't
|
||||
// necessary. It's just good practice not have this be dirty
|
||||
new_set.init_participant_indexes();
|
||||
crate::NewSet::send(&mut txn, &new_set);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -2,11 +2,15 @@
|
||||
#![doc = include_str!("../README.md")]
|
||||
#![deny(missing_docs)]
|
||||
|
||||
use std::collections::HashMap;
|
||||
|
||||
use scale::{Encode, Decode};
|
||||
use borsh::{io, BorshSerialize, BorshDeserialize};
|
||||
use borsh::{BorshSerialize, BorshDeserialize};
|
||||
|
||||
use dkg::Participant;
|
||||
|
||||
use serai_client::{
|
||||
primitives::{NetworkId, PublicKey, Signature},
|
||||
primitives::{NetworkId, SeraiAddress, Signature},
|
||||
validator_sets::primitives::{Session, ValidatorSet, KeyPair, SlashReport},
|
||||
in_instructions::primitives::SignedBatch,
|
||||
Transaction,
|
||||
@@ -26,22 +30,9 @@ pub use publish_batch::PublishBatchTask;
|
||||
mod publish_slash_report;
|
||||
pub use publish_slash_report::PublishSlashReportTask;
|
||||
|
||||
fn borsh_serialize_validators<W: io::Write>(
|
||||
validators: &Vec<(PublicKey, u16)>,
|
||||
writer: &mut W,
|
||||
) -> Result<(), io::Error> {
|
||||
// This doesn't use `encode_to` as `encode_to` panics if the writer returns an error
|
||||
writer.write_all(&validators.encode())
|
||||
}
|
||||
|
||||
fn borsh_deserialize_validators<R: io::Read>(
|
||||
reader: &mut R,
|
||||
) -> Result<Vec<(PublicKey, u16)>, io::Error> {
|
||||
Decode::decode(&mut scale::IoReader(reader)).map_err(io::Error::other)
|
||||
}
|
||||
|
||||
/// The information for a new set.
|
||||
#[derive(Clone, Debug, BorshSerialize, BorshDeserialize)]
|
||||
#[borsh(init = init_participant_indexes)]
|
||||
pub struct NewSetInformation {
|
||||
/// The set.
|
||||
pub set: ValidatorSet,
|
||||
@@ -52,13 +43,34 @@ pub struct NewSetInformation {
|
||||
/// The threshold to use.
|
||||
pub threshold: u16,
|
||||
/// The validators, with the amount of key shares they have.
|
||||
#[borsh(
|
||||
serialize_with = "borsh_serialize_validators",
|
||||
deserialize_with = "borsh_deserialize_validators"
|
||||
)]
|
||||
pub validators: Vec<(PublicKey, u16)>,
|
||||
pub validators: Vec<(SeraiAddress, u16)>,
|
||||
/// The eVRF public keys.
|
||||
pub evrf_public_keys: Vec<([u8; 32], Vec<u8>)>,
|
||||
/// The participant indexes, indexed by their validator.
|
||||
#[borsh(skip)]
|
||||
pub participant_indexes: HashMap<SeraiAddress, Vec<Participant>>,
|
||||
/// The validators, indexed by their participant indexes.
|
||||
#[borsh(skip)]
|
||||
pub participant_indexes_reverse_lookup: HashMap<Participant, SeraiAddress>,
|
||||
}
|
||||
|
||||
impl NewSetInformation {
|
||||
fn init_participant_indexes(&mut self) {
|
||||
let mut next_i = 1;
|
||||
self.participant_indexes = HashMap::with_capacity(self.validators.len());
|
||||
self.participant_indexes_reverse_lookup = HashMap::with_capacity(self.validators.len());
|
||||
for (validator, weight) in &self.validators {
|
||||
let mut these_is = Vec::with_capacity((*weight).into());
|
||||
for _ in 0 .. *weight {
|
||||
let this_i = Participant::new(next_i).unwrap();
|
||||
next_i += 1;
|
||||
|
||||
these_is.push(this_i);
|
||||
self.participant_indexes_reverse_lookup.insert(this_i, *validator);
|
||||
}
|
||||
self.participant_indexes.insert(*validator, these_is);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
mod _public_db {
|
||||
|
||||
Reference in New Issue
Block a user