|
1 |
| -# Testing Multi-node Blueprints |
| 1 | +# Testing Multi-node Blueprints |
| 2 | + |
| 3 | +This guide explains how to test blueprints that require multiple nodes for distributed computation, such as threshold cryptography protocols or consensus protocols. We'll walk through setting up a test environment and running multi-node tests using our SDK's testing utilities. |
| 4 | + |
| 5 | +## Overview |
| 6 | + |
| 7 | +When developing multi-node blueprints, testing requires simulating a network of nodes that can communicate and coordinate with each other. Our SDK provides testing utilities that make it easy to: |
| 8 | + |
| 9 | +- Set up a simulated multi-node network environment |
| 10 | +- Configure individual nodes with custom handlers |
| 11 | +- Submit jobs and verify results across nodes |
| 12 | +- Test distributed protocols and consensus mechanisms |
| 13 | + |
| 14 | +## Test Environment Setup |
| 15 | + |
| 16 | +### Prerequisites |
| 17 | + |
| 18 | +First, include the necessary testing utilities in your project: |
| 19 | + |
| 20 | +```rust |
| 21 | +use blueprint_sdk::testing::utils::tangle::TangleTestHarness; |
| 22 | +use blueprint_sdk::testing::utils::harness::TestHarness; |
| 23 | +use blueprint_sdk::testing::tempfile; |
| 24 | +use blueprint_sdk::logging; |
| 25 | +``` |
| 26 | + |
| 27 | +### Basic Setup |
| 28 | + |
| 29 | +1. **Initialize Logging and Error Handling**: |
| 30 | + |
| 31 | +```rust |
| 32 | +logging::setup_log(); |
| 33 | +``` |
| 34 | + |
| 35 | +2. **Create Test Harness**: |
| 36 | + |
| 37 | +```rust |
| 38 | +let tmp_dir = tempfile::TempDir::new()?; |
| 39 | +let harness = TangleTestHarness::setup(tmp_dir).await? |
| 40 | +``` |
| 41 | + |
| 42 | +## Setting Up Multi-node Services |
| 43 | + |
| 44 | +### Node Configuration |
| 45 | + |
| 46 | +1. **Initialize Test Environment**: |
| 47 | + |
| 48 | +```rust |
| 49 | +// N specifies number of nodes (e.g. N = 3) |
| 50 | +let (mut test_env, service_id, blueprint_id) = harness.setup_services::<N>(false).await?; |
| 51 | +test_env.initialize().await?; |
| 52 | +``` |
| 53 | + |
| 54 | +2. **Configure Individual Nodes**: |
| 55 | + |
| 56 | +```rust |
| 57 | +let handles = test_env.node_handles().await; |
| 58 | +for handle in handles { |
| 59 | +// Get node configuration |
| 60 | +let config = handle.gadget_config().await; |
| 61 | +// Initialize node-specific context |
| 62 | +let blueprint_ctx = YourContext::new(config.clone()).await?; |
| 63 | +// Add job handlers |
| 64 | +let job_handler = YourJobHandler::new(&config, blueprint_ctx.clone()).await?; |
| 65 | +handle.add_job(job_handler).await; |
| 66 | +} |
| 67 | +``` |
| 68 | + |
| 69 | +3. **Allow Time for Network Setup**: |
| 70 | + |
| 71 | +```rust |
| 72 | +// Wait for network handshakes |
| 73 | +tokio::time::sleep(std::time::Duration::from_secs(10)).await; |
| 74 | +``` |
| 75 | + |
| 76 | +4. **Start the Environment**: |
| 77 | + |
| 78 | +```rust |
| 79 | +test_env.start().await?; |
| 80 | +``` |
| 81 | + |
| 82 | +## Running Tests |
| 83 | + |
| 84 | +### Submitting Jobs |
| 85 | + |
| 86 | +To test your blueprint's functionality, submit jobs and verify their results: |
| 87 | + |
| 88 | +```rust |
| 89 | +// Submit a job with arguments |
| 90 | +let job = harness |
| 91 | + .submit_job( |
| 92 | + service_id, |
| 93 | + JOB_ID, |
| 94 | + vec![InputValue::Uint16(2)] // Example job argument |
| 95 | +) |
| 96 | +.await?; |
| 97 | +logging::info!("Submitted job {JOB_ID} with service ID {service_id}"); |
| 98 | +``` |
| 99 | + |
| 100 | +### Verifying Results |
| 101 | + |
| 102 | +Wait for job completion and verify the results: |
| 103 | + |
| 104 | +```rust |
| 105 | +// Wait for job execution |
| 106 | +let results = harness.wait_for_job_execution(service_id, job).await?; |
| 107 | +assert_eq!(results.service_id, service_id); |
| 108 | + |
| 109 | +// Verify outputs |
| 110 | +if !expected_outputs.is_empty() { |
| 111 | +assert_eq!( |
| 112 | + results.result.len(), |
| 113 | + expected_outputs.len(), |
| 114 | + "Number of outputs doesn't match expected" |
| 115 | +); |
| 116 | + |
| 117 | +// Add more verification logic as needed... |
| 118 | +} |
| 119 | +``` |
| 120 | + |
| 121 | +## Best Practices |
| 122 | + |
| 123 | +1. **Error Handling**: Always implement proper error handling and logging to diagnose test failures. |
| 124 | + |
| 125 | +2. **Network Delays**: Include appropriate delays for network initialization and handshakes. |
| 126 | + |
| 127 | +3. **Verification**: Thoroughly verify all job outputs against expected results. |
| 128 | + |
| 129 | +4. **Cleanup**: Use temporary directories that are automatically cleaned up after tests. |
| 130 | + |
| 131 | +5. **Logging**: Implement comprehensive logging to track test progress and debug issues. |
| 132 | + |
| 133 | +## Example: Complete Test Structure |
| 134 | + |
| 135 | +Here's a complete example showing how to structure a multi-node test: |
| 136 | + |
| 137 | +```rust |
| 138 | +#[tokio::test(flavor = "multi_thread")] |
| 139 | +async fn test_blueprint() -> color_eyre::Result<()> { |
| 140 | + logging::setup_log(); |
| 141 | + let tmp_dir = tempfile::TempDir::new()?; |
| 142 | + let harness = TangleTestHarness::setup(tmp_dir).await?; |
| 143 | + |
| 144 | + // Initialize nodes |
| 145 | + let (mut test_env, service_id, ) = harness.setup_services::<3>(false).await?; |
| 146 | + test_env.initialize().await?; |
| 147 | + |
| 148 | + // Configure nodes |
| 149 | + let handles = test_env.node_handles().await; |
| 150 | + for handle in handles { |
| 151 | + // Add handlers |
| 152 | + // ... |
| 153 | + } |
| 154 | + |
| 155 | + // Wait for network setup |
| 156 | + tokio::time::sleep(std::time::Duration::from_secs(10)).await; |
| 157 | + test_env.start().await?; |
| 158 | + |
| 159 | + // Run test jobs |
| 160 | + let job = harness.submit_job(service_id, JOB_ID, vec![/ args /]).await?; |
| 161 | + let results = harness.wait_for_job_execution(service_id, job).await?; |
| 162 | + |
| 163 | + // Verify results |
| 164 | + assert_eq!(results.service_id, service_id); |
| 165 | + |
| 166 | + // Additional verification... |
| 167 | + Ok(()) |
| 168 | +} |
| 169 | +``` |
0 commit comments