Use scale instead of bincode throughout processor-messages/processor DB

scale is canonical, bincode is not.
This commit is contained in:
Luke Parker 2023-09-02 07:53:14 -04:00
parent f7e49e1f90
commit 7d8e08d5b4
No known key found for this signature in database
6 changed files with 39 additions and 43 deletions

2
Cargo.lock generated
View file

@ -8383,8 +8383,8 @@ dependencies = [
name = "serai-processor-messages" name = "serai-processor-messages"
version = "0.1.0" version = "0.1.0"
dependencies = [ dependencies = [
"bincode",
"dkg", "dkg",
"parity-scale-codec",
"serai-in-instructions-primitives", "serai-in-instructions-primitives",
"serai-primitives", "serai-primitives",
"serai-tokens-primitives", "serai-tokens-primitives",

View file

@ -16,8 +16,8 @@ rustdoc-args = ["--cfg", "docsrs"]
[dependencies] [dependencies]
zeroize = { version = "1", features = ["derive"] } zeroize = { version = "1", features = ["derive"] }
scale = { package = "parity-scale-codec", version = "3", default-features = false }
serde = { version = "1", features = ["derive"] } serde = { version = "1", features = ["derive"] }
bincode = "1"
dkg = { path = "../../crypto/dkg", features = ["serde"] } dkg = { path = "../../crypto/dkg", features = ["serde"] }

View file

@ -2,6 +2,7 @@ use std::collections::HashMap;
use zeroize::Zeroize; use zeroize::Zeroize;
use scale::{Encode, Decode};
use serde::{Serialize, Deserialize}; use serde::{Serialize, Deserialize};
use dkg::{Participant, ThresholdParams}; use dkg::{Participant, ThresholdParams};
@ -11,7 +12,7 @@ use in_instructions_primitives::SignedBatch;
use tokens_primitives::OutInstructionWithBalance; use tokens_primitives::OutInstructionWithBalance;
use validator_sets_primitives::{ValidatorSet, KeyPair}; 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 struct SubstrateContext {
pub serai_time: u64, pub serai_time: u64,
pub network_latest_finalized_block: BlockHash, pub network_latest_finalized_block: BlockHash,
@ -20,7 +21,7 @@ pub struct SubstrateContext {
pub mod key_gen { pub mod key_gen {
use super::*; 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 struct KeyGenId {
pub set: ValidatorSet, pub set: ValidatorSet,
pub attempt: u32, pub attempt: u32,
@ -57,7 +58,7 @@ pub mod key_gen {
pub mod sign { pub mod sign {
use super::*; 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 struct SignId {
pub key: Vec<u8>, pub key: Vec<u8>,
pub id: [u8; 32], 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 { pub enum ProcessorMessage {
// Created preprocess for the specified signing protocol. // Created preprocess for the specified signing protocol.
Preprocess { id: SignId, preprocess: Vec<u8> }, 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 { pub enum ProcessorMessage {
SubstrateBlockAck { network: NetworkId, block: u64, plans: Vec<[u8; 32]> }, SubstrateBlockAck { network: NetworkId, block: u64, plans: Vec<[u8; 32]> },
BatchPreprocess { id: SignId, block: BlockHash, preprocess: Vec<u8> }, BatchPreprocess { id: SignId, block: BlockHash, preprocess: Vec<u8> },
@ -147,7 +148,7 @@ pub mod coordinator {
pub mod substrate { pub mod substrate {
use super::*; use super::*;
#[derive(Clone, PartialEq, Eq, Debug, Serialize, Deserialize)] #[derive(Clone, PartialEq, Eq, Debug, Encode, Decode, Serialize, Deserialize)]
pub enum CoordinatorMessage { pub enum CoordinatorMessage {
ConfirmKeyPair { ConfirmKeyPair {
context: SubstrateContext, 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 { pub enum ProcessorMessage {
Update { batch: SignedBatch }, 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 /// 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 /// values. While the values should be consistent per intent, that assumption isn't required
/// here. /// here.
// TODO: Should this use borsh intead of bincode?
pub fn intent(&self) -> Vec<u8> { pub fn intent(&self) -> Vec<u8> {
match self { match self {
CoordinatorMessage::KeyGen(msg) => { CoordinatorMessage::KeyGen(msg) => {
@ -263,21 +263,21 @@ impl CoordinatorMessage {
}; };
let mut res = vec![COORDINATOR_UID, TYPE_KEY_GEN_UID, sub]; let mut res = vec![COORDINATOR_UID, TYPE_KEY_GEN_UID, sub];
res.extend(&bincode::serialize(id).unwrap()); res.extend(&id.encode());
res res
} }
CoordinatorMessage::Sign(msg) => { CoordinatorMessage::Sign(msg) => {
let (sub, id) = match msg { let (sub, id) = match msg {
// Unique since SignId includes a hash of the network, and specific transaction info // Unique since SignId includes a hash of the network, and specific transaction info
sign::CoordinatorMessage::Preprocesses { id, .. } => (0, bincode::serialize(id).unwrap()), sign::CoordinatorMessage::Preprocesses { id, .. } => (0, id.encode()),
sign::CoordinatorMessage::Shares { id, .. } => (1, bincode::serialize(id).unwrap()), sign::CoordinatorMessage::Shares { id, .. } => (1, id.encode()),
sign::CoordinatorMessage::Reattempt { id } => (2, bincode::serialize(id).unwrap()), sign::CoordinatorMessage::Reattempt { id } => (2, id.encode()),
// The coordinator should report all reported completions to the processor // The coordinator should report all reported completions to the processor
// Accordingly, the intent is a combination of plan ID and actual TX // 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, // While transaction alone may suffice, that doesn't cover cross-chain TX ID conflicts,
// which are possible // which are possible
sign::CoordinatorMessage::Completed { id, tx, .. } => { 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 { let (sub, id) = match msg {
// Unique since this embeds the batch ID (hash of it, including its network) and attempt // Unique since this embeds the batch ID (hash of it, including its network) and attempt
coordinator::CoordinatorMessage::BatchPreprocesses { id, .. } => { coordinator::CoordinatorMessage::BatchPreprocesses { id, .. } => {
(0, bincode::serialize(id).unwrap()) (0, id.encode())
} }
coordinator::CoordinatorMessage::BatchShares { id, .. } => { coordinator::CoordinatorMessage::BatchShares { id, .. } => {
(1, bincode::serialize(id).unwrap()) (1, id.encode())
} }
coordinator::CoordinatorMessage::BatchReattempt { id, .. } => { coordinator::CoordinatorMessage::BatchReattempt { id, .. } => {
(2, bincode::serialize(id).unwrap()) (2, id.encode())
} }
}; };
@ -307,10 +307,10 @@ impl CoordinatorMessage {
let (sub, id) = match msg { let (sub, id) = match msg {
// Unique since there's only one key pair for a set // Unique since there's only one key pair for a set
substrate::CoordinatorMessage::ConfirmKeyPair { set, .. } => { substrate::CoordinatorMessage::ConfirmKeyPair { set, .. } => {
(0, bincode::serialize(set).unwrap()) (0, set.encode())
} }
substrate::CoordinatorMessage::SubstrateBlock { network, block, .. } => { 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]; let mut res = vec![PROCESSSOR_UID, TYPE_KEY_GEN_UID, sub];
res.extend(&bincode::serialize(id).unwrap()); res.extend(&id.encode());
res res
} }
ProcessorMessage::Sign(msg) => { ProcessorMessage::Sign(msg) => {
let (sub, id) = match msg { let (sub, id) = match msg {
// Unique since SignId // Unique since SignId
sign::ProcessorMessage::Preprocess { id, .. } => (0, bincode::serialize(id).unwrap()), sign::ProcessorMessage::Preprocess { id, .. } => (0, id.encode()),
sign::ProcessorMessage::Share { id, .. } => (1, bincode::serialize(id).unwrap()), sign::ProcessorMessage::Share { id, .. } => (1, id.encode()),
// Unique since a processor will only sign a TX once // Unique since a processor will only sign a TX once
sign::ProcessorMessage::Completed { id, .. } => (2, id.to_vec()), sign::ProcessorMessage::Completed { id, .. } => (2, id.to_vec()),
}; };
@ -359,14 +359,14 @@ impl ProcessorMessage {
ProcessorMessage::Coordinator(msg) => { ProcessorMessage::Coordinator(msg) => {
let (sub, id) = match msg { let (sub, id) = match msg {
coordinator::ProcessorMessage::SubstrateBlockAck { network, block, .. } => { coordinator::ProcessorMessage::SubstrateBlockAck { network, block, .. } => {
(0, bincode::serialize(&(network, block)).unwrap()) (0, (network, block).encode())
} }
// Unique since SignId // Unique since SignId
coordinator::ProcessorMessage::BatchPreprocess { id, .. } => { coordinator::ProcessorMessage::BatchPreprocess { id, .. } => {
(1, bincode::serialize(id).unwrap()) (1, id.encode())
} }
coordinator::ProcessorMessage::BatchShare { id, .. } => { coordinator::ProcessorMessage::BatchShare { id, .. } => {
(2, bincode::serialize(id).unwrap()) (2, id.encode())
} }
}; };
@ -378,7 +378,7 @@ impl ProcessorMessage {
let (sub, id) = match msg { let (sub, id) = match msg {
// Unique since network and ID binding // Unique since network and ID binding
substrate::ProcessorMessage::Update { batch, .. } => { substrate::ProcessorMessage::Update { batch, .. } => {
(0, bincode::serialize(&(batch.batch.network, batch.batch.id)).unwrap()) (0, (batch.batch.network, batch.batch.id).encode())
} }
}; };

View file

@ -15,6 +15,7 @@ use frost::{
use log::info; use log::info;
use scale::Encode;
use serai_client::validator_sets::primitives::{ValidatorSet, KeyPair}; use serai_client::validator_sets::primitives::{ValidatorSet, KeyPair};
use messages::key_gen::*; use messages::key_gen::*;
@ -34,7 +35,7 @@ impl<N: Network, D: Db> KeyGenDb<N, D> {
} }
fn params_key(set: &ValidatorSet) -> Vec<u8> { 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) { fn save_params(txn: &mut D::Transaction<'_>, set: &ValidatorSet, params: &ThresholdParams) {
txn.put(Self::params_key(set), bincode::serialize(params).unwrap()); 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 // A former attempt may become the finalized attempt, even if it doesn't in a timely manner
// Overwriting its commitments would be accordingly poor // Overwriting its commitments would be accordingly poor
fn commitments_key(id: &KeyGenId) -> Vec<u8> { 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( fn save_commitments(
txn: &mut D::Transaction<'_>, txn: &mut D::Transaction<'_>,
@ -64,8 +65,8 @@ impl<N: Network, D: Db> KeyGenDb<N, D> {
.unwrap() .unwrap()
} }
fn generated_keys_key(set: ValidatorSet, key_pair: (&[u8], &[u8])) -> Vec<u8> { fn generated_keys_key(set: ValidatorSet, key_pair: (&[u8; 32], &[u8])) -> Vec<u8> {
Self::key_gen_key(b"generated_keys", bincode::serialize(&(set, key_pair)).unwrap()) Self::key_gen_key(b"generated_keys", (set, key_pair).encode())
} }
fn save_keys( fn save_keys(
txn: &mut D::Transaction<'_>, txn: &mut D::Transaction<'_>,
@ -78,10 +79,7 @@ impl<N: Network, D: Db> KeyGenDb<N, D> {
txn.put( txn.put(
Self::generated_keys_key( Self::generated_keys_key(
id.set, id.set,
( (&substrate_keys.group_key().to_bytes(), network_keys.group_key().to_bytes().as_ref()),
substrate_keys.group_key().to_bytes().as_ref(),
network_keys.group_key().to_bytes().as_ref(),
),
), ),
keys, keys,
); );
@ -107,10 +105,8 @@ impl<N: Network, D: Db> KeyGenDb<N, D> {
set: ValidatorSet, set: ValidatorSet,
key_pair: KeyPair, key_pair: KeyPair,
) -> (ThresholdKeys<Ristretto>, ThresholdKeys<N::Curve>) { ) -> (ThresholdKeys<Ristretto>, ThresholdKeys<N::Curve>) {
let (keys_vec, keys) = Self::read_keys( let (keys_vec, keys) =
txn, Self::read_keys(txn, &Self::generated_keys_key(set, (&key_pair.0 .0, key_pair.1.as_ref())));
&Self::generated_keys_key(set, (key_pair.0.as_ref(), key_pair.1.as_ref())),
);
assert_eq!(key_pair.0 .0, keys.0.group_key().to_bytes()); assert_eq!(key_pair.0 .0, keys.0.group_key().to_bytes());
assert_eq!( assert_eq!(
{ {

View file

@ -11,6 +11,7 @@ use frost::{
use log::{info, debug, warn, error}; use log::{info, debug, warn, error};
use scale::Encode;
use messages::sign::*; use messages::sign::*;
use crate::{ use crate::{
Get, DbTxn, Db, Get, DbTxn, Db,
@ -74,7 +75,7 @@ impl<N: Network, D: Db> SignerDb<N, D> {
} }
fn attempt_key(id: &SignId) -> Vec<u8> { 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) { fn attempt(txn: &mut D::Transaction<'_>, id: &SignId) {
txn.put(Self::attempt_key(id), []); txn.put(Self::attempt_key(id), []);

View file

@ -3,9 +3,7 @@ use std::collections::{VecDeque, HashMap};
use rand_core::OsRng; use rand_core::OsRng;
use scale::Encode;
use transcript::{Transcript, RecommendedTranscript}; use transcript::{Transcript, RecommendedTranscript};
use frost::{ use frost::{
curve::Ristretto, curve::Ristretto,
ThresholdKeys, ThresholdKeys,
@ -18,6 +16,7 @@ use frost_schnorrkel::Schnorrkel;
use log::{info, debug, warn}; use log::{info, debug, warn};
use scale::Encode;
use serai_client::{ use serai_client::{
primitives::NetworkId, primitives::NetworkId,
in_instructions::primitives::{Batch, SignedBatch, batch_message}, in_instructions::primitives::{Batch, SignedBatch, batch_message},
@ -63,7 +62,7 @@ impl<D: Db> SubstrateSignerDb<D> {
} }
fn attempt_key(id: &SignId) -> Vec<u8> { 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) { fn attempt(txn: &mut D::Transaction<'_>, id: &SignId) {
txn.put(Self::attempt_key(id), []); txn.put(Self::attempt_key(id), []);