From 987aa5189a7761011f29afa2064431f0e57da495 Mon Sep 17 00:00:00 2001 From: Luke Parker Date: Sun, 16 Oct 2022 10:06:27 -0400 Subject: [PATCH] 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. --- Cargo.lock | 1 + substrate/tendermint/Cargo.toml | 2 ++ substrate/tendermint/src/ext.rs | 22 +++++++++++++++------- substrate/tendermint/src/lib.rs | 10 ++++++---- substrate/tendermint/tests/ext.rs | 4 +++- 5 files changed, 27 insertions(+), 12 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index ac7fd188..08e23cb5 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -8870,6 +8870,7 @@ version = "0.1.0" dependencies = [ "async-recursion", "async-trait", + "parity-scale-codec", "tokio", ] diff --git a/substrate/tendermint/Cargo.toml b/substrate/tendermint/Cargo.toml index d626357a..6b59433a 100644 --- a/substrate/tendermint/Cargo.toml +++ b/substrate/tendermint/Cargo.toml @@ -8,6 +8,8 @@ authors = ["Luke Parker "] 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"] } diff --git a/substrate/tendermint/src/ext.rs b/substrate/tendermint/src/ext.rs index 7e0f9ec3..407baf61 100644 --- a/substrate/tendermint/src/ext.rs +++ b/substrate/tendermint/src/ext.rs @@ -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 ValidatorId for V {} +pub trait ValidatorId: + Send + Sync + Clone + Copy + PartialEq + Eq + Hash + Debug + Encode + Decode +{ +} +impl 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; } diff --git a/substrate/tendermint/src/lib.rs b/substrate/tendermint/src/lib.rs index f0abb0dd..7b7666ec 100644 --- a/substrate/tendermint/src/lib.rs +++ b/substrate/tendermint/src/lib.rs @@ -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 { Proposal(Option, B), Prevote(Option), @@ -42,7 +44,7 @@ impl Data { } } -#[derive(Clone, PartialEq, Debug)] +#[derive(Clone, PartialEq, Debug, Encode, Decode)] pub struct Message { sender: V, @@ -52,7 +54,7 @@ pub struct Message { data: Data, } -#[derive(Clone, Copy, PartialEq, Eq, Debug)] +#[derive(Clone, Copy, PartialEq, Eq, Debug, Encode, Decode)] pub enum TendermintError { Malicious(V), Temporal, diff --git a/substrate/tendermint/tests/ext.rs b/substrate/tendermint/tests/ext.rs index 5846b8fc..7c15f57b 100644 --- a/substrate/tendermint/tests/ext.rs +++ b/substrate/tendermint/tests/ext.rs @@ -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>,