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

Commit

Permalink
More ergonomic digest items
Browse files Browse the repository at this point in the history
  • Loading branch information
davxy committed Aug 24, 2023
1 parent 97dd71f commit bc4a697
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 32 deletions.
55 changes: 27 additions & 28 deletions primitives/consensus/sassafras/src/digests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,11 @@ use scale_info::TypeInfo;
use sp_runtime::{DigestItem, RuntimeDebug};
use sp_std::vec::Vec;

/// Sassafras slot assignment pre-digest.
/// Epoch slot claim digest entry.
///
/// This is mandatory for each block.
#[derive(Clone, RuntimeDebug, Encode, Decode, MaxEncodedLen, TypeInfo)]
pub struct PreDigest {
pub struct SlotClaim {
/// Authority index that claimed the slot.
pub authority_idx: AuthorityIndex,
/// Corresponding slot number.
Expand All @@ -43,18 +45,22 @@ pub struct PreDigest {

/// Information about the next epoch.
///
/// This is broadcast in the first block of each epoch.
/// This is mandatory in the first block of each epoch.
#[derive(Clone, PartialEq, Eq, Encode, Decode, RuntimeDebug)]
pub struct NextEpochDescriptor {
/// Authorities list.
pub authorities: Vec<AuthorityId>,
/// Epoch randomness.
pub randomness: Randomness,
/// Configurable parameters. If not present previous epoch parameters are used.
/// Epoch configurable parameters.
///
/// If not present previous epoch parameters are used.
pub config: Option<EpochConfiguration>,
}

/// Consensus log item.
/// Runtime digest entries.
///
/// Entries which may be generated by on-chain code.
#[derive(Decode, Encode, Clone, PartialEq, Eq)]
pub enum ConsensusLog {
/// Provides information about the next epoch parameters.
Expand All @@ -65,35 +71,28 @@ pub enum ConsensusLog {
OnDisabled(AuthorityIndex),
}

/// A digest item which is usable by Sassafras.
pub trait CompatibleDigestItem {
/// Construct a digest item which contains a `PreDigest`.
fn sassafras_pre_digest(seal: PreDigest) -> Self;

/// If this item is a `PreDigest`, return it.
fn as_sassafras_pre_digest(&self) -> Option<PreDigest>;

/// Construct a digest item which contains an `AuthoritySignature`.
fn sassafras_seal(signature: AuthoritySignature) -> Self;

/// If this item is an `AuthoritySignature`, return it.
fn as_sassafras_seal(&self) -> Option<AuthoritySignature>;
impl TryFrom<&DigestItem> for SlotClaim {
type Error = ();
fn try_from(item: &DigestItem) -> Result<Self, Self::Error> {
item.pre_runtime_try_to(&SASSAFRAS_ENGINE_ID).ok_or(())
}
}

impl CompatibleDigestItem for DigestItem {
fn sassafras_pre_digest(digest: PreDigest) -> Self {
DigestItem::PreRuntime(SASSAFRAS_ENGINE_ID, digest.encode())
impl From<&SlotClaim> for DigestItem {
fn from(claim: &SlotClaim) -> Self {
DigestItem::PreRuntime(SASSAFRAS_ENGINE_ID, claim.encode())
}
}

fn as_sassafras_pre_digest(&self) -> Option<PreDigest> {
self.pre_runtime_try_to(&SASSAFRAS_ENGINE_ID)
impl TryFrom<&DigestItem> for AuthoritySignature {
type Error = ();
fn try_from(item: &DigestItem) -> Result<Self, Self::Error> {
item.seal_try_to(&SASSAFRAS_ENGINE_ID).ok_or(())
}
}

fn sassafras_seal(signature: AuthoritySignature) -> Self {
impl From<&AuthoritySignature> for DigestItem {
fn from(signature: &AuthoritySignature) -> Self {
DigestItem::Seal(SASSAFRAS_ENGINE_ID, signature.encode())
}

fn as_sassafras_seal(&self) -> Option<AuthoritySignature> {
self.seal_try_to(&SASSAFRAS_ENGINE_ID)
}
}
12 changes: 8 additions & 4 deletions primitives/consensus/sassafras/src/vrf.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@

//! Utilities related to VRF input, output and signatures.

use crate::{Randomness, TicketBody, TicketId, SASSAFRAS_ENGINE_ID};
use crate::{Randomness, TicketBody, TicketId};
use scale_codec::Encode;
use sp_consensus_slots::Slot;
use sp_std::vec::Vec;
Expand Down Expand Up @@ -52,7 +52,11 @@ pub fn slot_claim_input(randomness: &Randomness, slot: Slot, epoch: u64) -> VrfI
/// Signing-data to claim slot ownership during block production.
pub fn slot_claim_sign_data(randomness: &Randomness, slot: Slot, epoch: u64) -> VrfSignData {
let vrf_input = slot_claim_input(randomness, slot, epoch);
VrfSignData::new_unchecked(&SASSAFRAS_ENGINE_ID, Some("slot-claim-transcript"), Some(vrf_input))
VrfSignData::new_unchecked(
b"sassafras-slot-claim-transcript-v1.0",
Option::<&[u8]>::None,
Some(vrf_input),
)
}

/// VRF input to generate the ticket id.
Expand All @@ -74,8 +78,8 @@ pub fn revealed_key_input(randomness: &Randomness, attempt: u32, epoch: u64) ->
/// Data to be signed via ring-vrf.
pub fn ticket_body_sign_data(ticket_body: &TicketBody, ticket_id_input: VrfInput) -> VrfSignData {
VrfSignData::new_unchecked(
&SASSAFRAS_ENGINE_ID,
&[b"ticket-body-transcript", ticket_body.encode().as_slice()],
b"sassafras-ticket-body-transcript-v1.0",
Some(ticket_body.encode().as_slice()),
Some(ticket_id_input),
)
}
Expand Down

0 comments on commit bc4a697

Please sign in to comment.