kcode-k1-bootstrap-state 0.2.0

Canonical first-run Loom bootstrap state
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
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
#![doc = include_str!("../Documentation.md")]
#![forbid(unsafe_code)]

use kcode_k1_peering::K1Peering;
use kcode_k1_txn_ordering::{K1TxnOrdering, Subsystem, SubsystemId, TxId};
use semver::Version;
use std::sync::{Arc, Mutex, MutexGuard, Weak};

const SUBSYSTEM: &str = "k1-loom-bootstrap";
const WIRE_VERSION: u8 = 2;

#[derive(Clone, Debug, Eq, PartialEq)]
pub struct BeginRecord {
    username: String,
    full_name: String,
    public_key: [u8; 32],
}

impl BeginRecord {
    pub fn new(username: String, full_name: String, public_key: [u8; 32]) -> Self {
        Self {
            username,
            full_name,
            public_key,
        }
    }

    pub fn username(&self) -> &str {
        &self.username
    }

    pub fn full_name(&self) -> &str {
        &self.full_name
    }

    pub const fn public_key(&self) -> [u8; 32] {
        self.public_key
    }
}

#[derive(Clone, Debug, Eq, PartialEq)]
pub struct ImportedPackage {
    logical_name: String,
    version: Version,
}

impl ImportedPackage {
    pub fn new(logical_name: String, version: Version) -> Result<Self, String> {
        if logical_name.is_empty() {
            return Err("imported package logical name is empty".to_owned());
        }
        if !version.pre.is_empty() || !version.build.is_empty() {
            return Err("imported package version must be stable".to_owned());
        }
        Ok(Self {
            logical_name,
            version,
        })
    }

    pub fn logical_name(&self) -> &str {
        &self.logical_name
    }

    pub fn version(&self) -> &Version {
        &self.version
    }
}

#[derive(Clone, Debug, Eq, PartialEq)]
pub struct CompleteRecord {
    begin: BeginRecord,
    user_id: [u8; 12],
    group_id: [u8; 12],
    profile_id: [u8; 12],
    root_id: [u8; 12],
    packages: Vec<ImportedPackage>,
}

impl CompleteRecord {
    pub fn new(
        begin: BeginRecord,
        user_id: [u8; 12],
        group_id: [u8; 12],
        profile_id: [u8; 12],
        root_id: [u8; 12],
        mut packages: Vec<ImportedPackage>,
    ) -> Result<Self, String> {
        packages.sort_by(|left, right| {
            left.logical_name
                .cmp(&right.logical_name)
                .then_with(|| left.version.cmp(&right.version))
        });
        if packages.windows(2).any(|pair| {
            pair[0].logical_name == pair[1].logical_name && pair[0].version == pair[1].version
        }) {
            return Err("duplicate imported package coordinate".to_owned());
        }
        Ok(Self {
            begin,
            user_id,
            group_id,
            profile_id,
            root_id,
            packages,
        })
    }

    pub fn begin(&self) -> &BeginRecord {
        &self.begin
    }

    pub const fn user_id(&self) -> [u8; 12] {
        self.user_id
    }

    pub const fn group_id(&self) -> [u8; 12] {
        self.group_id
    }

    pub const fn profile_id(&self) -> [u8; 12] {
        self.profile_id
    }

    pub const fn root_id(&self) -> [u8; 12] {
        self.root_id
    }

    pub fn packages(&self) -> &[ImportedPackage] {
        &self.packages
    }
}

#[derive(Clone, Debug, Eq, PartialEq)]
pub enum BootstrapStatus {
    Empty,
    Begun {
        record: BeginRecord,
        transaction: TxId,
    },
    Complete {
        record: CompleteRecord,
        transaction: TxId,
    },
}

#[derive(Clone)]
pub struct K1BootstrapState {
    inner: Arc<Inner>,
}

struct Inner {
    peering: Arc<K1Peering>,
    state: Mutex<State>,
}

struct State {
    status: BootstrapStatus,
    available: bool,
}

struct Handler(Weak<Inner>);

#[derive(Debug)]
enum Event {
    Begin(BeginRecord),
    Complete(CompleteRecord),
}

impl K1BootstrapState {
    pub fn open(ordering: Arc<K1TxnOrdering>, peering: Arc<K1Peering>) -> Result<Self, String> {
        let inner = Arc::new(Inner {
            peering,
            state: Mutex::new(State {
                status: BootstrapStatus::Empty,
                available: true,
            }),
        });
        ordering.register_subsystem(
            SubsystemId::from_str(SUBSYSTEM)?,
            None,
            Arc::new(Handler(Arc::downgrade(&inner))),
        )?;
        Ok(Self { inner })
    }

