Update book code snippets to block-style DSL helpers

This commit is contained in:
andrussal 2025-12-03 06:20:59 +01:00
parent 61e7fe4e25
commit 0fb44c6955
4 changed files with 76 additions and 78 deletions

View File

@ -45,20 +45,20 @@ These binaries use the framework API (`ScenarioBuilder`) to construct and execut
Scenarios are defined using a fluent builder pattern: Scenarios are defined using a fluent builder pattern:
```rust ```rust
let mut plan = ScenarioBuilder::topology() let mut plan = ScenarioBuilder::topology_with(|t| {
.network_star() // Topology configuration t.network_star() // Topology configuration
.validators(3) .validators(3)
.executors(2) .executors(2)
.apply() })
.wallets(50) // Wallet seeding .wallets(50) // Wallet seeding
.transactions() // Transaction workload .transactions_with(|txs| {
.rate(5) txs.rate(5)
.users(20) .users(20)
.apply() })
.da() // DA workload .da_with(|da| {
.channel_rate(1) da.channel_rate(1)
.blob_rate(2) .blob_rate(2)
.apply() })
.expect_consensus_liveness() // Expectations .expect_consensus_liveness() // Expectations
.with_run_duration(Duration::from_secs(90)) .with_run_duration(Duration::from_secs(90))
.build(); .build();

View File

