Skip to content
This repository has been archived by the owner on Nov 15, 2023. It is now read-only.

Commit

Permalink
Append SignedCommitment to block justifications (#177)
Browse files Browse the repository at this point in the history
* Append SignedCommitment

* add BeefyParams

* add WorkerParams

* use warn

* versioned variant for SignedCommitment
  • Loading branch information
adoerr authored May 11, 2021
1 parent 686d0a7 commit 0b24750
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 1 deletion.
36 changes: 36 additions & 0 deletions primitives/beefy/src/commitment.rs
Original file line number Diff line number Diff line change
Expand Up @@ -95,13 +95,24 @@ impl<TBlockNumber, TPayload, TSignature> SignedCommitment<TBlockNumber, TPayload
}
}

/// A [SignedCommitment] with a version number. This variant will be appended
/// to the block justifications for the block for which the signed commitment
/// has been generated.
#[derive(Clone, Debug, PartialEq, codec::Encode, codec::Decode)]
pub enum VersionedCommitment<N, P, S> {
#[codec(index = 1)]
/// Current active version
V1(SignedCommitment<N, P, S>),
}

#[cfg(test)]
mod tests {
use super::*;
use codec::Decode;

type TestCommitment = Commitment<u128, String>;
type TestSignedCommitment = SignedCommitment<u128, String, Vec<u8>>;
type TestVersionedCommitment = VersionedCommitment<u128, String, Vec<u8>>;

#[test]
fn commitment_encode_decode() {
Expand Down Expand Up @@ -195,4 +206,29 @@ mod tests {
assert!(c < d);
assert!(b < d);
}

#[test]
fn versioned_commitment_encode_decode() {
let commitment: TestCommitment = Commitment {
payload: "Hello World!".into(),
block_number: 5,
validator_set_id: 0,
};

let signed = SignedCommitment {
commitment,
signatures: vec![None, None, Some(vec![1, 2, 3, 4]), Some(vec![5, 6, 7, 8])],
};

let versioned = TestVersionedCommitment::V1(signed.clone());

let encoded = codec::Encode::encode(&versioned);

assert_eq!(1, encoded[0]);
assert_eq!(encoded[1..], codec::Encode::encode(&signed));

let decoded = TestVersionedCommitment::decode(&mut &*encoded);

assert_eq!(decoded, Ok(versioned));
}
}
2 changes: 1 addition & 1 deletion primitives/beefy/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
mod commitment;
pub mod witness;

pub use commitment::{Commitment, SignedCommitment};
pub use commitment::{Commitment, SignedCommitment, VersionedCommitment};

use codec::{Codec, Decode, Encode};
use sp_core::H256;
Expand Down

0 comments on commit 0b24750

Please sign in to comment.