use thiserror::Error;
use codec::{Decode, Encode};
use super::{InvalidDisputeVote, SignedDisputeStatement, ValidDisputeVote};
use polkadot_primitives::{
CandidateReceiptV2 as CandidateReceipt, DisputeStatement, SessionIndex, SessionInfo,
ValidatorIndex,
};
#[derive(Debug, Clone)]
pub struct DisputeMessage(UncheckedDisputeMessage);
#[derive(Clone, Encode, Decode, Debug)]
pub struct UncheckedDisputeMessage {
pub candidate_receipt: CandidateReceipt,
pub session_index: SessionIndex,
pub invalid_vote: InvalidDisputeVote,
pub valid_vote: ValidDisputeVote,
}
#[derive(Error, Debug)]
pub enum Error {
#[error("Candidate hashes of the two votes did not match up")]
CandidateHashMismatch,
#[error("Session indices of the two votes did not match up")]
SessionIndexMismatch,
#[error("Valid statement validator key did not match session information")]
InvalidValidKey,
#[error("Invalid statement validator key did not match session information")]
InvalidInvalidKey,
#[error("Hash of candidate receipt did not match provided hash")]
InvalidCandidateReceipt,
#[error("Valid statement has kind `invalid`")]
ValidStatementHasInvalidKind,
#[error("Invalid statement has kind `valid`")]
InvalidStatementHasValidKind,
#[error("The valid statement had an invalid validator index")]
ValidStatementInvalidValidatorIndex,
#[error("The invalid statement had an invalid validator index")]
InvalidStatementInvalidValidatorIndex,
}
impl DisputeMessage {
pub fn from_signed_statements(
valid_statement: SignedDisputeStatement,
valid_index: ValidatorIndex,
invalid_statement: SignedDisputeStatement,
invalid_index: ValidatorIndex,
candidate_receipt: CandidateReceipt,
session_info: &SessionInfo,
) -> Result<Self, Error> {
let candidate_hash = *valid_statement.candidate_hash();
if candidate_hash != *invalid_statement.candidate_hash() {
return Err(Error::CandidateHashMismatch);
}
let session_index = valid_statement.session_index();
if session_index != invalid_statement.session_index() {
return Err(Error::SessionIndexMismatch);
}
let valid_id = session_info
.validators
.get(valid_index)
.ok_or(Error::ValidStatementInvalidValidatorIndex)?;
let invalid_id = session_info
.validators
.get(invalid_index)
.ok_or(Error::InvalidStatementInvalidValidatorIndex)?;
if valid_id != valid_statement.validator_public() {
return Err(Error::InvalidValidKey);
}
if invalid_id != invalid_statement.validator_public() {
return Err(Error::InvalidInvalidKey);
}
if candidate_receipt.hash() != candidate_hash {
return Err(Error::InvalidCandidateReceipt);
}
let valid_kind = match valid_statement.statement() {
DisputeStatement::Valid(v) => v,
_ => return Err(Error::ValidStatementHasInvalidKind),
};
let invalid_kind = match invalid_statement.statement() {
DisputeStatement::Invalid(v) => v,
_ => return Err(Error::InvalidStatementHasValidKind),
};
let valid_vote = ValidDisputeVote {
validator_index: valid_index,
signature: valid_statement.validator_signature().clone(),
kind: valid_kind.clone(),
};
let invalid_vote = InvalidDisputeVote {
validator_index: invalid_index,
signature: invalid_statement.validator_signature().clone(),
kind: *invalid_kind,
};
Ok(DisputeMessage(UncheckedDisputeMessage {
candidate_receipt,
session_index,
valid_vote,
invalid_vote,
}))
}
pub fn candidate_receipt(&self) -> &CandidateReceipt {
&self.0.candidate_receipt
}
pub fn session_index(&self) -> SessionIndex {
self.0.session_index
}
pub fn invalid_vote(&self) -> &InvalidDisputeVote {
&self.0.invalid_vote
}
pub fn valid_vote(&self) -> &ValidDisputeVote {
&self.0.valid_vote
}
}
impl UncheckedDisputeMessage {
pub fn try_into_signed_votes(
self,
session_info: &SessionInfo,
) -> Result<
(
CandidateReceipt,
(SignedDisputeStatement, ValidatorIndex),
(SignedDisputeStatement, ValidatorIndex),
),
(),
> {
let Self { candidate_receipt, session_index, valid_vote, invalid_vote } = self;
let candidate_hash = candidate_receipt.hash();
let vote_valid = {
let ValidDisputeVote { validator_index, signature, kind } = valid_vote;
let validator_public = session_info.validators.get(validator_index).ok_or(())?.clone();
(
SignedDisputeStatement::new_checked(
DisputeStatement::Valid(kind),
candidate_hash,
session_index,
validator_public,
signature,
)?,
validator_index,
)
};
let vote_invalid = {
let InvalidDisputeVote { validator_index, signature, kind } = invalid_vote;
let validator_public = session_info.validators.get(validator_index).ok_or(())?.clone();
(
SignedDisputeStatement::new_checked(
DisputeStatement::Invalid(kind),
candidate_hash,
session_index,
validator_public,
signature,
)?,
validator_index,
)
};
Ok((candidate_receipt, vote_valid, vote_invalid))
}
}
impl From<DisputeMessage> for UncheckedDisputeMessage {
fn from(message: DisputeMessage) -> Self {
message.0
}
}