use std::collections::{
btree_map::{Entry as Bentry, Keys as Bkeys},
BTreeMap, BTreeSet,
};
use codec::{Decode, Encode};
use sp_application_crypto::AppCrypto;
use sp_keystore::{Error as KeystoreError, KeystorePtr};
use polkadot_primitives::{
CandidateHash, CandidateReceipt, CompactStatement, DisputeStatement, EncodeAs,
InvalidDisputeStatementKind, SessionIndex, SigningContext, UncheckedSigned,
ValidDisputeStatementKind, ValidatorId, ValidatorIndex, ValidatorSignature,
};
mod message;
pub use message::{DisputeMessage, Error as DisputeMessageCheckError, UncheckedDisputeMessage};
mod status;
pub use status::{dispute_is_inactive, DisputeStatus, Timestamp, ACTIVE_DURATION_SECS};
#[derive(Debug, Clone)]
pub struct SignedDisputeStatement {
dispute_statement: DisputeStatement,
candidate_hash: CandidateHash,
validator_public: ValidatorId,
validator_signature: ValidatorSignature,
session_index: SessionIndex,
}
#[derive(Debug)]
pub enum SignedDisputeStatementError {
KeyStoreError(KeystoreError),
PayloadError,
}
#[derive(Debug, Clone)]
pub struct CandidateVotes {
pub candidate_receipt: CandidateReceipt,
pub valid: ValidCandidateVotes,
pub invalid: BTreeMap<ValidatorIndex, (InvalidDisputeStatementKind, ValidatorSignature)>,
}
pub type ValidVoteData = (ValidatorIndex, (ValidDisputeStatementKind, ValidatorSignature));
pub type InvalidVoteData = (ValidatorIndex, (InvalidDisputeStatementKind, ValidatorSignature));
impl CandidateVotes {
pub fn voted_indices(&self) -> BTreeSet<ValidatorIndex> {
let mut keys: BTreeSet<_> = self.valid.keys().cloned().collect();
keys.extend(self.invalid.keys().cloned());
keys
}
}
#[derive(Debug, Clone)]
pub struct ValidCandidateVotes {
votes: BTreeMap<ValidatorIndex, (ValidDisputeStatementKind, ValidatorSignature)>,
}
impl ValidCandidateVotes {
pub fn new() -> Self {
Self { votes: BTreeMap::new() }
}
pub fn insert_vote(
&mut self,
validator_index: ValidatorIndex,
kind: ValidDisputeStatementKind,
sig: ValidatorSignature,
) -> bool {
match self.votes.entry(validator_index) {
Bentry::Vacant(vacant) => {
vacant.insert((kind, sig));
true
},
Bentry::Occupied(mut occupied) => match occupied.get().0 {
ValidDisputeStatementKind::BackingValid(_) |
ValidDisputeStatementKind::BackingSeconded(_) => false,
ValidDisputeStatementKind::Explicit |
ValidDisputeStatementKind::ApprovalChecking |
ValidDisputeStatementKind::ApprovalCheckingMultipleCandidates(_) => {
occupied.insert((kind.clone(), sig));
kind != occupied.get().0
},
},
}
}
pub fn retain<F>(&mut self, f: F)
where
F: FnMut(&ValidatorIndex, &mut (ValidDisputeStatementKind, ValidatorSignature)) -> bool,
{
self.votes.retain(f)
}
pub fn keys(
&self,
) -> Bkeys<'_, ValidatorIndex, (ValidDisputeStatementKind, ValidatorSignature)> {
self.votes.keys()
}
pub fn raw(
&self,
) -> &BTreeMap<ValidatorIndex, (ValidDisputeStatementKind, ValidatorSignature)> {
&self.votes
}
}
impl FromIterator<(ValidatorIndex, (ValidDisputeStatementKind, ValidatorSignature))>
for ValidCandidateVotes
{
fn from_iter<T>(iter: T) -> Self
where
T: IntoIterator<Item = (ValidatorIndex, (ValidDisputeStatementKind, ValidatorSignature))>,
{
Self { votes: BTreeMap::from_iter(iter) }
}
}
impl From<ValidCandidateVotes>
for BTreeMap<ValidatorIndex, (ValidDisputeStatementKind, ValidatorSignature)>
{
fn from(wrapped: ValidCandidateVotes) -> Self {
wrapped.votes
}
}
impl IntoIterator for ValidCandidateVotes {
type Item = (ValidatorIndex, (ValidDisputeStatementKind, ValidatorSignature));
type IntoIter = <BTreeMap<ValidatorIndex, (ValidDisputeStatementKind, ValidatorSignature)> as IntoIterator>::IntoIter;
fn into_iter(self) -> Self::IntoIter {
self.votes.into_iter()
}
}
impl SignedDisputeStatement {
pub fn new_unchecked_from_trusted_source(
dispute_statement: DisputeStatement,
candidate_hash: CandidateHash,
session_index: SessionIndex,
validator_public: ValidatorId,
validator_signature: ValidatorSignature,
) -> Self {
SignedDisputeStatement {
dispute_statement,
candidate_hash,
validator_public,
validator_signature,
session_index,
}
}
pub fn new_checked(
dispute_statement: DisputeStatement,
candidate_hash: CandidateHash,
session_index: SessionIndex,
validator_public: ValidatorId,
validator_signature: ValidatorSignature,
) -> Result<Self, ()> {
dispute_statement
.check_signature(&validator_public, candidate_hash, session_index, &validator_signature)
.map(|_| SignedDisputeStatement {
dispute_statement,
candidate_hash,
validator_public,
validator_signature,
session_index,
})
}
pub fn sign_explicit(
keystore: &KeystorePtr,
valid: bool,
candidate_hash: CandidateHash,
session_index: SessionIndex,
validator_public: ValidatorId,
) -> Result<Option<Self>, SignedDisputeStatementError> {
let dispute_statement = if valid {
DisputeStatement::Valid(ValidDisputeStatementKind::Explicit)
} else {
DisputeStatement::Invalid(InvalidDisputeStatementKind::Explicit)
};
let data = dispute_statement
.payload_data(candidate_hash, session_index)
.map_err(|_| SignedDisputeStatementError::PayloadError)?;
let signature = keystore
.sr25519_sign(ValidatorId::ID, validator_public.as_ref(), &data)
.map_err(SignedDisputeStatementError::KeyStoreError)?
.map(|sig| Self {
dispute_statement,
candidate_hash,
validator_public,
validator_signature: sig.into(),
session_index,
});
Ok(signature)
}
pub fn statement(&self) -> &DisputeStatement {
&self.dispute_statement
}
pub fn candidate_hash(&self) -> &CandidateHash {
&self.candidate_hash
}
pub fn validator_public(&self) -> &ValidatorId {
&self.validator_public
}
pub fn validator_signature(&self) -> &ValidatorSignature {
&self.validator_signature
}
pub fn into_validator_signature(self) -> ValidatorSignature {
self.validator_signature
}
pub fn session_index(&self) -> SessionIndex {
self.session_index
}
pub fn from_backing_statement<T>(
backing_statement: &UncheckedSigned<T, CompactStatement>,
signing_context: SigningContext,
validator_public: ValidatorId,
) -> Result<Self, ()>
where
for<'a> &'a T: Into<CompactStatement>,
T: EncodeAs<CompactStatement>,
{
let (statement_kind, candidate_hash) = match backing_statement.unchecked_payload().into() {
CompactStatement::Seconded(candidate_hash) => (
ValidDisputeStatementKind::BackingSeconded(signing_context.parent_hash),
candidate_hash,
),
CompactStatement::Valid(candidate_hash) => (
ValidDisputeStatementKind::BackingValid(signing_context.parent_hash),
candidate_hash,
),
};
let dispute_statement = DisputeStatement::Valid(statement_kind);
Self::new_checked(
dispute_statement,
candidate_hash,
signing_context.session_index,
validator_public,
backing_statement.unchecked_signature().clone(),
)
}
}
#[derive(Clone, Encode, Decode, Debug)]
pub struct InvalidDisputeVote {
pub validator_index: ValidatorIndex,
pub signature: ValidatorSignature,
pub kind: InvalidDisputeStatementKind,
}
#[derive(Clone, Encode, Decode, Debug)]
pub struct ValidDisputeVote {
pub validator_index: ValidatorIndex,
pub signature: ValidatorSignature,
pub kind: ValidDisputeStatementKind,
}