|
2 | 2 | // SPDX-License-Identifier: Apache-2.0 |
3 | 3 |
|
4 | 4 | /// Locking configuration for audit trail records |
5 | | -/// |
6 | | -/// Controls when records can be deleted based on time window (records locked for N seconds) |
7 | | -/// or count window (last N records always locked). |
8 | 5 | module audit_trails::locking; |
9 | 6 |
|
10 | | -/// Controls when records can be deleted (time OR count based) |
11 | | -public struct LockingConfig has copy, drop, store { |
| 7 | +/// Defines a locking window (time OR count based) |
| 8 | +public struct LockingWindow has copy, drop, store { |
12 | 9 | /// Records locked for N seconds after creation |
13 | 10 | time_window_seconds: Option<u64>, |
14 | 11 | /// Last N records are always locked |
15 | 12 | count_window: Option<u64>, |
16 | 13 | } |
17 | 14 |
|
18 | | -// ===== Constructors ===== |
| 15 | +/// Top-level locking configuration for the audit trail |
| 16 | +public struct LockingConfig has copy, drop, store { |
| 17 | + /// Locking rules for record deletion |
| 18 | + delete_record_lock: LockingWindow, |
| 19 | +} |
| 20 | + |
| 21 | +// ===== LockingWindow Constructors ===== |
19 | 22 |
|
20 | | -/// Create a new locking configuration |
| 23 | +/// Create a new locking window |
21 | 24 | /// |
22 | 25 | /// - `time_window_seconds`: Records are locked for N seconds after creation (None = no time lock) |
23 | 26 | /// - `count_window`: Last N records are always locked (None = no count lock) |
24 | | -public fun new(time_window_seconds: Option<u64>, count_window: Option<u64>): LockingConfig { |
25 | | - LockingConfig { time_window_seconds, count_window } |
| 27 | +public fun new_window(time_window_seconds: Option<u64>, count_window: Option<u64>): LockingWindow { |
| 28 | + LockingWindow { time_window_seconds, count_window } |
| 29 | +} |
| 30 | + |
| 31 | +/// Create a locking window with no restrictions |
| 32 | +public fun window_none(): LockingWindow { |
| 33 | + LockingWindow { |
| 34 | + time_window_seconds: option::none(), |
| 35 | + count_window: option::none(), |
| 36 | + } |
| 37 | +} |
| 38 | + |
| 39 | +/// Create a time-based locking window |
| 40 | +public fun window_time_based(seconds: u64): LockingWindow { |
| 41 | + LockingWindow { |
| 42 | + time_window_seconds: option::some(seconds), |
| 43 | + count_window: option::none(), |
| 44 | + } |
| 45 | +} |
| 46 | + |
| 47 | +/// Create a count-based locking window |
| 48 | +public fun window_count_based(count: u64): LockingWindow { |
| 49 | + LockingWindow { |
| 50 | + time_window_seconds: option::none(), |
| 51 | + count_window: option::some(count), |
| 52 | + } |
| 53 | +} |
| 54 | + |
| 55 | +// ===== LockingConfig Constructors ===== |
| 56 | + |
| 57 | +/// Create a new locking configuration |
| 58 | +public fun new(delete_record_lock: LockingWindow): LockingConfig { |
| 59 | + LockingConfig { delete_record_lock } |
26 | 60 | } |
27 | 61 |
|
28 | 62 | /// Create a locking config with no restrictions |
29 | 63 | public fun none(): LockingConfig { |
30 | 64 | LockingConfig { |
31 | | - time_window_seconds: option::none(), |
32 | | - count_window: option::none(), |
| 65 | + delete_record_lock: window_none(), |
33 | 66 | } |
34 | 67 | } |
35 | 68 |
|
36 | | -/// Create a time-based locking config |
| 69 | +/// Create a locking config with time-based record deletion lock |
37 | 70 | public fun time_based(seconds: u64): LockingConfig { |
38 | 71 | LockingConfig { |
39 | | - time_window_seconds: option::some(seconds), |
40 | | - count_window: option::none(), |
| 72 | + delete_record_lock: window_time_based(seconds), |
41 | 73 | } |
42 | 74 | } |
43 | 75 |
|
44 | | -/// Create a count-based locking config |
| 76 | +/// Create a locking config with count-based record deletion lock |
45 | 77 | public fun count_based(count: u64): LockingConfig { |
46 | 78 | LockingConfig { |
47 | | - time_window_seconds: option::none(), |
48 | | - count_window: option::some(count), |
| 79 | + delete_record_lock: window_count_based(count), |
49 | 80 | } |
50 | 81 | } |
51 | 82 |
|
52 | | -// ===== Getters ===== |
| 83 | +// ===== LockingWindow Getters ===== |
53 | 84 |
|
54 | 85 | /// Get the time window in seconds (if set) |
55 | | -public fun time_window_seconds(config: &LockingConfig): &Option<u64> { |
56 | | - &config.time_window_seconds |
| 86 | +public fun time_window_seconds(window: &LockingWindow): &Option<u64> { |
| 87 | + &window.time_window_seconds |
57 | 88 | } |
58 | 89 |
|
59 | 90 | /// Get the count window (if set) |
60 | | -public fun count_window(config: &LockingConfig): &Option<u64> { |
61 | | - &config.count_window |
| 91 | +public fun count_window(window: &LockingWindow): &Option<u64> { |
| 92 | + &window.count_window |
62 | 93 | } |
63 | 94 |
|
64 | | -// ===== Locking Logic ===== |
| 95 | +// ===== LockingConfig Getters ===== |
| 96 | + |
| 97 | +/// Get the record deletion locking window |
| 98 | +public fun delete_record_lock(config: &LockingConfig): &LockingWindow { |
| 99 | + &config.delete_record_lock |
| 100 | +} |
| 101 | + |
| 102 | +// ===== Locking Logic (LockingWindow) ===== |
65 | 103 |
|
66 | 104 | /// Check if a record is locked based on time window |
67 | 105 | /// |
68 | 106 | /// Returns true if the record was created within the time window |
69 | | -public fun is_time_locked(config: &LockingConfig, record_timestamp: u64, current_time: u64): bool { |
70 | | - if (config.time_window_seconds.is_none()) { |
| 107 | +public fun is_time_locked(window: &LockingWindow, record_timestamp: u64, current_time: u64): bool { |
| 108 | + if (window.time_window_seconds.is_none()) { |
71 | 109 | return false |
72 | 110 | }; |
73 | 111 |
|
74 | | - let time_window_ms = (*config.time_window_seconds.borrow()) * 1000; |
| 112 | + let time_window_ms = (*window.time_window_seconds.borrow()) * 1000; |
75 | 113 | let record_age = current_time - record_timestamp; |
76 | 114 | record_age < time_window_ms |
77 | 115 | } |
78 | 116 |
|
79 | 117 | /// Check if a record is locked based on count window |
80 | 118 | /// |
81 | 119 | /// Returns true if the record is among the last N records |
82 | | -public fun is_count_locked(config: &LockingConfig, sequence_number: u64, total_records: u64): bool { |
83 | | - if (config.count_window.is_none()) { |
| 120 | +public fun is_count_locked(window: &LockingWindow, sequence_number: u64, total_records: u64): bool { |
| 121 | + if (window.count_window.is_none()) { |
84 | 122 | return false |
85 | 123 | }; |
86 | 124 |
|
87 | | - let count_window = *config.count_window.borrow(); |
| 125 | + let count_window = *window.count_window.borrow(); |
88 | 126 |
|
89 | 127 | let records_after = total_records - sequence_number - 1; |
90 | 128 | records_after < count_window |
91 | 129 | } |
92 | 130 |
|
93 | | -/// Check if a record is locked (either by time or count) |
| 131 | +/// Check if a record is locked by a window (either by time or count) |
| 132 | +public fun is_window_locked( |
| 133 | + window: &LockingWindow, |
| 134 | + sequence_number: u64, |
| 135 | + record_timestamp: u64, |
| 136 | + total_records: u64, |
| 137 | + current_time: u64, |
| 138 | +): bool { |
| 139 | + is_time_locked(window, record_timestamp, current_time) |
| 140 | + || is_count_locked(window, sequence_number, total_records) |
| 141 | +} |
| 142 | + |
| 143 | +// ===== Locking Logic (LockingConfig) ===== |
| 144 | + |
| 145 | +/// Check if a record is locked for deletion |
94 | 146 | public fun is_locked( |
95 | 147 | config: &LockingConfig, |
96 | 148 | sequence_number: u64, |
97 | 149 | record_timestamp: u64, |
98 | 150 | total_records: u64, |
99 | 151 | current_time: u64, |
100 | 152 | ): bool { |
101 | | - is_time_locked(config, record_timestamp, current_time) |
102 | | - || is_count_locked(config, sequence_number, total_records) |
| 153 | + is_window_locked( |
| 154 | + &config.delete_record_lock, |
| 155 | + sequence_number, |
| 156 | + record_timestamp, |
| 157 | + total_records, |
| 158 | + current_time, |
| 159 | + ) |
103 | 160 | } |
0 commit comments