gemot 0.1.1

Community domain and assembly layer for Moot lifecycle, governance, replication, and Tessera.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
//! Deterministic constitution fold and the shared governance chokepoint.

use std::collections::BTreeSet;

use p2panda_core::Operation;
use p2panda_core::cbor::encode_cbor;
use proofs::Digest;

use super::wire::{ConstitutionExt, from_operation, verify};
use super::{AmendmentRule, ConstitutionEvent, ConstitutionRules};

/// A community action evaluated under the accepted constitution.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum GovernedAction {
    /// Replace the constitution's current rules.
    AmendConstitution,
}

/// Accepted current constitution of one Moot.
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct Constitution {
    /// Governed Moot.
    pub moot_id: [u8; 32],
    /// Governance root fixed by genesis.
    pub founder: [u8; 32],
    /// Parent constitution for a fork.
    pub parent_constitution: Option<Digest>,
    /// Parent revision where a fork diverged.
    pub divergence_point: Option<Digest>,
    /// Current readable rules.
    pub rules: ConstitutionRules,
    /// Accepted operation identity of the current revision.
    pub revision: Digest,
}

/// Rejection while applying a constitution operation to live state.
#[derive(Clone, Debug, PartialEq, Eq, thiserror::Error)]
pub enum ConstitutionError {
    /// Operation signature or body commitment failed.
    #[error("constitution operation signature is invalid")]
    InvalidSignature,
    /// Operation addresses another Moot.
    #[error("constitution operation addresses another moot")]
    WrongMoot,
    /// Operation body did not decode.
    #[error("constitution operation is malformed")]
    Malformed,
    /// Readable rules did not match their committed digest.
    #[error("constitution rules do not match their digest")]
    RulesDigest,
    /// A second genesis cannot advance an established constitution.
    #[error("constitution is already founded")]
    AlreadyFounded,
    /// Amendment does not advance the accepted revision.
    #[error("amendment does not name the accepted prior revision")]
    StaleRevision,
    /// Actor is not permitted by the prior constitution.
    #[error("actor is not authorized to amend this constitution")]
    Unauthorized,
    /// One quorum signature alone cannot advance the accepted constitution.
    #[error("amendment still needs additional quorum signatures")]
    PendingQuorum,
}

/// Evaluate one governed action against the current shared law.
pub fn authorize_governed(
    action: GovernedAction,
    actor: [u8; 32],
    constitution: &Constitution,
) -> bool {
    match action {
        GovernedAction::AmendConstitution => constitution
            .rules
            .amendment
            .permits(constitution.founder, actor),
    }
}

fn quorum_revision(previous: &Digest, rules_hash: &Digest) -> Digest {
    let encoded = encode_cbor(&(previous, rules_hash))
        .expect("constitution quorum revision components always CBOR-encode");
    let mut bytes = b"gemot/constitution/quorum-revision/v1\0".to_vec();
    bytes.extend_from_slice(&encoded);
    Digest::blake3(&bytes)
}

type AmendmentProposal = ([u8; 32], ConstitutionRules, Digest, BTreeSet<[u8; 32]>);

fn accepted_amendment(
    state: &Constitution,
    candidates: &[([u8; 32], [u8; 32], ConstitutionEvent)],
) -> Option<([u8; 32], ConstitutionRules, Digest)> {
    let mut proposals: Vec<AmendmentProposal> = Vec::new();
    for (hash, author, event) in candidates {
        let ConstitutionEvent::Amended {
            previous,
            rules,
            rules_hash,
            ..
        } = event
        else {
            continue;
        };
        if *previous != state.revision
            || !authorize_governed(GovernedAction::AmendConstitution, *author, state)
        {
            continue;
        }
        if let Some((canonical, _, _, supporters)) = proposals
            .iter_mut()
            .find(|(_, _, proposed_hash, _)| *proposed_hash == *rules_hash)
        {
            *canonical = (*canonical).min(*hash);
            supporters.insert(*author);
        } else {
            proposals.push((
                *hash,
                rules.clone(),
                rules_hash.clone(),
                BTreeSet::from([*author]),
            ));
        }
    }
    proposals.sort_by_key(|(canonical, _, _, _)| *canonical);
    proposals
        .into_iter()
        .find(|(_, _, _, supporters)| supporters.len() >= state.rules.amendment.threshold())
        .map(|(canonical, rules, rules_hash, _)| (canonical, rules, rules_hash))
}

