tape-core 0.4.4

A decentralized object store using Solana.
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
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
use bytemuck::{Pod, Zeroable};
use crate::types::*;

#[derive(Debug, Clone, PartialEq, Eq)]
pub enum EpochScheduleError {
    EpochInPast,
    EpochTooFar,
    IndexOutOfBounds,
    StartNotAfterBase,
    EndNotAfterStart,
    RangeTooLarge,
    ExceedsFutureEpochs,
    Overflow,
    Underflow,
}

#[repr(C)]
#[derive(Clone, Copy, Debug, Default, PartialEq)]
pub struct EpochUsage {
    reserved: StorageUnits,
    paid: Coin<TAPE>,
}

impl EpochUsage {
    pub fn new(reserved: StorageUnits, paid: Coin<TAPE>) -> Self {
        Self { reserved, paid }
    }

    #[inline]
    pub fn reserved(&self) -> StorageUnits {
        self.reserved
    }

    #[inline]
    pub fn paid(&self) -> Coin<TAPE> {
        self.paid
    }
}

unsafe impl Zeroable for EpochUsage {}
unsafe impl Pod for EpochUsage {}

#[repr(C)]
#[derive(Clone, Copy, Debug, PartialEq)]
pub struct EpochSchedule<const N: usize> {
    /// The current epoch number for index 0 in the buffer.
    pub now: EpochNumber,

    /// The per-epoch usage as a contiguous ring buffer of size N.
    pub values: RingBuffer<EpochUsage, N>,
}

unsafe impl<const N: usize> Zeroable for EpochSchedule<N> {}
unsafe impl<const N: usize> Pod for EpochSchedule<N> {}

impl<const N: usize> EpochSchedule<N> {
    pub fn new() -> Self {
        Self::new_at(EpochNumber(0))
    }

    pub fn new_at(start_epoch: EpochNumber) -> Self {
        let front = (start_epoch.as_u64() as usize) % N;
        Self {
            values: RingBuffer::filled_zero_at(front),
            now: start_epoch,
        }
    }

    #[inline]
    pub fn current_epoch(&self) -> EpochNumber {
        self.now
    }

    /// Advance to the next epoch, returning the EpochUsage of the current epoch.
    pub fn advance_epoch(&mut self) -> EpochUsage {
        debug_assert!(self.values.len() == N);

        let current = self.values.front().copied().unwrap_or(EpochUsage::zeroed());

        // Push zeroed entry for the new future epoch
        self.values.push(EpochUsage::zeroed());

        // Advance the epoch number
        self.now.increment();

        current
    }

    /// Get the per-epoch scheduled usage/fees for the provided epoch.
    #[inline]
    pub fn get(&self, epoch: EpochNumber) -> Result<EpochUsage, EpochScheduleError> {
        debug_assert!(self.values.len() == N);

        if epoch < self.now {
            return Err(EpochScheduleError::EpochInPast);
        }
        if epoch >= EpochNumber(self.now.as_u64() + N as u64) {
            return Err(EpochScheduleError::EpochTooFar);
        }

        let index = (epoch - self.now).as_u64() as usize;
        self.values.get(index).copied().ok_or(EpochScheduleError::IndexOutOfBounds)
    }

    /// Reserve capacity and fees per-epoch in [start_epoch, end_epoch) (end exclusive).
    /// Adds `units` to reserved and `fee` to paid each epoch in the range.
    pub fn reserve_capacity(
        &mut self,
        units: StorageUnits,
        fee: Coin<TAPE>,
        start_epoch: EpochNumber,
        end_epoch: EpochNumber,
    ) -> Result<(), EpochScheduleError> {
        debug_assert!(self.values.len() == N);

        let (start_offset, end_offset) = get_offsets::<N>(self.now, start_epoch, end_epoch)?;

        for i in start_offset..end_offset {
            let entry = self.values
                .get_mut(i)
                .ok_or(EpochScheduleError::IndexOutOfBounds)?;

            entry.reserved = entry.reserved
                .checked_add(units)
                .ok_or(EpochScheduleError::Overflow)?;

            entry.paid = entry.paid
                .checked_add(fee)
                .ok_or(EpochScheduleError::Overflow)?;
        }
        Ok(())
    }

