Giter VIP home page Giter VIP logo

minichain's Introduction

minichain

License CI Codecov

  • Using the freshest Python 3 syntax
  • UTXO model
  • Nakamoto consensus, or PoW
  • P2P part is powered by asyncio and kademlia
  • Elliptic curve part is powered by coincurve

How to run

Install dependencies first by running poetry install.

Open 3 terminals and run separately:

python -m chain 8999 --mine --debug # block time is around 5s
python -m chain 9000 -b 127.0.0.1 8999 --debug  # no mining
python -m chain 9001 -b 127.0.0.1 9000 --debug --mine  # connecting second node

How to implement

Find peers

This repo is leveraging Kademlia algorithm for finding peers via UDP and syncing blockchain via TCP.

You can create a UDP server and a TCP server listening at the same port to simplify the logic. When it's found peers by Kademlia, our node just directly connects to that IP and port to start syncing blockchain data.

Sync blocks

A simple example about syncing the latest block:

  1. Sending REQUEST_LATEST_BLOCK message to a peer or peers.
  2. When receiving REQUEST_LATEST_BLOCK, send back the message RECEIVE_LATEST_BLOCK with block data.
  3. When receiving RECEIVE_LATEST_BLOCK:
    1. Check the received block is valid or not;
    2. If valid, add it to our blockchain and broadcast it to peers;
    3. Else, check if the block is ahead;
      1. If ahead, sending REQUEST_BLOCKCHAIN to peers and wait for the incoming blockchain data.
      2. Else, which means our blockchain is the freshest, do nothing.

For more details, check the p2p.py code. The logic is simple, but more powerful protocols (like log replication of Raft protocol) are based on the simple ideas behind the implementation here.

Reference

Go check this and this to learn the basics and stay tuned with this repo!

minichain's People

Contributors

dependabot[bot] avatar dependabot-preview[bot] avatar kigawas avatar dependabot-support avatar neverknowstomorrow 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.