* Remove NetworkId from processor-messages

Because intent binds to the sender/receiver, it's not needed for intent.

The processor knows what the network is.

The coordinator knows which to use because it's sending this message to the
processor for that network.

Also removes the unused zeroize.

* ProcessorMessage::Completed use Session instead of key

* Move SubstrateSignId to Session

* Finish replacing key with session
This commit is contained in:
Luke Parker
2023-11-26 12:14:23 -05:00
committed by GitHub
parent b79cf8abde
commit 571195bfda
31 changed files with 304 additions and 455 deletions

View File

@@ -1,18 +1,16 @@
use std::collections::HashMap;
use zeroize::Zeroize;
use scale::{Encode, Decode};
use borsh::{BorshSerialize, BorshDeserialize};
use dkg::{Participant, ThresholdParams};
use serai_primitives::{BlockHash, NetworkId};
use serai_primitives::BlockHash;
use in_instructions_primitives::{Batch, SignedBatch};
use coins_primitives::OutInstructionWithBalance;
use validator_sets_primitives::{ValidatorSet, KeyPair};
use validator_sets_primitives::{Session, KeyPair};
#[derive(Clone, Copy, PartialEq, Eq, Debug, Zeroize, BorshSerialize, BorshDeserialize)]
#[derive(Clone, Copy, PartialEq, Eq, Debug, BorshSerialize, BorshDeserialize)]
pub struct SubstrateContext {
pub serai_time: u64,
pub network_latest_finalized_block: BlockHash,
@@ -22,20 +20,10 @@ pub mod key_gen {
use super::*;
#[derive(
Clone,
Copy,
PartialEq,
Eq,
Hash,
Debug,
Zeroize,
Encode,
Decode,
BorshSerialize,
BorshDeserialize,
Clone, Copy, PartialEq, Eq, Hash, Debug, Encode, Decode, BorshSerialize, BorshDeserialize,
)]
pub struct KeyGenId {
pub set: ValidatorSet,
pub session: Session,
pub attempt: u32,
}
@@ -116,11 +104,9 @@ pub mod key_gen {
pub mod sign {
use super::*;
#[derive(
Clone, PartialEq, Eq, Hash, Debug, Zeroize, Encode, Decode, BorshSerialize, BorshDeserialize,
)]
#[derive(Clone, PartialEq, Eq, Hash, Debug, Encode, Decode, BorshSerialize, BorshDeserialize)]
pub struct SignId {
pub key: Vec<u8>,
pub session: Session,
pub id: [u8; 32],
pub attempt: u32,
}
@@ -134,7 +120,7 @@ pub mod sign {
// Re-attempt a signing protocol.
Reattempt { id: SignId },
// Completed a signing protocol already.
Completed { key: Vec<u8>, id: [u8; 32], tx: Vec<u8> },
Completed { session: Session, id: [u8; 32], tx: Vec<u8> },
}
impl CoordinatorMessage {
@@ -142,17 +128,17 @@ pub mod sign {
None
}
pub fn key(&self) -> &[u8] {
pub fn session(&self) -> Session {
match self {
CoordinatorMessage::Preprocesses { id, .. } => &id.key,
CoordinatorMessage::Shares { id, .. } => &id.key,
CoordinatorMessage::Reattempt { id } => &id.key,
CoordinatorMessage::Completed { key, .. } => key,
CoordinatorMessage::Preprocesses { id, .. } => id.session,
CoordinatorMessage::Shares { id, .. } => id.session,
CoordinatorMessage::Reattempt { id } => id.session,
CoordinatorMessage::Completed { session, .. } => *session,
}
}
}
#[derive(Clone, PartialEq, Eq, Debug, Zeroize, BorshSerialize, BorshDeserialize)]
#[derive(Clone, PartialEq, Eq, Debug, BorshSerialize, BorshDeserialize)]
pub enum ProcessorMessage {
// Participant sent an invalid message during the sign protocol.
InvalidParticipant { id: SignId, participant: Participant },
@@ -161,7 +147,7 @@ pub mod sign {
// Signed share for the specified signing protocol.
Share { id: SignId, shares: Vec<Vec<u8>> },
// Completed a signing protocol already.
Completed { key: Vec<u8>, id: [u8; 32], tx: Vec<u8> },
Completed { session: Session, id: [u8; 32], tx: Vec<u8> },
}
}
@@ -178,28 +164,16 @@ pub mod coordinator {
}
#[derive(
Clone,
Copy,
PartialEq,
Eq,
Hash,
Debug,
Zeroize,
Encode,
Decode,
BorshSerialize,
BorshDeserialize,
Clone, Copy, PartialEq, Eq, Hash, Debug, Encode, Decode, BorshSerialize, BorshDeserialize,
)]
pub enum SubstrateSignableId {
CosigningSubstrateBlock([u8; 32]),
Batch([u8; 5]),
}
#[derive(
Clone, PartialEq, Eq, Hash, Debug, Zeroize, Encode, Decode, BorshSerialize, BorshDeserialize,
)]
#[derive(Clone, PartialEq, Eq, Hash, Debug, Encode, Decode, BorshSerialize, BorshDeserialize)]
pub struct SubstrateSignId {
pub key: [u8; 32],
pub session: Session,
pub id: SubstrateSignableId,
pub attempt: u32,
}
@@ -226,26 +200,17 @@ pub mod coordinator {
CoordinatorMessage::BatchReattempt { .. } => None,
}
}
pub fn key(&self) -> &[u8] {
match self {
CoordinatorMessage::CosignSubstrateBlock { id, .. } => &id.key,
CoordinatorMessage::SubstratePreprocesses { id, .. } => &id.key,
CoordinatorMessage::SubstrateShares { id, .. } => &id.key,
CoordinatorMessage::BatchReattempt { id } => &id.key,
}
}
}
#[derive(Clone, PartialEq, Eq, Debug, Zeroize, BorshSerialize, BorshDeserialize)]
#[derive(Clone, PartialEq, Eq, Debug, BorshSerialize, BorshDeserialize)]
pub struct PlanMeta {
pub key: Vec<u8>,
pub session: Session,
pub id: [u8; 32],
}
#[derive(Clone, PartialEq, Eq, Debug, Zeroize, BorshSerialize, BorshDeserialize)]
#[derive(Clone, PartialEq, Eq, Debug, BorshSerialize, BorshDeserialize)]
pub enum ProcessorMessage {
SubstrateBlockAck { network: NetworkId, block: u64, plans: Vec<PlanMeta> },
SubstrateBlockAck { block: u64, plans: Vec<PlanMeta> },
InvalidParticipant { id: SubstrateSignId, participant: Participant },
CosignPreprocess { id: SubstrateSignId, preprocesses: Vec<[u8; 64]> },
BatchPreprocess { id: SubstrateSignId, block: BlockHash, preprocesses: Vec<[u8; 64]> },
@@ -261,12 +226,11 @@ pub mod substrate {
pub enum CoordinatorMessage {
ConfirmKeyPair {
context: SubstrateContext,
set: ValidatorSet,
session: Session,
key_pair: KeyPair,
},
SubstrateBlock {
context: SubstrateContext,
network: NetworkId,
block: u64,
burns: Vec<OutInstructionWithBalance>,
batches: Vec<u32>,
@@ -283,7 +247,7 @@ pub mod substrate {
}
}
#[derive(Clone, PartialEq, Eq, Debug, Zeroize, BorshSerialize, BorshDeserialize)]
#[derive(Clone, PartialEq, Eq, Debug, BorshSerialize, BorshDeserialize)]
pub enum ProcessorMessage {
Batch { batch: Batch },
SignedBatch { batch: SignedBatch },
@@ -364,7 +328,7 @@ impl CoordinatorMessage {
pub fn intent(&self) -> Vec<u8> {
match self {
CoordinatorMessage::KeyGen(msg) => {
// Unique since key gen ID embeds the validator set and attempt
// Unique since key gen ID embeds the session and attempt
let (sub, id) = match msg {
key_gen::CoordinatorMessage::GenerateKey { id, .. } => (0, id),
key_gen::CoordinatorMessage::Commitments { id, .. } => (1, id),
@@ -409,11 +373,9 @@ impl CoordinatorMessage {
}
CoordinatorMessage::Substrate(msg) => {
let (sub, id) = match msg {
// Unique since there's only one key pair for a set
substrate::CoordinatorMessage::ConfirmKeyPair { set, .. } => (0, set.encode()),
substrate::CoordinatorMessage::SubstrateBlock { network, block, .. } => {
(1, (network, block).encode())
}
// Unique since there's only one key pair for a session
substrate::CoordinatorMessage::ConfirmKeyPair { session, .. } => (0, session.encode()),
substrate::CoordinatorMessage::SubstrateBlock { block, .. } => (1, block.encode()),
};
let mut res = vec![COORDINATOR_UID, TYPE_SUBSTRATE_UID, sub];
@@ -464,9 +426,7 @@ impl ProcessorMessage {
}
ProcessorMessage::Coordinator(msg) => {
let (sub, id) = match msg {
coordinator::ProcessorMessage::SubstrateBlockAck { network, block, .. } => {
(0, (network, block).encode())
}
coordinator::ProcessorMessage::SubstrateBlockAck { block, .. } => (0, block.encode()),
// Unique since SubstrateSignId
coordinator::ProcessorMessage::InvalidParticipant { id, .. } => (1, id.encode()),
coordinator::ProcessorMessage::CosignPreprocess { id, .. } => (2, id.encode()),