Author SHA1 Message Date
marc ac9d8a108b 8.84/10 2022-02-23 21:47:59 +01:00
3 changed files with 17 additions and 38 deletions
-9
View File
@@ -1,9 +0,0 @@
start:
./app.py -p 8000
update:
pipenv update
pipenv lock -r > requirements.txt
build: update
docker-compose build node
push: build
docker-compose push node
+16 -11
View File
@@ -1,11 +1,12 @@
#!/usr/bin/env python
"""Flask server to serve the block API."""
from time import time
from flask import Flask, request
import json
from uuid import uuid4
from blockchain import Blockchain, Transaction, Block
from time import time
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.add_argument("-p", "--port", type=int, help="Port number", default=80)
@@ -15,18 +16,19 @@ port = args.port
app = Flask(__name__)
node_identifier = str(uuid4()).replace("-", "")
NODE_IDENTIFIER = str(uuid4()).replace("-", "")
blockchain = Blockchain()
@app.route("/mine", methods=["GET"])
def mine():
"""Mine samy coin."""
last_block = blockchain.blocks[-1]
last_proof = last_block.proof
proof = blockchain.proof_of_work(last_proof)
transaction = Transaction("Network", node_identifier, 50)
transaction = Transaction("Network", NODE_IDENTIFIER, 50)
blockchain.new_transaction(transaction)
block = Block(
len(blockchain.blocks) + 1,
@@ -42,6 +44,7 @@ def mine():
@app.route("/transactions/new", methods=["POST"])
def new_transaction():
"""Submit a new transaction."""
values = request.get_json()
transaction = Transaction(values["sender"], values["recipient"], values["amount"])
blockchain.new_transaction(transaction)
@@ -51,27 +54,29 @@ def new_transaction():
@app.route("/chain", methods=["GET"])
def chain():
"""Display the block chain."""
return json.dumps([b.__dict__ for b in blockchain.blocks])
@app.route("/nodes", methods=["GET"])
def nodes():
"""List known nodes."""
return json.dumps(list(blockchain.nodes))
@app.route("/register", methods=["POST"])
def register_nodes():
values = request.get_json()
nodes = values["nodes"]
for node in nodes:
"""Register a new node."""
json_values = request.get_json()
for node in json_values["nodes"]:
blockchain.register_node(node)
return "New node has been added\n"
@app.route("/resolve", methods=["GET"])
def consensus():
def resolve():
"""Resolve the blockchain."""
replaced = blockchain.resolve_conflicts()
if replaced:
+1 -18
View File
@@ -1,19 +1,2 @@
#
# These requirements were autogenerated by pipenv
# To regenerate from the project's Pipfile, run:
#
# pipenv lock --requirements
#
-i https://pypi.org/simple
certifi==2021.10.8
charset-normalizer==2.0.12; python_version >= '3'
click==8.0.4; python_version >= '3.6'
flask==2.0.2
idna==3.3; python_version >= '3'
itsdangerous==2.1.0; python_version >= '3.7'
jinja2==3.0.3; python_version >= '3.6'
markupsafe==2.1.0; python_version >= '3.7'
Flask==2.0.2
requests==2.27.1
urllib3==1.26.8; python_version >= '2.7' and python_version not in '3.0, 3.1, 3.2, 3.3, 3.4' and python_version < '4'
werkzeug==2.0.3; python_version >= '3.6'