Implement serialization via parity's scale codec

Ideally, this would be generic. Unfortunately, the generic API serde 
doesn't natively support borsh, nor SCALE, and while there is a serde 
SCALE crate, it's old. While it may be complete, it's not worth working 
with.

While we could still grab bincode, and a variety of other formats, it 
wasn't worth it to go custom and for Serai, we'll be using SCALE almost 
everywhere anyways.
This commit is contained in:
Luke Parker 2022-10-16 10:06:27 -04:00
parent 85962c00a9
commit 987aa5189a
No known key found for this signature in database
GPG key ID: F9F1386DB1E119B6
5 changed files with 27 additions and 12 deletions

1
Cargo.lock generated
View file

@ -8870,6 +8870,7 @@ version = "0.1.0"
dependencies = [
"async-recursion",
"async-trait",
"parity-scale-codec",
"tokio",
]

View file

@ -8,6 +8,8 @@ authors = ["Luke Parker <lukeparker5132@gmail.com>"]
edition = "2021"
[dependencies]
parity-scale-codec = { version = "3.2", features = ["derive"] }
async-recursion = "1.0"
async-trait = "0.1"
tokio = { version = "1", features = ["macros", "rt", "sync"] }

View file

@ -1,15 +1,23 @@
use core::{hash::Hash, fmt::Debug};
use std::sync::Arc;
use parity_scale_codec::{Encode, Decode};
use crate::Message;
pub trait ValidatorId: Send + Sync + Clone + Copy + PartialEq + Eq + Hash + Debug {}
impl<V: Send + Sync + Clone + Copy + PartialEq + Eq + Hash + Debug> ValidatorId for V {}
pub trait ValidatorId:
Send + Sync + Clone + Copy + PartialEq + Eq + Hash + Debug + Encode + Decode
{
}
impl<V: Send + Sync + Clone + Copy + PartialEq + Eq + Hash + Debug + Encode + Decode> ValidatorId
for V
{
}
// Type aliases which are distinct according to the type system
#[derive(Clone, Copy, PartialEq, Eq, Hash, Debug)]
#[derive(Clone, Copy, PartialEq, Eq, Hash, Debug, Encode, Decode)]
pub struct BlockNumber(pub u32);
#[derive(Clone, Copy, PartialEq, Eq, Hash, Debug)]
#[derive(Clone, Copy, PartialEq, Eq, Hash, Debug, Encode, Decode)]
pub struct Round(pub u16);
pub trait SignatureScheme {
@ -40,7 +48,7 @@ pub trait Weights: Send + Sync {
fn proposer(&self, number: BlockNumber, round: Round) -> Self::ValidatorId;
}
#[derive(Clone, Copy, PartialEq, Eq, Debug)]
#[derive(Clone, Copy, PartialEq, Eq, Debug, Encode, Decode)]
pub enum BlockError {
// Invalid behavior entirely
Fatal,
@ -48,8 +56,8 @@ pub enum BlockError {
Temporal,
}
pub trait Block: Send + Sync + Clone + PartialEq + Debug {
type Id: Send + Sync + Copy + Clone + PartialEq + Debug;
pub trait Block: Send + Sync + Clone + PartialEq + Debug + Encode + Decode {
type Id: Send + Sync + Copy + Clone + PartialEq + Debug + Encode + Decode;
fn id(&self) -> Self::Id;
}

View file

@ -4,6 +4,8 @@ use std::{
collections::HashMap,
};
use parity_scale_codec::{Encode, Decode};
use tokio::{
task::{JoinHandle, yield_now},
sync::{
@ -18,14 +20,14 @@ use ext::*;
mod message_log;
use message_log::MessageLog;
#[derive(Clone, Copy, PartialEq, Eq, Hash, Debug)]
#[derive(Clone, Copy, PartialEq, Eq, Hash, Debug, Encode, Decode)]
enum Step {
Propose,
Prevote,
Precommit,
}
#[derive(Clone, PartialEq, Debug)]
#[derive(Clone, PartialEq, Debug, Encode, Decode)]
enum Data<B: Block> {
Proposal(Option<Round>, B),
Prevote(Option<B::Id>),
@ -42,7 +44,7 @@ impl<B: Block> Data<B> {
}
}
#[derive(Clone, PartialEq, Debug)]
#[derive(Clone, PartialEq, Debug, Encode, Decode)]
pub struct Message<V: ValidatorId, B: Block> {
sender: V,
@ -52,7 +54,7 @@ pub struct Message<V: ValidatorId, B: Block> {
data: Data<B>,
}
#[derive(Clone, Copy, PartialEq, Eq, Debug)]
#[derive(Clone, Copy, PartialEq, Eq, Debug, Encode, Decode)]
pub enum TendermintError<V: ValidatorId> {
Malicious(V),
Temporal,

View file

@ -1,5 +1,7 @@
use std::sync::Arc;
use parity_scale_codec::{Encode, Decode};
use tokio::sync::RwLock;
use tendermint_machine::{ext::*, Message, TendermintMachine, TendermintHandle};
@ -45,7 +47,7 @@ impl Weights for TestWeights {
}
}
#[derive(Clone, PartialEq, Debug)]
#[derive(Clone, PartialEq, Debug, Encode, Decode)]
struct TestBlock {
id: TestBlockId,
valid: Result<(), BlockError>,