Skip to content

Instantly share code, notes, and snippets.

@dvf
Last active June 29, 2019 19:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dvf/731ecdf992893284636673f533f907fd to your computer and use it in GitHub Desktop.
Save dvf/731ecdf992893284636673f533f907fd to your computer and use it in GitHub Desktop.
Step 8: The Mining Endpoint
import hashlib
import json
from time import time
from uuid import uuid4
from flask import Flask, jsonify, request
...
@app.route('/mine', methods=['GET'])
def mine():
# We run the proof of work algorithm to get the next proof...
last_block = blockchain.last_block
last_proof = last_block['proof']
proof = blockchain.proof_of_work(last_proof)
# We must receive a reward for finding the proof.
# The sender is "0" to signify that this node has mined a new coin.
blockchain.new_transaction(
sender="0",
recipient=node_identifier,
amount=1,
)
# Forge the new Block by adding it to the chain
previous_hash = blockchain.hash(last_block)
block = blockchain.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
@Niedzwiedzw
Copy link

What's node_identifier

@d2718nis
Copy link

The node_identifier defined a little bit earlier at the Step 6: Setting up Flask. Briefly, it's a unique identifier of the node (aka the miner) which successfully produced a new block: solved the mathematical problem known as proof of work. It will be rewarded with 1 coin of revenue at this example.

You could get more info regarding the mining process here: https://en.bitcoin.it/wiki/Mining#Reward

@william0353
Copy link

I got an error: Flask not found

from flask import Flask, jsonify, request
ModuleNotFoundError: No module named 'flask'

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment