Skip to main content

moothold/
event.rs

1/* This Source Code Form is subject to the terms of the Mozilla Public
2 * License, v. 2.0. If a copy of the MPL was not distributed with this
3 * file, You can obtain one at https://mozilla.org/MPL/2.0/. */
4
5use serde::{Deserialize, Serialize};
6
7use crate::{CompositionPolicy, MootId};
8
9/// Stable identity of a holding of autonomous moots.
10#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, PartialOrd, Ord, Serialize, Deserialize)]
11pub struct MootholdId(pub [u8; 32]);
12
13/// Settings governing one member moot's direct relationship to the holding.
14#[derive(Clone, Copy, Debug, PartialEq, Eq, Serialize, Deserialize)]
15pub struct MemberTerms {
16    /// Weight assigned to this moot's reputation facts, in basis points.
17    pub concord_weight_bp: u16,
18    /// Unreciprocated service credit tolerated before further requests stop.
19    pub reciprocity_tolerance: u64,
20}
21
22impl MemberTerms {
23    pub const MAX_CONCORD_WEIGHT_BP: u16 = 10_000;
24
25    pub fn new(concord_weight_bp: u16, reciprocity_tolerance: u64) -> Option<Self> {
26        (concord_weight_bp <= Self::MAX_CONCORD_WEIGHT_BP).then_some(Self {
27            concord_weight_bp,
28            reciprocity_tolerance,
29        })
30    }
31
32    pub(crate) fn is_valid(self) -> bool {
33        self.concord_weight_bp <= Self::MAX_CONCORD_WEIGHT_BP
34    }
35}
36
37/// Founder-signed changes to one Moothold aggregate.
38#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
39pub enum MootholdEvent {
40    Founded {
41        moothold_id: MootholdId,
42        name: String,
43        founder: [u8; 32],
44        initial_moot: MootId,
45        initial_terms: MemberTerms,
46        composition: CompositionPolicy,
47        at_ms: u64,
48    },
49    MootAdmitted {
50        previous: [u8; 32],
51        moot: MootId,
52        terms: MemberTerms,
53        at_ms: u64,
54    },
55    MootRemoved {
56        previous: [u8; 32],
57        moot: MootId,
58        at_ms: u64,
59    },
60    CompositionChanged {
61        previous: [u8; 32],
62        composition: CompositionPolicy,
63        at_ms: u64,
64    },
65}
66
67impl MootholdEvent {
68    pub(crate) fn at_ms(&self) -> u64 {
69        match self {
70            Self::Founded { at_ms, .. }
71            | Self::MootAdmitted { at_ms, .. }
72            | Self::MootRemoved { at_ms, .. }
73            | Self::CompositionChanged { at_ms, .. } => *at_ms,
74        }
75    }
76
77    pub(crate) fn previous(&self) -> Option<[u8; 32]> {
78        match self {
79            Self::Founded { .. } => None,
80            Self::MootAdmitted { previous, .. }
81            | Self::MootRemoved { previous, .. }
82            | Self::CompositionChanged { previous, .. } => Some(*previous),
83        }
84    }
85
86    pub(crate) fn is_well_formed(&self) -> bool {
87        match self {
88            Self::Founded {
89                name,
90                initial_terms,
91                ..
92            } => !name.trim().is_empty() && initial_terms.is_valid(),
93            Self::MootAdmitted { terms, .. } => terms.is_valid(),
94            Self::MootRemoved { .. } | Self::CompositionChanged { .. } => true,
95        }
96    }
97}