mod.rs

Overview

This file provides functionality related to conditional misbehavior simulation, enabled via the misbehave feature flag. It defines data structures and a function to load misbehavior rules from a YAML configuration file, which can be used to simulate specific faulty behaviors in the system, particularly for testing purposes involving fork scenarios.

Data Structures

MisbehaveRules

A container struct representing the set of misbehavior rules loaded from configuration.

if let Ok(Some(rules)) = misbehave_rules() {
    println!("Loaded misbehavior rules: {:?}", rules);
}

ForkTest

Represents parameters defining a fork test interval, specifying the range of sequence numbers where the fork misbehavior applies.

println!("Fork test from {} to {}", fork_test.from_seq, fork_test.to_seq);

Functions

misbehave_rules() -> anyhow::Result<Option<MisbehaveRules>>

Reads misbehavior rules from a YAML file specified by the environment variable MISBEHAVE_RULES_PATH. Returns an optional MisbehaveRules instance if the file is present and parsed successfully.

match misbehave_rules() {
    Ok(Some(rules)) => {
        // Apply misbehavior rules
    }
    Ok(None) => {
        // Misbehavior disabled
    }
    Err(e) => {
        eprintln!("Failed to load misbehavior rules: {}", e);
    }
}

Implementation Details

Interaction with Other System Components

Diagram: Module Structure and Workflow

flowchart TD
A["Start: misbehave_rules()"] --> B{MISBEHAVE_RULES_PATH set?}
B -- Yes --> C[Open file at path]
C --> D[Read file contents]
D --> E[Parse YAML into MisbehaveRules]
E --> F["Return Ok(Some(MisbehaveRules))"]
B -- No --> G["Return Ok(None)"]
C -->|Error| H[Return Err]
D -->|Error| H
E -->|Error| H

This flowchart illustrates the decision and processing flow within the misbehave_rules function, showing how the environment variable drives file reading and YAML parsing leading to loading or disabling misbehavior rules.