Compare commits
1
Commits
makefile
...
fixbadpython
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
ac9d8a108b |
@@ -1,11 +1,12 @@
|
|||||||
#!/usr/bin/env python
|
#!/usr/bin/env python
|
||||||
|
"""Flask server to serve the block API."""
|
||||||
|
|
||||||
from time import time
|
|
||||||
from flask import Flask, request
|
|
||||||
import json
|
import json
|
||||||
from uuid import uuid4
|
from time import time
|
||||||
from blockchain import Blockchain, Transaction, Block
|
|
||||||
import argparse
|
import argparse
|
||||||
|
from uuid import uuid4
|
||||||
|
from flask import Flask, request
|
||||||
|
from blockchain import Blockchain, Transaction, Block
|
||||||
|
|
||||||
parser = argparse.ArgumentParser(description="A blockchain simulation")
|
parser = argparse.ArgumentParser(description="A blockchain simulation")
|
||||||
parser.add_argument("-p", "--port", type=int, help="Port number", default=80)
|
parser.add_argument("-p", "--port", type=int, help="Port number", default=80)
|
||||||
@@ -15,18 +16,19 @@ port = args.port
|
|||||||
|
|
||||||
app = Flask(__name__)
|
app = Flask(__name__)
|
||||||
|
|
||||||
node_identifier = str(uuid4()).replace("-", "")
|
NODE_IDENTIFIER = str(uuid4()).replace("-", "")
|
||||||
|
|
||||||
blockchain = Blockchain()
|
blockchain = Blockchain()
|
||||||
|
|
||||||
|
|
||||||
@app.route("/mine", methods=["GET"])
|
@app.route("/mine", methods=["GET"])
|
||||||
def mine():
|
def mine():
|
||||||
|
"""Mine samy coin."""
|
||||||
last_block = blockchain.blocks[-1]
|
last_block = blockchain.blocks[-1]
|
||||||
last_proof = last_block.proof
|
last_proof = last_block.proof
|
||||||
proof = blockchain.proof_of_work(last_proof)
|
proof = blockchain.proof_of_work(last_proof)
|
||||||
|
|
||||||
transaction = Transaction("Network", node_identifier, 50)
|
transaction = Transaction("Network", NODE_IDENTIFIER, 50)
|
||||||
blockchain.new_transaction(transaction)
|
blockchain.new_transaction(transaction)
|
||||||
block = Block(
|
block = Block(
|
||||||
len(blockchain.blocks) + 1,
|
len(blockchain.blocks) + 1,
|
||||||
@@ -42,6 +44,7 @@ def mine():
|
|||||||
|
|
||||||
@app.route("/transactions/new", methods=["POST"])
|
@app.route("/transactions/new", methods=["POST"])
|
||||||
def new_transaction():
|
def new_transaction():
|
||||||
|
"""Submit a new transaction."""
|
||||||
values = request.get_json()
|
values = request.get_json()
|
||||||
transaction = Transaction(values["sender"], values["recipient"], values["amount"])
|
transaction = Transaction(values["sender"], values["recipient"], values["amount"])
|
||||||
blockchain.new_transaction(transaction)
|
blockchain.new_transaction(transaction)
|
||||||
@@ -51,27 +54,29 @@ def new_transaction():
|
|||||||
|
|
||||||
@app.route("/chain", methods=["GET"])
|
@app.route("/chain", methods=["GET"])
|
||||||
def chain():
|
def chain():
|
||||||
|
"""Display the block chain."""
|
||||||
return json.dumps([b.__dict__ for b in blockchain.blocks])
|
return json.dumps([b.__dict__ for b in blockchain.blocks])
|
||||||
|
|
||||||
|
|
||||||
@app.route("/nodes", methods=["GET"])
|
@app.route("/nodes", methods=["GET"])
|
||||||
def nodes():
|
def nodes():
|
||||||
|
"""List known nodes."""
|
||||||
return json.dumps(list(blockchain.nodes))
|
return json.dumps(list(blockchain.nodes))
|
||||||
|
|
||||||
|
|
||||||
@app.route("/register", methods=["POST"])
|
@app.route("/register", methods=["POST"])
|
||||||
def register_nodes():
|
def register_nodes():
|
||||||
values = request.get_json()
|
"""Register a new node."""
|
||||||
nodes = values["nodes"]
|
json_values = request.get_json()
|
||||||
|
for node in json_values["nodes"]:
|
||||||
for node in nodes:
|
|
||||||
blockchain.register_node(node)
|
blockchain.register_node(node)
|
||||||
|
|
||||||
return "New node has been added\n"
|
return "New node has been added\n"
|
||||||
|
|
||||||
|
|
||||||
@app.route("/resolve", methods=["GET"])
|
@app.route("/resolve", methods=["GET"])
|
||||||
def consensus():
|
def resolve():
|
||||||
|
"""Resolve the blockchain."""
|
||||||
replaced = blockchain.resolve_conflicts()
|
replaced = blockchain.resolve_conflicts()
|
||||||
|
|
||||||
if replaced:
|
if replaced:
|
||||||
|
|||||||
Reference in New Issue
Block a user