goblockchain
This repo contains a simple/basic blockchain realisation in Go, with a basic code organization. We use:
- gin-gonic/gin package to start and serve HTTP server
- crypto/sha1 to get SHA1 hashes
goblockchain use Taskfile (a Makefile alternative).
Please read the Building Blockchain from Scratch in Python article in order to know more about this repository.
Build the app
$ go build -o bin/goBlockChain internal/main.go
or
$ task build
Run the app
$ ./bin/goBlockChain
or
$ task run
The default port is 8080, you can test the application in a browser or with curl:
$ curl 127.0.0.1:8080/chain
You can choose a different port and run more than one copy of goBlockChain on your local host. For example:
$ ./bin/goBlockChain -port 8081
List of the endpoints:
- GET /ping check node status
- GET /mine mine a new block
- GET /chain get the current state of the blockchain on a node
- GET /nodes/resolve get actual copy of the blockchain
- GET /mine/complexity/increase increase the difficulty of mining blocks
- GET /mine/complexity/decrease decrease the difficulty of mining blocks
- POST /transactions/new make a new transaction
- POST /nodes/register add a new node to the list of nodes
Example of sending a POST request to add a new transaction to the blockchain:
$ curl -X POST -H "Content-Type: application/json" -d '{ "sender": "1914116639ac11ec83092c6fc90649b9", "recipient": "7e93670390396556d432206c1c3231fbb", "amount": 10}' "http://localhost:8080/transactions/new"