subsoil 0.2.0

Soil primitives foundation crate
Documentation
// This file is part of Soil.

// Copyright (C) Soil contributors.
// Copyright (C) Parity Technologies (UK) Ltd.
// SPDX-License-Identifier: Apache-2.0 OR GPL-3.0-or-later WITH Classpath-exception-2.0

//! Sassafras digests structures and helpers.

use super::{
	ticket::TicketClaim, vrf::VrfSignature, AuthorityId, AuthorityIndex, AuthoritySignature,
	EpochConfiguration, Randomness, Slot, SASSAFRAS_ENGINE_ID,
};

use codec::{Decode, Encode, MaxEncodedLen};
use scale_info::TypeInfo;

use crate::runtime::DigestItem;
#[cfg(not(feature = "std"))]
use alloc::vec::Vec;

/// Epoch slot claim digest entry.
///
/// This is mandatory for each block.
#[derive(Clone, Debug, Encode, Decode, MaxEncodedLen, TypeInfo)]
pub struct SlotClaim {
	/// Authority index that claimed the slot.
	pub authority_idx: AuthorityIndex,
	/// Corresponding slot number.
	pub slot: Slot,
	/// Slot claim VRF signature.
	pub vrf_signature: VrfSignature,
	/// Ticket auxiliary information for claim check.
	pub ticket_claim: Option<TicketClaim>,
}

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

/// 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.
	#[codec(index = 1)]
	NextEpochData(NextEpochDescriptor),
	/// Disable the authority with given index.
	#[codec(index = 2)]
	OnDisabled(AuthorityIndex),
}

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 From<&SlotClaim> for DigestItem {
	fn from(claim: &SlotClaim) -> Self {
		DigestItem::PreRuntime(SASSAFRAS_ENGINE_ID, claim.encode())
	}
}

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(())
	}
}

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