thruput-native-exec 0.58.0

Native execution environment for testing Thruput smart contracts
Documentation
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
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
use crate::test_environment::{
    DBWrapper, mock_genesis_root_contract, test_network_hash,
};
use std::cmp::Ordering;
use std::collections::BTreeMap;
use std::sync::OnceLock;
use thruput_xdr::network_config::{
    ConfigParam, ConfigParamID, NetworkConfig, NetworkConfigV0,
};

/// The configuration a database takes when none is supplied: the stock
/// genesis values of the hogd build this crate is linked against.
///
/// Read from hogd rather than restated here, so it cannot drift from the
/// binary actually executing the tests. The first call creates (and frees) a
/// throwaway database to ask; the answer is cached for the process.
///
/// This is the base [`NetworkConfigBuilder`] overrides are applied to, and
/// what a [`TestEnvironment`](crate::test_environment::TestEnvironment)
/// built without configuration runs under.
pub fn stock_network_config() -> NetworkConfig {
    static STOCK: OnceLock<NetworkConfig> = OnceLock::new();
    STOCK
        .get_or_init(|| {
            let db = DBWrapper::new(
                None,
                &test_network_hash(),
                &mock_genesis_root_contract(),
                None,
            );
            db.get_network_config()
        })
        .clone()
}

/// The parameters `config` states, in id order.
///
/// The list is a prefix of the id space: a parameter past its end is not
/// stated by this configuration and takes the value the protocol defines
/// for it.
pub fn config_params(config: &NetworkConfig) -> &[ConfigParam] {
    match config {
        NetworkConfig::_0 { v0 } => &v0.params.0,
        other => panic!(
            "unsupported network configuration version: {other:?}\n\
             This build of the SDK only knows NetworkConfig::_0."
        ),
    }
}

/// The value `config` states for `param`, or `None` when it states none.
///
/// ```no_run
/// use thruput_native_exec::network_config;
/// use thruput_xdr::network_config::{ConfigParam, ConfigParamID};
///
/// let config = network_config::stock_network_config();
/// let cap = match network_config::find_config_param(
///     &config,
///     ConfigParamID::kConfigParamMaxSHA256Buffers,
/// ) {
///     Some(ConfigParam::kConfigParamMaxSHA256Buffers {
///         max_sha256_buffers,
///     }) => *max_sha256_buffers,
///     _ => panic!("this chain states no SHA-256 buffer cap"),
/// };
/// assert!(cap > 0);
/// ```
pub fn find_config_param(
    config: &NetworkConfig,
    param: ConfigParamID,
) -> Option<&ConfigParam> {
    config_params(config).iter().find(|p| p.param() == param)
}

/// Builder for a [`NetworkConfig`] stating non-default execution
/// parameters.
///
/// Overrides are applied to a base configuration -- [`stock_network_config`]
/// unless [`with_base`](Self::with_base) says otherwise -- so setting one
/// parameter leaves every other one at the value the chain would have used:
///
/// ```no_run
/// use thruput_native_exec::network_config::NetworkConfigBuilder;
///
/// let config = NetworkConfigBuilder::new()
///     .with_max_sha256_buffers(2)
///     .build();
/// ```
///
/// Most tests never name this type: the same `with_*` setters are on
/// [`TestEnvironmentBuilder`](crate::test_environment_builder::TestEnvironmentBuilder),
/// which builds the configuration and the environment together.
///
/// A setter is only usable against a hogd build that knows its parameter.
/// The stock configuration states what that build's genesis states, and
/// [`build`](Self::build) refuses to leave a gap below a newer parameter,
/// so the ones protocol 0.35 introduced
/// ([`with_max_contract_size_bytes`](Self::with_max_contract_size_bytes)
/// and later) need a hogd built from that protocol or newer.
#[derive(Clone, Debug, Default)]
#[must_use = "a NetworkConfigBuilder does nothing until `build` is called"]
pub struct NetworkConfigBuilder {
    base: Option<NetworkConfig>,
    overrides: BTreeMap<ConfigParamID, ConfigParam>,
}

impl NetworkConfigBuilder {
    /// A builder with no overrides, over the stock configuration.
    pub fn new() -> Self {
        Self::default()
    }

