merkle_machines 0.6.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/CHANGELOG.md +91 -0
- data/Cargo.lock +314 -0
- data/Cargo.toml +28 -0
- data/LICENSE.txt +21 -0
- data/README.md +326 -0
- data/ext/merkle_machines_native/core/CHANGELOG.md +66 -0
- data/ext/merkle_machines_native/core/Cargo.toml +20 -0
- data/ext/merkle_machines_native/core/README.md +114 -0
- data/ext/merkle_machines_native/core/src/lib.rs +16 -0
- data/ext/merkle_machines_native/core/src/refusal.rs +54 -0
- data/ext/merkle_machines_native/core/src/request.rs +60 -0
- data/ext/merkle_machines_native/core/src/schema.rs +193 -0
- data/ext/merkle_machines_native/core/src/survey/buffer.rs +61 -0
- data/ext/merkle_machines_native/core/src/survey.rs +552 -0
- data/ext/merkle_machines_native/extconf.rb +2 -0
- data/ext/merkle_machines_native/ffi/Cargo.toml +21 -0
- data/ext/merkle_machines_native/ffi/extconf.rb +14 -0
- data/ext/merkle_machines_native/ffi/src/init.rs +50 -0
- data/ext/merkle_machines_native/ffi/src/lib.rs +453 -0
- data/ext/merkle_machines_native/ffi/src/values.rs +47 -0
- data/lib/merkle_machines/survey.rb +43 -0
- data/lib/merkle_machines/values.rb +63 -0
- data/lib/merkle_machines/version.rb +5 -0
- data/lib/merkle_machines.rb +8 -0
- data/sig/merkle_machines.rbs +142 -0
- metadata +92 -0
|
@@ -0,0 +1,552 @@
|
|
|
1
|
+
//! The bounded, resumable comparison traversal.
|
|
2
|
+
|
|
3
|
+
use periscope_machines::Issuer;
|
|
4
|
+
|
|
5
|
+
mod buffer;
|
|
6
|
+
|
|
7
|
+
use crate::{
|
|
8
|
+
refusal::Refusal,
|
|
9
|
+
request::{Request, Versions},
|
|
10
|
+
schema::{LeafRange, Node, Schema},
|
|
11
|
+
};
|
|
12
|
+
|
|
13
|
+
/// Outcome of a successful [`Survey::begin`].
|
|
14
|
+
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
|
15
|
+
pub enum Begun {
|
|
16
|
+
/// The root digests are equal; nothing to traverse.
|
|
17
|
+
Identical,
|
|
18
|
+
/// The root digests differ; requests are waiting.
|
|
19
|
+
Diverged,
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
/// Where a survey stands.
|
|
23
|
+
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
|
24
|
+
pub enum Phase {
|
|
25
|
+
/// No session: never begun, or aborted.
|
|
26
|
+
Idle,
|
|
27
|
+
/// Nodes are still waiting to be requested, answered or expanded.
|
|
28
|
+
Surveying,
|
|
29
|
+
/// Every node is resolved; leaf ranges are still buffered.
|
|
30
|
+
Draining,
|
|
31
|
+
/// Every node is resolved and every range has been taken.
|
|
32
|
+
Complete,
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
/// What [`Survey::abort`] did. Both are honoured; neither is a refusal.
|
|
36
|
+
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
|
37
|
+
pub enum Aborting {
|
|
38
|
+
/// A session was open and it was dropped.
|
|
39
|
+
Aborted,
|
|
40
|
+
/// No session was open: an idempotent repeat that changed nothing.
|
|
41
|
+
AlreadyIdle,
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
|
45
|
+
enum State {
|
|
46
|
+
Free,
|
|
47
|
+
Queued,
|
|
48
|
+
InFlight,
|
|
49
|
+
/// Answered; the mask holds the differing children not yet moved out.
|
|
50
|
+
Expanded(u64),
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
/// The session a survey has open, with everything it pinned.
|
|
54
|
+
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
|
55
|
+
struct Open {
|
|
56
|
+
session: u64,
|
|
57
|
+
schema: Schema,
|
|
58
|
+
versions: Versions,
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
|
62
|
+
struct Slot {
|
|
63
|
+
node: Node,
|
|
64
|
+
state: State,
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
/// The entry a diverged session starts from, borrowed before `begin` changes
|
|
68
|
+
/// anything.
|
|
69
|
+
enum Start<'a> {
|
|
70
|
+
Range(&'a mut LeafRange),
|
|
71
|
+
Root(&'a mut Slot),
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
const FREE: Slot = Slot {
|
|
75
|
+
node: Node::ROOT,
|
|
76
|
+
state: State::Free,
|
|
77
|
+
};
|
|
78
|
+
const EMPTY: LeafRange = LeafRange { start: 0, end: 0 };
|
|
79
|
+
|
|
80
|
+
/// Locates the leaves on which two digest trees differ.
|
|
81
|
+
///
|
|
82
|
+
/// `P` bounds the nodes held at once (queued, in flight or partly expanded) and
|
|
83
|
+
/// `O` bounds the buffered leaf ranges. The survey stores no digests.
|
|
84
|
+
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
|
85
|
+
pub struct Survey<const P: usize, const O: usize> {
|
|
86
|
+
/// The open session; `None` while idle.
|
|
87
|
+
open: Option<Open>,
|
|
88
|
+
/// Issues session ids; the latest one is the open (or last) session.
|
|
89
|
+
issuer: Issuer,
|
|
90
|
+
slots: [Slot; P],
|
|
91
|
+
out: [LeafRange; O],
|
|
92
|
+
buffered: usize,
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
impl<const P: usize, const O: usize> Default for Survey<P, O> {
|
|
96
|
+
fn default() -> Self {
|
|
97
|
+
Self::new()
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
impl<const P: usize, const O: usize> Survey<P, O> {
|
|
102
|
+
/// An idle survey in scope zero.
|
|
103
|
+
#[must_use]
|
|
104
|
+
pub const fn new() -> Self {
|
|
105
|
+
Self::with_scope(0)
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
/// An idle survey whose sessions, and so its requests, carry `scope`.
|
|
109
|
+
///
|
|
110
|
+
/// Requests are only meaningful to the survey that issued them. Two
|
|
111
|
+
/// surveys in the same scope issue the same session ids, so give every
|
|
112
|
+
/// survey whose requests could meet its own scope.
|
|
113
|
+
#[must_use]
|
|
114
|
+
pub const fn with_scope(scope: u16) -> Self {
|
|
115
|
+
Self::build(Issuer::new(scope))
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
/// An idle survey that continues `scope` after a restart: `issued` is
|
|
119
|
+
/// what [`issued`](Self::issued) answered when the caller saved it.
|
|
120
|
+
///
|
|
121
|
+
/// Only the session sequence is restored. No session is open and nothing
|
|
122
|
+
/// is queued or buffered: every request issued before the restart is
|
|
123
|
+
/// [`Refusal::Stale`], and comparing again is the caller's job.
|
|
124
|
+
/// [`session`](Self::session) answers the last session issued before the
|
|
125
|
+
/// restart, which is not open (`None` when `issued` is zero: nothing was
|
|
126
|
+
/// ever begun in this scope). The next session carries sequence
|
|
127
|
+
/// `issued + 1`, so none is reissued as long as the saved count is not
|
|
128
|
+
/// lower than what the survey had really issued; save it before sending a
|
|
129
|
+
/// request out.
|
|
130
|
+
///
|
|
131
|
+
/// `None` when `issued` is beyond `periscope_machines::SEQUENCE_MAX`,
|
|
132
|
+
/// which no survey reaches.
|
|
133
|
+
#[must_use]
|
|
134
|
+
pub const fn resumed(scope: u16, issued: u64) -> Option<Self> {
|
|
135
|
+
match Issuer::resumed(scope, issued) {
|
|
136
|
+
Some(sequence) => Some(Self::build(sequence)),
|
|
137
|
+
None => None,
|
|
138
|
+
}
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
const fn build(issuer: Issuer) -> Self {
|
|
142
|
+
Self {
|
|
143
|
+
open: None,
|
|
144
|
+
issuer,
|
|
145
|
+
slots: [FREE; P],
|
|
146
|
+
out: [EMPTY; O],
|
|
147
|
+
buffered: 0,
|
|
148
|
+
}
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
/// Open a session comparing two trees whose root digests the caller read at
|
|
152
|
+
/// `versions`.
|
|
153
|
+
///
|
|
154
|
+
/// # Errors
|
|
155
|
+
/// [`Refusal::Busy`], [`Refusal::Schema`], [`Refusal::Undersized`] or
|
|
156
|
+
/// [`Refusal::Exhausted`]; the survey is unchanged.
|
|
157
|
+
pub fn begin<D: PartialEq>(
|
|
158
|
+
&mut self,
|
|
159
|
+
local: Schema,
|
|
160
|
+
remote: Schema,
|
|
161
|
+
versions: Versions,
|
|
162
|
+
local_root: &D,
|
|
163
|
+
remote_root: &D,
|
|
164
|
+
) -> Result<Begun, Refusal> {
|
|
165
|
+
if matches!(self.phase(), Phase::Surveying | Phase::Draining) {
|
|
166
|
+
return Err(Refusal::Busy);
|
|
167
|
+
}
|
|
168
|
+
local.compatible(&remote).map_err(Refusal::Schema)?;
|
|
169
|
+
if P < usize::from(local.depth()) {
|
|
170
|
+
return Err(Refusal::Undersized);
|
|
171
|
+
}
|
|
172
|
+
// Where a diverged session starts, taken before anything changes: a
|
|
173
|
+
// single-leaf tree buffers its one range, a deeper one queues the root.
|
|
174
|
+
// A survey without room for it (`O == 0`) is undersized.
|
|
175
|
+
let start = if local.depth() == 0 {
|
|
176
|
+
Start::Range(self.out.first_mut().ok_or(Refusal::Undersized)?)
|
|
177
|
+
} else {
|
|
178
|
+
if O == 0 {
|
|
179
|
+
return Err(Refusal::Undersized);
|
|
180
|
+
}
|
|
181
|
+
Start::Root(self.slots.first_mut().ok_or(Refusal::Undersized)?)
|
|
182
|
+
};
|
|
183
|
+
// Last check: a session id is only spent on a session that opens.
|
|
184
|
+
let session = match self.issuer.issue() {
|
|
185
|
+
Ok(session) => session,
|
|
186
|
+
Err(periscope_machines::Refusal::Exhausted) => return Err(Refusal::Exhausted),
|
|
187
|
+
};
|
|
188
|
+
self.open = Some(Open {
|
|
189
|
+
session,
|
|
190
|
+
schema: local,
|
|
191
|
+
versions,
|
|
192
|
+
});
|
|
193
|
+
if local_root == remote_root {
|
|
194
|
+
return Ok(Begun::Identical);
|
|
195
|
+
}
|
|
196
|
+
match start {
|
|
197
|
+
Start::Range(range) => {
|
|
198
|
+
*range = LeafRange { start: 0, end: 1 };
|
|
199
|
+
self.buffered = 1;
|
|
200
|
+
}
|
|
201
|
+
Start::Root(slot) => {
|
|
202
|
+
*slot = Slot {
|
|
203
|
+
node: Node::ROOT,
|
|
204
|
+
state: State::Queued,
|
|
205
|
+
};
|
|
206
|
+
}
|
|
207
|
+
}
|
|
208
|
+
Ok(Begun::Diverged)
|
|
209
|
+
}
|
|
210
|
+
|
|
211
|
+
/// Hand out the next node whose child digests are wanted, deepest first.
|
|
212
|
+
/// `None` when nothing is queued: either answers or drained output are
|
|
213
|
+
/// awaited, or the survey is finished.
|
|
214
|
+
pub fn next_request(&mut self) -> Option<Request> {
|
|
215
|
+
// Idle: nothing is queued, and no session to name.
|
|
216
|
+
let session = self.open?.session;
|
|
217
|
+
// Deepest first, then leftmost; on a tie the earlier slot stays.
|
|
218
|
+
let slot = self
|
|
219
|
+
.slots
|
|
220
|
+
.iter_mut()
|
|
221
|
+
.filter(|slot| slot.state == State::Queued)
|
|
222
|
+
.reduce(|held, slot| {
|
|
223
|
+
if (slot.node.level, held.node.index) > (held.node.level, slot.node.index) {
|
|
224
|
+
slot
|
|
225
|
+
} else {
|
|
226
|
+
held
|
|
227
|
+
}
|
|
228
|
+
})?;
|
|
229
|
+
slot.state = State::InFlight;
|
|
230
|
+
Some(Request::issued(session, slot.node))
|
|
231
|
+
}
|
|
232
|
+
|
|
233
|
+
/// Supply both trees' child digests for an in-flight node, in child order.
|
|
234
|
+
///
|
|
235
|
+
/// # Errors
|
|
236
|
+
/// [`Refusal::Stale`] (a request of another session or another survey's
|
|
237
|
+
/// scope), [`Refusal::Unexpected`], [`Refusal::VersionChanged`],
|
|
238
|
+
/// [`Refusal::Arity`] or [`Refusal::Inconsistent`]; the survey is unchanged
|
|
239
|
+
/// and the node stays in flight.
|
|
240
|
+
pub fn answer<D: PartialEq>(
|
|
241
|
+
&mut self,
|
|
242
|
+
request: Request,
|
|
243
|
+
versions: Versions,
|
|
244
|
+
local: &[D],
|
|
245
|
+
remote: &[D],
|
|
246
|
+
) -> Result<(), Refusal> {
|
|
247
|
+
let (open, slot) = self.in_flight_slot(request)?;
|
|
248
|
+
if versions != open.versions {
|
|
249
|
+
return Err(Refusal::VersionChanged);
|
|
250
|
+
}
|
|
251
|
+
let fanout = usize::from(open.schema.fanout());
|
|
252
|
+
if local.len() != fanout || remote.len() != fanout {
|
|
253
|
+
return Err(Refusal::Arity);
|
|
254
|
+
}
|
|
255
|
+
let mut mask = 0_u64;
|
|
256
|
+
// `bit < fanout <= MAX_FANOUT == 64`, so the shift stays in range.
|
|
257
|
+
for (bit, (ours, theirs)) in local.iter().zip(remote).enumerate() {
|
|
258
|
+
if ours != theirs {
|
|
259
|
+
mask |= 1 << bit;
|
|
260
|
+
}
|
|
261
|
+
}
|
|
262
|
+
if mask == 0 {
|
|
263
|
+
return Err(Refusal::Inconsistent);
|
|
264
|
+
}
|
|
265
|
+
slot.state = State::Expanded(mask);
|
|
266
|
+
self.settle(open.schema);
|
|
267
|
+
Ok(())
|
|
268
|
+
}
|
|
269
|
+
|
|
270
|
+
/// Put an in-flight node back in the queue, for a request that was lost.
|
|
271
|
+
///
|
|
272
|
+
/// # Errors
|
|
273
|
+
/// [`Refusal::Stale`] when the request belongs to another session or
|
|
274
|
+
/// another survey's scope, or no session is open; [`Refusal::Unexpected`]
|
|
275
|
+
/// when the node is not in flight; the survey is unchanged.
|
|
276
|
+
pub fn requeue(&mut self, request: Request) -> Result<(), Refusal> {
|
|
277
|
+
let (_, slot) = self.in_flight_slot(request)?;
|
|
278
|
+
slot.state = State::Queued;
|
|
279
|
+
Ok(())
|
|
280
|
+
}
|
|
281
|
+
|
|
282
|
+
/// Put every in-flight node back in the queue, for a dropped connection.
|
|
283
|
+
/// Returns how many were requeued.
|
|
284
|
+
pub fn requeue_all(&mut self) -> usize {
|
|
285
|
+
let mut moved = 0;
|
|
286
|
+
for slot in &mut self.slots {
|
|
287
|
+
if slot.state == State::InFlight {
|
|
288
|
+
slot.state = State::Queued;
|
|
289
|
+
// At most `P` slots, so this cannot overflow.
|
|
290
|
+
moved += 1;
|
|
291
|
+
}
|
|
292
|
+
}
|
|
293
|
+
moved
|
|
294
|
+
}
|
|
295
|
+
|
|
296
|
+
/// Take the lowest buffered leaf range. Taking makes room, so nodes held
|
|
297
|
+
/// back by a full buffer move on.
|
|
298
|
+
pub fn pop_range(&mut self) -> Option<LeafRange> {
|
|
299
|
+
let mut one = [EMPTY];
|
|
300
|
+
(self.drain(&mut one) == 1).then_some(one[0])
|
|
301
|
+
}
|
|
302
|
+
|
|
303
|
+
/// Move buffered ranges into the front of `out`, lowest first, until it is
|
|
304
|
+
/// full or no range is left, then let held-back nodes use the room. Returns
|
|
305
|
+
/// how many were written; those are sorted and do not touch each other, and
|
|
306
|
+
/// the rest of `out` is untouched.
|
|
307
|
+
pub fn drain(&mut self, out: &mut [LeafRange]) -> usize {
|
|
308
|
+
let Some(live) = self.out.get_mut(..self.buffered) else {
|
|
309
|
+
debug_assert!(false, "buffered never exceeds O");
|
|
310
|
+
return 0;
|
|
311
|
+
};
|
|
312
|
+
let mut taken = 0;
|
|
313
|
+
for (slot, range) in out.iter_mut().zip(live.iter()) {
|
|
314
|
+
*slot = *range;
|
|
315
|
+
taken += 1;
|
|
316
|
+
}
|
|
317
|
+
// The zip bounds `taken <= live.len() == buffered`, so the shift's
|
|
318
|
+
// range is in bounds and the subtraction cannot wrap.
|
|
319
|
+
live.copy_within(taken.., 0);
|
|
320
|
+
self.buffered -= taken;
|
|
321
|
+
// Taking makes room; an idle survey buffers nothing and holds nothing.
|
|
322
|
+
if taken > 0
|
|
323
|
+
&& let Some(open) = self.open
|
|
324
|
+
{
|
|
325
|
+
self.settle(open.schema);
|
|
326
|
+
}
|
|
327
|
+
taken
|
|
328
|
+
}
|
|
329
|
+
|
|
330
|
+
/// Drop the session. Outstanding requests become stale and buffered ranges
|
|
331
|
+
/// are discarded. Never refuses: [`Aborting::AlreadyIdle`] when no session
|
|
332
|
+
/// was open, which is the effect asked for already holding, and nothing changed.
|
|
333
|
+
pub const fn abort(&mut self) -> Aborting {
|
|
334
|
+
if self.open.is_none() {
|
|
335
|
+
return Aborting::AlreadyIdle;
|
|
336
|
+
}
|
|
337
|
+
self.open = None;
|
|
338
|
+
self.slots = [FREE; P];
|
|
339
|
+
self.buffered = 0;
|
|
340
|
+
Aborting::Aborted
|
|
341
|
+
}
|
|
342
|
+
|
|
343
|
+
/// Where the survey stands.
|
|
344
|
+
#[must_use]
|
|
345
|
+
pub fn phase(&self) -> Phase {
|
|
346
|
+
if self.open.is_none() {
|
|
347
|
+
Phase::Idle
|
|
348
|
+
} else if self.pending() > 0 {
|
|
349
|
+
Phase::Surveying
|
|
350
|
+
} else if self.buffered > 0 {
|
|
351
|
+
Phase::Draining
|
|
352
|
+
} else {
|
|
353
|
+
Phase::Complete
|
|
354
|
+
}
|
|
355
|
+
}
|
|
356
|
+
|
|
357
|
+
/// Id of the latest session issued in this scope; its requests carry it.
|
|
358
|
+
/// `None` while no session was ever begun.
|
|
359
|
+
///
|
|
360
|
+
/// It is the latest session, open or not: [`phase`](Self::phase) says
|
|
361
|
+
/// whether one is open. After [`abort`](Self::abort) it still answers the
|
|
362
|
+
/// aborted session, and a [`resumed`](Self::resumed) survey, idle until
|
|
363
|
+
/// its next `begin`, answers the last session issued before the restart
|
|
364
|
+
/// (`None` when it was resumed from a count of zero).
|
|
365
|
+
#[must_use]
|
|
366
|
+
pub const fn session(&self) -> Option<u64> {
|
|
367
|
+
self.issuer.last()
|
|
368
|
+
}
|
|
369
|
+
|
|
370
|
+
/// Sessions issued in this scope so far, the open one included: the count
|
|
371
|
+
/// to save for [`resumed`](Self::resumed).
|
|
372
|
+
#[must_use]
|
|
373
|
+
pub const fn issued(&self) -> u64 {
|
|
374
|
+
self.issuer.issued()
|
|
375
|
+
}
|
|
376
|
+
|
|
377
|
+
/// The scope carried by every session, and so every request, of this survey.
|
|
378
|
+
#[must_use]
|
|
379
|
+
pub const fn scope(&self) -> u16 {
|
|
380
|
+
self.issuer.scope()
|
|
381
|
+
}
|
|
382
|
+
|
|
383
|
+
/// Schema of the open session.
|
|
384
|
+
#[must_use]
|
|
385
|
+
pub const fn schema(&self) -> Option<Schema> {
|
|
386
|
+
match self.open {
|
|
387
|
+
Some(open) => Some(open.schema),
|
|
388
|
+
None => None,
|
|
389
|
+
}
|
|
390
|
+
}
|
|
391
|
+
|
|
392
|
+
/// Tree versions pinned by the open session.
|
|
393
|
+
#[must_use]
|
|
394
|
+
pub const fn versions(&self) -> Option<Versions> {
|
|
395
|
+
match self.open {
|
|
396
|
+
Some(open) => Some(open.versions),
|
|
397
|
+
None => None,
|
|
398
|
+
}
|
|
399
|
+
}
|
|
400
|
+
|
|
401
|
+
/// Nodes held: queued, in flight or partly expanded. Never above `P`.
|
|
402
|
+
#[must_use]
|
|
403
|
+
pub fn pending(&self) -> usize {
|
|
404
|
+
self.slots
|
|
405
|
+
.iter()
|
|
406
|
+
.filter(|slot| slot.state != State::Free)
|
|
407
|
+
.count()
|
|
408
|
+
}
|
|
409
|
+
|
|
410
|
+
/// Nodes requested and not yet answered.
|
|
411
|
+
#[must_use]
|
|
412
|
+
pub fn in_flight(&self) -> usize {
|
|
413
|
+
self.count(State::InFlight)
|
|
414
|
+
}
|
|
415
|
+
|
|
416
|
+
/// Leaf ranges waiting to be taken. Never above `O`.
|
|
417
|
+
#[must_use]
|
|
418
|
+
pub const fn buffered(&self) -> usize {
|
|
419
|
+
self.buffered
|
|
420
|
+
}
|
|
421
|
+
|
|
422
|
+
/// Origin first: a request whose session this survey's issuer never
|
|
423
|
+
/// handed out names nothing here, whatever node it carries.
|
|
424
|
+
fn live(&self, request: Request) -> Option<Open> {
|
|
425
|
+
if !self.issuer.owns(request.session()) {
|
|
426
|
+
return None;
|
|
427
|
+
}
|
|
428
|
+
self.open.filter(|open| open.session == request.session())
|
|
429
|
+
}
|
|
430
|
+
|
|
431
|
+
/// The open session and the slot of the request's node, which must be in
|
|
432
|
+
/// flight: [`Refusal::Stale`] first, then [`Refusal::Unexpected`].
|
|
433
|
+
fn in_flight_slot(&mut self, request: Request) -> Result<(Open, &mut Slot), Refusal> {
|
|
434
|
+
let open = self.live(request).ok_or(Refusal::Stale)?;
|
|
435
|
+
let node = request.node();
|
|
436
|
+
let slot = self
|
|
437
|
+
.slots
|
|
438
|
+
.iter_mut()
|
|
439
|
+
.find(|slot| slot.state == State::InFlight && slot.node == node)
|
|
440
|
+
.ok_or(Refusal::Unexpected)?;
|
|
441
|
+
Ok((open, slot))
|
|
442
|
+
}
|
|
443
|
+
|
|
444
|
+
fn count(&self, state: State) -> usize {
|
|
445
|
+
self.slots.iter().filter(|s| s.state == state).count()
|
|
446
|
+
}
|
|
447
|
+
|
|
448
|
+
fn free(&self) -> usize {
|
|
449
|
+
self.count(State::Free)
|
|
450
|
+
}
|
|
451
|
+
|
|
452
|
+
/// Move differing children out of answered nodes for as long as the
|
|
453
|
+
/// pending slots and the output buffer allow.
|
|
454
|
+
fn settle(&mut self, schema: Schema) {
|
|
455
|
+
loop {
|
|
456
|
+
let mut progressed = false;
|
|
457
|
+
for slot in 0..P {
|
|
458
|
+
progressed |= self.spill(slot, schema);
|
|
459
|
+
}
|
|
460
|
+
if !progressed {
|
|
461
|
+
break;
|
|
462
|
+
}
|
|
463
|
+
}
|
|
464
|
+
}
|
|
465
|
+
|
|
466
|
+
/// Overwrite the slot at `slot`. Every caller read that index with
|
|
467
|
+
/// `get` first and `P` is fixed, so the slot is always there.
|
|
468
|
+
fn put(&mut self, slot: usize, value: Slot) {
|
|
469
|
+
if let Some(held) = self.slots.get_mut(slot) {
|
|
470
|
+
*held = value;
|
|
471
|
+
}
|
|
472
|
+
}
|
|
473
|
+
|
|
474
|
+
fn spill(&mut self, slot: usize, schema: Schema) -> bool {
|
|
475
|
+
let Some(&Slot {
|
|
476
|
+
node,
|
|
477
|
+
state: State::Expanded(mut mask),
|
|
478
|
+
}) = self.slots.get(slot)
|
|
479
|
+
else {
|
|
480
|
+
return false;
|
|
481
|
+
};
|
|
482
|
+
// Only nodes above the leaf level are ever queued, so an expanded
|
|
483
|
+
// node has `node.level < depth <= u8::MAX` and `node.index <
|
|
484
|
+
// fanout^level`: the child level fits a `u8`, and every child index
|
|
485
|
+
// up to `base + fanout` is at most `fanout^depth`, which `Schema::new`
|
|
486
|
+
// proved fits a `u64`.
|
|
487
|
+
let level = node.level + 1;
|
|
488
|
+
let base = node.index * u64::from(schema.fanout());
|
|
489
|
+
let mut progressed = false;
|
|
490
|
+
while mask != 0 {
|
|
491
|
+
let low = mask.trailing_zeros();
|
|
492
|
+
if level == schema.depth() {
|
|
493
|
+
// Bit `low` is set, so `1 <= run <= 64 - low` and the shift
|
|
494
|
+
// below is at most 63.
|
|
495
|
+
let run = (mask >> low).trailing_ones();
|
|
496
|
+
let start = base + u64::from(low);
|
|
497
|
+
if !self.buffer(start, start + u64::from(run)) {
|
|
498
|
+
break;
|
|
499
|
+
}
|
|
500
|
+
mask &= !((u64::MAX >> (u64::BITS - run)) << low);
|
|
501
|
+
} else {
|
|
502
|
+
let child = Slot {
|
|
503
|
+
node: Node {
|
|
504
|
+
level,
|
|
505
|
+
index: base + u64::from(low),
|
|
506
|
+
},
|
|
507
|
+
state: State::Queued,
|
|
508
|
+
};
|
|
509
|
+
if mask.is_power_of_two() {
|
|
510
|
+
// The last child inherits its parent's slot.
|
|
511
|
+
self.put(slot, child);
|
|
512
|
+
return true;
|
|
513
|
+
}
|
|
514
|
+
// Keep enough free slots to walk one path from this child down
|
|
515
|
+
// to the leaves, so some node can always finish and free a slot.
|
|
516
|
+
// This arm has `level < depth`, so the reserve cannot wrap.
|
|
517
|
+
let reserve = usize::from(schema.depth() - 1 - level);
|
|
518
|
+
let free = self.free();
|
|
519
|
+
let Some(empty) = self
|
|
520
|
+
.slots
|
|
521
|
+
.iter_mut()
|
|
522
|
+
.find(|slot| slot.state == State::Free && slot.node == Node::ROOT)
|
|
523
|
+
else {
|
|
524
|
+
break;
|
|
525
|
+
};
|
|
526
|
+
// `empty` is one of the `free` slots, so after taking it
|
|
527
|
+
// `free - 1` remain: keep going only while that covers `reserve`.
|
|
528
|
+
if free <= reserve {
|
|
529
|
+
break;
|
|
530
|
+
}
|
|
531
|
+
*empty = child;
|
|
532
|
+
// `mask != 0` inside the loop, so this clears its lowest bit.
|
|
533
|
+
mask &= mask - 1;
|
|
534
|
+
}
|
|
535
|
+
progressed = true;
|
|
536
|
+
}
|
|
537
|
+
// The parent's own slot is never the free one taken above (it is
|
|
538
|
+
// expanded), so it still holds `node`.
|
|
539
|
+
self.put(
|
|
540
|
+
slot,
|
|
541
|
+
if mask == 0 {
|
|
542
|
+
FREE
|
|
543
|
+
} else {
|
|
544
|
+
Slot {
|
|
545
|
+
node,
|
|
546
|
+
state: State::Expanded(mask),
|
|
547
|
+
}
|
|
548
|
+
},
|
|
549
|
+
);
|
|
550
|
+
progressed
|
|
551
|
+
}
|
|
552
|
+
}
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
[package]
|
|
2
|
+
name = "merkle_machines_native"
|
|
3
|
+
version = "0.1.7"
|
|
4
|
+
edition.workspace = true
|
|
5
|
+
rust-version.workspace = true
|
|
6
|
+
license.workspace = true
|
|
7
|
+
publish = false
|
|
8
|
+
|
|
9
|
+
[lib]
|
|
10
|
+
crate-type = ["cdylib"]
|
|
11
|
+
test = false
|
|
12
|
+
doctest = false
|
|
13
|
+
|
|
14
|
+
[dependencies]
|
|
15
|
+
merkle-machines.workspace = true
|
|
16
|
+
magnus.workspace = true
|
|
17
|
+
rb-sys.workspace = true
|
|
18
|
+
matryona-machines = { workspace = true, features = ["ruby"] }
|
|
19
|
+
|
|
20
|
+
[lints]
|
|
21
|
+
workspace = true
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
require 'mkmf'
|
|
3
|
+
require 'pathname'
|
|
4
|
+
require 'rb_sys/mkmf'
|
|
5
|
+
|
|
6
|
+
create_rust_makefile('merkle_machines_native/merkle_machines_native') do |e|
|
|
7
|
+
e.ext_dir = Pathname(__dir__).relative_path_from(Pathname(Dir.pwd)).to_s
|
|
8
|
+
e.profile = ENV.fetch('RB_SYS_CARGO_PROFILE', :release).to_sym
|
|
9
|
+
end
|
|
10
|
+
makefile = File.join(Dir.pwd, 'Makefile')
|
|
11
|
+
File.write(makefile, File.read(makefile).gsub(
|
|
12
|
+
/^RB_SYS_CARGO_MANIFEST_DIR \?=.*$/,
|
|
13
|
+
"RB_SYS_CARGO_MANIFEST_DIR ?= #{File.expand_path(__dir__)}"
|
|
14
|
+
))
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
//! Private native entry point.
|
|
2
|
+
use crate::{
|
|
3
|
+
MAX_FANOUT, OUTPUT_CAPACITY, PENDING_CAPACITY, Survey, check_node, check_request, check_schema,
|
|
4
|
+
check_versions, request_scope, schema_compatible, schema_leaves, schema_span, schema_width,
|
|
5
|
+
};
|
|
6
|
+
use magnus::{Error, Ruby, function, method, prelude::*};
|
|
7
|
+
use matryona_machines::ruby::Class;
|
|
8
|
+
|
|
9
|
+
#[magnus::init]
|
|
10
|
+
fn init(ruby: &Ruby) -> Result<(), Error> {
|
|
11
|
+
let module = ruby.define_module("MerkleMachines")?;
|
|
12
|
+
crate::values::resolve(ruby, module)?;
|
|
13
|
+
module.const_set("PENDING_CAPACITY", PENDING_CAPACITY)?;
|
|
14
|
+
module.const_set("OUTPUT_CAPACITY", OUTPUT_CAPACITY)?;
|
|
15
|
+
module.const_set("MAX_FANOUT", MAX_FANOUT)?;
|
|
16
|
+
module.define_singleton_method("check_schema", function!(check_schema, 4))?;
|
|
17
|
+
module.define_singleton_method("check_node", function!(check_node, 2))?;
|
|
18
|
+
module.define_singleton_method("check_versions", function!(check_versions, 2))?;
|
|
19
|
+
module.define_singleton_method("check_request", function!(check_request, 2))?;
|
|
20
|
+
module.define_singleton_method("schema_leaves", function!(schema_leaves, 1))?;
|
|
21
|
+
module.define_singleton_method("schema_width", function!(schema_width, 2))?;
|
|
22
|
+
module.define_singleton_method("schema_span", function!(schema_span, 2))?;
|
|
23
|
+
module.define_singleton_method("schema_compatible", function!(schema_compatible, 2))?;
|
|
24
|
+
module.define_singleton_method("request_scope", function!(request_scope, 1))?;
|
|
25
|
+
let class = module.define_class("Survey", ruby.class_object())?;
|
|
26
|
+
class.undef_default_alloc_func();
|
|
27
|
+
class.define_singleton_method("new", function!(Survey::new, 0))?;
|
|
28
|
+
class.define_singleton_method("with_scope", function!(Survey::with_scope, 1))?;
|
|
29
|
+
class.define_singleton_method("resumed", function!(Survey::resumed, 2))?;
|
|
30
|
+
class.define_method("scope", method!(Survey::scope, 0))?;
|
|
31
|
+
class.define_method("issued", method!(Survey::issued, 0))?;
|
|
32
|
+
class.define_method("begin", method!(Survey::begin, 5))?;
|
|
33
|
+
class.define_method("next_request", method!(Survey::next_request, 0))?;
|
|
34
|
+
class.define_method("answer", method!(Survey::answer, 4))?;
|
|
35
|
+
class.define_method("requeue", method!(Survey::requeue, 1))?;
|
|
36
|
+
class.define_method("pop_range", method!(Survey::pop_range, 0))?;
|
|
37
|
+
class.define_method("drain", method!(Survey::drain, 1))?;
|
|
38
|
+
class.define_private_method("native_schema", method!(Survey::schema, 0))?;
|
|
39
|
+
class.define_private_method("native_versions", method!(Survey::versions, 0))?;
|
|
40
|
+
class.define_method("requeue_all", method!(Survey::requeue_all, 0))?;
|
|
41
|
+
class.define_method("abort", method!(Survey::abort, 0))?;
|
|
42
|
+
class.define_method("phase", method!(Survey::phase, 0))?;
|
|
43
|
+
class.define_method("session", method!(Survey::session, 0))?;
|
|
44
|
+
class.define_method("pending", method!(Survey::pending, 0))?;
|
|
45
|
+
class.define_method("in_flight", method!(Survey::in_flight, 0))?;
|
|
46
|
+
class.define_method("buffered", method!(Survey::buffered, 0))?;
|
|
47
|
+
// `new`, `with_scope` and `resumed` answer a Survey: its class is fixed
|
|
48
|
+
// now, so no conversion runs `Object.const_get` later.
|
|
49
|
+
Class::pin_wrapped::<Survey>(ruby, class).map(drop)
|
|
50
|
+
}
|