Replace bincode with borsh (#452)

* Add SignalsConfig to chain_spec

* Correct multiexp feature flagging for rand_core std

* Remove bincode for borsh

Replaces a non-canonical encoding with a canonical encoding which additionally
should be faster.

Also fixes an issue where we used bincode in transcripts where it cannot be
trusted.

This ended up fixing a myriad of other bugs observed, unfortunately.
Accordingly, it either has to be merged or the bug fixes from it must be ported
to a new PR.

* Make serde optional, minimize usage

* Make borsh an optional dependency of substrate/ crates

* Remove unused dependencies

* Use [u8; 64] where possible in the processor messages

* Correct borsh feature flagging
This commit is contained in:
Luke Parker
2023-11-25 04:01:11 -05:00
committed by GitHub
parent 6b2876351e
commit b296be8515
52 changed files with 468 additions and 309 deletions

View File

@@ -20,7 +20,7 @@ serde = { version = "1", default-features = false, features = ["std", "derive"]
# Encoders
hex = { version = "0.4", default-features = false, features = ["std"] }
bincode = { version = "1", default-features = false }
borsh = { version = "1", default-features = false, features = ["std", "derive", "de_strict_order"] }
serde_json = { version = "1", default-features = false, features = ["std"] }
# Libs
@@ -43,7 +43,7 @@ serai-db = { path = "../common/db", optional = true }
serai-env = { path = "../common/env" }
serai-primitives = { path = "../substrate/primitives" }
serai-primitives = { path = "../substrate/primitives", features = ["borsh", "serde"] }
jsonrpsee = { version = "0.16", default-features = false, features = ["server"], optional = true }
simple-request = { path = "../common/request", default-features = false }

View File

@@ -76,7 +76,7 @@ mod binaries {
[&[u8::try_from(domain.len()).unwrap()], domain, key.as_ref()].concat()
}
fn intent_key(from: Service, to: Service, intent: &[u8]) -> Vec<u8> {
key(b"intent_seen", bincode::serialize(&(from, to, intent)).unwrap())
key(b"intent_seen", borsh::to_vec(&(from, to, intent)).unwrap())
}
let mut db = db.write().unwrap();
let mut txn = db.txn();

View File

@@ -1,11 +1,14 @@
use transcript::{Transcript, RecommendedTranscript};
use ciphersuite::{group::GroupEncoding, Ciphersuite, Ristretto};
use borsh::{BorshSerialize, BorshDeserialize};
use serde::{Serialize, Deserialize};
use serai_primitives::NetworkId;
#[derive(Clone, Copy, PartialEq, Eq, Hash, Debug, Serialize, Deserialize)]
#[derive(
Clone, Copy, PartialEq, Eq, Hash, Debug, BorshSerialize, BorshDeserialize, Serialize, Deserialize,
)]
pub enum Service {
Processor(NetworkId),
Coordinator,
@@ -36,9 +39,9 @@ pub fn message_challenge(
) -> <Ristretto as Ciphersuite>::F {
let mut transcript = RecommendedTranscript::new(b"Serai Message Queue v0.1 Message");
transcript.domain_separate(b"metadata");
transcript.append_message(b"from", bincode::serialize(&from).unwrap());
transcript.append_message(b"from", borsh::to_vec(&from).unwrap());
transcript.append_message(b"from_key", from_key.to_bytes());
transcript.append_message(b"to", bincode::serialize(&to).unwrap());
transcript.append_message(b"to", borsh::to_vec(&to).unwrap());
transcript.append_message(b"intent", intent);
transcript.domain_separate(b"message");
transcript.append_message(b"msg", msg);
@@ -56,9 +59,9 @@ pub fn ack_challenge(
) -> <Ristretto as Ciphersuite>::F {
let mut transcript = RecommendedTranscript::new(b"Serai Message Queue v0.1 Ackowledgement");
transcript.domain_separate(b"metadata");
transcript.append_message(b"to", bincode::serialize(&to).unwrap());
transcript.append_message(b"to", borsh::to_vec(&to).unwrap());
transcript.append_message(b"to_key", to_key.to_bytes());
transcript.append_message(b"from", bincode::serialize(&from).unwrap());
transcript.append_message(b"from", borsh::to_vec(&from).unwrap());
transcript.domain_separate(b"message");
transcript.append_message(b"id", id.to_le_bytes());
transcript.domain_separate(b"signature");

View File

@@ -10,7 +10,7 @@ impl<D: Db> Queue<D> {
}
fn message_count_key(&self) -> Vec<u8> {
Self::key(b"message_count", bincode::serialize(&(self.1, self.2)).unwrap())
Self::key(b"message_count", borsh::to_vec(&(self.1, self.2)).unwrap())
}
pub(crate) fn message_count(&self) -> u64 {
self
@@ -21,7 +21,7 @@ impl<D: Db> Queue<D> {
}
fn last_acknowledged_key(&self) -> Vec<u8> {
Self::key(b"last_acknowledged", bincode::serialize(&(self.1, self.2)).unwrap())
Self::key(b"last_acknowledged", borsh::to_vec(&(self.1, self.2)).unwrap())
}
pub(crate) fn last_acknowledged(&self) -> Option<u64> {
self
@@ -31,7 +31,7 @@ impl<D: Db> Queue<D> {
}
fn message_key(&self, id: u64) -> Vec<u8> {
Self::key(b"message", bincode::serialize(&(self.1, self.2, id)).unwrap())
Self::key(b"message", borsh::to_vec(&(self.1, self.2, id)).unwrap())
}
// TODO: This is fine as-used, yet gets from the DB while having a txn. It should get from the
// txn