    /// Apply the overrides to `base` instead of to
    /// [`stock_network_config`].
    ///
    /// Use this to start from a configuration a test already has in hand
    /// (e.g. one read back off another environment). `base` must itself
    /// state a gapless prefix of the id space, as any configuration hogd
    /// accepts does.
    pub fn with_base(mut self, base: NetworkConfig) -> Self {
        self.base = Some(base);
        self
    }

    /// State `param`, replacing whatever the base says for it.
    ///
    /// The typed setters below cover the parameters this SDK release knows;
    /// this is the escape hatch for one it does not.
    pub fn with_config_param(mut self, param: ConfigParam) -> Self {
        self.overrides.insert(param.param(), param);
        self
    }

    /// Concurrently open incremental SHA-256 buffers permitted per invocation
    /// frame (the cap `thruput_sdk::Hasher` allocates against).
    pub fn with_max_sha256_buffers(self, max_sha256_buffers: u32) -> Self {
        self.with_config_param(ConfigParam::kConfigParamMaxSHA256Buffers {
            max_sha256_buffers,
        })
    }

    /// Invocation frames one transaction may have open at once.
    ///
    /// The harness opens its frames directly, so one the executor refuses is
    /// an unrecoverable panic rather than the error a contract would observe.
    /// To assert on the cap, have the frame call through the Invoke syscall
    /// (`thruput_sdk::call::try_call_raw_with`), which reports it as
    /// `RawCallError::DepthExceeded`.
    pub fn with_max_cross_invoke_depth(
        self,
        max_cross_invoke_depth: u32,
    ) -> Self {
        self.with_config_param(ConfigParam::kConfigParamMaxCrossInvokeDepth {
            max_cross_invoke_depth,
        })
    }

    /// Contracts one transaction may attach in `contracts_to_deploy`.
    pub fn with_max_new_contracts_per_tx(
        self,
        max_new_contracts_per_tx: u32,
    ) -> Self {
        self.with_config_param(ConfigParam::kConfigParamMaxNewContractsPerTx {
            max_new_contracts_per_tx,
        })
    }

    /// Bytes one bytestring storage item may hold.
    pub fn with_max_bytestring_len(self, max_bytestring_len: u32) -> Self {
        self.with_config_param(ConfigParam::kConfigParamMaxBytestringLen {
            max_bytestring_len,
        })
    }

    /// Bytes one attached contract's code blob (`ContractBlob.data`) may
    /// hold, checked before any attached contract is verified or executed.
    pub fn with_max_contract_size_bytes(
        self,
        max_contract_size_bytes: u32,
    ) -> Self {
        self.with_config_param(ConfigParam::kConfigParamMaxContractSizeBytes {
            max_contract_size_bytes,
        })
    }

    /// Storage savepoints one transaction may have open at once: one per
    /// `TryInvoke` frame still executing, plus the one the root commit
    /// takes.
    pub fn with_max_savepoint_depth(self, max_savepoint_depth: u32) -> Self {
        self.with_config_param(ConfigParam::kConfigParamMaxSavepointDepth {
            max_savepoint_depth,
        })
    }

    /// Bytes one incremental SHA-256 buffer may absorb between resets, the
    /// per-buffer companion of
    /// [`with_max_sha256_buffers`](Self::with_max_sha256_buffers).
    pub fn with_max_sha256_buffer_bytes(
        self,
        max_sha256_buffer_bytes: u32,
    ) -> Self {
        self.with_config_param(ConfigParam::kConfigParamMaxSHA256BufferBytes {
            max_sha256_buffer_bytes,
        })
    }

    /// Levels of `AuthTokenNode` nesting an auth token may carry, counted
    /// from its root.
    pub fn with_max_auth_token_depth(self, max_auth_token_depth: u32) -> Self {
        self.with_config_param(ConfigParam::kConfigParamMaxAuthTokenDepth {
            max_auth_token_depth,
        })
    }

    /// Bytes of code (`code_size` in the artifact header) an RV32 artifact
    /// may carry to be attached as a contract.
    pub fn with_max_rv32_code_bytes(self, max_rv32_code_bytes: u32) -> Self {
        self.with_config_param(ConfigParam::kConfigParamMaxRv32CodeBytes {
            max_rv32_code_bytes,
        })
    }

