ThornChain is a simplistic cryptocurrency built in Golang in my spare time. The goal is to have a fully functional and independent peer to peer blockchain that can send and receive coins. The consensus mechanism utilised is Proof of Stake.
This is just a self education project to start learning about blockchain development.
- HTTP server:
- Current blockchain state is available at
localhost:8080/blocks
- Node's current peerstore is available at
localhost:8080/peers
- Find balance of an address at
localhost:8080/getbalance/{address}
- Write blocks via
POST
requests tolocalhost:8080
- Current blockchain state is available at
- Peer to peer network:
- Each node acts as a host and as a listener for other nodes
- New nodes request a copy of the blockchain and peer store immediately
- Blockchain state and peer store propagates among nodes
- Orphaned nodes will try and reconnect to other nodes in the network
- Proof of Stake:
- Every node in the network is a potential block validator. The chance of being chosen to validate a block depends on the balance of the node's address (can be found under peer ID in
localhost:8080/peers
) - The winner is determined based on a random number generated with a seed. The seed for the random number generator is the hash of the previous block. This means that all nodes can perform the same calculation and reach the same result and use it to verify the validity of each block.
- Every node in the network is a potential block validator. The chance of being chosen to validate a block depends on the balance of the node's address (can be found under peer ID in
- Basic blockchain functions:
- Write blocks in JSON format in the console
- Block hash calculations
- Block generation
- Block validations
The wallet/balance logic is pretty terrible at the moment, something to work on in the future.
A JSON block structure can be sent to the node in either the console of the node or sent via POST
request to localhost:8080
. The other information such as block validator, hash and previous hash will automatically be recorded.
{"from":"Microsoft","to": "IBM","amount":1000}
Unfortunately, I used a library that's fairly difficult to install/work with. The setup is more complicated than it should be, but the below steps should be good enough to follow.
Assuming you already have Golang installed:
- Install go-libp2p.
go get -v -d github.com/libp2p/go-libp2p/...
- Navigate to the cloned directory above
- Install package.
make
- Install dependencies.
make deps
- Navigate to where you want to install the development environment
- Install the development environment.
go get -v -d github.com/libp2p/go-libp2p-examples
- Navigate into the go-libp2p-examples folder
- Install ThornChain.
git clone https://github.com/ThornM9/ThornChain.git
- Install some random dependencies:
go get -v -d github.com/gorilla/mux
go get -v -d github.com/davecgh/go-spew/spew
go get -v -d github.com/joho/godotenv
- Navigate into the ThornChain directory
- Finally, run the node.
go run main.go
With any luck, that should be it. Check current features for usage.