impl Constitution {
    /// Deterministically fold a retained operation corpus.
    ///
    /// The configured founder is the external root of governance supplied by
    /// the Moot declaration. Invalid candidates are ignored. A founder's
    /// concurrent amendments of one revision resolve by operation hash.
    pub fn fold<'a, I>(moot_id: [u8; 32], expected_founder: [u8; 32], operations: I) -> Option<Self>
    where
        I: IntoIterator<Item = &'a Operation<ConstitutionExt>>,
    {
        let mut candidates: Vec<([u8; 32], [u8; 32], ConstitutionEvent)> = operations
            .into_iter()
            .filter_map(|operation| {
                if operation.header.extensions.moot_id != moot_id || !verify(operation) {
                    return None;
                }
                let event = from_operation(operation).ok()?;
                if !event.rules_are_bound() {
                    return None;
                }
                Some((
                    *operation.hash.as_bytes(),
                    *operation.header.verifying_key.as_bytes(),
                    event,
                ))
            })
            .collect();
        candidates.sort_by_key(|candidate| candidate.0);

        let (genesis_hash, _, genesis) = candidates.iter().find(|(_, author, event)| {
            matches!(
                event,
                ConstitutionEvent::Genesis {
                    moot_id: event_moot,
                    founder,
                    ..
                } if *event_moot == moot_id
                    && *founder == expected_founder
                    && *author == expected_founder
            )
        })?;
        let ConstitutionEvent::Genesis {
            founder,
            parent_constitution,
            divergence_point,
            rules,
            ..
        } = genesis
        else {
            unreachable!()
        };
        let mut state = Self {
            moot_id,
            founder: *founder,
            parent_constitution: parent_constitution.clone(),
            divergence_point: divergence_point.clone(),
            rules: rules.clone(),
            revision: Digest::p2panda_operation(*genesis_hash),
        };

        while let Some((hash, rules, rules_hash)) = accepted_amendment(&state, &candidates) {
            let quorum = matches!(state.rules.amendment, AmendmentRule::MemberQuorum { .. });
            state.rules = rules;
            state.revision = if quorum {
                quorum_revision(&state.revision, &rules_hash)
            } else {
                Digest::p2panda_operation(hash)
            };
        }
        Some(state)
    }

    /// Apply one next operation to a live fold.
    pub fn apply(
        &mut self,
        operation: &Operation<ConstitutionExt>,
    ) -> Result<(), ConstitutionError> {
        if operation.header.extensions.moot_id != self.moot_id {
            return Err(ConstitutionError::WrongMoot);
        }
        if !verify(operation) {
            return Err(ConstitutionError::InvalidSignature);
        }
        let event = from_operation(operation).map_err(|_| ConstitutionError::Malformed)?;
        if !event.rules_are_bound() {
            return Err(ConstitutionError::RulesDigest);
        }
        let ConstitutionEvent::Amended {
            previous, rules, ..
        } = event
        else {
            return Err(ConstitutionError::AlreadyFounded);
        };
        if previous != self.revision {
            return Err(ConstitutionError::StaleRevision);
        }
        let actor = *operation.header.verifying_key.as_bytes();
        if !authorize_governed(GovernedAction::AmendConstitution, actor, self) {
            return Err(ConstitutionError::Unauthorized);
        }
        if matches!(self.rules.amendment, AmendmentRule::MemberQuorum { .. }) {
            return Err(ConstitutionError::PendingQuorum);
        }
        self.rules = rules;
        self.revision = Digest::p2panda_operation(*operation.hash.as_bytes());
        Ok(())
    }
}

#[cfg(test)]
mod tests {
    use std::collections::BTreeSet;

    use super::*;
    use crate::moot::constitution::{ConstitutionEvent, ConstitutionRules, to_operation};
    use identity::{Ed25519Keypair, IdentityProvider, InMemoryProvider};

    const MOOT: [u8; 32] = [0x63; 32];

    fn keypair(seed: u8) -> Ed25519Keypair {
        InMemoryProvider::from_seed([seed; 32])
            .derive_keypair(b"constitution-fold")
            .unwrap()
    }