    /// Cancel previously reserved capacity and fees per-epoch in [start_epoch, end_epoch).
    /// Subtracts `units` from reserved and `fee` from paid each epoch in the range.
    pub fn cancel_capacity(
        &mut self,
        units: StorageUnits,
        fee: Coin<TAPE>,
        start_epoch: EpochNumber,
        end_epoch: EpochNumber,
    ) -> Result<(), EpochScheduleError> {
        debug_assert!(self.values.len() == N);

        let (start_offset, mut end_offset) = get_offsets::<N>(self.now, start_epoch, end_epoch)?;

        end_offset = end_offset.min(self.values.len());

        for i in start_offset..end_offset {
            let entry = self.values
                .get_mut(i)
                .ok_or(EpochScheduleError::IndexOutOfBounds)?;

            entry.reserved = entry.reserved
                .checked_sub(units)
                .ok_or(EpochScheduleError::Underflow)?;

            entry.paid = entry.paid
                .checked_sub(fee)
                .ok_or(EpochScheduleError::Underflow)?;
        }
        Ok(())
    }

    /// Check if there is capacity for additional reserved units in [start_epoch, end_epoch).
    /// Fees are not considered for capacity.
    pub fn has_capacity_for(
        &self,
        additional_units: StorageUnits,
        max_capacity: StorageUnits,
        start_epoch: EpochNumber,
        end_epoch: EpochNumber,
    ) -> bool {
        debug_assert!(self.values.len() == N);

        let start = start_epoch.as_u64();
        let end = end_epoch.as_u64();

        for i in start..end {
            let epoch = EpochNumber(i);
            match self.get(epoch) {
                Ok(entry) => {
                    let Some(new_used) = entry.reserved
                        .checked_add(additional_units) else {
                        return false;
                    };

                    if new_used > max_capacity {
                        return false;
                    }

                }
                Err(_) => return false,
            }
        }

        true
    }
}

impl<const N: usize> Default for EpochSchedule<N> {
    fn default() -> Self {
        Self::new()
    }
}

