A tracer SDK for Overlord like consensus algorithm, helps you to debug or optimize the algorithm.
The consensus algorithm always plays with a distributed system, debugging or optimizing is so hard. If we can record events to describe what happens when consensus state changes, and then replay with a visualization dashboard, the debugging or optimizing would be easier.
Let's starts with a dead simple trace. This example shows how to use the sdk for write the TracePoint in file with JSON format.
Cargo.toml
toml
[dependencies]
moodyblues-sdk = { git = "https://github.com/nervosnetwork/moodyblues-client-rust" }
consensus.rs ```rust use moodyblues_sdk::trace;
struct ConsensusStateMachine { epochid: u64, roundid: u64 }
impl ConsensusStateMachine { fn newepoch(&mut self, epochid: u64) { self.epochid = self.epochid + 1; self.roundid = 0; // create a trace point mark as starts with epoch trace::startepoch(epoch_id); }
fn newround(&mut self, roundid: u64) { self.round_id = self.round;
// create a trace point mark as starts with round
trace::start_round(round_id);
} }
struct Consensus;
impl Consensus { fn verifysingature(signature: Signature, hash: Hash) { trace::reportcustom("verifysingature".tostring(), Some(json!({ "hash": hash, "signature": signature }))) } } ```
main.rs
```rust use std::fs::File; use std::io::Write; use std::sync::Mutex;
use serdejson::{json, tostring};
use moodybluessdk::event::{EventType, TraceEvent}; use moodybluessdk::point::{Metadata, TracePoint}; use moodyblues_sdk::trace;
struct WriteReporter
impl
impl
fn metadata(&self) -> Metadata {
// information of current node
Metadata {
address: "0x0000000000000000000000000000000000000000".to_string(),
}
}
fn now(&self) -> u64 {
// timestamp for each point
now()
}
}
fn main() { trace::setboxedtracer(WriteReporter::new(File::create("log.log").unwrap())); } ```
TODO for now, jump to trace.rs
for more information. The API may be frequent changes before the first release version.