// This file is part of Substrate.

// Copyright (C) Parity Technologies (UK) Ltd.
// SPDX-License-Identifier: Apache-2.0

// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// 	http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

//! Sassafras digests structures and helpers.

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

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

#[cfg(not(feature = "std"))]
use alloc::vec::Vec;
use sp_runtime::{DigestItem, RuntimeDebug};

/// Epoch slot claim digest entry.
///
/// This is mandatory for each block.
#[derive(Clone, RuntimeDebug, 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, RuntimeDebug)]
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())
	}
}