    pub fn status(&self) -> Result<BootstrapStatus, String> {
        Ok(ready(&self.inner)?.status.clone())
    }

    pub fn begin(&self, record: BeginRecord) -> Result<TxId, String> {
        match self.status()? {
            BootstrapStatus::Begun {
                record: existing,
                transaction,
            } if existing == record => {
                return Ok(transaction);
            }
            BootstrapStatus::Complete {
                record: existing,
                transaction,
            } if existing.begin == record => return Ok(transaction),
            BootstrapStatus::Empty => {}
            _ => return Err("bootstrap Begin conflicts with canonical state".to_owned()),
        }
        self.submit(Event::Begin(record.clone()))?;
        match self.status()? {
            BootstrapStatus::Begun {
                record: existing,
                transaction,
            } if existing == record => Ok(transaction),
            BootstrapStatus::Complete {
                record: existing,
                transaction,
            } if existing.begin == record => Ok(transaction),
            _ => Err("bootstrap Begin lost to conflicting canonical state".to_owned()),
        }
    }

    pub fn complete(&self, record: CompleteRecord) -> Result<TxId, String> {
        match self.status()? {
            BootstrapStatus::Begun {
                record: existing, ..
            } if existing == record.begin => {}
            BootstrapStatus::Complete {
                record: existing,
                transaction,
            } if existing == record => {
                return Ok(transaction);
            }
            BootstrapStatus::Empty => return Err("bootstrap has not begun".to_owned()),
            _ => return Err("bootstrap Complete conflicts with canonical state".to_owned()),
        }
        self.submit(Event::Complete(record.clone()))?;
        match self.status()? {
            BootstrapStatus::Complete {
                record: existing,
                transaction,
            } if existing == record => Ok(transaction),
            _ => Err("bootstrap Complete lost to conflicting canonical state".to_owned()),
        }
    }

    fn submit(&self, event: Event) -> Result<TxId, String> {
        self.inner
            .peering
            .submit_txn(SubsystemId::from_str(SUBSYSTEM)?, &encode(&event)?)
    }
}

impl Subsystem for Handler {
    fn submit_txn(&self, id: TxId, payload: &[u8]) -> Result<(), String> {
        let inner = self
            .0
            .upgrade()
            .ok_or_else(|| "bootstrap state is unavailable".to_owned())?;
        let event = decode(payload).map_err(|error| fail(&inner, error))?;
        let mut state = ready(&inner)?;
        match (&state.status, event) {
            (BootstrapStatus::Empty, Event::Begin(record)) => {
                state.status = BootstrapStatus::Begun {
                    record,
                    transaction: id,
                };
            }
            (BootstrapStatus::Begun { record: begun, .. }, Event::Complete(record))
                if *begun == record.begin =>
            {
                state.status = BootstrapStatus::Complete {
                    record,
                    transaction: id,
                };
            }
            _ => {}
        }
        Ok(())
    }

    fn reorg(&self) -> Result<(), String> {
        let inner = self
            .0
            .upgrade()
            .ok_or_else(|| "bootstrap state is unavailable".to_owned())?;
        lock(&inner)?.available = false;
        Ok(())
    }
}

fn lock(inner: &Inner) -> Result<MutexGuard<'_, State>, String> {
    inner
        .state
        .lock()
        .map_err(|_| "bootstrap state lock poisoned".to_owned())
}

fn ready(inner: &Inner) -> Result<MutexGuard<'_, State>, String> {
    let state = lock(inner)?;
    if state.available {
        Ok(state)
    } else {
        Err("bootstrap state is unavailable".to_owned())
    }
}

fn fail(inner: &Inner, error: String) -> String {
    if let Ok(mut state) = lock(inner) {
        state.available = false;
    }
    error
}

fn encode(event: &Event) -> Result<Vec<u8>, String> {
    let mut out = vec![
        WIRE_VERSION,
        match event {
            Event::Begin(_) => 1,
            Event::Complete(_) => 2,
        },
    ];
    match event {
        Event::Begin(record) => put_begin(&mut out, record)?,
        Event::Complete(record) => {
            put_begin(&mut out, &record.begin)?;
            out.extend_from_slice(&record.user_id);
            out.extend_from_slice(&record.group_id);
            out.extend_from_slice(&record.profile_id);
            out.extend_from_slice(&record.root_id);
            put_u64(&mut out, record.packages.len())?;
            for package in &record.packages {
                put_string(&mut out, &package.logical_name)?;
                put_string(&mut out, &package.version.to_string())?;
            }
        }
    }
    Ok(out)
}

