Python mini-blockChain (Transaction,POW,mining)🍪

암호화폐!

Here, I will introduce a simple implement of the mining a coin, which is important process to lock the block-chain. The processes are, creating transaction data, hash calculation, and proof-of-work (POW). Those will finally lock the block at the end of the chain. this whole process will be generated through Flask Server.




BLOCKCHAIN for DUMMIES

Everybody talks these days about the Blockchain. Everybody. But ask somebody about it. They will have a hard time trying to explain to you what is Blockchain.

[image ref.]: Blockchain for dummies



0. INTRO 👋

I made some simple structure of blockchain, but this is not for the project, but for the own personal study, the better understanding how blockchain work in simple way. So, please just refer it as one of many sample materials.



 Language   Library   Data form   Reference 
Python Flask JSON https://goo.gl/M6XU5v



1. HOW TO RUN

Run bash window on the target folder and type in ‘python server.py’. Then the first block, index 1, will be automatically generated if there is no stored history file named chain.json.

regarding chain.json file, there’s no chain.json at first. it is generated only if you like to. (I prepare ‘write’ menu on webpage)

When ‘chain.json’, a written history file, exists, the block history will be restored autonomously. To avoid at first, I changed the file name to ‘chain_old.json’, which has the several depth of block history.



a



2. HOW IT SHOWS

‘http://localhost:5000’ in browser



a



3. ABOUT JSON

In block_class, there’re 3 json files.

  1. blcok.json - the first intention, but to many hashs
  2. chains_old.json - already described on the top.
  3. genesis.json - Ethurium genesis block format, just refer to it

This is not exact as it can be, but just alludes coinbase transaction. Because it is compensation for mining, sender is nobody, recipient is randomly generated node ID and amount is set as 100, form of variable for mining reward.

1
2
3
4
5
    {
     "sender": "0",
     "recipient": "17460cdf96bc403ca792bf08a7f87d4d",
     "amount": 100
    }



a



4. OTHERS

As a beginner trying to bite a little dent on learning the blockchain technology, other constructive opinoins are always welcomed and let’s make it together! 😏



5. REFERENCES

  1. Making Blockchain with Python ( Only available Part.1)
  2. Python Blockchain (Create Block, Transaction, mining)



6. CODE PART

The rest of code part is at onitonitonito/block_chain_study

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
"""
# BlockChain API with Python Flask server
"""
# import json
# from textwrap import dedent

from uuid import uuid4
from flask import (Flask, jsonify, request, render_template, redirect)
from block_class.block_chain import BlockChain


app = Flask(__name__)

# to create 32-bits UID - '11864aaa-d1b2-45af-9c5a-c21dda71c6fc'
node_identifier = str(uuid4()).replace("-", "")

bc = BlockChain()


@app.route("/", methods=["GET"])
def index():
    return render_template('./chain_help.html')


@app.route("/chains", methods=["GET"])
def full_chain():
    response = {
        "chain": bc.chain,
        "length": len(bc.chain),
    }

    return jsonify(response), 200


@app.route("/mine", methods=["GET"])
def mine():
    # To Calculate POW, depending on the previous hash (compensation=100)
    last_block = bc.last_block
    last_proof = last_block["proof"]

    proof = bc.proof_of_work(last_proof)
    conpensation = 100

    bc.new_transaction(
        sender="0",
        recipient=node_identifier,
        amount=conpensation)

    previous_hash = bc.hash(last_block)
    block = bc.new_block(proof, previous_hash)

    response = {
        "message":         "... new block forged! ...",
        "index":           block["index"],
        "transactions":    block["transactions"],
        "proof":           block["proof"],
        "previous_hash":   block["previous_hash"]
    }

    return jsonify(response), 200


@app.route("/transactions/new", methods=["GET", "POST"])
def new_transaction():
    # to write and show the sender, recipient, amount if exist
    if request.method == "POST":
        # values = request.get_json()

        sender = request.form["sender"]
        recipient = request.form["recipient"]
        amount = request.form["amount"]

        values = {
            "sender": sender,
            "recipient": recipient,
            "amount": amount}

        required = ["sender", "recipient", "amount"]

        if not all(k in values for k in required):
            return "... missing values ...", 400

        index = bc.new_transaction(
            values["sender"],
            values["recipient"],
            values["amount"])

        response = {
            "message": "... Transaction will be added to Block {0}".format(index),
            "current TX": bc.current_transactions
        }
        return jsonify(response), 201

    else:
        return render_template("./new_TX.html"), 200


@app.route("/transactions", methods=["GET"])
def show_transaction():
    # In order to record in the block, query the collected, before
    response = bc.current_transactions
    return jsonify(response), 200


@app.route("/transactions/all", methods=["GET"])
def show_transaction_all():
    # all transaction details are extracted (mining compensation is excluded)
    echo = bc.show_all_transaction(bc.chain)
    echo = "<pre>" + echo + "</pre>"

    return echo

@app.route("/write", methods=["GET"])
def write_chains():
    # The block, collected so far is stored in the Json file.
    chains = bc.write_json()
    response = {
        "chains": chains,
        "message": "...  Writing whole chains to chains.json ..."
    }
    return jsonify(response), 200


if __name__ == "__main__":
    app.run(host="0.0.0.0", port=5000, debug=True)


Written on ... October 7, 2018