use bitcoin::hashes::{sha256, Hash};
use parity_scale_codec::{Decode, Encode};
use crate::encoding::{Decodable, Encodable};
use crate::epoch::ConsensusItem;
use crate::PeerId;
#[derive(Clone, Debug, PartialEq, Eq, Encodable, Decodable)]
pub struct AcceptedItem {
pub item: ConsensusItem,
pub peer: PeerId,
}
#[derive(Clone, Debug, PartialEq, Eq, Encodable, Decodable)]
pub struct SessionOutcome {
pub items: Vec<AcceptedItem>,
}
impl SessionOutcome {
pub fn header(&self, index: u64) -> [u8; 40] {
let mut header = [0; 40];
header[..8].copy_from_slice(&index.to_be_bytes());
let leaf_hashes = self
.items
.iter()
.map(Encodable::consensus_hash::<sha256::Hash>);
if let Some(root) = bitcoin::merkle_tree::calculate_root(leaf_hashes) {
header[8..].copy_from_slice(&root.to_byte_array());
}
header
}
}
#[derive(Clone, Debug, Encodable, Decodable, Encode, Decode, PartialEq, Eq, Hash)]
pub struct SchnorrSignature(pub [u8; 64]);
#[derive(Clone, Debug, Encodable, Decodable, Eq, PartialEq)]
pub struct SignedSessionOutcome {
pub session_outcome: SessionOutcome,
pub signatures: std::collections::BTreeMap<PeerId, SchnorrSignature>,
}
#[derive(Debug, Clone, Eq, PartialEq, Encodable, Decodable)]
pub enum SessionStatus {
Initial,
Pending(Vec<AcceptedItem>),
Complete(SessionOutcome),
}