A reference implementation of blockchain in Go to demonstrate how blockchain works. For education purpose.

Mini-Blockchain

Mini-Blockchain is a reference design for a blockchain system to demostate a full end2end flow in current blockchain technology.

There are so many open source projects of blockchain implementation, like ethereum, bitcoin, eos etc.. But even running their code will take u couple of days to setup.

The goal of the project is to build a "Hello-world" version of blockchain to help people understand how the foundamental technology without wasting time reading in a huge code base, to simulate the different roles, like user, miner, booster, and demostrate how they participate the blockchain network. And it also simplify the deployment, instead of configuring tons of library and debugging a version in a real distributed network, it is single machine version to easy the debug, test, and iterate.

Since it is primarily designed as a learning tool for people to learn the blockchain end to end. It is meant to be as simple to understand as possible while still providing clear structure and workflow.

What is this not?

Mini-Blockchain is not meant for real usage as it is a simplified version and has many hard coded parameters. It's just meant for learning.

It also may just simply stop working at any time since there are some depandences.

What is included?

This reference system is written in Go.(Don't ask me why use Go. I just fucking lost my mind.)

  • a main.go to boost the blockchian and simnulate the workflow
  • ./core to implement the blockchain
  • ./role to implement different actors in the blockchain ecosystem.
  • ./test to implement some unit tests to keep the code in some quality(even very little)

Parts of the system

This is how bitcoin mining works. I follow the same flow except checking the 2016th block.

SOME DEFINITIONS:

User - anyone who as an account(hash address) is a user. User holds the coins in their account and is able to send/receive coins to/from other users.

Transaction - any coin transfer between two users is a transaction. The activities of users will generate transaction, which is the source of transaction pool.

Miner - a special role, who doesn't generate transaction, but collect/validate transaction from the pool.

Block - a collection of validated transactions. Every miner can propose a block, but only the one acknowledged by most of the miners will be the official block in the chian.

Difficulty - a measure of how difficult it is to find a hash below a given target. Valid blocks must have a hash below this target. Mining pools also have a pool-specific share difficulty setting a lower limit for shares. In bitcoin, the network difficulty changes every 2016 blocks. For my implementation, the difficulty changes every block.

Nonce - a 32-bit (4-byte) field to random the hash generation. Any change to the block data (such as the nonce) will make the block hash completely different. The resulting hash has to be a value less than the current difficulty and so will have to have a certain number of leading zero bits to be less than that. As this iterative calculation requires time and resources, the presentation of the block with the correct nonce value constitutes proof of work.

Reward - when a block is discovered, the miner may award themselves a certain number of bitcoins, which is agreed-upon by everyone in the network. Normally the rewarding transaction is the first transaction in a block proposed by the miner.

Fee - The miner is also awarded the fees paid by users sending transactions. The fee is an incentive for the miner to include the transaction in their block. In the future, as the number of new bitcoins miners are allowed to create in each block dwindles, the fees will make up a much more important percentage of mining income. Ethereum is a good example of fee usage.

How does the simulated workflow work?

First, it will start to initialize an empty blockchain. Note, for every blockchain project, boosting it from beginning is the tricky part. In this implementation, I created a empty chian and then add a empty block as the head.

Second, it creates 1 miner to represent Consortium Blockchain(if you don't know what is consortium blockchain, read this). The miner will submit several empty blocks and earn rewards for each block.

Third, it creates 10 users and vest some coins for users to trade with each other. The vesting is implemented by transferring the coins from miner to each user.

Forth, each user will start to randomly trade with each other and submit his transaction to the trasnactuon pool for mining.

Fifty, miner keeps mining, validate transaction, confirm block.

Cool, we got that? Great. Let's actually build this thing now.

Building the code

First, you need to install Golang if Mac

brew install go 

if Ubuntu

sudo apt-get update && sudo apt-get -y upgrade && sudo apt-get install -y golang-go

Once you have installed and rebooted, log in, then open up the program “terminal.” Now run the command…

sudo apt-get update && sudo apt-get -y upgrade && sudo apt-get -y install git

Now you can clone the repository to get the full code.

cd ~/ && git clone https://github.com/codingtmd/mini-blockchain.git 

Do not forget to download the missing library:

go get -d -v .

Go to the directory and build the code

go build

Then run it with fun

go run main.go

And you will see the console output as below workflow

If you need an IDE, normally I use microsoft visual studio code and Go Plugin

Things to know

The logging uses loggo. Plz check the configuration and usage here: https://github.com/juju/loggo

Cool future work / Areas you can contribute / TODOs

  • Use msg to communicate infro between miners, users. (Currently just function call)
  • Same-input tramnsaction merging
  • Add multi-miner support
  • A simple script to initialize a blockchain.
  • Create a config for all hard code parameters.
  • Better logging.
  • Make a web UI to look into operation details, like etherscan.io
  • Create PoS support
  • Create a wallet implementation and web UI
  • Add some animations that make it easier to understand.
  • Build a dApp
  • Add ICO simulation like how to vest coins to user

Reading List

If you are not familiar with blockchain and its technology, below info will help u to ramp up the knowledge.

Articles:

Some white papers

Authors

Lei Zhang, https://www.linkedin.com/in/codingtmd

Contact me

Leave me a message if you want to participate for fun.

Similar Resources

Data structure,Algorithms implemented in Go (for education)

 Data structure,Algorithms implemented in Go (for education)

Data structure,Algorithms implemented in Go (for education) List of Content : 1. Math - 2. String - 3. Conversions - 4. Sort - 5. Search - 6. Data str

Dec 13, 2022

GoPlus - The Go+ language for engineering, STEM education, and data science

The Go+ language for engineering, STEM education, and data science Summary about Go+ What are mainly impressions about Go+? A static typed language. F

Jan 8, 2023

Kava - Reference implementation of Kava, a blockchain for cross-chain DeFi. Built using the cosmos-sdk

DeFi for Crypto. Telegram | Medium | Discord Reference implementation of Kava, a

Apr 4, 2022

This is an example to demonstrate implementation golang microservices using domain driven design principles and sugestions from go-kit

go-kit DDD Domain Driven Design is prevelent and rising standard for organizing your microservice code. This design architecture emphasis on Code orga

Feb 9, 2022

Go language implementation of a blockchain based on the BDLS BFT protocol. The implementation was adapted from Ethereum and Sperax implementation

BDLS protocol based PoS Blockchain Most functionalities of this client is similar to the Ethereum golang implementation. If you do not find your quest

Oct 14, 2022

Server-sent live updates: protocol and reference implementation

Server-sent live updates: protocol and reference implementation

Protocol and Reference Implementation Mercure is a protocol allowing to push data updates to web browsers and other HTTP clients in a convenient, fast

Jan 1, 2023

Merchant API reference implementation

mAPI More details available in the BRFC Spec for Merchant API. The old golang (v1.1) implementation is no longer being maintained and has been moved t

Dec 14, 2022

Sample Driver that provides reference implementation for Container Object Storage Interface (COSI) API

cosi-driver-minio Sample Driver that provides reference implementation for Container Object Storage Interface (COSI) API Community, discussion, contri

Oct 10, 2022

Reference go implementation of globalDCE protocol

globalDCE-go This is the reference implementation of the command line interface of globalDCE coded in the go programming language. This project is sti

Nov 15, 2021

Reference go implementation of globaldce protocol

globaldce-go This is the reference implementation of the command line interface of globaldce coded in the go programming language. This project is sti

Nov 8, 2021

Reference implementation of globaldce protocol coded in go

globaldce This is the reference implementation of the command line interface of globaldce coded in the go programming language. This project is still

Nov 15, 2021

Reference implementation of the PLAN Data Model and core components

PLAN is a free and open platform for groups to securely communicate, collaborate, and coordinate projects and activities.

Nov 2, 2021

An OpenID Connect reference implementation in Golang

oidc-go-client An OpenID Connect reference implementation in Golang Getting started First clone the repository: git clone https://github.com/yufuid/oi

Dec 3, 2021

Reference Golang implementation of the Alphanet - Network of Momentum Phase 0

Zenon Node Reference Golang implementation of the Alphanet - Network of Momentum Phase 0. Building from source Building znnd requires both a Go (versi

Dec 25, 2022

Rosetta-ergo provides a reference implementation of the Rosetta API for Ergo in Golang

Rosetta-ergo provides a reference implementation of the Rosetta API for Ergo in Golang

Rosetta Ergo Overview rosetta-ergo provides a reference implementation of the Rosetta API for Ergo in Golang. If you haven't heard of the Rosetta API,

Aug 9, 2022

whatsup is the reference server implementation for the fmrl protocol

whatsup whatsup is the reference server implementation for the fmrl protocol. Currently whatsup has no web interface, but may gain one in the future.

Mar 25, 2022

Simple project to demonstrate the loading of eBPF programs via florianl/go-tc.

tc-skeleton Simple project to demonstrate the loading of eBPF programs via florianl/go-tc.

Dec 23, 2022

A template project to demonstrate how to run WebAssembly functions as sidecar microservices in dapr

A template project to demonstrate how to run WebAssembly functions as sidecar microservices in dapr

Live Demo 1. Introduction DAPR is a portable, event-driven runtime that makes it easy for any developer to build resilient, stateless and stateful app

Jan 3, 2023

Simple Golang API to demonstrate file upload to fireabase storage and retrieving url of uploaded file.

go-firebase-storage -Work in progress 🛠️ Simple Golang API that uses Firebase as its backend to demonstrate various firebase services using Go such a

Oct 4, 2021
Go implementation of systemd Journal's native API for logging

journald Package journald offers Go implementation of systemd Journal's native API for logging. Key features are: based on a connection-less socket wo

Dec 23, 2022
A Statsd implementation written in GO lang

statsgod Statsgod is a metric aggregation service inspired by the statsd project. Written in Golang, it increases performance and can be deployed with

Oct 1, 2022
A logr LogSink implementation using Zerolog

Zerologr A logr LogSink implementation using Zerolog. Usage import ( "os" "github.com/go-logr/logr" "github.com/hn8/zerologr" "github

Nov 17, 2022
An open source Pusher server implementation compatible with Pusher client libraries written in GO

Try browsing the code on Sourcegraph! IPÊ An open source Pusher server implementation compatible with Pusher client libraries written in Go. Why I wro

Jan 3, 2023
An implementation of A* in Golang

General This is an implementation of the a star path finding algoritm written in Golang. State This software is in pre-alpha state. Development starte

Jan 7, 2022
A logr LogSink implementation using bytes.Buffer

buflogr A logr LogSink implementation using bytes.Buffer. Usage import ( "bytes" "fmt" "github.com/go-logr/logr" "github.com/tonglil/buflogr" )

Jan 6, 2023
Gale-Shapley algoritm implementation in Go

Stable matching Gale-Shapley algoritm implementation in Go. Inspired by Numberphile video. See the explanation on Wikipedia Inputs N×N table of propos

Feb 12, 2022
A golang implementation of the Open Pixel Control protocol

__ ___ ___ _____ ___ /'_ `\ / __`\ _______ / __`\/\ '__`\ /'___\ /\ \L\ \/\ \L\ \/\______\/\ \L\ \ \ \L\ \/\ \__/ \ \

Jul 3, 2022
The full power of the Go Compiler directly in your browser, including a virtual file system implementation. Deployable as a static website.
The full power of the Go Compiler directly in your browser, including a virtual file system implementation. Deployable as a static website.

Static Go Playground Features Full Go Compiler running on the browser. Supports using custom build tags. Incremental builds (build cache). Supports mu

Jun 16, 2022
A test repo to demonstrate the current (go1.17.2) issue when trying to use retractA test repo to demonstrate the current (go1.17.2) issue when trying to use retract

test-go-mod-retract This is a test repo to demonstrate the current (go1.17.2) issue when trying to use retract in go.mod to retract a version in a non

Oct 16, 2021