Author SHA1 Message Date
marc afab0fe059 started the fix 2022-02-23 21:59:45 +01:00
2 changed files with 16 additions and 15 deletions
+10 -15
View File
@@ -1,12 +1,11 @@
#!/usr/bin/env python
"""Flask server to serve the block API."""
import json
from time import time
import argparse
from uuid import uuid4
from flask import Flask, request
import json
from uuid import uuid4
from blockchain import Blockchain, Transaction, Block
import argparse
parser = argparse.ArgumentParser(description="A blockchain simulation")
parser.add_argument("-p", "--port", type=int, help="Port number", default=80)
@@ -16,19 +15,18 @@ 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,
@@ -44,7 +42,6 @@ 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)
@@ -54,29 +51,27 @@ 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():
"""Register a new node."""
json_values = request.get_json()
for node in json_values["nodes"]:
values = request.get_json()
nodes = values["nodes"]
for node in nodes:
blockchain.register_node(node)
return "New node has been added\n"
@app.route("/resolve", methods=["GET"])
def resolve():
"""Resolve the blockchain."""
def consensus():
replaced = blockchain.resolve_conflicts()
if replaced:
+6
View File
@@ -1,4 +1,5 @@
#!/usr/bin/env python
"""Block chain according to me."""
import hashlib
from time import time
@@ -10,6 +11,7 @@ DIFFICULTY = 4
class Block:
"""Block according to me."""
def __init__(self, index, timestamp, transactions, previous_hash, proof):
self.index = index
self.timestamp = timestamp
@@ -19,6 +21,7 @@ class Block:
self.hash = self.hash()
def hash(self):
"""Hash a block using sha256."""
data = self.__dict__
data["transactions"] = [t.__dict__ for t in self.transactions]
block_string = json.dumps(data, sort_keys=True).encode()
@@ -29,16 +32,19 @@ class Block:
class Transaction:
"""Transaction of Samy coin."""
def __init__(self, sender, recipient, amount):
self.sender = sender
self.recipient = recipient
self.amount = amount
def __str__(self):
"""String representation of a transaction."""
return f"FROM: {self.sender} TO: {self.recipient} AMOUNT: {self.amount}"
class Blockchain:
"""Blockchain"""
def __init__(self):
self.blocks = []
self.blocks.append(self.create_first_block())