mirror of
https://github.com/serai-dex/serai.git
synced 2024-12-23 03:59:22 +00:00
Only emit Preprocesses/Shares when participating
This commit is contained in:
parent
2702384c70
commit
8c1d8a2658
1 changed files with 139 additions and 110 deletions
|
@ -157,7 +157,7 @@ fn read_known_to_exist_data<D: Db, G: Get>(
|
||||||
attempt: u32,
|
attempt: u32,
|
||||||
bytes: Vec<u8>,
|
bytes: Vec<u8>,
|
||||||
signed: Option<&Signed>,
|
signed: Option<&Signed>,
|
||||||
) -> HashMap<Participant, Vec<u8>> {
|
) -> Option<HashMap<Participant, Vec<u8>>> {
|
||||||
let mut data = HashMap::new();
|
let mut data = HashMap::new();
|
||||||
for validator in spec.validators().iter().map(|validator| validator.0) {
|
for validator in spec.validators().iter().map(|validator| validator.0) {
|
||||||
data.insert(
|
data.insert(
|
||||||
|
@ -175,16 +175,19 @@ fn read_known_to_exist_data<D: Db, G: Get>(
|
||||||
}
|
}
|
||||||
assert_eq!(data.len(), usize::from(needed));
|
assert_eq!(data.len(), usize::from(needed));
|
||||||
|
|
||||||
// Remove our own piece of data
|
// Remove our own piece of data, if we were involved
|
||||||
assert!(data
|
if data
|
||||||
.remove(
|
.remove(
|
||||||
&spec
|
&spec
|
||||||
.i(Ristretto::generator() * key.deref())
|
.i(Ristretto::generator() * key.deref())
|
||||||
.expect("handling a message for a Tributary we aren't part of")
|
.expect("handling a message for a Tributary we aren't part of"),
|
||||||
)
|
)
|
||||||
.is_some());
|
.is_some()
|
||||||
|
{
|
||||||
data
|
Some(data)
|
||||||
|
} else {
|
||||||
|
None
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn dkg_confirmation_nonces(
|
pub fn dkg_confirmation_nonces(
|
||||||
|
@ -204,7 +207,7 @@ pub fn generated_key_pair<D: Db>(
|
||||||
TributaryDb::<D>::save_currently_completing_key_pair(txn, spec.genesis(), key_pair);
|
TributaryDb::<D>::save_currently_completing_key_pair(txn, spec.genesis(), key_pair);
|
||||||
|
|
||||||
let attempt = 0; // TODO
|
let attempt = 0; // TODO
|
||||||
let preprocesses = read_known_to_exist_data::<D, _>(
|
let Some(preprocesses) = read_known_to_exist_data::<D, _>(
|
||||||
txn,
|
txn,
|
||||||
spec,
|
spec,
|
||||||
key,
|
key,
|
||||||
|
@ -214,7 +217,9 @@ pub fn generated_key_pair<D: Db>(
|
||||||
attempt,
|
attempt,
|
||||||
vec![],
|
vec![],
|
||||||
None,
|
None,
|
||||||
);
|
) else {
|
||||||
|
panic!("wasn't a participant in confirming a key pair");
|
||||||
|
};
|
||||||
DkgConfirmer::share(spec, key, preprocesses, key_pair)
|
DkgConfirmer::share(spec, key, preprocesses, key_pair)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -317,19 +322,21 @@ pub async fn handle_application_tx<
|
||||||
|
|
||||||
match tx {
|
match tx {
|
||||||
Transaction::DkgCommitments(attempt, bytes, signed) => {
|
Transaction::DkgCommitments(attempt, bytes, signed) => {
|
||||||
if let Some(commitments) =
|
match handle(txn, Zone::Dkg, b"dkg_commitments", spec.n(), [0; 32], attempt, bytes, &signed) {
|
||||||
handle(txn, Zone::Dkg, b"dkg_commitments", spec.n(), [0; 32], attempt, bytes, &signed)
|
Some(Some(commitments)) => {
|
||||||
{
|
log::info!("got all DkgCommitments for {}", hex::encode(genesis));
|
||||||
log::info!("got all DkgCommitments for {}", hex::encode(genesis));
|
processors
|
||||||
processors
|
.send(
|
||||||
.send(
|
spec.set().network,
|
||||||
spec.set().network,
|
CoordinatorMessage::KeyGen(key_gen::CoordinatorMessage::Commitments {
|
||||||
CoordinatorMessage::KeyGen(key_gen::CoordinatorMessage::Commitments {
|
id: KeyGenId { set: spec.set(), attempt },
|
||||||
id: KeyGenId { set: spec.set(), attempt },
|
commitments,
|
||||||
commitments,
|
}),
|
||||||
}),
|
)
|
||||||
)
|
.await;
|
||||||
.await;
|
}
|
||||||
|
Some(None) => panic!("wasn't a participant in DKG commitments"),
|
||||||
|
None => {}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -366,27 +373,27 @@ pub async fn handle_application_tx<
|
||||||
confirmation_nonces.to_vec(),
|
confirmation_nonces.to_vec(),
|
||||||
&signed,
|
&signed,
|
||||||
);
|
);
|
||||||
if let Some(shares) =
|
match handle(txn, Zone::Dkg, b"dkg_shares", spec.n(), [0; 32], attempt, bytes, &signed) {
|
||||||
handle(txn, Zone::Dkg, b"dkg_shares", spec.n(), [0; 32], attempt, bytes, &signed)
|
Some(Some(shares)) => {
|
||||||
{
|
log::info!("got all DkgShares for {}", hex::encode(genesis));
|
||||||
log::info!("got all DkgShares for {}", hex::encode(genesis));
|
assert!(confirmation_nonces.is_some());
|
||||||
assert!(confirmation_nonces.is_some());
|
processors
|
||||||
processors
|
.send(
|
||||||
.send(
|
spec.set().network,
|
||||||
spec.set().network,
|
CoordinatorMessage::KeyGen(key_gen::CoordinatorMessage::Shares {
|
||||||
CoordinatorMessage::KeyGen(key_gen::CoordinatorMessage::Shares {
|
id: KeyGenId { set: spec.set(), attempt },
|
||||||
id: KeyGenId { set: spec.set(), attempt },
|
shares,
|
||||||
shares,
|
}),
|
||||||
}),
|
)
|
||||||
)
|
.await;
|
||||||
.await;
|
}
|
||||||
} else {
|
Some(None) => panic!("wasn't a participant in DKG shares"),
|
||||||
assert!(confirmation_nonces.is_none());
|
None => assert!(confirmation_nonces.is_none()),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
Transaction::DkgConfirmed(attempt, shares, signed) => {
|
Transaction::DkgConfirmed(attempt, shares, signed) => {
|
||||||
if let Some(shares) = handle(
|
match handle(
|
||||||
txn,
|
txn,
|
||||||
Zone::Dkg,
|
Zone::Dkg,
|
||||||
DKG_CONFIRMATION_SHARES,
|
DKG_CONFIRMATION_SHARES,
|
||||||
|
@ -396,37 +403,43 @@ pub async fn handle_application_tx<
|
||||||
shares.to_vec(),
|
shares.to_vec(),
|
||||||
&signed,
|
&signed,
|
||||||
) {
|
) {
|
||||||
log::info!("got all DkgConfirmed for {}", hex::encode(genesis));
|
Some(Some(shares)) => {
|
||||||
|
log::info!("got all DkgConfirmed for {}", hex::encode(genesis));
|
||||||
|
|
||||||
let preprocesses = read_known_to_exist_data::<D, _>(
|
let Some(preprocesses) = read_known_to_exist_data::<D, _>(
|
||||||
txn,
|
txn,
|
||||||
spec,
|
spec,
|
||||||
key,
|
key,
|
||||||
DKG_CONFIRMATION_NONCES,
|
DKG_CONFIRMATION_NONCES,
|
||||||
[0; 32],
|
[0; 32],
|
||||||
spec.n(),
|
spec.n(),
|
||||||
attempt,
|
attempt,
|
||||||
vec![],
|
vec![],
|
||||||
None,
|
None,
|
||||||
);
|
) else {
|
||||||
|
panic!("wasn't a participant in DKG confirmation nonces");
|
||||||
|
};
|
||||||
|
|
||||||
let key_pair = TributaryDb::<D>::currently_completing_key_pair(txn, genesis)
|
let key_pair = TributaryDb::<D>::currently_completing_key_pair(txn, genesis)
|
||||||
.unwrap_or_else(|| {
|
.unwrap_or_else(|| {
|
||||||
panic!(
|
panic!(
|
||||||
"in DkgConfirmed handling, which happens after everyone {}",
|
"in DkgConfirmed handling, which happens after everyone {}",
|
||||||
"(including us) fires DkgConfirmed, yet no confirming key pair"
|
"(including us) fires DkgConfirmed, yet no confirming key pair"
|
||||||
)
|
)
|
||||||
});
|
});
|
||||||
let Ok(sig) = DkgConfirmer::complete(spec, key, preprocesses, &key_pair, shares) else {
|
let Ok(sig) = DkgConfirmer::complete(spec, key, preprocesses, &key_pair, shares) else {
|
||||||
// TODO: Full slash
|
// TODO: Full slash
|
||||||
todo!();
|
todo!();
|
||||||
};
|
};
|
||||||
|
|
||||||
publish_serai_tx(
|
publish_serai_tx(
|
||||||
spec.set(),
|
spec.set(),
|
||||||
Serai::set_validator_set_keys(spec.set().network, key_pair, Signature(sig)),
|
Serai::set_validator_set_keys(spec.set().network, key_pair, Signature(sig)),
|
||||||
)
|
)
|
||||||
.await;
|
.await;
|
||||||
|
}
|
||||||
|
Some(None) => panic!("wasn't a participant in DKG confirmination shares"),
|
||||||
|
None => {}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -453,7 +466,7 @@ pub async fn handle_application_tx<
|
||||||
}
|
}
|
||||||
|
|
||||||
Transaction::BatchPreprocess(data) => {
|
Transaction::BatchPreprocess(data) => {
|
||||||
if let Some(preprocesses) = handle(
|
match handle(
|
||||||
txn,
|
txn,
|
||||||
Zone::Batch,
|
Zone::Batch,
|
||||||
b"batch_preprocess",
|
b"batch_preprocess",
|
||||||
|
@ -463,19 +476,23 @@ pub async fn handle_application_tx<
|
||||||
data.data,
|
data.data,
|
||||||
&data.signed,
|
&data.signed,
|
||||||
) {
|
) {
|
||||||
processors
|
Some(Some(preprocesses)) => {
|
||||||
.send(
|
processors
|
||||||
spec.set().network,
|
.send(
|
||||||
CoordinatorMessage::Coordinator(coordinator::CoordinatorMessage::BatchPreprocesses {
|
spec.set().network,
|
||||||
id: SignId { key: vec![], id: data.plan, attempt: data.attempt },
|
CoordinatorMessage::Coordinator(coordinator::CoordinatorMessage::BatchPreprocesses {
|
||||||
preprocesses,
|
id: SignId { key: vec![], id: data.plan, attempt: data.attempt },
|
||||||
}),
|
preprocesses,
|
||||||
)
|
}),
|
||||||
.await;
|
)
|
||||||
|
.await;
|
||||||
|
}
|
||||||
|
Some(None) => {}
|
||||||
|
None => {}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Transaction::BatchShare(data) => {
|
Transaction::BatchShare(data) => {
|
||||||
if let Some(shares) = handle(
|
match handle(
|
||||||
txn,
|
txn,
|
||||||
Zone::Batch,
|
Zone::Batch,
|
||||||
b"batch_share",
|
b"batch_share",
|
||||||
|
@ -485,23 +502,27 @@ pub async fn handle_application_tx<
|
||||||
data.data,
|
data.data,
|
||||||
&data.signed,
|
&data.signed,
|
||||||
) {
|
) {
|
||||||
processors
|
Some(Some(shares)) => {
|
||||||
.send(
|
processors
|
||||||
spec.set().network,
|
.send(
|
||||||
CoordinatorMessage::Coordinator(coordinator::CoordinatorMessage::BatchShares {
|
spec.set().network,
|
||||||
id: SignId { key: vec![], id: data.plan, attempt: data.attempt },
|
CoordinatorMessage::Coordinator(coordinator::CoordinatorMessage::BatchShares {
|
||||||
shares: shares
|
id: SignId { key: vec![], id: data.plan, attempt: data.attempt },
|
||||||
.into_iter()
|
shares: shares
|
||||||
.map(|(validator, share)| (validator, share.try_into().unwrap()))
|
.into_iter()
|
||||||
.collect(),
|
.map(|(validator, share)| (validator, share.try_into().unwrap()))
|
||||||
}),
|
.collect(),
|
||||||
)
|
}),
|
||||||
.await;
|
)
|
||||||
|
.await;
|
||||||
|
}
|
||||||
|
Some(None) => {}
|
||||||
|
None => {}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
Transaction::SignPreprocess(data) => {
|
Transaction::SignPreprocess(data) => {
|
||||||
if let Some(preprocesses) = handle(
|
match handle(
|
||||||
txn,
|
txn,
|
||||||
Zone::Sign,
|
Zone::Sign,
|
||||||
b"sign_preprocess",
|
b"sign_preprocess",
|
||||||
|
@ -511,19 +532,23 @@ pub async fn handle_application_tx<
|
||||||
data.data,
|
data.data,
|
||||||
&data.signed,
|
&data.signed,
|
||||||
) {
|
) {
|
||||||
processors
|
Some(Some(preprocesses)) => {
|
||||||
.send(
|
processors
|
||||||
spec.set().network,
|
.send(
|
||||||
CoordinatorMessage::Sign(sign::CoordinatorMessage::Preprocesses {
|
spec.set().network,
|
||||||
id: SignId { key: todo!(), id: data.plan, attempt: data.attempt },
|
CoordinatorMessage::Sign(sign::CoordinatorMessage::Preprocesses {
|
||||||
preprocesses,
|
id: SignId { key: todo!(), id: data.plan, attempt: data.attempt },
|
||||||
}),
|
preprocesses,
|
||||||
)
|
}),
|
||||||
.await;
|
)
|
||||||
|
.await;
|
||||||
|
}
|
||||||
|
Some(None) => {}
|
||||||
|
None => {}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Transaction::SignShare(data) => {
|
Transaction::SignShare(data) => {
|
||||||
if let Some(shares) = handle(
|
match handle(
|
||||||
txn,
|
txn,
|
||||||
Zone::Sign,
|
Zone::Sign,
|
||||||
b"sign_share",
|
b"sign_share",
|
||||||
|
@ -533,15 +558,19 @@ pub async fn handle_application_tx<
|
||||||
data.data,
|
data.data,
|
||||||
&data.signed,
|
&data.signed,
|
||||||
) {
|
) {
|
||||||
processors
|
Some(Some(shares)) => {
|
||||||
.send(
|
processors
|
||||||
spec.set().network,
|
.send(
|
||||||
CoordinatorMessage::Sign(sign::CoordinatorMessage::Shares {
|
spec.set().network,
|
||||||
id: SignId { key: todo!(), id: data.plan, attempt: data.attempt },
|
CoordinatorMessage::Sign(sign::CoordinatorMessage::Shares {
|
||||||
shares,
|
id: SignId { key: todo!(), id: data.plan, attempt: data.attempt },
|
||||||
}),
|
shares,
|
||||||
)
|
}),
|
||||||
.await;
|
)
|
||||||
|
.await;
|
||||||
|
}
|
||||||
|
Some(None) => {}
|
||||||
|
None => {}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Transaction::SignCompleted(_, _, _) => todo!(),
|
Transaction::SignCompleted(_, _, _) => todo!(),
|
||||||
|
|
Loading…
Reference in a new issue