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
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
//! Simplified configuration builder for the SDK.
use std::time::Duration;
use net::config::{
AdapterConfig, BackpressureMode, BatchConfig, EventBusConfig, EventBusConfigBuilder,
ScalingPolicy,
};
/// Backpressure strategy.
#[derive(Debug, Clone, Copy)]
pub enum Backpressure {
/// Drop newest events when buffer is full.
DropNewest,
/// Drop oldest events when buffer is full (default).
DropOldest,
/// Fail the producer when buffer is full.
FailProducer,
/// Sample events at the given rate (1 in N). Requires runtime support.
Sample(u32),
}
impl From<Backpressure> for BackpressureMode {
fn from(bp: Backpressure) -> Self {
match bp {
Backpressure::DropNewest => BackpressureMode::DropNewest,
Backpressure::DropOldest => BackpressureMode::DropOldest,
Backpressure::FailProducer => BackpressureMode::FailProducer,
Backpressure::Sample(rate) => BackpressureMode::Sample { rate },
}
}
}
/// Builder for constructing a [`Net`](crate::Net) node.
pub struct NetBuilder {
pub(crate) inner: EventBusConfigBuilder,
pub(crate) adapter: Option<AdapterConfig>,
/// Caller-owned identity. Stored here so `Net::from_builder` can
/// plumb it into whichever adapter consumes keypairs; the
/// event-bus itself is adapter-agnostic and doesn't use it.
#[cfg(feature = "net")]
pub(crate) identity: Option<crate::identity::Identity>,
}
impl NetBuilder {
pub(crate) fn new() -> Self {
Self {
inner: EventBusConfig::builder(),
adapter: None,
#[cfg(feature = "net")]
identity: None,
}
}
/// Pin this node to a caller-owned [`Identity`](crate::Identity).
///
/// Stored on the builder and handed to whichever adapter consumes
/// keypairs (today: the mesh adapter when `net` is enabled). For
/// an event-bus-only node without a mesh adapter, the identity is
/// retained but unused — it becomes load-bearing once you wire in
/// a `NetAdapterConfig`.
#[cfg(feature = "net")]
pub fn identity(mut self, identity: crate::identity::Identity) -> Self {
self.identity = Some(identity);
self
}
/// Set the number of shards.
pub fn shards(mut self, n: u16) -> Self {
self.inner = self.inner.num_shards(n);
self
}
/// Set the ring buffer capacity per shard (must be power of 2).
pub fn buffer_capacity(mut self, capacity: usize) -> Self {
self.inner = self.inner.ring_buffer_capacity(capacity);
self
}
/// Set the backpressure strategy.
pub fn backpressure(mut self, bp: Backpressure) -> Self {
self.inner = self.inner.backpressure_mode(bp.into());
self
}
/// Use the high-throughput batch preset.
pub fn high_throughput(mut self) -> Self {
self.inner = self.inner.batch(BatchConfig::high_throughput());
self
}
/// Use the low-latency batch preset.
pub fn low_latency(mut self) -> Self {
self.inner = self.inner.batch(BatchConfig::low_latency());
self
}
/// Set a custom batch configuration.
pub fn batch(mut self, batch: BatchConfig) -> Self {
self.inner = self.inner.batch(batch);
self
}
/// Enable dynamic scaling.
pub fn scaling(mut self, policy: ScalingPolicy) -> Self {
self.inner = self.inner.scaling(policy);
self
}
/// Set the adapter timeout.
pub fn adapter_timeout(mut self, timeout: Duration) -> Self {
self.inner = self.inner.adapter_timeout(timeout);
self
}
/// Use in-memory transport (no persistence, single process).
pub fn memory(mut self) -> Self {
self.adapter = Some(AdapterConfig::Noop);
self
}
/// Use Redis Streams transport.
#[cfg(feature = "redis")]
pub fn redis(mut self, config: net::config::RedisAdapterConfig) -> Self {
self.adapter = Some(AdapterConfig::Redis(config));
self
}
/// Use NATS JetStream transport.
#[cfg(feature = "jetstream")]
pub fn jetstream(mut self, config: net::config::JetStreamAdapterConfig) -> Self {
self.adapter = Some(AdapterConfig::JetStream(config));
self
}
/// Use Net encrypted UDP mesh transport.
#[cfg(feature = "net")]
pub fn mesh(mut self, config: net::adapter::net::NetAdapterConfig) -> Self {
self.adapter = Some(AdapterConfig::Net(Box::new(config)));
self
}
/// Build the configuration, consuming the builder.
pub(crate) fn build_config(mut self) -> crate::error::Result<EventBusConfig> {
// The `mut adapter` binding is mutated only inside the
// `#[cfg(feature = "net")]` block below; when the `net`
// feature is off it's unused-mut. Suppress the warning
// narrowly so `-D warnings` builds without `net` stay
// clean.
#[cfg_attr(not(feature = "net"), allow(unused_mut))]
if let Some(mut adapter) = self.adapter {
// Plumb the caller-supplied identity into the adapter if
// it consumes keypairs. Today only the `net` adapter
// does; other adapters drop the identity silently (the
// `identity()` docstring flags this). Without this
// injection the builder's `identity(...)` would be a
// no-op — the mesh would generate a fresh keypair every
// run and `node_id` / `entity_id` would drift across
// restarts.
//
// Pre-fix this unconditionally overwrote
// `net_cfg.entity_keypair`. A caller who configured the
// adapter via `.mesh(NetAdapterConfig::initiator(...).with_entity_keypair(kp_a))`
// and ALSO called `.identity(id_b)` got `id_b` silently
// — both inputs were caller-supplied keypairs and one
// was discarded with no error or precedence note. Both
// are load-bearing for `node_id` / `entity_id`, so the
// wrong one routes the node under the wrong identity.
// Fix: error on conflicting keypairs (different
// entity_ids); allow when both are set to the same
// keypair (idempotent); inject when only `identity()`
// was set (the original support path).
#[cfg(feature = "net")]
if let (Some(id), AdapterConfig::Net(ref mut net_cfg)) =
(self.identity.as_ref(), &mut adapter)
{
let builder_kp = id.keypair();
if let Some(adapter_kp) = net_cfg.entity_keypair.as_ref() {
if adapter_kp.entity_id() != builder_kp.entity_id() {
return Err(crate::error::SdkError::Config(format!(
"conflicting identities: NetAdapterConfig::with_entity_keypair \
pinned entity_id {} but NetBuilder::identity pinned {}. \
Set the keypair on exactly one of them.",
adapter_kp.entity_id(),
builder_kp.entity_id(),
)));
}
// Same entity_id: identical key material, no-op.
} else {
net_cfg.entity_keypair = Some((**builder_kp).clone());
}
}
self.inner = self.inner.adapter(adapter);
}
self.inner
.build()
.map_err(|e| crate::error::SdkError::Config(e.to_string()))
}
}
#[cfg(all(test, feature = "net"))]
mod tests {
use super::*;
use net::adapter::net::NetAdapterConfig;
use std::net::SocketAddr;
fn sample_net_adapter() -> NetAdapterConfig {
let bind: SocketAddr = "127.0.0.1:0".parse().unwrap();
let peer: SocketAddr = "127.0.0.1:1".parse().unwrap();
NetAdapterConfig::initiator(bind, peer, [0x00; 32], [0x00; 32])
}
/// Regression for a cubic-flagged P1: `NetBuilder::identity(...)`
/// stored the identity on the builder but `build_config` never
/// plumbed it into the adapter. Callers pinning a seed expected
/// a stable `node_id` / `entity_id` across restarts and silently
/// got a fresh keypair every build.
///
/// Test constructs a builder with a seeded identity + a `Net`
/// adapter via `.mesh(...)`, calls `build_config`, and asserts
/// the resulting `NetAdapterConfig.entity_keypair` contains the
/// caller's key — not `None` (pre-fix behaviour) and not a
/// freshly generated one.
#[test]
fn net_builder_identity_plumbs_into_adapter() {
let seed = [0x42u8; 32];
let identity = crate::identity::Identity::from_seed(seed);
let expected_entity_id = identity.entity_id().clone();
let config = NetBuilder::new()
.mesh(sample_net_adapter())
.identity(identity)
.build_config()
.expect("build_config");
let AdapterConfig::Net(net_cfg) = config.adapter else {
panic!("adapter lost its Net variant during build_config");
};
let kp = net_cfg
.entity_keypair
.as_ref()
.expect("entity_keypair should have been plumbed from NetBuilder::identity");
assert_eq!(
kp.entity_id(),
&expected_entity_id,
"plumbed entity_id must match the pinned identity's",
);
}
/// Regression for the other half of the same contract: a
/// builder with no identity must leave `entity_keypair` unset
/// so the mesh's internal default (generate a fresh keypair)
/// is honoured. The fix mustn't accidentally inject an empty /
/// default keypair when the caller didn't pin one.
#[test]
fn net_builder_without_identity_leaves_adapter_keypair_unset() {
let config = NetBuilder::new()
.mesh(sample_net_adapter())
.build_config()
.expect("build_config");
let AdapterConfig::Net(net_cfg) = config.adapter else {
panic!("adapter lost its Net variant");
};
assert!(
net_cfg.entity_keypair.is_none(),
"entity_keypair must stay unset when the builder has no identity",
);
}
/// When both `NetAdapterConfig::with_entity_keypair`
/// and `NetBuilder::identity` are set with DIFFERENT entity
/// IDs, build_config must error rather than silently picking
/// one. Both inputs are caller-supplied; both are load-bearing
/// for `node_id` / `entity_id`; silently overriding routes the
/// node under the wrong identity.
#[test]
fn build_config_errors_on_conflicting_identities() {
let kp_a = net::adapter::net::identity::EntityKeypair::generate();
let kp_b_seed = [0xAAu8; 32];
let identity_b = crate::identity::Identity::from_seed(kp_b_seed);
// Sanity — distinct entity_ids.
assert_ne!(kp_a.entity_id(), identity_b.entity_id());
let mesh_cfg = sample_net_adapter().with_entity_keypair(kp_a);
let err = NetBuilder::new()
.mesh(mesh_cfg)
.identity(identity_b)
.build_config()
.expect_err(
"build_config must reject conflicting keypairs; \
pre-fix it silently let identity() win",
);
let msg = format!("{}", err);
assert!(
msg.contains("conflicting identities"),
"expected 'conflicting identities' in error, got: {}",
msg
);
}
/// Setting the SAME keypair on both sides is
/// idempotent and must build cleanly. Documents the
/// "identical key material, no-op" path.
#[test]
fn build_config_accepts_matching_identities_on_both_sides() {
let seed = [0x11u8; 32];
let identity = crate::identity::Identity::from_seed(seed);
let same_kp = (**identity.keypair()).clone();
let expected_id = identity.entity_id().clone();
let mesh_cfg = sample_net_adapter().with_entity_keypair(same_kp);
let config = NetBuilder::new()
.mesh(mesh_cfg)
.identity(identity)
.build_config()
.expect("matching keypairs on both sides must build");
let AdapterConfig::Net(net_cfg) = config.adapter else {
panic!("adapter lost its Net variant");
};
assert_eq!(
net_cfg.entity_keypair.as_ref().unwrap().entity_id(),
&expected_id,
);
}
/// Setting the keypair via the adapter only
/// (no `.identity()` call) preserves the adapter's choice —
/// the build path must NOT overwrite it with a default.
#[test]
fn build_config_preserves_adapter_keypair_when_no_builder_identity() {
let kp = net::adapter::net::identity::EntityKeypair::generate();
let expected_id = kp.entity_id().clone();
let mesh_cfg = sample_net_adapter().with_entity_keypair(kp);
let config = NetBuilder::new()
.mesh(mesh_cfg)
.build_config()
.expect("build_config");
let AdapterConfig::Net(net_cfg) = config.adapter else {
panic!("adapter lost its Net variant");
};
assert_eq!(
net_cfg.entity_keypair.as_ref().unwrap().entity_id(),
&expected_id,
"adapter-side entity_keypair must survive when builder has no identity",
);
}
}