Skip to main content

tributary_framework/tendermint/
slash.rs

1use alloc::vec::Vec;
2use std::io;
3
4use borsh::{BorshSerialize as _, BorshDeserialize as _};
5
6use tendermint::SlashReason;
7
8use crate::ReadWrite;
9use super::{ValidatorSet, AggregateSignature, SignatureScheme};
10
11type Signature = [u8; 64];
12type Hash = [u8; 32];
13
14/// A slash for a validator.
15///
16/// When included in a block, the validator is not excluded from the Tendermint consensus process.
17/// The validator's signed transactions WILL NOT be accepted into the mempool anymore however. The
18/// application SHOULD also restrict this validator from future participation at the application
19/// level.
20#[derive(Clone, Debug)]
21pub struct Slash {
22  pub(super) validator: [u8; 32],
23  pub(super) slash_reason: SlashReason<Signature, AggregateSignature, Hash>,
24}
25
26impl Slash {
27  /// The slashed validator.
28  pub fn validator(&self) -> &[u8; 32] {
29    &self.validator
30  }
31
32  /// Verify a slash.
33  pub(crate) fn verify(
34    &self,
35    genesis: [u8; 32],
36    validator_set: &ValidatorSet,
37    signature_scheme: &SignatureScheme,
38  ) -> Result<(), ::tendermint::InvalidReason> {
39    self.slash_reason.verify(genesis, validator_set, signature_scheme, self.validator)
40  }
41}
42
43impl ReadWrite for Slash {
44  fn read(mut reader: impl io::Read) -> io::Result<Self> {
45    let validator = <[u8; 32]>::deserialize_reader(&mut reader)?;
46    let slash_reason = SlashReason::deserialize_reader(&mut reader)?;
47    Ok(Slash { validator, slash_reason })
48  }
49
50  fn write(&self, mut writer: impl io::Write) -> io::Result<()> {
51    let Self { validator, slash_reason } = self;
52    validator.serialize(&mut writer)?;
53    slash_reason.serialize(&mut writer)
54  }
55
56  fn serialize(&self) -> Vec<u8> {
57    let mut res = Vec::with_capacity(128);
58    self.write(&mut res).expect("`<Vec<u8> as io::Write>` is infallible");
59    res
60  }
61}
62
63impl crate::merkle::Digestible for Slash {
64  /// This solely hashes the validator as the semantics of this are independent of the exact reason
65  /// why this validator is being slashed.
66  fn update(&self, digest: &mut (impl ?Sized + blake2::digest::Update)) {
67    digest.update(&self.validator);
68  }
69}