forked from Vero-protocol/vero-core-contracts
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathevents.rs
More file actions
265 lines (221 loc) · 8.89 KB
/
Copy pathevents.rs
File metadata and controls
265 lines (221 loc) · 8.89 KB
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
#![allow(missing_docs)]
use soroban_sdk::{symbol_short, Address, Env};
// `task_id`/`weight` are validated as `u64` in validation.rs and can
// exceed `u32::MAX`. This guard fails to compile if either constant is
// ever redefined to a type that can't round-trip through the `u64`s below.
const _: () = {
let _: u64 = crate::validation::MAX_TASK_ID;
let _: u64 = crate::validation::MAX_WEIGHT_THRESHOLD;
};
/// Emits an event when a task reaches consensus.
/// `task_id`/`weight` are carried as full `u64` values (see #159) — no
/// packing/truncation, so ids and weights above `u32::MAX` stay intact.
pub fn emit_task_resolved(env: &Env, task_id: u64, total_weight: u64) {
env.events()
.publish((symbol_short!("resolved"),), (task_id, total_weight));
}
/// Emits an event when a guardian casts a weighted vote.
/// `task_id`/`weight` are carried as full `u64` values (see #159) — no
/// packing/truncation, so ids and weights above `u32::MAX` stay intact.
pub fn emit_weighted_vote(env: &Env, task_id: u64, guardian: &Address, weight: u64) {
env.events().publish(
(symbol_short!("wt_vote"),),
(guardian.clone(), task_id, weight),
);
}
/// Emits an event when the pause state is toggled.
pub fn emit_pause_toggled(env: &Env, paused: bool) {
env.events().publish((symbol_short!("paused"),), paused);
}
pub fn emit_reward_stream_started(env: &Env, task_id: u64, contributor: &Address) {
env.events()
.publish((symbol_short!("rw_start"),), (task_id, contributor.clone()));
}
pub fn emit_reward_stream_failed(env: &Env, task_id: u64, contributor: &Address) {
env.events()
.publish((symbol_short!("rw_fail"),), (task_id, contributor.clone()));
}
/// Emits an event when the circuit breaker trips and pauses the contract.
///
/// Event topic: `"cb_trip"` (circuit_breaker_triggered)
/// Event data: `failure_count`
pub fn emit_circuit_breaker_triggered(env: &Env, failure_count: u32) {
env.events()
.publish((symbol_short!("cb_trip"),), failure_count);
}
/// Emits an event when an authenticated observer reports a failure.
///
/// Event topic: `"cb_report"` (failure_reported)
/// Event data: `(reporter, new_failure_count)`
pub fn emit_failure_reported(env: &Env, reporter: &Address, failure_count: u32) {
env.events().publish(
(symbol_short!("cb_report"),),
(reporter.clone(), failure_count),
);
}
/// Emits an event when "trusted reporters only" mode is toggled.
///
/// Event topic: `"cb_trust"`
/// Event data: `(admin, enabled)`
pub fn emit_trusted_reporters_only_set(env: &Env, admin: &Address, enabled: bool) {
env.events()
.publish((symbol_short!("cb_trust"),), (admin.clone(), enabled));
}
pub fn emit_role_granted(env: &Env, caller: &Address, target: &Address, role: u8) {
// Pack role into u32 for Soroban SDK compatibility
env.events().publish(
(symbol_short!("role_gr"),),
(caller.clone(), target.clone(), role as u32),
);
}
pub fn emit_role_revoked(env: &Env, caller: &Address, target: &Address, role: u8) {
// Pack role into u32 for Soroban SDK compatibility
env.events().publish(
(symbol_short!("role_rv"),),
(caller.clone(), target.clone(), role as u32),
);
}
pub fn emit_task_cancelled(env: &Env, task_id: u64) {
env.events().publish((symbol_short!("cancel"),), task_id);
}
pub fn emit_task_purged(env: &Env, task_id: u64) {
env.events().publish((symbol_short!("purged"),), task_id);
}
pub fn emit_contract_initialized(env: &Env, admin: &Address) {
env.events()
.publish((symbol_short!("inited"),), (admin.clone(),));
}
pub fn emit_guardian_added(env: &Env, admin: &Address, guardian: &Address) {
env.events().publish(
(symbol_short!("gd_add"),),
(admin.clone(), guardian.clone()),
);
}
pub fn emit_guardian_removed(env: &Env, admin: &Address, guardian: &Address) {
env.events()
.publish((symbol_short!("gd_rm"),), (admin.clone(), guardian.clone()));
}
pub fn emit_reputation_set(env: &Env, admin: &Address, guardian: &Address, score: u64) {
env.events().publish(
(symbol_short!("rep_set"),),
(admin.clone(), guardian.clone(), score),
);
}
pub fn emit_tokens_locked(env: &Env, guardian: &Address, amount: i128) {
env.events()
.publish((symbol_short!("tk_lock"),), (guardian.clone(), amount));
}
pub fn emit_timelock_started(env: &Env, guardian: &Address) {
env.events()
.publish((symbol_short!("tm_start"),), (guardian.clone(),));
}
pub fn emit_tokens_unlocked(env: &Env, guardian: &Address, amount: i128) {
env.events()
.publish((symbol_short!("tk_unlk"),), (guardian.clone(), amount));
}
pub fn emit_emergency_recovery(env: &Env, admin: &Address, recipient: &Address, amount: i128) {
env.events().publish(
(symbol_short!("em_rec"),),
(admin.clone(), recipient.clone(), amount),
);
}
pub fn emit_guardian_resigned(env: &Env, guardian: &Address) {
env.events()
.publish((symbol_short!("gd_res"),), (guardian.clone(),));
}
pub fn emit_threshold_set(env: &Env, admin: &Address, threshold: u64) {
env.events()
.publish((symbol_short!("th_set"),), (admin.clone(), threshold));
}
pub fn emit_vault_set(env: &Env, admin: &Address, vault: &Address) {
env.events()
.publish((symbol_short!("vault"),), (admin.clone(), vault.clone()));
}
pub fn emit_task_registered(env: &Env, admin: &Address, task_id: u64) {
env.events()
.publish((symbol_short!("reg"),), (admin.clone(), task_id));
}
pub fn emit_task_archived(env: &Env, task_id: u64) {
env.events().publish((symbol_short!("archived"),), task_id);
}
pub fn emit_circuit_breaker_reset(env: &Env, admin: &Address) {
env.events()
.publish((symbol_short!("cb_rst"),), (admin.clone(),));
}
pub fn emit_contract_upgraded(env: &Env, admin: &Address, wasm_hash: &soroban_sdk::BytesN<32>) {
env.events().publish(
(symbol_short!("upgraded"),),
(admin.clone(), wasm_hash.clone()),
);
}
pub fn emit_upgrade_signers_set(env: &Env, signer_count: u32, threshold: u32) {
env.events()
.publish((symbol_short!("up_sig"),), (signer_count, threshold));
}
pub fn emit_upgrade_proposed(env: &Env, signer: &Address) {
env.events()
.publish((symbol_short!("up_prop"),), (signer.clone(),));
}
pub fn emit_upgrade_approved(env: &Env, signer: &Address, count: u32, threshold: u32) {
env.events().publish(
(symbol_short!("up_app"),),
(signer.clone(), count, threshold),
);
}
pub fn emit_upgrade_executed(env: &Env) {
env.events().publish((symbol_short!("up_exec"),), ());
}
pub fn emit_upgrade_cancelled(env: &Env) {
env.events().publish((symbol_short!("up_cncl"),), ());
}
pub fn emit_snapshot_recorded(env: &Env, timestamp: u64) {
env.events().publish((symbol_short!("snap"),), timestamp);
}
/// Emits an event when vault funds are successfully released.
pub fn emit_vault_release_success(env: &Env, task_id: u64) {
env.events().publish((symbol_short!("vault_ok"),), task_id);
}
/// Emits an event when vault release fails (but does not revert the transaction).
pub fn emit_vault_release_failed(env: &Env, task_id: u64) {
env.events().publish((symbol_short!("vault_err"),), task_id);
}
#[cfg(test)]
mod tests {
use super::*;
use soroban_sdk::testutils::{Address as _, Events as _};
use soroban_sdk::TryIntoVal;
// Regression test for #159: task_id/weight above u32::MAX must reach
// the event log unchanged instead of being silently truncated.
#[test]
fn large_task_id_and_weight_survive_resolved_event() {
let env = Env::default();
let contract_id = env.register_contract(None, crate::VeroContract);
let big_task_id: u64 = (u32::MAX as u64) + 12345;
let big_weight: u64 = (u32::MAX as u64) + 67890;
env.as_contract(&contract_id, || {
emit_task_resolved(&env, big_task_id, big_weight);
});
let published = env.events().all();
let (_contract, _topics, data) = published.last().unwrap();
let (task_id, weight): (u64, u64) = data.try_into_val(&env).unwrap();
assert_eq!(task_id, big_task_id);
assert_eq!(weight, big_weight);
}
#[test]
fn large_task_id_and_weight_survive_weighted_vote_event() {
let env = Env::default();
let contract_id = env.register_contract(None, crate::VeroContract);
let guardian = Address::generate(&env);
let big_task_id: u64 = (u32::MAX as u64) + 1;
let big_weight: u64 = (u32::MAX as u64) + 1;
env.as_contract(&contract_id, || {
emit_weighted_vote(&env, big_task_id, &guardian, big_weight);
});
let published = env.events().all();
let (_contract, _topics, data) = published.last().unwrap();
let (g, task_id, weight): (Address, u64, u64) = data.try_into_val(&env).unwrap();
assert_eq!(g, guardian);
assert_eq!(task_id, big_task_id);
assert_eq!(weight, big_weight);
}
}