9 Commits
Author SHA1 Message Date
Samy Haffoudhi 7247aa966e Merge pull request #9 from MarcPartensky/makefile
makefile update
2022-02-23 22:26:33 +01:00
marc 269658d9d8 update requirements.txt 2022-02-23 22:24:12 +01:00
Samy Haffoudhi a458dc64ee Merge pull request #8 from MarcPartensky/makefile
makefile
2022-02-23 22:20:48 +01:00
marc 6f9191fd38 no trailing lines 2022-02-23 22:18:31 +01:00
Samy Haffoudhi d5dfe96b81 Merge pull request #7 from MarcPartensky/makefile
few make methods
2022-02-23 22:18:06 +01:00
marc 5e8a2d80c4 fix bad makefile 2022-02-23 22:17:22 +01:00
Samy Haffoudhi b44c72f30e fix bad python #2 2022-02-23 22:12:37 +01:00
marc e7599ce757 few make methods 2022-02-23 22:12:31 +01:00
Samy Haffoudhi 9258985a86 Merge pull request #6 from MarcPartensky/fixbadpython
fixbadpython
2022-02-23 21:52:00 +01:00
3 changed files with 25 additions and 2 deletions
+9
View File
@@ -0,0 +1,9 @@
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
+1 -1
View File
@@ -23,7 +23,7 @@ blockchain = Blockchain()
@app.route("/mine", methods=["GET"]) @app.route("/mine", methods=["GET"])
def mine(): def mine():
"""Mine samy coin.""" """Mine Samycoin."""
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)
+15 -1
View File
@@ -3,13 +3,14 @@
import hashlib import hashlib
from time import time from time import time
import json import json
import requests
from urllib.parse import urlparse from urllib.parse import urlparse
import requests
DIFFICULTY = 4 DIFFICULTY = 4
class Block: class Block:
"""Block of the blockkchain."""
def __init__(self, index, timestamp, transactions, previous_hash, proof): def __init__(self, index, timestamp, transactions, previous_hash, proof):
self.index = index self.index = index
self.timestamp = timestamp self.timestamp = timestamp
@@ -19,6 +20,7 @@ class Block:
self.hash = self.hash() self.hash = self.hash()
def hash(self): def hash(self):
"""Hash the block using sha256."""
data = self.__dict__ data = self.__dict__
data["transactions"] = [t.__dict__ for t in self.transactions] data["transactions"] = [t.__dict__ for t in self.transactions]
block_string = json.dumps(data, sort_keys=True).encode() block_string = json.dumps(data, sort_keys=True).encode()
@@ -29,6 +31,7 @@ class Block:
class Transaction: class Transaction:
"""Transaction class."""
def __init__(self, sender, recipient, amount): def __init__(self, sender, recipient, amount):
self.sender = sender self.sender = sender
self.recipient = recipient self.recipient = recipient
@@ -39,6 +42,7 @@ class Transaction:
class Blockchain: class Blockchain:
"""Blockchain class."""
def __init__(self): def __init__(self):
self.blocks = [] self.blocks = []
self.blocks.append(self.create_first_block()) self.blocks.append(self.create_first_block())
@@ -46,20 +50,25 @@ class Blockchain:
self.nodes = set() self.nodes = set()
def register_node(self, address): def register_node(self, address):
"""Register a new node."""
parsed_url = urlparse(address).netloc parsed_url = urlparse(address).netloc
self.nodes.add(parsed_url) self.nodes.add(parsed_url)
def create_first_block(self): def create_first_block(self):
"""create the initial block."""
return Block(1, time(), [], 1, 100) return Block(1, time(), [], 1, 100)
def new_transaction(self, transaction): def new_transaction(self, transaction):
"""Add a new transaction."""
self.transactions.append(transaction) self.transactions.append(transaction)
def new_block(self, block): def new_block(self, block):
"""Add a new block."""
self.transactions = [] self.transactions = []
self.blocks.append(block) self.blocks.append(block)
def proof_of_work(self, last_proof): def proof_of_work(self, last_proof):
"""Computes the proof."""
proof = 0 proof = 0
while not self.valid_proof(proof, last_proof): while not self.valid_proof(proof, last_proof):
proof += 1 proof += 1
@@ -67,11 +76,13 @@ class Blockchain:
@staticmethod @staticmethod
def valid_proof(proof, last_proof): def valid_proof(proof, last_proof):
"""Tests proof."""
guess = f"{last_proof}{proof}".encode() guess = f"{last_proof}{proof}".encode()
guess_hash = hashlib.sha256(guess).hexdigest() guess_hash = hashlib.sha256(guess).hexdigest()
return guess_hash[:DIFFICULTY] == "0" * DIFFICULTY return guess_hash[:DIFFICULTY] == "0" * DIFFICULTY
def valid_chain(self, chain): def valid_chain(self, chain):
"""Tests the validity of a chain."""
last_block = chain[0] last_block = chain[0]
current_index = 1 current_index = 1
@@ -90,6 +101,7 @@ class Blockchain:
return True return True
def json_to_transactions(self, transactions_json): def json_to_transactions(self, transactions_json):
"""Converts json to a list of transactions."""
transactions = [ transactions = [
Transaction( Transaction(
transaction["sender"], transaction["sender"],
@@ -102,6 +114,7 @@ class Blockchain:
return transactions return transactions
def json_to_chain(self, chain_json): def json_to_chain(self, chain_json):
"""Convert json to a chain."""
chain = [ chain = [
Block( Block(
block["index"], block["index"],
@@ -116,6 +129,7 @@ class Blockchain:
return chain return chain
def resolve_conflicts(self): def resolve_conflicts(self):
"""Resolve conflicts using the consensus algorithm."""
neighbours = self.nodes neighbours = self.nodes
new_chain = None new_chain = None
max_lengh = len(self.blocks) max_lengh = len(self.blocks)