Skip to content

Reverendyz/blockchain-example

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

3 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Blockchain example

A project written in go that uses gin-gonic/gin to expose an REST endpoint that receives a data field and hashes it to get a valid hash depending on the difficulty level provided.

Running in your local

Pre-reqs

Clone this repository and

go run cmd/server/main.go

this will expose the endpoint in your localhost (127.0.0.1) with 8080 port as default. If you want to address another IP or port, use HOST and PORT environment variable.

Example request

curl -X POST http://localhost:8080/hash \
  -H "Content-Type: application/json" \
  -d '{
    "version": "1.0",
    "data": [
      {
        "source": "Alice",
        "destination": "Bob",
        "value": 100,
        "id": 1
      },
      {
        "source": "Bob",
        "destination": "Charlie",
        "value": 50,
        "id": 2
      }
    ],
    "difficultyTarget": 5
  }'

Example Response

{
  "version": "1.0",
  "previousHash": "",
  "data": [
    {
      "source": "Alice",
      "destination": "Bob",
      "value": 100,
      "id": 1
    },
    {
      "source": "Bob",
      "destination": "Charlie",
      "value": 50,
      "id": 2
    }
  ],
  "difficultyTarget": 6,
  "nonce": 15924360,
  "hash": "00000037c62d3ad024b26d6cbf5a19a5917744e14ae813b9403133e6d00407fa"
}

Logic behind this project

Inside a block chain you feed the previous hash to the new block with the current transactions ongoing. Meanwhile, the script will try to use the compute power to find the valid has, defined by its difficulty level, so it can mine that block and pass in to the another one.

That iteration will hash the data with all transactions done in the block and will include a new field called Nonce that basically will tell how many iterations the code done to get the "valid" block.

Purpose

This is a learning project from someone that likes the blockchain logic and exploring some golang capabilities.

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages