Skip to content

Commit 5b53976

Browse files
authored
Merge pull request #460 from EspressoSystems/tw/cbor
Use CBOR for certified block.
2 parents eb60170 + c7e4cf3 commit 5b53976

File tree

23 files changed

+335
-96
lines changed

23 files changed

+335
-96
lines changed

Cargo.lock

Lines changed: 38 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
[workspace]
22
members = [
3+
"adapters",
34
"cliquenet",
45
"metrics",
56
"multisig",
@@ -67,6 +68,7 @@ ethereum_ssz = "0.9.0"
6768
futures = { version = "0.3", default-features = false, features = ["alloc"] }
6869
generic-array = { version = "0.14.7", features = ["serde", "zeroize"] }
6970
http = "1.3.1"
71+
minicbor = { version = "2.1.1", features = ["full"] }
7072
nohash-hasher = "0.2"
7173
parking_lot = "0.12.3"
7274
portpicker = "0.1.1"

adapters/Cargo.toml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
[package]
2+
name = "adapters"
3+
version.workspace = true
4+
edition.workspace = true
5+
rust-version.workspace = true
6+
7+
[dependencies]
8+
bytes = { workspace = true }
9+
committable = { workspace = true }
10+
minicbor = { workspace = true }

adapters/src/bytes.rs

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
use std::convert::Infallible;
2+
3+
use bytes::{Bytes, BytesMut};
4+
use minicbor::decode::{Decoder, Error as DecodeError};
5+
use minicbor::encode::{Encoder, Error as EncodeError, Write};
6+
7+
pub fn encode<C, W>(b: &Bytes, e: &mut Encoder<W>, _: &mut C) -> Result<(), EncodeError<W::Error>>
8+
where
9+
W: Write,
10+
{
11+
e.bytes(b)?.ok()
12+
}
13+
14+
pub fn decode<'b, C>(d: &mut Decoder<'b>, _: &mut C) -> Result<Bytes, DecodeError> {
15+
Ok(Bytes::copy_from_slice(d.bytes()?))
16+
}
17+
18+
/// `BytesWrite` can be used to encode directly into `BytesMut`.
19+
#[derive(Default)]
20+
pub struct BytesWriter(BytesMut);
21+
22+
impl Write for BytesWriter {
23+
type Error = Infallible;
24+
25+
fn write_all(&mut self, buf: &[u8]) -> Result<(), Self::Error> {
26+
self.0.extend_from_slice(buf);
27+
Ok(())
28+
}
29+
}
30+
31+
impl From<BytesWriter> for BytesMut {
32+
fn from(value: BytesWriter) -> Self {
33+
value.0
34+
}
35+
}

adapters/src/commitment.rs

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
use committable::{Commitment, Committable};
2+
use minicbor::decode::{Decoder, Error as DecodeError};
3+
use minicbor::encode::{Encoder, Error as EncodeError, Write};
4+
5+
const LEN: usize = 32;
6+
7+
pub fn encode<D, C, W>(
8+
d: &Commitment<D>,
9+
e: &mut Encoder<W>,
10+
_: &mut C,
11+
) -> Result<(), EncodeError<W::Error>>
12+
where
13+
W: Write,
14+
D: Committable,
15+
{
16+
e.bytes(d.as_ref())?.ok()
17+
}
18+
19+
pub fn decode<'b, D, C>(d: &mut Decoder<'b>, _: &mut C) -> Result<Commitment<D>, DecodeError>
20+
where
21+
D: Committable,
22+
{
23+
let p = d.position();
24+
let a = d.bytes()?;
25+
<[u8; LEN]>::try_from(a)
26+
.map(Commitment::from_raw)
27+
.map_err(|e| DecodeError::custom(e).at(p))
28+
}

adapters/src/lib.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
//! Provide CBOR encode/decode functionality for types that do not implement `Encode`/`Decode`.
2+
3+
pub mod bytes;
4+
pub mod commitment;

multisig/Cargo.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,15 @@ rust-version.workspace = true
99
benchmark = []
1010

1111
[dependencies]
12+
adapters = { path = "../adapters" }
1213
bimap = { workspace = true }
1314
bincode = { workspace = true }
1415
bs58 = { workspace = true }
1516
committable = { workspace = true }
1617
constant_time_eq = { workspace = true }
1718
ed25519-compact = { workspace = true }
1819
either = { workspace = true }
20+
minicbor = { workspace = true }
1921
rayon = { workspace = true }
2022
secp256k1 = { workspace = true }
2123
serde = { workspace = true }