/// Get the start and end offsets in the usage buffer for the specified epoch range.
pub fn get_offsets<const N: usize>(
    base_epoch: EpochNumber,
    start_epoch: EpochNumber,
    end_epoch: EpochNumber,
) -> Result<(usize, usize), EpochScheduleError> {

    if start_epoch < base_epoch {
        return Err(EpochScheduleError::StartNotAfterBase);
    }

    if end_epoch <= start_epoch {
        return Err(EpochScheduleError::EndNotAfterStart);
    }

    // Window and horizon (end is exclusive)
    let epoch_count = (end_epoch - start_epoch).as_u64();
    let horizon = (end_epoch - base_epoch).as_u64();

    // Must be addressable within the ring capacity
    if epoch_count > N as u64 {
        return Err(EpochScheduleError::RangeTooLarge);
    }
    if horizon > N as u64 {
        return Err(EpochScheduleError::ExceedsFutureEpochs);
    }

    assert!(epoch_count >= 1);
    assert!(horizon >= epoch_count);

    // [start, end), end exclusive
    let start_offset = (start_epoch - base_epoch).as_u64() as usize;
    let end_offset = start_offset + epoch_count as usize;

    Ok((start_offset, end_offset))
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::types::{EpochNumber, StorageUnits};

    const N: usize = 5;

    // Helpers
    fn epoch(n: u64) -> EpochNumber { EpochNumber(n) }
    fn storage(n: u64) -> StorageUnits { StorageUnits::mb(n) }
    fn tape(n: u64) -> Coin<TAPE> { TAPE(n) }

    #[test]
    fn test_get_offsets_success() {
        let base = EpochNumber(0);
        let res = get_offsets::<N>(base, EpochNumber(0), EpochNumber(1));
        assert_eq!(res, Ok((0, 1)));

        let res = get_offsets::<N>(base, EpochNumber(2), EpochNumber(4));
        assert_eq!(res, Ok((2, 4)));

        // Max range
        let res = get_offsets::<N>(base, EpochNumber(0), EpochNumber(N as u64));
        assert_eq!(res, Ok((0, N)));

        // Max horizon with small range
        let res = get_offsets::<N>(base, EpochNumber(N as u64 - 1), EpochNumber(N as u64));
        assert_eq!(res, Ok((N - 1, N)));
    }

    #[test]
    fn test_get_offsets_errors() {
        let base = EpochNumber(10);

        // Start before base
        assert_eq!(
            get_offsets::<N>(base, EpochNumber(9), EpochNumber(11)),
            Err(EpochScheduleError::StartNotAfterBase)
        );

        // End not after start
        assert_eq!(
            get_offsets::<N>(base, EpochNumber(10), EpochNumber(10)),
            Err(EpochScheduleError::EndNotAfterStart)
        );
        assert_eq!(
            get_offsets::<N>(base, EpochNumber(11), EpochNumber(10)),
            Err(EpochScheduleError::EndNotAfterStart)
        );

        // Range too large
        assert_eq!(
            get_offsets::<N>(base, EpochNumber(10), EpochNumber(10 + N as u64 + 1)),
            Err(EpochScheduleError::RangeTooLarge)
        );
    }

    #[test]
    fn accounting_new() {
        let db: EpochSchedule<N> = EpochSchedule::new();
        assert_eq!(db.now, epoch(0));
        assert_eq!(db.values.len(), N);

        for i in 0..N {
            assert_eq!(
                db.get(epoch(i as u64)).unwrap(),
                EpochUsage::new(storage(0), tape(0))
            );
        }
    }

    #[test]
    fn accounting_get() {
        let db: EpochSchedule<N> = EpochSchedule::new();

        // Valid
        for i in 0..N as u64 {
            assert_eq!(
                db.get(epoch(i)).unwrap(),
                EpochUsage::new(storage(0), tape(0))
            );
        }

        // Errors
        assert_eq!(
            db.get(epoch(u64::MAX)),
            Err(EpochScheduleError::EpochTooFar)
        );
    }

    #[test]
    fn accounting_advance() {
        let mut db: EpochSchedule<N> = EpochSchedule::new();

        for _ in 0..10 {
            let current = db.advance_epoch();
            assert_eq!(current, EpochUsage::new(storage(0), tape(0)));
            assert_eq!(db.values.len(), N);
        }

        assert_eq!(db.now, epoch(10));
        for i in 0..N as u64 {
            assert_eq!(
                db.get(epoch(10 + i)).unwrap(),
                EpochUsage::new(storage(0), tape(0))
            );
        }
    }

    #[test]
    fn accounting_reserve_and_cancel() {
        let mut db: EpochSchedule<N> = EpochSchedule::new();
        let units = storage(100);
        let fee = tape(1000);

        // Reserve in epochs 1..3 (1,2)
        db.reserve_capacity(units, fee, epoch(1), epoch(3)).unwrap();

        let e0 = db.get(epoch(0)).unwrap();
        assert_eq!(e0.reserved(), storage(0));
        assert_eq!(e0.paid(), tape(0));

        let e1 = db.get(epoch(1)).unwrap();
        assert_eq!(e1.reserved(), units);
        assert_eq!(e1.paid(), fee);

        let e2 = db.get(epoch(2)).unwrap();
        assert_eq!(e2.reserved(), units);
        assert_eq!(e2.paid(), fee);

        let e3 = db.get(epoch(3)).unwrap();
        assert_eq!(e3.reserved(), storage(0));
        assert_eq!(e3.paid(), tape(0));

        // Cancel the same range
        db.cancel_capacity(units, fee, epoch(1), epoch(3)).unwrap();

        for i in 0..N as u64 {
            let e = db.get(epoch(i)).unwrap();
            assert_eq!(e.reserved(), storage(0));
            assert_eq!(e.paid(), tape(0));
        }
    }

    #[test]
    fn accounting_reserve_error_ranges() {
        let mut db: EpochSchedule<N> = EpochSchedule::new();
        let units = storage(100);
        let fee = tape(1000);

        // Invalid ranges
        assert_eq!(
            db.reserve_capacity(units, fee, epoch(0), epoch(0)),
            Err(EpochScheduleError::EndNotAfterStart)
        );
        assert_eq!(
            db.reserve_capacity(units, fee, epoch(0), epoch(N as u64 + 1)),
            Err(EpochScheduleError::RangeTooLarge)
        );
        assert_eq!(
            db.reserve_capacity(units, fee, epoch(N as u64), epoch(N as u64 + 1)),
            Err(EpochScheduleError::ExceedsFutureEpochs)
        );
    }

    #[test]
    fn accounting_reserve_overflow() {
        let mut db: EpochSchedule<N> = EpochSchedule::new();

        // Overflow via reserved
        let max_units = storage(u64::MAX);
        db.reserve_capacity(max_units, tape(0), epoch(0), epoch(1)).unwrap();
        assert_eq!(
            db.reserve_capacity(storage(1), tape(0), epoch(0), epoch(1)),
            Err(EpochScheduleError::Overflow)
        );

        // Overflow via paid
        let mut db2: EpochSchedule<N> = EpochSchedule::new();
        let max_fee = tape(u64::MAX);
        db2.reserve_capacity(storage(0), max_fee, epoch(0), epoch(1)).unwrap();
        assert_eq!(
            db2.reserve_capacity(storage(0), tape(1), epoch(0), epoch(1)),
            Err(EpochScheduleError::Overflow)
        );
    }

    #[test]
    fn accounting_cancel_underflow() {
        let mut db: EpochSchedule<N> = EpochSchedule::new();

        // Underflow via reserved
        assert_eq!(
            db.cancel_capacity(storage(1), tape(0), epoch(0), epoch(1)),
            Err(EpochScheduleError::Underflow)
        );

        // Underflow via paid
        assert_eq!(
            db.cancel_capacity(storage(0), tape(1), epoch(0), epoch(1)),
            Err(EpochScheduleError::Underflow)
        );

        // Invalid range
        assert_eq!(
            db.cancel_capacity(storage(1), tape(1), epoch(0), epoch(0)),
            Err(EpochScheduleError::EndNotAfterStart)
        );
    }

    #[test]
    fn accounting_capacity() {
        let mut db: EpochSchedule<N> = EpochSchedule::new();
        let max_cap = storage(200);
        let add_units = storage(100);

        // Initially all zero, should have capacity
        assert!(db.has_capacity_for(add_units, max_cap, epoch(0), epoch(N as u64)));

        // Reserve units without adding any fee
        db.reserve_capacity(add_units, tape(0), epoch(0), epoch(N as u64)).unwrap();

        // Now used=100, add 50 <= 200 ok; add 101 > 200 no
        assert!(db.has_capacity_for(storage(50), max_cap, epoch(0), epoch(N as u64)));
        assert!(!db.has_capacity_for(storage(101), max_cap, epoch(0), epoch(N as u64)));

        // Out of range
        assert!(!db.has_capacity_for(add_units, max_cap, epoch(0), epoch(N as u64 + 1)));
        assert!(!db.has_capacity_for(add_units, max_cap, epoch(u64::MAX - 1), epoch(u64::MAX)));

        // Overflow case
        assert!(!db.has_capacity_for(storage(u64::MAX), max_cap, epoch(0), epoch(1)));
    }

    #[test]
    fn accounting_advance_schedules() {
        let mut db: EpochSchedule<N> = EpochSchedule::new();
        let units = storage(100);
        let fee = tape(100);

        // Schedule both usage and fee on epochs 2..4
        db.reserve_capacity(units, fee, epoch(2), epoch(4)).unwrap();

        assert_eq!(db.get(epoch(0)).unwrap(), EpochUsage::new(storage(0), tape(0)));
        assert_eq!(db.get(epoch(1)).unwrap(), EpochUsage::new(storage(0), tape(0)));
        assert_eq!(db.get(epoch(2)).unwrap(), EpochUsage::new(units, fee));
        assert_eq!(db.get(epoch(3)).unwrap(), EpochUsage::new(units, fee));
        assert_eq!(db.get(epoch(4)).unwrap(), EpochUsage::new(storage(0), tape(0)));

        // Advance once: return zeros, now=1, new for 5 is zero
        let ret = db.advance_epoch();
        assert_eq!(ret, EpochUsage::new(storage(0), tape(0)));
        assert_eq!(db.now, epoch(1));
        assert_eq!(db.get(epoch(5)).unwrap(), EpochUsage::new(storage(0), tape(0)));

        // Advance again: return zeros, now=2
        let ret = db.advance_epoch();
        assert_eq!(ret, EpochUsage::new(storage(0), tape(0)));
        assert_eq!(db.now, epoch(2));

        // Advance again: return scheduled (old epoch 2), now=3
        let ret = db.advance_epoch();
        assert_eq!(ret, EpochUsage::new(units, fee));
        assert_eq!(db.now, epoch(3));
    }
}