blockchain-runtime 0.1.0

Blockchain-agnostic runtime abstraction for dynamic analysis, testing, and simulation
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
# Testing Guide

Guide for testing blockchain code using Blockchain Runtime.

## Testing Strategies

### Unit Testing

Test individual contract functions in isolation:

```rust
#[tokio::test]
async fn test_token_transfer() {
    let runtime = get_test_runtime();
    let config = RuntimeConfig::default();
    let env = runtime.create_environment(config).await.unwrap();
    
    // Deploy token contract
    let bytecode = compile_token_contract();
    let address = runtime.deploy_contract(&env, &bytecode, &[]).await.unwrap();
    
    // Test transfer
    let args = encode_args(&["recipient", "100"]);
    let result = runtime.call_function(&env, &address, "transfer", &args).await.unwrap();
    
    assert!(decode_bool(&result));
    
    runtime.destroy(env).await.unwrap();
}
```

### Integration Testing

Test contract interactions:

```rust
#[tokio::test]
async fn test_defi_protocol() {
    let runtime = get_test_runtime();
    let env = runtime.create_environment(RuntimeConfig::default()).await.unwrap();
    
    // Deploy multiple contracts
    let token = deploy_token(&runtime, &env).await.unwrap();
    let pool = deploy_pool(&runtime, &env).await.unwrap();
    let router = deploy_router(&runtime, &env).await.unwrap();
    
    // Test workflow
    approve_token(&runtime, &env, &token, &pool).await.unwrap();
    add_liquidity(&runtime, &env, &router, &pool).await.unwrap();
    swap_tokens(&runtime, &env, &router).await.unwrap();
    
    runtime.destroy(env).await.unwrap();
}
```

### Property-Based Testing

Use proptest for randomized testing:

```rust
use proptest::prelude::*;

proptest! {
    #[tokio::test]
    async fn test_transfer_properties(
        amount in 0u64..1000000,
        recipient in "[a-f0-9]{40}",
    ) {
        let runtime = get_test_runtime();
        let env = runtime.create_environment(RuntimeConfig::default()).await?;
        
        let address = deploy_contract(&runtime, &env).await?;
        let args = encode_transfer(recipient, amount)?;
        
        let result = runtime.call_function(&env, &address, "transfer", &args).await;
        
        // Properties that should always hold
        prop_assert!(result.is_ok() || is_expected_error(&result));
        
        runtime.destroy(env).await?;
    }
}
```

## Test Organization

### Test Fixtures

```rust
struct TestFixture {
    runtime: Box<dyn BlockchainRuntime>,
    env: RuntimeEnvironment,
}

impl TestFixture {
    async fn new() -> Result<Self> {
        let runtime = get_test_runtime();
        let env = runtime.create_environment(RuntimeConfig::default()).await?;
        
        Ok(Self { runtime, env })
    }
    
    async fn deploy_contract(&self, bytecode: &[u8]) -> Result<String> {
        self.runtime.deploy_contract(&self.env, bytecode, &[]).await
    }
}

impl Drop for TestFixture {
    fn drop(&mut self) {
        // Clean up in background (can't use async in Drop)
        // Consider using tokio::spawn or similar
    }
}
```

### Test Helpers

```rust
mod test_helpers {
    use super::*;
    
    pub async fn deploy_and_init(runtime: &dyn BlockchainRuntime, env: &RuntimeEnvironment) -> Result<String> {
        let bytecode = compile_test_contract();
        let address = runtime.deploy_contract(env, &bytecode, &[]).await?;
        
        // Initialize
        let init_args = encode_args(&["init_value"]);
        runtime.call_function(env, &address, "initialize", &init_args).await?;
        
        Ok(address)
    }
    
    pub fn encode_args(args: &[&str]) -> Vec<u8> {
        // Your encoding logic
        vec![]
    }
}
```

## Mock Runtime for Testing

Create a mock runtime for fast testing:

```rust
use blockchain_runtime::*;
use async_trait::async_trait;

pub struct MockRuntime {
    deployed_contracts: std::sync::Arc<std::sync::Mutex<HashMap<String, Vec<u8>>>>,
}

impl MockRuntime {
    pub fn new() -> Self {
        Self {
            deployed_contracts: std::sync::Arc::new(std::sync::Mutex::new(HashMap::new())),
        }
    }
}

#[async_trait]
impl BlockchainRuntime for MockRuntime {
    fn blockchain_id(&self) -> &str {
        "mock"
    }
    
    async fn create_environment(&self, _config: RuntimeConfig) -> anyhow::Result<RuntimeEnvironment> {
        Ok(RuntimeEnvironment {
            environment_id: "mock-env".to_string(),
            blockchain_id: "mock".to_string(),
            runtime_type: RuntimeType::InMemory,
            endpoint_url: "mock://localhost".to_string(),
            state: EnvironmentState::Ready,
            metadata: HashMap::new(),
        })
    }
    
    async fn deploy_contract(
        &self,
        _env: &RuntimeEnvironment,
        bytecode: &[u8],
        _constructor_args: &[u8],
    ) -> anyhow::Result<String> {
        let address = format!("0x{:040x}", rand::random::<u128>());
        self.deployed_contracts.lock().unwrap().insert(address.clone(), bytecode.to_vec());
        Ok(address)
    }
    
    // ... implement other methods with mocked behavior
    
    async fn destroy(&self, _env: RuntimeEnvironment) -> anyhow::Result<()> {
        Ok(())
    }
    
    async fn is_available(&self) -> bool {
        true
    }
    
    fn capabilities(&self) -> RuntimeCapabilities {
        RuntimeCapabilities {
            supports_contract_deployment: true,
            supports_function_calls: true,
            supports_state_inspection: true,
            supports_event_monitoring: true,
            supports_gas_estimation: true,
            supports_time_travel: true,
            max_execution_time_seconds: 60,
        }
    }
}
```

