Skip to content

Just a self-made implementation of a Blockchain, with the purpose to understand how things work under the hood... :)

License

Notifications You must be signed in to change notification settings

gvko/blockchain

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

12 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

blockchain

Just a self-made implementation of a Blockchain, with the purpose to understand how things work under the hood... :)

API

Get the blockchain of the current node

Returns the Blockchain of the current node.

Request: GET /chain

Response:

{
    "chain": [
        {
            "index": 1,
            "timestamp": 1509811828046,
            "transactions": [],
            "proof": 100,
            "previousHash": 1
        },
        {
            "index": 2,
            "timestamp": 1509811857403,
            "transactions": [
                {
                    "sender": "cfd49fee-dd0b-47a3-b7b4-b0bff0f12fcf",
                    "recipient": "cfd49fee-as6d-47jk-23b4-ua4o60f12fcf",
                    "amount": 5
                },
                {
                    "sender": "0",
                    "recipient": "c8968a3e-dac8-4c35-b1bb-be1a594b2ad5",
                    "amount": 1
                }
            ],
            "proof": 40545,
            "previousHash": "951ee32d76e9e671da00539d7263c52c5553b905f4e0508bca11c0eb50947254"
        }
    ],
    "length": 2
}

Create a new transaction

Registers a new transaction in the current node's Blockchain, that will be added to the next mined block.

Request: POST /transactions/new

{
	"sender": "cfd49fee-dd0b-47a3-b7b4-b0bff0f12fcf",
	"recipient": "cfd49fee-as6d-47jk-23b4-ua4o60f12fcf",
	"amount": 5
}

Response:

"New transaction will be added to block 2"

Mine a new block

Mines a new block in the current node's Blockchain.

Request: GET /mine

Response:

{
    "message": "New Block forged.",
    "index": 2,
    "transactions": [
        {
            "sender": "cfd49fee-dd0b-47a3-b7b4-b0bff0f12fcf",
            "recipient": "cfd49fee-as6d-47jk-23b4-ua4o60f12fcf",
            "amount": 5
        },
        {
            "sender": "0",
            "recipient": "c8968a3e-dac8-4c35-b1bb-be1a594b2ad5",
            "amount": 1
        }
    ],
    "proof": 40545,
    "previousHash": "951ee32d76e9e671da00539d7263c52c5553b905f4e0508bca11c0eb50947254"
}

Register a new node

Registers a new node (obviously...).

NOTE: You have to have another server instance running for each node. Pass the URL + port of the instance.

Request: POST /nodes/register

{
	"nodes": [
		"http://127.0.0.1:3001"
	]
}

Response:

{
    "message": "New nodes have been added",
    "nodes": [
        "127.0.0.1:3001"
    ]
}

Resolve the nodes' Blockchains

The node with the longest Blockchain is the authoritative.

Request: GET /nodes/resolve

Response:

If our chain was shorter:

{
    "message": "Our chain was replaced",
    "newChain": [
        // the blocks of the Blockchain as objects
    ]
}

If our chain was longer:

{
    "message": "Our chain is authoritative",
    "chain": [
        // the blocks of the Blockchain as objects
    ]
}

About

Just a self-made implementation of a Blockchain, with the purpose to understand how things work under the hood... :)

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published