Skip to content

Commit bc512d8

Browse files
committed
refactor: Rename audit_trails module to main and update locking configuration structure
1 parent c2b11bd commit bc512d8

File tree

2 files changed

+92
-35
lines changed

2 files changed

+92
-35
lines changed

audit-trails-move/sources/audit_trails.move

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
/// references its predecessor, ensuring verifiable continuity and integrity.
88
///
99
/// Records are addressed by trail_id + sequence_number
10-
module audit_trails::audit_trails;
10+
module audit_trails::main;
1111

1212
use audit_trails::capabilities::{Self, Capability};
1313
use audit_trails::locking::{Self, LockingConfig};
@@ -47,7 +47,7 @@ public struct AuditTrail<D: store + copy> has key, store {
4747
/// Deletion locking rules
4848
locking_config: LockingConfig,
4949
/// Role name -> set of permissions (TODO: implement)
50-
permissions: VecMap<String, VecSet<Permission>>,
50+
roles: VecMap<String, VecSet<Permission>>,
5151
/// Set at creation, cannot be changed
5252
immutable_metadata: TrailImmutableMetadata,
5353
/// Can be updated by holders of MetadataUpdate permission
@@ -140,7 +140,7 @@ public fun create<D: store + copy>(
140140
record_count,
141141
records,
142142
locking_config,
143-
permissions: vec_map::empty(),
143+
roles: vec_map::empty(),
144144
immutable_metadata: trail_metadata,
145145
updatable_metadata,
146146
issued_capabilities: iota::vec_set::empty(),

audit-trails-move/sources/locking.move

Lines changed: 89 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -2,102 +2,159 @@
22
// SPDX-License-Identifier: Apache-2.0
33

44
/// 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).
85
module audit_trails::locking;
96

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 {
129
/// Records locked for N seconds after creation
1310
time_window_seconds: Option<u64>,
1411
/// Last N records are always locked
1512
count_window: Option<u64>,
1613
}
1714

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 =====
1922

20-
/// Create a new locking configuration
23+
/// Create a new locking window
2124
///
2225
/// - `time_window_seconds`: Records are locked for N seconds after creation (None = no time lock)
2326
/// - `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 }
2660
}
2761

2862
/// Create a locking config with no restrictions
2963
public fun none(): LockingConfig {
3064
LockingConfig {
31-
time_window_seconds: option::none(),
32-
count_window: option::none(),
65+
delete_record_lock: window_none(),
3366
}
3467
}
3568

36-
/// Create a time-based locking config
69+
/// Create a locking config with time-based record deletion lock
3770
public fun time_based(seconds: u64): LockingConfig {
3871
LockingConfig {
39-
time_window_seconds: option::some(seconds),
40-
count_window: option::none(),
72+
delete_record_lock: window_time_based(seconds),
4173
}
4274
}
4375

44-
/// Create a count-based locking config
76+
/// Create a locking config with count-based record deletion lock
4577
public fun count_based(count: u64): LockingConfig {
4678
LockingConfig {
47-
time_window_seconds: option::none(),
48-
count_window: option::some(count),
79+
delete_record_lock: window_count_based(count),
4980
}
5081
}
5182

52-
// ===== Getters =====
83+
// ===== LockingWindow Getters =====
5384

5485
/// 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
5788
}
5889

5990
/// 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
6293
}
6394

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) =====
65103

66104
/// Check if a record is locked based on time window
67105
///
68106
/// 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()) {
71109
return false
72110
};
73111

74-
let time_window_ms = (*config.time_window_seconds.borrow()) * 1000;
112+
let time_window_ms = (*window.time_window_seconds.borrow()) * 1000;
75113
let record_age = current_time - record_timestamp;
76114
record_age < time_window_ms
77115
}
78116

79117
/// Check if a record is locked based on count window
80118
///
81119
/// 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()) {
84122
return false
85123
};
86124

87-
let count_window = *config.count_window.borrow();
125+
let count_window = *window.count_window.borrow();
88126

89127
let records_after = total_records - sequence_number - 1;
90128
records_after < count_window
91129
}
92130

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
94146
public fun is_locked(
95147
config: &LockingConfig,
96148
sequence_number: u64,
97149
record_timestamp: u64,
98150
total_records: u64,
99151
current_time: u64,
100152
): 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+
)
103160
}

0 commit comments

Comments
 (0)