use crate::hash::CryptoHash;
use crate::merkle::MerklePath;
use crate::sharding::{EncodedShardChunk, ShardChunk, ShardChunkHeader};
use crate::types::AccountId;
use crate::validator_signer::ValidatorSigner;
use borsh::{BorshDeserialize, BorshSerialize};
use unc_crypto::Signature;
pub type TrieValue = std::sync::Arc<[u8]>;
#[derive(BorshSerialize, BorshDeserialize, serde::Serialize, Debug, Clone, Eq, PartialEq)]
pub enum PartialState {
TrieValues(Vec<TrieValue>),
}
impl Default for PartialState {
fn default() -> Self {
PartialState::TrieValues(vec![])
}
}
impl PartialState {
pub fn len(&self) -> usize {
let Self::TrieValues(values) = self;
values.len()
}
}
#[derive(BorshSerialize, BorshDeserialize, PartialEq, Eq, Clone, Debug)]
pub struct BlockDoubleSign {
pub left_block_header: Vec<u8>,
pub right_block_header: Vec<u8>,
}
impl std::fmt::Display for BlockDoubleSign {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> Result<(), std::fmt::Error> {
write!(f, "{:?}", self)
}
}
#[derive(BorshSerialize, BorshDeserialize, PartialEq, Eq, Clone, Debug)]
pub struct ChunkProofs {
pub block_header: Vec<u8>,
pub merkle_proof: MerklePath,
pub chunk: Box<MaybeEncodedShardChunk>,
}
#[allow(clippy::large_enum_variant)] #[derive(BorshSerialize, BorshDeserialize, PartialEq, Eq, Clone, Debug)]
pub enum MaybeEncodedShardChunk {
Encoded(EncodedShardChunk),
Decoded(ShardChunk),
}
#[derive(BorshSerialize, BorshDeserialize, PartialEq, Eq, Clone, Debug)]
pub struct ChunkState {
pub prev_block_header: Vec<u8>,
pub block_header: Vec<u8>,
pub prev_merkle_proof: MerklePath,
pub prev_chunk: ShardChunk,
pub merkle_proof: MerklePath,
pub chunk_header: ShardChunkHeader,
pub partial_state: PartialState,
}
#[derive(BorshSerialize, BorshDeserialize, PartialEq, Eq, Clone, Debug)]
#[allow(clippy::large_enum_variant)]
pub enum ChallengeBody {
BlockDoubleSign(BlockDoubleSign),
ChunkProofs(ChunkProofs),
ChunkState(ChunkState),
}
#[derive(BorshSerialize, BorshDeserialize, PartialEq, Eq, Clone, Debug)]
#[borsh(init=init)]
pub struct Challenge {
pub body: ChallengeBody,
pub account_id: AccountId,
pub signature: Signature,
#[borsh(skip)]
pub hash: CryptoHash,
}
impl Challenge {
pub fn init(&mut self) {
self.hash = CryptoHash::hash_borsh(&self.body);
}
pub fn produce(body: ChallengeBody, signer: &dyn ValidatorSigner) -> Self {
let (hash, signature) = signer.sign_challenge(&body);
Self { body, account_id: signer.validator_id().clone(), signature, hash }
}
}
pub type Challenges = Vec<Challenge>;
#[derive(
BorshSerialize,
BorshDeserialize,
PartialEq,
Eq,
Clone,
Debug,
serde::Serialize,
serde::Deserialize,
)]
pub struct SlashedValidator {
pub account_id: AccountId,
pub is_double_sign: bool,
}
impl SlashedValidator {
pub fn new(account_id: AccountId, is_double_sign: bool) -> Self {
SlashedValidator { account_id, is_double_sign }
}
}
pub type ChallengesResult = Vec<SlashedValidator>;