fn put_begin(out: &mut Vec<u8>, record: &BeginRecord) -> Result<(), String> {
    put_string(out, &record.username)?;
    put_string(out, &record.full_name)?;
    out.extend_from_slice(&record.public_key);
    Ok(())
}

fn put_string(out: &mut Vec<u8>, value: &str) -> Result<(), String> {
    put_u64(out, value.len())?;
    out.try_reserve(value.len())
        .map_err(|_| "bootstrap encoding allocation failed".to_owned())?;
    out.extend_from_slice(value.as_bytes());
    Ok(())
}

fn put_u64(out: &mut Vec<u8>, value: usize) -> Result<(), String> {
    out.extend_from_slice(
        &u64::try_from(value)
            .map_err(|_| "bootstrap encoding length overflow".to_owned())?
            .to_le_bytes(),
    );
    Ok(())
}

fn decode(bytes: &[u8]) -> Result<Event, String> {
    let mut input = Input { bytes, at: 0 };
    if input.byte()? != WIRE_VERSION {
        return Err("unsupported bootstrap state wire version".to_owned());
    }
    let kind = input.byte()?;
    let begin = input.begin()?;
    let event = match kind {
        1 => Event::Begin(begin),
        2 => {
            let user_id = input.array()?;
            let group_id = input.array()?;
            let profile_id = input.array()?;
            let root_id = input.array()?;
            let count = input.length()?;
            let mut packages = Vec::new();
            packages
                .try_reserve(count)
                .map_err(|_| "bootstrap package allocation failed".to_owned())?;
            for _ in 0..count {
                packages.push(ImportedPackage::new(
                    input.string()?,
                    input
                        .string()?
                        .parse()
                        .map_err(|_| "invalid bootstrap package version".to_owned())?,
                )?);
            }
            Event::Complete(CompleteRecord::new(
                begin, user_id, group_id, profile_id, root_id, packages,
            )?)
        }
        _ => return Err("unknown bootstrap state event kind".to_owned()),
    };
    if input.at != bytes.len() {
        return Err("trailing bootstrap state bytes".to_owned());
    }
    Ok(event)
}

struct Input<'a> {
    bytes: &'a [u8],
    at: usize,
}

impl Input<'_> {
    fn take(&mut self, count: usize) -> Result<&[u8], String> {
        let end = self
            .at
            .checked_add(count)
            .ok_or_else(|| "bootstrap state length overflow".to_owned())?;
        let value = self
            .bytes
            .get(self.at..end)
            .ok_or_else(|| "truncated bootstrap state event".to_owned())?;
        self.at = end;
        Ok(value)
    }

    fn byte(&mut self) -> Result<u8, String> {
        Ok(self.take(1)?[0])
    }

    fn array<const N: usize>(&mut self) -> Result<[u8; N], String> {
        self.take(N)?
            .try_into()
            .map_err(|_| "invalid bootstrap fixed field".to_owned())
    }

    fn length(&mut self) -> Result<usize, String> {
        usize::try_from(u64::from_le_bytes(self.array()?))
            .map_err(|_| "bootstrap state length overflow".to_owned())
    }

    fn string(&mut self) -> Result<String, String> {
        let length = self.length()?;
        String::from_utf8(self.take(length)?.to_vec())
            .map_err(|_| "bootstrap state string is not UTF-8".to_owned())
    }

    fn begin(&mut self) -> Result<BeginRecord, String> {
        Ok(BeginRecord::new(
            self.string()?,
            self.string()?,
            self.array()?,
        ))
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn version_two_wire_round_trips_without_code_hashes() {
        let begin = BeginRecord::new("admin".into(), "Admin".into(), [2; 32]);
        let packages = vec![ImportedPackage::new("loom".into(), Version::new(0, 1, 0)).unwrap()];
        let complete =
            CompleteRecord::new(begin, [4; 12], [5; 12], [6; 12], [7; 12], packages).unwrap();
        let encoded = encode(&Event::Complete(complete.clone())).unwrap();
        assert_eq!(encoded[0], 2);
        assert!(matches!(decode(&encoded).unwrap(), Event::Complete(found) if found == complete));
        assert!(decode(&[1, 1]).unwrap_err().contains("wire version"));
    }
}