Giter VIP home page Giter VIP logo

tinychain's Introduction

Build A Blockchain with Rust

English | 简体中文

Table of Contents

Intro

This project aims to demonstrate the basic principles of blockchain through a distributed ledger. The main features include:

  • HTTP JSON API provides users with interfaces such as transfer and some query apis;
  • P2P Protocol is used for interaction between nodes, and data is serialized/deserialized by protobuf. The functions include peer discovery, transaction broadcast, block broadcast, and block synchronization;
  • PoW is used as the consensus mechanism;
  • Sled, an embedded key-value database, is used as the storage backend;
  • For the convenience of demonstration, there is a wallet in each node that stores the users' private keys, so that the node can sign the transaction on behalf of users.

Architecture

See 01 | Architecture for details.

From a holistic perspective, this project is a workspace, consisting of three crates: tinychain, tinyp2p, and wallet.

  • tinychain: core business.
  • tinyp2p: a tinychain-specific p2p protocol based on rust-libp2p.
  • wallet: user private key management.

tinychain | Dependency Inversion

See 01 | Architecture for details.

In tinychain, it is divided into three layers according to responsibilities:

  • Network Layer: responsible for interacting with the outside world, including processing HTTP requests and interacting with other peers.
  • Biz Layer: based on the principle of Dependency Inversion, it defines the behavior (traits) of the network and data layers, getting rid of the dependency on them.
    • trait PeerClient: the network needs to implement this trait to send data to other nodes.
    • trait State: the data needs to implement this trait to save the local state.
  • Data Layer: responsible for saving the state.

tinychain::biz | Read/Write Separation

See 09 | Biz Layer: How to Do Read/Write Separation? for details.

The biz layer achieves lock-free programming through read/write separation. That is to say, any thread can "read", but only one thread can "write". In this project, there are two main write operations: (1) Adding user transfer data to the transaction pool; (2) Adding blocks to the database. From the above figure, only the Miner thread has write permission. When other threads need to write, they send the data to the Miner to write via the channel.

tinyp2p | CSP Concurrency Model

See 07 | tinyp2p: A CSP Concurrency Model for details.

  • p2p_client is used to process user requests. In p2p_client, the request is converted to cmd and sent to the channel.
  • A background thread exclusively owns mut p2p_server, and gets cmd from the channel one by one to execute.
  • Users can register event_handlers in p2p_server. When data is received from a remote node, event_handlers are called to process the data.

Demo

See 10 | Data Layer & Demo for details.

  1. View the commands: RUST_LOG=info ./target/debug/tinychain

  2. Create an account: RUST_LOG=info ./target/debug/tinychain new-account

  3. Query account balance and block information

tinychain's People

Contributors

jacob-chia 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.