jam-std-common 0.1.29

Common datatypes and utilities for the JAM nodes and tooling
Documentation
use codec::{Decode, Encode, MaxEncodedLen};
use core::fmt;
use jam_types::{HeaderHash, Slot, StateRootHash};

/// Target for GRANDPA votes and hence what is finalized. Header hash together with the posterior
/// state root hash.
#[derive(
	Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Encode, Decode, MaxEncodedLen,
)]
pub struct GrandpaTarget {
	/// Hash of the block header.
	pub hash: HeaderHash,
	/// Posterior state root hash of the block.
	pub state_root: StateRootHash,
}

impl GrandpaTarget {
	pub fn new(hash: HeaderHash, state_root: StateRootHash) -> Self {
		GrandpaTarget { hash, state_root }
	}
}

impl fmt::Display for GrandpaTarget {
	fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
		write!(f, "{} (state root {})", self.hash, self.state_root)
	}
}

/// Identifier of a GRANDPA set of authorities. Although it is similar to EpochIndex it is not
/// the same. GrandpaSetId starts at 0 at genesis and increments for every set change. This will
/// normally happen at epoch changes but is not quaranteed to. EpochIndex is a function of time
/// since JAM common era, hence they might be skipped (eg, after genesis or if the network is down)
/// but GRANDPA sets are never skipped. They are always incremental from the previous set.
pub type GrandpaSetId = u32;

/// The round indicator.
pub type GrandpaRoundNumber = u64;

#[derive(Debug, Clone, PartialEq, Eq, Encode, Decode, MaxEncodedLen)]
pub enum GrandpaMessage {
	#[codec(index = 0)]
	Prevote(GrandpaTarget, Slot),
	#[codec(index = 1)]
	Precommit(GrandpaTarget, Slot),
	#[codec(index = 2)]
	PrimaryPropose(GrandpaTarget, Slot),
}