Coder Social home page Coder Social logo

moodyblues-client-rust's Introduction

MoodyBlues SDK For Rust

Intro

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.

Quick start

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

[dependencies]
moodyblues-sdk = { git = "https://github.com/nervosnetwork/moodyblues-client-rust" }
use std::fs::File;
use std::io::Write;
use std::sync::Mutex;

use serde_json::{json, to_string};

use moodyblues_sdk::event::{EventType, TraceEvent};
use moodyblues_sdk::point::{Metadata, TracePoint};
use moodyblues_sdk::trace;
use moodyblues_sdk::time::now;

struct ConsensusStateMachine {
    epoch_id: u64,
    round_id: u64,
}

impl ConsensusStateMachine {
    fn new_epoch(&mut self, epoch_id: u64) {
        self.epoch_id = &self.epoch_id + 1;
        self.round_id = 0;
        // create a trace point mark as starts with epoch
        trace::start_epoch(epoch_id);
    }

    fn new_round(&mut self, round_id: u64) {
        self.round_id = round_id;

        // create a trace point mark as starts with round
        trace::start_round(self.round_id, self.epoch_id);
    }
}

struct Consensus;

impl Consensus {
    fn verify_signature(signature: String, hash: String) {
        trace::custom(
            "verify_signature".to_string(),
            Some(json!({
              "hash": hash,
              "signature": signature
            })),
        )
    }
}

struct WriteReporter<W: Write + Send + 'static> {
    reporter: Mutex<W>,
}

impl<W: Write + Send + 'static> WriteReporter<W> {
    fn new(writable: W) -> Box<WriteReporter<W>> {
        Box::new(WriteReporter {
            reporter: Mutex::new(writable),
        })
    }
}

impl<W: Write + Send + 'static> trace::Trace for WriteReporter<W> {
    fn report(&self, point: TracePoint) {
        let mut file = self.reporter.lock().unwrap();
        file.write_all(to_string(&point).unwrap().as_bytes());
    }

    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::set_boxed_tracer(WriteReporter::new(File::create("log.log").unwrap()));
    trace::start_epoch(1);
}

Documentation

TODO for now, jump to trace.rs for more information. The API may be frequent changes before the first release version.

moodyblues-client-rust's People

Contributors

homura avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.