    #[test]
    fn fold_is_order_independent_and_uses_the_prior_rule() {
        let founder = keypair(1);
        let intruder = keypair(2);
        let founder_id = founder.public_key().to_bytes();
        let genesis_event = ConstitutionEvent::genesis(
            MOOT,
            founder_id,
            None,
            None,
            ConstitutionRules::founder_only(founder_id),
            1,
        );
        let genesis = to_operation(&founder, MOOT, &genesis_event, 0, None);
        let genesis_revision = Digest::p2panda_operation(*genesis.hash.as_bytes());

        let mut accepted_rules = ConstitutionRules::founder_only(founder_id);
        accepted_rules.checkpoint_signers.insert([9; 32]);
        let accepted_event =
            ConstitutionEvent::amended(genesis_revision.clone(), accepted_rules.clone(), 2);
        let accepted = to_operation(
            &founder,
            MOOT,
            &accepted_event,
            1,
            Some(*genesis.hash.as_bytes()),
        );
        let rejected_event = ConstitutionEvent::amended(
            genesis_revision,
            ConstitutionRules::founder_only(founder_id),
            2,
        );
        let rejected = to_operation(&intruder, MOOT, &rejected_event, 0, None);

        let forward = Constitution::fold(MOOT, founder_id, [&genesis, &accepted, &rejected]);
        let reverse = Constitution::fold(MOOT, founder_id, [&rejected, &accepted, &genesis]);
        assert_eq!(forward, reverse);
        assert_eq!(forward.unwrap().rules, accepted_rules);
    }

    #[test]
    fn live_apply_rejects_a_non_founder_without_changing_state() {
        let founder = keypair(1);
        let intruder = keypair(2);
        let founder_id = founder.public_key().to_bytes();
        let genesis_event = ConstitutionEvent::genesis(
            MOOT,
            founder_id,
            None,
            None,
            ConstitutionRules::founder_only(founder_id),
            1,
        );
        let genesis = to_operation(&founder, MOOT, &genesis_event, 0, None);
        let mut state = Constitution::fold(MOOT, founder_id, [&genesis]).unwrap();
        let before = state.clone();
        let event = ConstitutionEvent::amended(
            state.revision.clone(),
            ConstitutionRules::founder_only(founder_id),
            2,
        );
        let operation = to_operation(&intruder, MOOT, &event, 0, None);
        assert_eq!(
            state.apply(&operation),
            Err(ConstitutionError::Unauthorized)
        );
        assert_eq!(state, before);
    }

    #[test]
    fn quorum_amendment_needs_distinct_electors_and_has_a_stable_revision() {
        let founder = keypair(1);
        let alice = keypair(2);
        let bob = keypair(3);
        let carol = keypair(4);
        let founder_id = founder.public_key().to_bytes();
        let electorate = BTreeSet::from([
            alice.public_key().to_bytes(),
            bob.public_key().to_bytes(),
            carol.public_key().to_bytes(),
        ]);
        let genesis_event = ConstitutionEvent::genesis(
            MOOT,
            founder_id,
            None,
            None,
            ConstitutionRules::founder_only(founder_id),
            1,
        );
        let genesis = to_operation(&founder, MOOT, &genesis_event, 0, None);
        let genesis_revision = Digest::p2panda_operation(*genesis.hash.as_bytes());

        let mut quorum_rules = ConstitutionRules::founder_only(founder_id);
        quorum_rules.amendment = AmendmentRule::MemberQuorum {
            electorate,
            threshold: 2,
        };
        let quorum = to_operation(
            &founder,
            MOOT,
            &ConstitutionEvent::amended(genesis_revision, quorum_rules.clone(), 2),
            1,
            Some(*genesis.hash.as_bytes()),
        );
        let quorum_revision = Digest::p2panda_operation(*quorum.hash.as_bytes());

        let mut next_rules = quorum_rules.clone();
        next_rules.checkpoint_signers = BTreeSet::from([alice.public_key().to_bytes()]);
        let alice_support = to_operation(
            &alice,
            MOOT,
            &ConstitutionEvent::amended(quorum_revision.clone(), next_rules.clone(), 3),
            0,
            None,
        );
        let bob_support = to_operation(
            &bob,
            MOOT,
            &ConstitutionEvent::amended(quorum_revision.clone(), next_rules.clone(), 3),
            0,
            None,
        );
        let carol_support = to_operation(
            &carol,
            MOOT,
            &ConstitutionEvent::amended(quorum_revision, next_rules.clone(), 3),
            0,
            None,
        );

        let pending =
            Constitution::fold(MOOT, founder_id, [&genesis, &quorum, &alice_support]).unwrap();
        assert_eq!(pending.rules, quorum_rules);

        let accepted = Constitution::fold(
            MOOT,
            founder_id,
            [&genesis, &quorum, &alice_support, &bob_support],
        )
        .unwrap();
        assert_eq!(accepted.rules, next_rules);
        let revision = accepted.revision.clone();

        let late_support = Constitution::fold(
            MOOT,
            founder_id,
            [
                &genesis,
                &quorum,
                &alice_support,
                &bob_support,
                &carol_support,
            ],
        )
        .unwrap();
        assert_eq!(late_support.revision, revision);
    }
}