mirror of
https://github.com/serai-dex/serai.git
synced 2025-12-09 04:39:24 +00:00
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:
1
Cargo.lock
generated
1
Cargo.lock
generated
@@ -8870,6 +8870,7 @@ version = "0.1.0"
|
|||||||
dependencies = [
|
dependencies = [
|
||||||
"async-recursion",
|
"async-recursion",
|
||||||
"async-trait",
|
"async-trait",
|
||||||
|
"parity-scale-codec",
|
||||||
"tokio",
|
"tokio",
|
||||||
]
|
]
|
||||||
|
|
||||||
|
|||||||
@@ -8,6 +8,8 @@ authors = ["Luke Parker <lukeparker5132@gmail.com>"]
|
|||||||
edition = "2021"
|
edition = "2021"
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
|
parity-scale-codec = { version = "3.2", features = ["derive"] }
|
||||||
|
|
||||||
async-recursion = "1.0"
|
async-recursion = "1.0"
|
||||||
async-trait = "0.1"
|
async-trait = "0.1"
|
||||||
tokio = { version = "1", features = ["macros", "rt", "sync"] }
|
tokio = { version = "1", features = ["macros", "rt", "sync"] }
|
||||||
|
|||||||
@@ -1,15 +1,23 @@
|
|||||||
use core::{hash::Hash, fmt::Debug};
|
use core::{hash::Hash, fmt::Debug};
|
||||||
use std::sync::Arc;
|
use std::sync::Arc;
|
||||||
|
|
||||||
|
use parity_scale_codec::{Encode, Decode};
|
||||||
|
|
||||||
use crate::Message;
|
use crate::Message;
|
||||||
|
|
||||||
pub trait ValidatorId: Send + Sync + Clone + Copy + PartialEq + Eq + Hash + Debug {}
|
pub trait ValidatorId:
|
||||||
impl<V: Send + Sync + Clone + Copy + PartialEq + Eq + Hash + Debug> ValidatorId for V {}
|
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
|
// 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);
|
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 struct Round(pub u16);
|
||||||
|
|
||||||
pub trait SignatureScheme {
|
pub trait SignatureScheme {
|
||||||
@@ -40,7 +48,7 @@ pub trait Weights: Send + Sync {
|
|||||||
fn proposer(&self, number: BlockNumber, round: Round) -> Self::ValidatorId;
|
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 {
|
pub enum BlockError {
|
||||||
// Invalid behavior entirely
|
// Invalid behavior entirely
|
||||||
Fatal,
|
Fatal,
|
||||||
@@ -48,8 +56,8 @@ pub enum BlockError {
|
|||||||
Temporal,
|
Temporal,
|
||||||
}
|
}
|
||||||
|
|
||||||
pub trait Block: Send + Sync + Clone + PartialEq + Debug {
|
pub trait Block: Send + Sync + Clone + PartialEq + Debug + Encode + Decode {
|
||||||
type Id: Send + Sync + Copy + Clone + PartialEq + Debug;
|
type Id: Send + Sync + Copy + Clone + PartialEq + Debug + Encode + Decode;
|
||||||
|
|
||||||
fn id(&self) -> Self::Id;
|
fn id(&self) -> Self::Id;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -4,6 +4,8 @@ use std::{
|
|||||||
collections::HashMap,
|
collections::HashMap,
|
||||||
};
|
};
|
||||||
|
|
||||||
|
use parity_scale_codec::{Encode, Decode};
|
||||||
|
|
||||||
use tokio::{
|
use tokio::{
|
||||||
task::{JoinHandle, yield_now},
|
task::{JoinHandle, yield_now},
|
||||||
sync::{
|
sync::{
|
||||||
@@ -18,14 +20,14 @@ use ext::*;
|
|||||||
mod message_log;
|
mod message_log;
|
||||||
use message_log::MessageLog;
|
use message_log::MessageLog;
|
||||||
|
|
||||||
#[derive(Clone, Copy, PartialEq, Eq, Hash, Debug)]
|
#[derive(Clone, Copy, PartialEq, Eq, Hash, Debug, Encode, Decode)]
|
||||||
enum Step {
|
enum Step {
|
||||||
Propose,
|
Propose,
|
||||||
Prevote,
|
Prevote,
|
||||||
Precommit,
|
Precommit,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Clone, PartialEq, Debug)]
|
#[derive(Clone, PartialEq, Debug, Encode, Decode)]
|
||||||
enum Data<B: Block> {
|
enum Data<B: Block> {
|
||||||
Proposal(Option<Round>, B),
|
Proposal(Option<Round>, B),
|
||||||
Prevote(Option<B::Id>),
|
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> {
|
pub struct Message<V: ValidatorId, B: Block> {
|
||||||
sender: V,
|
sender: V,
|
||||||
|
|
||||||
@@ -52,7 +54,7 @@ pub struct Message<V: ValidatorId, B: Block> {
|
|||||||
data: Data<B>,
|
data: Data<B>,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Clone, Copy, PartialEq, Eq, Debug)]
|
#[derive(Clone, Copy, PartialEq, Eq, Debug, Encode, Decode)]
|
||||||
pub enum TendermintError<V: ValidatorId> {
|
pub enum TendermintError<V: ValidatorId> {
|
||||||
Malicious(V),
|
Malicious(V),
|
||||||
Temporal,
|
Temporal,
|
||||||
|
|||||||
@@ -1,5 +1,7 @@
|
|||||||
use std::sync::Arc;
|
use std::sync::Arc;
|
||||||
|
|
||||||
|
use parity_scale_codec::{Encode, Decode};
|
||||||
|
|
||||||
use tokio::sync::RwLock;
|
use tokio::sync::RwLock;
|
||||||
|
|
||||||
use tendermint_machine::{ext::*, Message, TendermintMachine, TendermintHandle};
|
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 {
|
struct TestBlock {
|
||||||
id: TestBlockId,
|
id: TestBlockId,
|
||||||
valid: Result<(), BlockError>,
|
valid: Result<(), BlockError>,
|
||||||
|
|||||||
Reference in New Issue
Block a user