|
| 1 | +//! IBC Domain type definition for [`TrustThreshold`] |
| 2 | +//! represented as a fraction with valid values in the |
| 3 | +//! range `[0, 1)`. |
| 4 | +
|
| 5 | +use std::{convert::TryFrom, fmt}; |
| 6 | + |
| 7 | +use serde::{Deserialize, Serialize}; |
| 8 | +use tendermint_proto::Protobuf; |
| 9 | + |
| 10 | +use ibc_proto::ibc::lightclients::tendermint::v1::Fraction; |
| 11 | +use tendermint::trust_threshold::TrustThresholdFraction; |
| 12 | + |
| 13 | +use crate::ics02_client::error::Error; |
| 14 | + |
| 15 | +/// [`TrustThreshold`] defines the level of trust that a client has |
| 16 | +/// towards a set of validators of a chain. |
| 17 | +/// |
| 18 | +/// A trust threshold is represented as a fraction, i.e., a numerator and |
| 19 | +/// and a denominator. |
| 20 | +/// A typical trust threshold is 1/3 in practice. |
| 21 | +/// This type accepts even a value of 0, (numerator = 0, denominator = 0), |
| 22 | +/// which is used in the client state of an upgrading client. |
| 23 | +#[derive(Copy, Clone, Debug, PartialEq, Eq, Serialize, Deserialize)] |
| 24 | +pub struct TrustThreshold { |
| 25 | + numerator: u64, |
| 26 | + denominator: u64, |
| 27 | +} |
| 28 | + |
| 29 | +impl TrustThreshold { |
| 30 | + /// Constant for a trust threshold of 1/3. |
| 31 | + pub const ONE_THIRD: Self = Self { |
| 32 | + numerator: 1, |
| 33 | + denominator: 3, |
| 34 | + }; |
| 35 | + |
| 36 | + /// Constant for a trust threshold of 2/3. |
| 37 | + pub const TWO_THIRDS: Self = Self { |
| 38 | + numerator: 2, |
| 39 | + denominator: 3, |
| 40 | + }; |
| 41 | + |
| 42 | + /// Constant for a trust threshold of 0/0. |
| 43 | + pub const ZERO: Self = Self { |
| 44 | + numerator: 0, |
| 45 | + denominator: 0, |
| 46 | + }; |
| 47 | + |
| 48 | + /// Instantiate a TrustThreshold with the given denominator and |
| 49 | + /// numerator. |
| 50 | + /// |
| 51 | + /// The constructor succeeds if long as the resulting fraction |
| 52 | + /// is in the range`[0, 1)`. |
| 53 | + pub fn new(numerator: u64, denominator: u64) -> Result<Self, Error> { |
| 54 | + // The two parameters cannot yield a fraction that is bigger or equal to 1 |
| 55 | + if (numerator > denominator) |
| 56 | + || (denominator == 0 && numerator != 0) |
| 57 | + || (numerator == denominator && numerator != 0) |
| 58 | + { |
| 59 | + return Err(Error::invalid_trust_threshold(numerator, denominator)); |
| 60 | + } |
| 61 | + |
| 62 | + Ok(Self { |
| 63 | + numerator, |
| 64 | + denominator, |
| 65 | + }) |
| 66 | + } |
| 67 | + |
| 68 | + /// The numerator of the fraction underlying this trust threshold. |
| 69 | + pub fn numerator(&self) -> u64 { |
| 70 | + self.numerator |
| 71 | + } |
| 72 | + |
| 73 | + /// The denominator of the fraction underlying this trust threshold. |
| 74 | + pub fn denominator(&self) -> u64 { |
| 75 | + self.denominator |
| 76 | + } |
| 77 | +} |
| 78 | + |
| 79 | +/// Conversion from Tendermint domain type into |
| 80 | +/// IBC domain type. |
| 81 | +impl From<TrustThresholdFraction> for TrustThreshold { |
| 82 | + fn from(t: TrustThresholdFraction) -> Self { |
| 83 | + Self { |
| 84 | + numerator: t.numerator(), |
| 85 | + denominator: t.denominator(), |
| 86 | + } |
| 87 | + } |
| 88 | +} |
| 89 | + |
| 90 | +/// Conversion from IBC domain type into |
| 91 | +/// Tendermint domain type. |
| 92 | +impl TryFrom<TrustThreshold> for TrustThresholdFraction { |
| 93 | + type Error = Error; |
| 94 | + |
| 95 | + fn try_from(t: TrustThreshold) -> Result<TrustThresholdFraction, Error> { |
| 96 | + Self::new(t.numerator, t.denominator) |
| 97 | + .map_err(|e| Error::failed_trust_threshold_conversion(t.numerator, t.denominator, e)) |
| 98 | + } |
| 99 | +} |
| 100 | + |
| 101 | +impl Protobuf<Fraction> for TrustThreshold {} |
| 102 | + |
| 103 | +impl From<TrustThreshold> for Fraction { |
| 104 | + fn from(t: TrustThreshold) -> Self { |
| 105 | + Self { |
| 106 | + numerator: t.numerator, |
| 107 | + denominator: t.denominator, |
| 108 | + } |
| 109 | + } |
| 110 | +} |
| 111 | + |
| 112 | +impl TryFrom<Fraction> for TrustThreshold { |
| 113 | + type Error = Error; |
| 114 | + |
| 115 | + fn try_from(value: Fraction) -> Result<Self, Self::Error> { |
| 116 | + Self::new(value.numerator, value.denominator) |
| 117 | + } |
| 118 | +} |
| 119 | + |
| 120 | +impl Default for TrustThreshold { |
| 121 | + fn default() -> Self { |
| 122 | + Self::ONE_THIRD |
| 123 | + } |
| 124 | +} |
| 125 | + |
| 126 | +impl fmt::Display for TrustThreshold { |
| 127 | + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
| 128 | + write!(f, "{}/{}", self.numerator, self.denominator) |
| 129 | + } |
| 130 | +} |
0 commit comments