multisig/src/cert.rs

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,23 @@ use std::{collections::BTreeMap, num::NonZeroUsize};
22

33
use committable::{Commitment, Committable, RawCommitmentBuilder};
44
use constant_time_eq::constant_time_eq;
5+
use minicbor::{Decode, Encode};
56
use serde::{Deserialize, Serialize};
67

78
use crate::{Committee, KeyId, PublicKey, Signature};
89

9-
#[derive(Debug, Clone, PartialEq, Eq, Hash, PartialOrd, Ord, Serialize, Deserialize)]
10+
#[derive(
11+
Debug, Clone, PartialEq, Eq, Hash, PartialOrd, Ord, Serialize, Deserialize, Encode, Decode,
12+
)]
13+
#[cbor(map)]
1014
pub struct Certificate<D: Committable> {
15+
#[cbor(n(0))]
1116
data: D,
17+
18+
#[cbor(n(1), with = "adapters::commitment")]
1219
commitment: Commitment<D>,
20+
21+
#[cbor(n(2))]
1322
signatures: BTreeMap<KeyId, Signature>,
1423
}
1524

multisig/src/committee.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ use std::sync::Arc;
55

66
use bimap::BiBTreeMap;
77
use committable::{Commitment, Committable, RawCommitmentBuilder};
8+
use minicbor::{Decode, Encode};
89
use serde::{Deserialize, Serialize};
910

1011
use super::{KeyId, PublicKey};
@@ -103,7 +104,10 @@ impl Committee {
103104
}
104105
}
105106

106-
#[derive(Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize, Deserialize)]
107+
#[derive(
108+
Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize, Deserialize, Encode, Decode,
109+
)]
110+
#[cbor(transparent)]
107111
#[serde(transparent)]
108112
pub struct CommitteeId(u64);
109113

multisig/src/envelope.rs

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,19 @@
11
use std::{hash::Hash, marker::PhantomData, ops::Deref};
22

33
use committable::{Commitment, Committable, RawCommitmentBuilder};
4+
use minicbor::{Decode, Encode};
45
use serde::{Deserialize, Serialize};
56

67
use crate::{Committee, Keypair, Signed};
78

89
/// Marker type to denote envelopes whose signature has not been validated.
9-
#[derive(Clone, Copy, Debug, Eq, PartialEq, PartialOrd, Ord, Hash, Serialize, Deserialize)]
10+
#[derive(
11+
Clone, Copy, Debug, Eq, PartialEq, PartialOrd, Ord, Hash, Serialize, Deserialize, Encode, Decode,
12+
)]
1013
pub enum Unchecked {}
1114

1215
/// Marker type to denote envelopes whose signature has been validated.
13-
#[derive(Clone, Copy, Debug, Eq, PartialEq, PartialOrd, Ord, Hash, Serialize)]
16+
#[derive(Clone, Copy, Debug, Eq, PartialEq, PartialOrd, Ord, Hash, Serialize, Encode)]
1417
pub enum Validated {}
1518

1619
/// An envelope contains data, its signed commitment hash and the signing key.
@@ -25,12 +28,18 @@ pub enum Validated {}
2528
/// let _: Envelope<Signature, Validated> =
2629
/// bincode::serde::decode_from_slice(&[], bincode::config::standard()).unwrap().0;
2730
/// ```
28-
#[derive(Clone, Debug, PartialEq, Eq, Hash, PartialOrd, Ord, Serialize, Deserialize)]
31+
#[derive(
32+
Clone, Debug, PartialEq, Eq, Hash, PartialOrd, Ord, Serialize, Deserialize, Encode, Decode,
33+
)]
2934
#[serde(bound(deserialize = "D: Deserialize<'de>, S: Deserialize<'de>"))]
35+
#[cbor(map)]
3036
pub struct Envelope<D: Committable, S> {
37+
#[cbor(n(0))]
3138
signed: Signed<D>,
39+
40+
#[cbor(skip)]
3241
#[serde(skip)]
33-
_marker: PhantomData<fn(S)>,
42+
_marker: PhantomData<fn(&S)>,
3443
}
3544

3645
impl<D: Committable> Envelope<D, Validated> {

0 commit comments

Comments
 (0)