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
use std::collections::VecDeque;
use chacha20poly1305::{XChaCha20Poly1305, XNonce};
use freenet_stdlib::prelude::{
ApplicationMessage, DelegateContainer, DelegateContext, DelegateKey, InboundDelegateMsg,
MessageOrigin, OutboundDelegateMsg, Parameters,
};
use super::super::native_api::{self, DelegateContextEntry};
use super::super::secrets_store::UserSecretContext;
use super::super::{Runtime, RuntimeResult};
pub(crate) trait DelegateRuntimeInterface {
fn inbound_app_message(
&mut self,
key: &DelegateKey,
params: &Parameters,
origin: Option<&MessageOrigin>,
user_context: Option<&UserSecretContext>,
inbound: Vec<InboundDelegateMsg>,
) -> RuntimeResult<Vec<OutboundDelegateMsg>>;
fn register_delegate(
&mut self,
delegate: DelegateContainer,
cipher: XChaCha20Poly1305,
nonce: XNonce,
) -> RuntimeResult<()>;
fn unregister_delegate(&mut self, key: &DelegateKey) -> RuntimeResult<()>;
}
impl DelegateRuntimeInterface for Runtime {
fn inbound_app_message(
&mut self,
delegate_key: &DelegateKey,
params: &Parameters,
origin: Option<&MessageOrigin>,
user_context: Option<&UserSecretContext>,
inbound: Vec<InboundDelegateMsg>,
) -> RuntimeResult<Vec<OutboundDelegateMsg>> {
let mut results = Vec::with_capacity(inbound.len());
if inbound.is_empty() {
return Ok(results);
}
let (mut running, api_version) = self.prepare_delegate_call(params, delegate_key, 4096)?;
let instance_id = running.id;
tracing::debug!(
delegate_key = %delegate_key,
api_version = %api_version,
"Starting delegate execution"
);
// Context state maintained across process() calls.
//
// `self.delegate_contexts` persists the delegate's `ctx.write()` bytes
// across separate `inbound_app_message` invocations so that, e.g., the
// bytes a delegate writes when emitting `RequestUserInput` are still
// readable via `ctx.read()` when the executor re-enters with the
// matching `UserResponse`. Without this, the delegate hits "received
// UserResponse with no pending context" because the Vec only used to
// live for one `inbound_app_message` call. See
// `native_api::DelegateContextCache`.
//
// Amortised TTL sweep on every entry prevents the cache from holding
// bytes for prompts whose `UserResponse` never arrives (user
// dismisses, app crashes, network partition).
native_api::prune_expired_contexts(&self.delegate_contexts);
// Keep the inherited-origins map tidy. Mark this delegate as just-used
// so the cleanup keeps its entry, then drop entries for delegates that
// have gone unused long enough. See `InheritedOriginsEntry`.
native_api::touch_inherited_origin(delegate_key);
native_api::prune_expired_inherited_origins();
let mut context: Vec<u8> = self
.delegate_contexts
.get(delegate_key)
.map(|entry| entry.bytes.clone())
.unwrap_or_default();
// Process all messages, collecting the result.
// Cleanup happens after the loop regardless of success/failure.
let process_result: RuntimeResult<()> = (|| {
for msg in inbound {
// The wildcard arm at the bottom of this match exists
// solely because `InboundDelegateMsg` is `#[non_exhaustive]`
// (stdlib 0.6.0+); every currently-known variant is
// enumerated above. Re-listing them in a `pat | _` shape
// (as `wildcard_enum_match_arm` would prefer) is needless
// duplication that defeats the safety net the wildcard
// provides for future variants.
#[allow(clippy::wildcard_enum_match_arm)]
match msg {
InboundDelegateMsg::ApplicationMessage(ApplicationMessage {
payload,
processed,
..
}) => {
// clone kept — delegates read message-level context
let app_msg = InboundDelegateMsg::ApplicationMessage(
ApplicationMessage::new(payload)
.processed(processed)
.with_context(DelegateContext::new(context.clone())),
);
let (outbound, updated_context) = self.exec_inbound_with_env(
delegate_key,
params,
origin,
user_context,
&app_msg,
std::mem::take(&mut context),
&running.handle,
instance_id,
api_version,
)?;
context = updated_context;
let mut outbound_queue = VecDeque::from(outbound);
self.process_outbound(
delegate_key,
&running.handle,
instance_id,
params,
origin,
&mut outbound_queue,
&mut context,
&mut results,
)?;
}
InboundDelegateMsg::UserResponse(response) => {
let (outbound, updated_context) = self.exec_inbound_with_env(
delegate_key,
params,
origin,
user_context,
&InboundDelegateMsg::UserResponse(response),
std::mem::take(&mut context),
&running.handle,
instance_id,
api_version,
)?;
context = updated_context;
let mut outbound_queue = VecDeque::from(outbound);
self.process_outbound(
delegate_key,
&running.handle,
instance_id,
params,
origin,
&mut outbound_queue,
&mut context,
&mut results,
)?;
}
InboundDelegateMsg::GetContractResponse(response) => {
let (outbound, updated_context) = self.exec_inbound_with_env(
delegate_key,
params,
origin,
user_context,
&InboundDelegateMsg::GetContractResponse(response),
std::mem::take(&mut context),
&running.handle,
instance_id,
api_version,
)?;
context = updated_context;
let mut outbound_queue = VecDeque::from(outbound);
self.process_outbound(
delegate_key,
&running.handle,
instance_id,
params,
origin,
&mut outbound_queue,
&mut context,
&mut results,
)?;
}
msg @ (InboundDelegateMsg::PutContractResponse(_)
| InboundDelegateMsg::UpdateContractResponse(_)
| InboundDelegateMsg::SubscribeContractResponse(_)
| InboundDelegateMsg::ContractNotification(_)
| InboundDelegateMsg::DelegateMessage(_)) => {
let (outbound, updated_context) = self.exec_inbound_with_env(
delegate_key,
params,
origin,
user_context,
&msg,
std::mem::take(&mut context),
&running.handle,
instance_id,
api_version,
)?;
context = updated_context;
let mut outbound_queue = VecDeque::from(outbound);
self.process_outbound(
delegate_key,
&running.handle,
instance_id,
params,
origin,
&mut outbound_queue,
&mut context,
&mut results,
)?;
}
// `InboundDelegateMsg` is `#[non_exhaustive]` (stdlib
// 0.6.0+). Future variants are forwarded to the WASM
// through the same generic exec path so a delegate
// built against a newer stdlib can handle them; the
// host neither inspects nor classifies their payload.
other => {
let (outbound, updated_context) = self.exec_inbound_with_env(
delegate_key,
params,
origin,
user_context,
&other,
std::mem::take(&mut context),
&running.handle,
instance_id,
api_version,
)?;
context = updated_context;
let mut outbound_queue = VecDeque::from(outbound);
self.process_outbound(
delegate_key,
&running.handle,
instance_id,
params,
origin,
&mut outbound_queue,
&mut context,
&mut results,
)?;
}
}
}
Ok(())
})();
// Always clean up the WASM Instance, even on error.
self.drop_running_instance(&mut running);
process_result?;
// Persist the (possibly mutated) context so the next call into this
// delegate sees what `ctx.write()` left behind. Skip the insert when
// empty to keep the map sparse — `unwrap_or_default()` covers the
// unset case symmetrically on read.
//
// Drop contexts that exceed `DelegateContext::MAX_SIZE`: the wire
// format would assert on the next call when the runtime threads the
// bytes back through `DelegateContext::new(...)`. The `ctx.write()`
// host function has no size cap of its own, so a delegate with a
// bug — or one trying to wedge the runtime — can otherwise stash a
// value that would crash on the very next call. Treat oversize as
// "delegate misbehavior, forget it" rather than "crash the node."
if context.is_empty() {
self.delegate_contexts.remove(delegate_key);
} else if context.len() < freenet_stdlib::prelude::DelegateContext::MAX_SIZE {
self.delegate_contexts.insert(
delegate_key.clone(),
DelegateContextEntry {
bytes: context,
last_write: tokio::time::Instant::now(),
},
);
} else {
tracing::warn!(
delegate_key = %delegate_key,
bytes = context.len(),
max = freenet_stdlib::prelude::DelegateContext::MAX_SIZE,
"Delegate ctx.write() exceeded DelegateContext::MAX_SIZE; \
dropping the persisted context to avoid a crash on the next call"
);
self.delegate_contexts.remove(delegate_key);
}
tracing::debug!(
count = results.len(),
"Final results returned by inbound_app_message"
);
Ok(results)
}
#[inline]
fn register_delegate(
&mut self,
delegate: DelegateContainer,
cipher: XChaCha20Poly1305,
nonce: XNonce,
) -> RuntimeResult<()> {
self.secret_store
.register_delegate(delegate.key().clone(), cipher, nonce)?;
self.delegate_store.store_delegate(delegate)
}
#[inline]
fn unregister_delegate(&mut self, key: &DelegateKey) -> RuntimeResult<()> {
self.delegate_modules.lock().unwrap().remove(key);
// Drop persisted ctx.write() bytes so an unregistered delegate can't
// hold onto stale state if it's later re-registered.
self.delegate_contexts.remove(key);
self.delegate_store.remove_delegate(key)
}
}