neo_syscalls/
host_notifications.rs1use neo_types::{NeoArray, NeoString, NeoValue};
18
19#[derive(Debug, Clone)]
20pub struct RecordedNotification {
21 pub event: String,
22 pub state: Vec<NeoValue>,
23}
24
25#[cfg(not(target_arch = "wasm32"))]
26mod inner {
27 use super::*;
28 use once_cell::sync::Lazy;
29 use std::sync::Mutex;
30
31 static RECORDED: Lazy<Mutex<Vec<RecordedNotification>>> = Lazy::new(|| Mutex::new(Vec::new()));
32
33 pub fn record(event: &NeoString, state: &NeoArray<NeoValue>) {
34 let mut g = RECORDED.lock().expect("notification recorder poisoned");
35 g.push(RecordedNotification {
36 event: event.as_str().to_string(),
37 state: state.iter().cloned().collect(),
38 });
39 }
40
41 pub fn take() -> Vec<RecordedNotification> {
42 let mut g = RECORDED.lock().expect("notification recorder poisoned");
43 std::mem::take(&mut *g)
44 }
45
46 pub fn reset() {
47 let mut g = RECORDED.lock().expect("notification recorder poisoned");
48 g.clear();
49 }
50}
51
52#[cfg(not(target_arch = "wasm32"))]
55pub fn record(event: &NeoString, state: &NeoArray<NeoValue>) {
56 inner::record(event, state);
57}
58#[cfg(target_arch = "wasm32")]
59pub fn record(_event: &NeoString, _state: &NeoArray<NeoValue>) {
60 }
63
64pub fn take() -> Vec<RecordedNotification> {
66 #[cfg(not(target_arch = "wasm32"))]
67 {
68 inner::take()
69 }
70 #[cfg(target_arch = "wasm32")]
71 {
72 Vec::new()
73 }
74}
75
76pub fn reset() {
78 #[cfg(not(target_arch = "wasm32"))]
79 {
80 inner::reset()
81 }
82}
83
84#[cfg(test)]
85mod tests {
86 use super::*;
87 use crate::NeoByteString;
88
89 #[test]
90 fn record_and_take_round_trip() {
91 reset();
92 let evt = NeoString::from_str("Transfer");
93 let mut state = NeoArray::new();
94 state.push(NeoValue::from(crate::NeoInteger::new(100i32)));
95 state.push(NeoValue::from(crate::NeoBoolean::new(true)));
96 record(&evt, &state);
97 let got = take();
98 assert_eq!(got.len(), 1);
99 assert_eq!(got[0].event, "Transfer");
100 assert_eq!(got[0].state.len(), 2);
101 }
102
103 #[test]
104 fn bytestring_value_round_trip() {
105 let _ = NeoByteString::from_slice(b"hello");
107 }
108}