mirror of
https://github.com/serai-dex/serai.git
synced 2025-01-08 20:09:54 +00:00
Use scale instead of bincode throughout processor-messages/processor DB
scale is canonical, bincode is not.
This commit is contained in:
parent
f7e49e1f90
commit
7d8e08d5b4
6 changed files with 39 additions and 43 deletions
2
Cargo.lock
generated
2
Cargo.lock
generated
|
@ -8383,8 +8383,8 @@ dependencies = [
|
|||
name = "serai-processor-messages"
|
||||
version = "0.1.0"
|
||||
dependencies = [
|
||||
"bincode",
|
||||
"dkg",
|
||||
"parity-scale-codec",
|
||||
"serai-in-instructions-primitives",
|
||||
"serai-primitives",
|
||||
"serai-tokens-primitives",
|
||||
|
|
|
@ -16,8 +16,8 @@ rustdoc-args = ["--cfg", "docsrs"]
|
|||
[dependencies]
|
||||
zeroize = { version = "1", features = ["derive"] }
|
||||
|
||||
scale = { package = "parity-scale-codec", version = "3", default-features = false }
|
||||
serde = { version = "1", features = ["derive"] }
|
||||
bincode = "1"
|
||||
|
||||
dkg = { path = "../../crypto/dkg", features = ["serde"] }
|
||||
|
||||
|
|
|
@ -2,6 +2,7 @@ use std::collections::HashMap;
|
|||
|
||||
use zeroize::Zeroize;
|
||||
|
||||
use scale::{Encode, Decode};
|
||||
use serde::{Serialize, Deserialize};
|
||||
|
||||
use dkg::{Participant, ThresholdParams};
|
||||
|
@ -11,7 +12,7 @@ use in_instructions_primitives::SignedBatch;
|
|||
use tokens_primitives::OutInstructionWithBalance;
|
||||
use validator_sets_primitives::{ValidatorSet, KeyPair};
|
||||
|
||||
#[derive(Clone, Copy, PartialEq, Eq, Debug, Zeroize, Serialize, Deserialize)]
|
||||
#[derive(Clone, Copy, PartialEq, Eq, Debug, Zeroize, Encode, Decode, Serialize, Deserialize)]
|
||||
pub struct SubstrateContext {
|
||||
pub serai_time: u64,
|
||||
pub network_latest_finalized_block: BlockHash,
|
||||
|
@ -20,7 +21,7 @@ pub struct SubstrateContext {
|
|||
pub mod key_gen {
|
||||
use super::*;
|
||||
|
||||
#[derive(Clone, Copy, PartialEq, Eq, Hash, Debug, Zeroize, Serialize, Deserialize)]
|
||||
#[derive(Clone, Copy, PartialEq, Eq, Hash, Debug, Zeroize, Encode, Decode, Serialize, Deserialize)]
|
||||
pub struct KeyGenId {
|
||||
pub set: ValidatorSet,
|
||||
pub attempt: u32,
|
||||
|
@ -57,7 +58,7 @@ pub mod key_gen {
|
|||
pub mod sign {
|
||||
use super::*;
|
||||
|
||||
#[derive(Clone, PartialEq, Eq, Hash, Debug, Zeroize, Serialize, Deserialize)]
|
||||
#[derive(Clone, PartialEq, Eq, Hash, Debug, Zeroize, Encode, Decode, Serialize, Deserialize)]
|
||||
pub struct SignId {
|
||||
pub key: Vec<u8>,
|
||||
pub id: [u8; 32],
|
||||
|
@ -91,7 +92,7 @@ pub mod sign {
|
|||
}
|
||||
}
|
||||
|
||||
#[derive(Clone, PartialEq, Eq, Debug, Zeroize, Serialize, Deserialize)]
|
||||
#[derive(Clone, PartialEq, Eq, Debug, Zeroize, Encode, Decode, Serialize, Deserialize)]
|
||||
pub enum ProcessorMessage {
|
||||
// Created preprocess for the specified signing protocol.
|
||||
Preprocess { id: SignId, preprocess: Vec<u8> },
|
||||
|
@ -136,7 +137,7 @@ pub mod coordinator {
|
|||
}
|
||||
}
|
||||
|
||||
#[derive(Clone, PartialEq, Eq, Debug, Zeroize, Serialize, Deserialize)]
|
||||
#[derive(Clone, PartialEq, Eq, Debug, Zeroize, Encode, Decode, Serialize, Deserialize)]
|
||||
pub enum ProcessorMessage {
|
||||
SubstrateBlockAck { network: NetworkId, block: u64, plans: Vec<[u8; 32]> },
|
||||
BatchPreprocess { id: SignId, block: BlockHash, preprocess: Vec<u8> },
|
||||
|
@ -147,7 +148,7 @@ pub mod coordinator {
|
|||
pub mod substrate {
|
||||
use super::*;
|
||||
|
||||
#[derive(Clone, PartialEq, Eq, Debug, Serialize, Deserialize)]
|
||||
#[derive(Clone, PartialEq, Eq, Debug, Encode, Decode, Serialize, Deserialize)]
|
||||
pub enum CoordinatorMessage {
|
||||
ConfirmKeyPair {
|
||||
context: SubstrateContext,
|
||||
|
@ -174,7 +175,7 @@ pub mod substrate {
|
|||
}
|
||||
}
|
||||
|
||||
#[derive(Clone, PartialEq, Eq, Debug, Zeroize, Serialize, Deserialize)]
|
||||
#[derive(Clone, PartialEq, Eq, Debug, Zeroize, Encode, Decode, Serialize, Deserialize)]
|
||||
pub enum ProcessorMessage {
|
||||
Update { batch: SignedBatch },
|
||||
}
|
||||
|
@ -251,7 +252,6 @@ impl CoordinatorMessage {
|
|||
/// This doesn't use H(msg.serialize()) as it's meant to be unique to intent, not unique to
|
||||
/// values. While the values should be consistent per intent, that assumption isn't required
|
||||
/// here.
|
||||
// TODO: Should this use borsh intead of bincode?
|
||||
pub fn intent(&self) -> Vec<u8> {
|
||||
match self {
|
||||
CoordinatorMessage::KeyGen(msg) => {
|
||||
|
@ -263,21 +263,21 @@ impl CoordinatorMessage {
|
|||
};
|
||||
|
||||
let mut res = vec![COORDINATOR_UID, TYPE_KEY_GEN_UID, sub];
|
||||
res.extend(&bincode::serialize(id).unwrap());
|
||||
res.extend(&id.encode());
|
||||
res
|
||||
}
|
||||
CoordinatorMessage::Sign(msg) => {
|
||||
let (sub, id) = match msg {
|
||||
// Unique since SignId includes a hash of the network, and specific transaction info
|
||||
sign::CoordinatorMessage::Preprocesses { id, .. } => (0, bincode::serialize(id).unwrap()),
|
||||
sign::CoordinatorMessage::Shares { id, .. } => (1, bincode::serialize(id).unwrap()),
|
||||
sign::CoordinatorMessage::Reattempt { id } => (2, bincode::serialize(id).unwrap()),
|
||||
sign::CoordinatorMessage::Preprocesses { id, .. } => (0, id.encode()),
|
||||
sign::CoordinatorMessage::Shares { id, .. } => (1, id.encode()),
|
||||
sign::CoordinatorMessage::Reattempt { id } => (2, id.encode()),
|
||||
// The coordinator should report all reported completions to the processor
|
||||
// Accordingly, the intent is a combination of plan ID and actual TX
|
||||
// While transaction alone may suffice, that doesn't cover cross-chain TX ID conflicts,
|
||||
// which are possible
|
||||
sign::CoordinatorMessage::Completed { id, tx, .. } => {
|
||||
(3, bincode::serialize(&(id, tx)).unwrap())
|
||||
(3, (id, tx).encode())
|
||||
}
|
||||
};
|
||||
|
||||
|
@ -289,13 +289,13 @@ impl CoordinatorMessage {
|
|||
let (sub, id) = match msg {
|
||||
// Unique since this embeds the batch ID (hash of it, including its network) and attempt
|
||||
coordinator::CoordinatorMessage::BatchPreprocesses { id, .. } => {
|
||||
(0, bincode::serialize(id).unwrap())
|
||||
(0, id.encode())
|
||||
}
|
||||
coordinator::CoordinatorMessage::BatchShares { id, .. } => {
|
||||
(1, bincode::serialize(id).unwrap())
|
||||
(1, id.encode())
|
||||
}
|
||||
coordinator::CoordinatorMessage::BatchReattempt { id, .. } => {
|
||||
(2, bincode::serialize(id).unwrap())
|
||||
(2, id.encode())
|
||||
}
|
||||
};
|
||||
|
||||
|
@ -307,10 +307,10 @@ impl CoordinatorMessage {
|
|||
let (sub, id) = match msg {
|
||||
// Unique since there's only one key pair for a set
|
||||
substrate::CoordinatorMessage::ConfirmKeyPair { set, .. } => {
|
||||
(0, bincode::serialize(set).unwrap())
|
||||
(0, set.encode())
|
||||
}
|
||||
substrate::CoordinatorMessage::SubstrateBlock { network, block, .. } => {
|
||||
(1, bincode::serialize(&(network, block)).unwrap())
|
||||
(1, (network, block).encode())
|
||||
}
|
||||
};
|
||||
|
||||
|
@ -340,14 +340,14 @@ impl ProcessorMessage {
|
|||
};
|
||||
|
||||
let mut res = vec![PROCESSSOR_UID, TYPE_KEY_GEN_UID, sub];
|
||||
res.extend(&bincode::serialize(id).unwrap());
|
||||
res.extend(&id.encode());
|
||||
res
|
||||
}
|
||||
ProcessorMessage::Sign(msg) => {
|
||||
let (sub, id) = match msg {
|
||||
// Unique since SignId
|
||||
sign::ProcessorMessage::Preprocess { id, .. } => (0, bincode::serialize(id).unwrap()),
|
||||
sign::ProcessorMessage::Share { id, .. } => (1, bincode::serialize(id).unwrap()),
|
||||
sign::ProcessorMessage::Preprocess { id, .. } => (0, id.encode()),
|
||||
sign::ProcessorMessage::Share { id, .. } => (1, id.encode()),
|
||||
// Unique since a processor will only sign a TX once
|
||||
sign::ProcessorMessage::Completed { id, .. } => (2, id.to_vec()),
|
||||
};
|
||||
|
@ -359,14 +359,14 @@ impl ProcessorMessage {
|
|||
ProcessorMessage::Coordinator(msg) => {
|
||||
let (sub, id) = match msg {
|
||||
coordinator::ProcessorMessage::SubstrateBlockAck { network, block, .. } => {
|
||||
(0, bincode::serialize(&(network, block)).unwrap())
|
||||
(0, (network, block).encode())
|
||||
}
|
||||
// Unique since SignId
|
||||
coordinator::ProcessorMessage::BatchPreprocess { id, .. } => {
|
||||
(1, bincode::serialize(id).unwrap())
|
||||
(1, id.encode())
|
||||
}
|
||||
coordinator::ProcessorMessage::BatchShare { id, .. } => {
|
||||
(2, bincode::serialize(id).unwrap())
|
||||
(2, id.encode())
|
||||
}
|
||||
};
|
||||
|
||||
|
@ -378,7 +378,7 @@ impl ProcessorMessage {
|
|||
let (sub, id) = match msg {
|
||||
// Unique since network and ID binding
|
||||
substrate::ProcessorMessage::Update { batch, .. } => {
|
||||
(0, bincode::serialize(&(batch.batch.network, batch.batch.id)).unwrap())
|
||||
(0, (batch.batch.network, batch.batch.id).encode())
|
||||
}
|
||||
};
|
||||
|
||||
|
|
|
@ -15,6 +15,7 @@ use frost::{
|
|||
|
||||
use log::info;
|
||||
|
||||
use scale::Encode;
|
||||
use serai_client::validator_sets::primitives::{ValidatorSet, KeyPair};
|
||||
use messages::key_gen::*;
|
||||
|
||||
|
@ -34,7 +35,7 @@ impl<N: Network, D: Db> KeyGenDb<N, D> {
|
|||
}
|
||||
|
||||
fn params_key(set: &ValidatorSet) -> Vec<u8> {
|
||||
Self::key_gen_key(b"params", bincode::serialize(set).unwrap())
|
||||
Self::key_gen_key(b"params", set.encode())
|
||||
}
|
||||
fn save_params(txn: &mut D::Transaction<'_>, set: &ValidatorSet, params: &ThresholdParams) {
|
||||
txn.put(Self::params_key(set), bincode::serialize(params).unwrap());
|
||||
|
@ -48,7 +49,7 @@ impl<N: Network, D: Db> KeyGenDb<N, D> {
|
|||
// A former attempt may become the finalized attempt, even if it doesn't in a timely manner
|
||||
// Overwriting its commitments would be accordingly poor
|
||||
fn commitments_key(id: &KeyGenId) -> Vec<u8> {
|
||||
Self::key_gen_key(b"commitments", bincode::serialize(id).unwrap())
|
||||
Self::key_gen_key(b"commitments", id.encode())
|
||||
}
|
||||
fn save_commitments(
|
||||
txn: &mut D::Transaction<'_>,
|
||||
|
@ -64,8 +65,8 @@ impl<N: Network, D: Db> KeyGenDb<N, D> {
|
|||
.unwrap()
|
||||
}
|
||||
|
||||
fn generated_keys_key(set: ValidatorSet, key_pair: (&[u8], &[u8])) -> Vec<u8> {
|
||||
Self::key_gen_key(b"generated_keys", bincode::serialize(&(set, key_pair)).unwrap())
|
||||
fn generated_keys_key(set: ValidatorSet, key_pair: (&[u8; 32], &[u8])) -> Vec<u8> {
|
||||
Self::key_gen_key(b"generated_keys", (set, key_pair).encode())
|
||||
}
|
||||
fn save_keys(
|
||||
txn: &mut D::Transaction<'_>,
|
||||
|
@ -78,10 +79,7 @@ impl<N: Network, D: Db> KeyGenDb<N, D> {
|
|||
txn.put(
|
||||
Self::generated_keys_key(
|
||||
id.set,
|
||||
(
|
||||
substrate_keys.group_key().to_bytes().as_ref(),
|
||||
network_keys.group_key().to_bytes().as_ref(),
|
||||
),
|
||||
(&substrate_keys.group_key().to_bytes(), network_keys.group_key().to_bytes().as_ref()),
|
||||
),
|
||||
keys,
|
||||
);
|
||||
|
@ -107,10 +105,8 @@ impl<N: Network, D: Db> KeyGenDb<N, D> {
|
|||
set: ValidatorSet,
|
||||
key_pair: KeyPair,
|
||||
) -> (ThresholdKeys<Ristretto>, ThresholdKeys<N::Curve>) {
|
||||
let (keys_vec, keys) = Self::read_keys(
|
||||
txn,
|
||||
&Self::generated_keys_key(set, (key_pair.0.as_ref(), key_pair.1.as_ref())),
|
||||
);
|
||||
let (keys_vec, keys) =
|
||||
Self::read_keys(txn, &Self::generated_keys_key(set, (&key_pair.0 .0, key_pair.1.as_ref())));
|
||||
assert_eq!(key_pair.0 .0, keys.0.group_key().to_bytes());
|
||||
assert_eq!(
|
||||
{
|
||||
|
|
|
@ -11,6 +11,7 @@ use frost::{
|
|||
|
||||
use log::{info, debug, warn, error};
|
||||
|
||||
use scale::Encode;
|
||||
use messages::sign::*;
|
||||
use crate::{
|
||||
Get, DbTxn, Db,
|
||||
|
@ -74,7 +75,7 @@ impl<N: Network, D: Db> SignerDb<N, D> {
|
|||
}
|
||||
|
||||
fn attempt_key(id: &SignId) -> Vec<u8> {
|
||||
Self::sign_key(b"attempt", bincode::serialize(id).unwrap())
|
||||
Self::sign_key(b"attempt", id.encode())
|
||||
}
|
||||
fn attempt(txn: &mut D::Transaction<'_>, id: &SignId) {
|
||||
txn.put(Self::attempt_key(id), []);
|
||||
|
|
|
@ -3,9 +3,7 @@ use std::collections::{VecDeque, HashMap};
|
|||
|
||||
use rand_core::OsRng;
|
||||
|
||||
use scale::Encode;
|
||||
use transcript::{Transcript, RecommendedTranscript};
|
||||
|
||||
use frost::{
|
||||
curve::Ristretto,
|
||||
ThresholdKeys,
|
||||
|
@ -18,6 +16,7 @@ use frost_schnorrkel::Schnorrkel;
|
|||
|
||||
use log::{info, debug, warn};
|
||||
|
||||
use scale::Encode;
|
||||
use serai_client::{
|
||||
primitives::NetworkId,
|
||||
in_instructions::primitives::{Batch, SignedBatch, batch_message},
|
||||
|
@ -63,7 +62,7 @@ impl<D: Db> SubstrateSignerDb<D> {
|
|||
}
|
||||
|
||||
fn attempt_key(id: &SignId) -> Vec<u8> {
|
||||
Self::sign_key(b"attempt", bincode::serialize(id).unwrap())
|
||||
Self::sign_key(b"attempt", id.encode())
|
||||
}
|
||||
fn attempt(txn: &mut D::Transaction<'_>, id: &SignId) {
|
||||
txn.put(Self::attempt_key(id), []);
|
||||
|
|
Loading…
Reference in a new issue