## CI/CD Integration

### GitHub Actions Example

```yaml
name: Contract Tests

on: [push, pull_request]

jobs:
  test:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v3
      
      - name: Install Rust
        uses: actions-rs/toolchain@v1
        with:
          toolchain: stable
      
      - name: Run tests
        run: cargo test
        
      - name: Run integration tests
        run: cargo test --test integration_tests
```

### Docker Compose for Tests

```yaml
version: '3.8'

services:
  ethereum-node:
    image: ethereum/client-go:latest
    ports:
      - "8545:8545"
    command: --dev --http --http.api eth,web3,net
  
  test-runner:
    build: .
    depends_on:
      - ethereum-node
    command: cargo test
```

## Performance Testing

### Benchmark Suite

```rust
use criterion::{black_box, criterion_group, criterion_main, Criterion};

fn benchmark_deployment(c: &mut Criterion) {
    let runtime = tokio::runtime::Runtime::new().unwrap();
    
    c.bench_function("deploy_contract", |b| {
        b.iter(|| {
            runtime.block_on(async {
                let blockchain_runtime = get_test_runtime();
                let env = blockchain_runtime.create_environment(RuntimeConfig::default()).await.unwrap();
                
                black_box(blockchain_runtime.deploy_contract(&env, &bytecode, &[]).await.unwrap());
                
                blockchain_runtime.destroy(env).await.unwrap();
            })
        })
    });
}

criterion_group!(benches, benchmark_deployment);
criterion_main!(benches);
```

## Fuzzing

### Contract Fuzzing

```rust
#[tokio::test]
async fn fuzz_contract() {
    let runtime = get_test_runtime();
    let env = runtime.create_environment(RuntimeConfig::default()).await.unwrap();
    let address = deploy_contract(&runtime, &env).await.unwrap();
    
    for _ in 0..1000 {
        let random_args = generate_random_args();
        
        let result = runtime.call_function(&env, &address, "fuzz_target", &random_args).await;
        
        // Should not panic or crash
        match result {
            Ok(_) => { /* expected */ }
            Err(e) => {
                // Check error is handled gracefully
                assert!(!e.to_string().contains("panic"));
            }
        }
    }
    
    runtime.destroy(env).await.unwrap();
}
```

## Coverage Analysis

```bash
# Install cargo-llvm-cov
cargo install cargo-llvm-cov

# Run tests with coverage
cargo llvm-cov --html

# Open coverage report
open target/llvm-cov/html/index.html
```

## Snapshot Testing

```rust
#[tokio::test]
async fn test_contract_state_snapshot() {
    let runtime = get_test_runtime();
    let env = runtime.create_environment(RuntimeConfig::default()).await.unwrap();
    
    let result = runtime.execute(&env, Path::new("contract.sol"), &inputs).await.unwrap();
    
    // Compare state changes to snapshot
    insta::assert_json_snapshot!(result.state_changes);
    
    runtime.destroy(env).await.unwrap();
}
```

## Best Practices

### 1. Parallel Test Execution

```rust
#[tokio::test(flavor = "multi_thread")]
async fn test_parallel_deployments() {
    let runtime = get_test_runtime();
    
    let tasks: Vec<_> = (0..10)
        .map(|i| {
            let runtime = runtime.clone();
            tokio::spawn(async move {
                let env = runtime.create_environment(RuntimeConfig::default()).await?;
                let address = runtime.deploy_contract(&env, &bytecode, &[]).await?;
                runtime.destroy(env).await?;
                anyhow::Ok(address)
            })
        })
        .collect();
    
    for task in tasks {
        task.await.unwrap().unwrap();
    }
}
```

### 2. Test Data Management

```rust
mod test_data {
    pub fn sample_token_bytecode() -> Vec<u8> {
        include_bytes!("../test_data/Token.bin").to_vec()
    }
    
    pub fn sample_nft_bytecode() -> Vec<u8> {
        include_bytes!("../test_data/NFT.bin").to_vec()
    }
}
```

### 3. Assertion Helpers

```rust
fn assert_successful_execution(result: &ExecutionResult) {
    assert!(result.success, "Execution failed: {:?}", result.error);
    assert!(result.error.is_none());
}

fn assert_gas_under(result: &ExecutionResult, max_gas: u64) {
    if let Some(gas) = result.metrics.get("gas_used") {
        let gas_used = gas.as_u64().unwrap_or(u64::MAX);
        assert!(gas_used < max_gas, "Gas {} exceeds limit {}", gas_used, max_gas);
    }
}
```

## Debugging Tests

### Enable Logging

```rust
#[tokio::test]
async fn test_with_logging() {
    tracing_subscriber::fmt::init();
    
    let runtime = get_test_runtime();
    // Test with full logging
}
```

### Inspect State

```rust
#[tokio::test]
async fn test_with_state_inspection() {
    let result = runtime.execute(&env, code, &inputs).await.unwrap();
    
    // Print all state changes
    println!("State changes:");
    for change in &result.state_changes {
        println!("  {:?}", change);
    }
    
    // Print all events
    println!("Events:");
    for event in &result.events {
        println!("  {:?}", event);
    }
}
```

## Conclusion

Effective testing with Blockchain Runtime:
- Use appropriate test types (unit, integration, property-based)
- Leverage mock runtime for fast tests
- Integrate with CI/CD
- Monitor performance
- Achieve high coverage

See [User Guide](./user-guide.md) for more usage patterns.