@ -23,21 +23,19 @@ use std::time::Duration;
use testing_framework_core::scenario::ScenarioBuilder; use testing_framework_core::scenario::ScenarioBuilder;
use testing_framework_workflows::workloads::chaos::RandomRestartWorkload; use testing_framework_workflows::workloads::chaos::RandomRestartWorkload;
let plan = ScenarioBuilder::topology() let plan = ScenarioBuilder::topology_with(|t| {
.network_star() t.network_star()
.validators(2) .validators(2)
.executors(1) .executors(1)
.apply() })
.enable_node_control() .enable_node_control()
.with_workload( .with_workload(RandomRestartWorkload::new(
RandomRestartWorkload::new( Duration::from_secs(45), // min delay
Duration::from_secs(45), // min delay Duration::from_secs(75), // max delay
Duration::from_secs(75), // max delay Duration::from_secs(120), // target cooldown
Duration::from_secs(120), // target cooldown true, // include validators
true, // include validators true, // include executors
true, // include executors ))
)
)
.expect_consensus_liveness() .expect_consensus_liveness()
.with_run_duration(Duration::from_secs(150)) .with_run_duration(Duration::from_secs(150))
.build(); .build();

View File

@ -16,11 +16,11 @@ use std::time::Duration;
## Topology ## Topology
```rust ```rust
ScenarioBuilder::topology() ScenarioBuilder::topology_with(|t| {
.network_star() // Star topology (all connect to seed node) t.network_star() // Star topology (all connect to seed node)
.validators(3) // Number of validator nodes .validators(3) // Number of validator nodes
.executors(2) // Number of executor nodes .executors(2) // Number of executor nodes
.apply() // Finish topology configuration }) // Finish topology configuration
``` ```
## Wallets ## Wallets
@ -32,31 +32,32 @@ ScenarioBuilder::topology()
## Transaction Workload ## Transaction Workload
```rust ```rust
.transactions() .transactions_with(|txs| {
.rate(5) // 5 transactions per block txs.rate(5) // 5 transactions per block
.users(20) // Use 20 of the seeded wallets .users(20) // Use 20 of the seeded wallets
.apply() // Finish transaction workload config }) // Finish transaction workload config
``` ```
## DA Workload ## DA Workload
```rust ```rust
.da() .da_with(|da| {
.channel_rate(1) // 1 channel operation per block da.channel_rate(1) // 1 channel operation per block
.blob_rate(2) // 2 blob dispersals per block .blob_rate(2) // 2 blob dispersals per block
.apply() // Finish DA workload config }) // Finish DA workload config
``` ```
## Chaos Workload (Requires `enable_node_control()`) ## Chaos Workload (Requires `enable_node_control()`)
```rust ```rust
.enable_node_control() // Enable node control capability .enable_node_control() // Enable node control capability
.chaos() .chaos_with(|c| {
.restart() // Random restart chaos c.restart() // Random restart chaos
.min_delay(Duration::from_secs(30)) // Min time between restarts .min_delay(Duration::from_secs(30)) // Min time between restarts
.max_delay(Duration::from_secs(60)) // Max time between restarts .max_delay(Duration::from_secs(60)) // Max time between restarts
.target_cooldown(Duration::from_secs(45)) // Cooldown after restart .target_cooldown(Duration::from_secs(45)) // Cooldown after restart
.apply() // Finish chaos workload config .apply()
})
``` ```
## Expectations ## Expectations
@ -106,20 +107,20 @@ use testing_framework_workflows::ScenarioBuilderExt;
use std::time::Duration; use std::time::Duration;
async fn run_test() -> Result<(), Box<dyn std::error::Error + Send + Sync>> { async fn run_test() -> Result<(), Box<dyn std::error::Error + Send + Sync>> {
let mut plan = ScenarioBuilder::topology() let mut plan = ScenarioBuilder::topology_with(|t| {
.network_star() t.network_star()
.validators(3) .validators(3)
.executors(2) .executors(2)
.apply() })
.wallets(50) .wallets(50)
.transactions() .transactions_with(|txs| {
.rate(5) // 5 transactions per block txs.rate(5) // 5 transactions per block
.users(20) .users(20)
.apply() })
.da() .da_with(|da| {
.channel_rate(1) // 1 channel operation per block da.channel_rate(1) // 1 channel operation per block
.blob_rate(2) // 2 blob dispersals per block .blob_rate(2) // 2 blob dispersals per block
.apply() })
.expect_consensus_liveness() .expect_consensus_liveness()
.with_run_duration(Duration::from_secs(90)) .with_run_duration(Duration::from_secs(90))
.build(); .build();

View File

@ -29,20 +29,20 @@ use testing_framework_workflows::ScenarioBuilderExt;
use std::time::Duration; use std::time::Duration;
// Define the scenario (1 validator + 1 executor, tx + DA workload) // Define the scenario (1 validator + 1 executor, tx + DA workload)
let mut plan = ScenarioBuilder::topology() let mut plan = ScenarioBuilder::topology_with(|t| {
.network_star() t.network_star()
.validators(1) .validators(1)
.executors(1) .executors(1)
.apply() })
.wallets(64) .wallets(64)
.transactions() .transactions_with(|txs| {
.rate(5) // 5 transactions per block txs.rate(5) // 5 transactions per block
.users(8) .users(8)
.apply() })
.da() .da_with(|da| {
.channel_rate(1) // 1 channel operation per block da.channel_rate(1) // 1 channel operation per block
.blob_rate(1) // 1 blob dispersal per block .blob_rate(1) // 1 blob dispersal per block
.apply() })
.expect_consensus_liveness() .expect_consensus_liveness()
.with_run_duration(Duration::from_secs(60)) .with_run_duration(Duration::from_secs(60))
.build(); .build();
@ -71,11 +71,11 @@ Let's unpack the code:
### 1. Topology Configuration ### 1. Topology Configuration
```rust ```rust
ScenarioBuilder::topology() ScenarioBuilder::topology_with(|t| {
.network_star() // Star topology: all nodes connect to seed t.network_star() // Star topology: all nodes connect to seed
.validators(1) // 1 validator node .validators(1) // 1 validator node
.executors(1) // 1 executor node (validator + DA dispersal) .executors(1) // 1 executor node (validator + DA dispersal)
.apply() })
``` ```
This defines **what** your test network looks like. This defines **what** your test network looks like.
@ -184,4 +184,3 @@ Now that you have a working test:
- **See more examples**: [Examples](examples.md) - **See more examples**: [Examples](examples.md)
- **API reference**: [Builder API Quick Reference](dsl-cheat-sheet.md) - **API reference**: [Builder API Quick Reference](dsl-cheat-sheet.md)
- **Debug failures**: [Troubleshooting](troubleshooting.md) - **Debug failures**: [Troubleshooting](troubleshooting.md)