    /// Bytes of guest data memory (`memory_bytes` in the artifact header)
    /// an RV32 artifact may declare to be attached as a contract.
    pub fn with_max_rv32_memory_bytes(
        self,
        max_rv32_memory_bytes: u32,
    ) -> Self {
        self.with_config_param(ConfigParam::kConfigParamMaxRv32MemoryBytes {
            max_rv32_memory_bytes,
        })
    }

    /// Whether this builder would produce exactly the stock configuration:
    /// no base of its own and nothing overridden.
    pub fn is_stock(&self) -> bool {
        self.base.is_none() && self.overrides.is_empty()
    }

    /// The configuration: the base with every override applied.
    ///
    /// Panics rather than hand hogd a list it would reject: the parameters
    /// must come out a gapless prefix of the id space (ids 0, 1, 2, ...),
    /// which is what gives one configuration one encoding, and hence one
    /// config hash.
    pub fn build(self) -> NetworkConfig {
        let base = self.base.unwrap_or_else(stock_network_config);
        let mut params = config_params(&base).to_vec();

        // Ascending id order (BTreeMap), so an override that extends the
        // prefix by one lands right after the parameters below it.
        for (param_id, param) in self.overrides {
            let id = param_id.0;
            let index = usize::try_from(id).unwrap_or_else(|_| {
                panic!("network configuration parameter id {id} is negative")
            });
            match index.cmp(&params.len()) {
                Ordering::Less => params[index] = param,
                Ordering::Equal => params.push(param),
                Ordering::Greater => panic!(
                    "cannot state {} (id {id}): the configuration states \
                     {} parameter(s), so this would leave ids {}..{id} \
                     unstated, and the list must be a gapless prefix of \
                     the id space. State the parameters in between too -- \
                     or, if this hogd build does not know id {id} at all, \
                     drop the override.",
                    param.param(),
                    params.len(),
                    params.len(),
                ),
            }
        }

        for (index, param) in params.iter().enumerate() {
            assert_eq!(
                param.param().0,
                index as i32,
                "network configuration parameters must be a gapless prefix \
                 of the id space in id order, but position {index} states \
                 {}. hogd rejects any other arrangement (fatally, from \
                 inside the FFI).",
                param.param(),
            );
        }

        NetworkConfig::_0 {
            v0: NetworkConfigV0 {
                params: params.try_into().expect(
                    "the parameter list cannot exceed the XDR length bound",
                ),
            },
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    fn config_of(params: Vec<ConfigParam>) -> NetworkConfig {
        NetworkConfig::_0 {
            v0: NetworkConfigV0 {
                params: params.try_into().unwrap(),
            },
        }
    }

    #[test]
    fn the_stock_config_states_a_gapless_prefix_of_the_id_space() {
        let stock = stock_network_config();
        let params = config_params(&stock);

        assert!(!params.is_empty(), "a chain states its configuration");
        for (index, param) in params.iter().enumerate() {
            assert_eq!(param.param().0, index as i32);
        }
    }

    #[test]
    fn a_builder_with_no_overrides_is_the_stock_config() {
        let builder = NetworkConfigBuilder::new();
        assert!(builder.is_stock());
        assert_eq!(builder.build(), stock_network_config());
    }

    #[test]
    fn setting_a_parameter_leaves_the_others_at_their_stock_values() {
        let stock = stock_network_config();
        let built = NetworkConfigBuilder::new()
            .with_max_sha256_buffers(1)
            .build();

        assert_eq!(
            find_config_param(
                &built,
                ConfigParamID::kConfigParamMaxSHA256Buffers
            ),
            Some(&ConfigParam::kConfigParamMaxSHA256Buffers {
                max_sha256_buffers: 1
            }),
        );
        assert_eq!(&config_params(&built)[1..], &config_params(&stock)[1..],);
    }

    #[test]
    fn the_last_setter_for_a_parameter_wins() {
        let built = NetworkConfigBuilder::new()
            .with_max_sha256_buffers(1)
            .with_max_sha256_buffers(4)
            .build();

        assert_eq!(
            find_config_param(
                &built,
                ConfigParamID::kConfigParamMaxSHA256Buffers
            ),
            Some(&ConfigParam::kConfigParamMaxSHA256Buffers {
                max_sha256_buffers: 4
            }),
        );
    }

    #[test]
    fn overrides_apply_to_an_explicit_base() {
        let base = config_of(vec![
            ConfigParam::kConfigParamMaxSHA256Buffers {
                max_sha256_buffers: 7,
            },
            ConfigParam::kConfigParamMaxCrossInvokeDepth {
                max_cross_invoke_depth: 9,
            },
        ]);
        let built = NetworkConfigBuilder::new()
            .with_base(base)
            .with_max_cross_invoke_depth(3)
            .build();

        assert_eq!(
            config_params(&built),
            [
                ConfigParam::kConfigParamMaxSHA256Buffers {
                    max_sha256_buffers: 7
                },
                ConfigParam::kConfigParamMaxCrossInvokeDepth {
                    max_cross_invoke_depth: 3
                },
            ],
        );
    }

    #[test]
    fn every_typed_setter_states_its_own_parameter() {
        // Applied in id order from an empty base, so each setter extends
        // the prefix by exactly one; the result names every parameter this
        // release knows, in the order the protocol numbers them.
        let built = NetworkConfigBuilder::new()
            .with_base(config_of(vec![]))
            .with_max_sha256_buffers(1)
            .with_max_cross_invoke_depth(2)
            .with_max_new_contracts_per_tx(3)
            .with_max_bytestring_len(4)
            .with_max_contract_size_bytes(5)
            .with_max_savepoint_depth(6)
            .with_max_sha256_buffer_bytes(7)
            .with_max_auth_token_depth(8)
            .with_max_rv32_code_bytes(9)
            .with_max_rv32_memory_bytes(10)
            .build();

        assert_eq!(
            config_params(&built),
            [
                ConfigParam::kConfigParamMaxSHA256Buffers {
                    max_sha256_buffers: 1
                },
                ConfigParam::kConfigParamMaxCrossInvokeDepth {
                    max_cross_invoke_depth: 2
                },
                ConfigParam::kConfigParamMaxNewContractsPerTx {
                    max_new_contracts_per_tx: 3
                },
                ConfigParam::kConfigParamMaxBytestringLen {
                    max_bytestring_len: 4
                },
                ConfigParam::kConfigParamMaxContractSizeBytes {
                    max_contract_size_bytes: 5
                },
                ConfigParam::kConfigParamMaxSavepointDepth {
                    max_savepoint_depth: 6
                },
                ConfigParam::kConfigParamMaxSHA256BufferBytes {
                    max_sha256_buffer_bytes: 7
                },
                ConfigParam::kConfigParamMaxAuthTokenDepth {
                    max_auth_token_depth: 8
                },
                ConfigParam::kConfigParamMaxRv32CodeBytes {
                    max_rv32_code_bytes: 9
                },
                ConfigParam::kConfigParamMaxRv32MemoryBytes {
                    max_rv32_memory_bytes: 10
                },
            ],
        );
    }

    #[test]
    fn an_override_may_extend_the_prefix_by_one() {
        let base =
            config_of(vec![ConfigParam::kConfigParamMaxSHA256Buffers {
                max_sha256_buffers: 7,
            }]);
        let built = NetworkConfigBuilder::new()
            .with_base(base)
            .with_max_cross_invoke_depth(3)
            .build();

        assert_eq!(config_params(&built).len(), 2);
    }

    // hogd fails a gapped list from inside the FFI, taking the whole test
    // binary with it. Refuse to build one in the first place.
    #[test]
    #[should_panic(expected = "gapless prefix")]
    fn an_override_that_would_leave_a_gap_is_refused() {
        let base = config_of(vec![]);
        let _ = NetworkConfigBuilder::new()
            .with_base(base)
            .with_max_cross_invoke_depth(3)
            .build();
    }

    #[test]
    #[should_panic(expected = "gapless prefix")]
    fn a_base_that_is_not_a_prefix_is_refused() {
        let base =
            config_of(vec![ConfigParam::kConfigParamMaxCrossInvokeDepth {
                max_cross_invoke_depth: 9,
            }]);
        let _ = NetworkConfigBuilder::new().with_base(base).build();
    }

    #[test]
    fn a_builder_with_a_base_is_not_stock() {
        assert!(
            !NetworkConfigBuilder::new()
                .with_base(stock_network_config())
                .is_stock()
        );
    }
}