From 1a91e2174c99d667d54df168755cafd3505ab11a Mon Sep 17 00:00:00 2001 From: mx Date: Sat, 2 Nov 2019 18:25:25 +0100 Subject: [PATCH 01/35] fix(genesis_block): transactions as array --- blockchain.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/blockchain.py b/blockchain.py index 6e93f0a..37b82b9 100644 --- a/blockchain.py +++ b/blockchain.py @@ -38,7 +38,7 @@ def _genesis_block(self): nonce = 55 previous_hash = 0 - b1 = Block(nonce, t1, 0, 0) + b1 = Block(nonce, [t1], 0, 0) self.chain.append(b1) From a78b8e42bd5bbc9dee2d822dc213fb12d1446088 Mon Sep 17 00:00:00 2001 From: mx Date: Sat, 2 Nov 2019 18:52:39 +0100 Subject: [PATCH 02/35] add(base ihm) --- app.py | 23 +++++++++++++++ templates/index.html | 68 ++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 91 insertions(+) create mode 100644 app.py create mode 100644 templates/index.html diff --git a/app.py b/app.py new file mode 100644 index 0000000..7e02175 --- /dev/null +++ b/app.py @@ -0,0 +1,23 @@ +from flask import Flask, render_template +from blockchain import Blockchain + +my_blockchain = Blockchain() + +app = Flask(__name__) + +""" +- List transactions that are currently not in a block. +- List blocks + +""" + + +@app.route('/') +def accueil(): + chain = my_blockchain.chain + current_transactions = my_blockchain.current_transactions + return render_template('index.html', chain=chain, current_transactions=current_transactions) + + +if __name__ == '__main__': + app.run(debug=True) diff --git a/templates/index.html b/templates/index.html new file mode 100644 index 0000000..107b3e7 --- /dev/null +++ b/templates/index.html @@ -0,0 +1,68 @@ + + + + + + + + + My blockchain + + + +
+ + + {% for block in chain %} + + + + + + {% endfor %} + + + {% for block in chain %} + + + + + + + {% endfor %} + + +
Creation timePrevious hashNumber of transactions
{{ block.timestamp }}{{ block.previous_hash }} + {{ block.transactions | length }}
+
+ + + + + From e8e52aa799b501f64b99ca56b1bf7649b18b9697 Mon Sep 17 00:00:00 2001 From: EmixMaxime Date: Tue, 5 Nov 2019 21:23:42 +0100 Subject: [PATCH 03/35] fix(blockchain): check if transaction signature valid and stock valid --- blockchain.py | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/blockchain.py b/blockchain.py index ad5814d..bfdd499 100644 --- a/blockchain.py +++ b/blockchain.py @@ -105,10 +105,16 @@ def submit_transaction(self, transaction): raise ValueError( 'transaction parameter should be a Transaction instance.') - transaction_verification = transaction.verify_signature() and self.check_sender_stock(transaction) + is_signature_valid = transaction.verify_signature() + is_stock_valid = self.check_sender_stock(transaction) - if transaction_verification: + if not is_signature_valid: print("Transaction signature is valid") + + if not is_stock_valid: + print("Transaction is impossible because stock invalid") + + if is_stock_valid and is_signature_valid: self.current_transactions.append(transaction) self.network.broadcast_transaction(jsonpickle.encode(transaction)) # Should I mine? From c02f0b98ae6127dff852933557af4edb7de864ea Mon Sep 17 00:00:00 2001 From: EmixMaxime Date: Tue, 5 Nov 2019 22:03:25 +0100 Subject: [PATCH 04/35] feat(network_integration): send real things. --- network_integration.py | 25 +++++++++++++++++++------ 1 file changed, 19 insertions(+), 6 deletions(-) diff --git a/network_integration.py b/network_integration.py index 5727988..4e66249 100644 --- a/network_integration.py +++ b/network_integration.py @@ -1,21 +1,34 @@ from blockchain_factory import blockchain_factory from wallet import Wallet from transaction import Transaction +from init_transactions import hashImg import time +appleHash = hashImg('./items/apple.png') + bc, network = blockchain_factory() time.sleep(5) -wallet = Wallet() -wallet2 = Wallet(1024, True) -transaction = Transaction(wallet.address, wallet2.address, "bonjour") -transaction2 = Transaction(wallet.address, wallet2.address, "bonjour2") -transaction3 = Transaction(wallet.address, wallet2.address, "bonjour3") +wallet = Wallet(testDatas=True) +wallet2 = Wallet(testDatas=True) + +transaction = Transaction(wallet.address, wallet2.address, appleHash) +transaction2 = Transaction(wallet.address, wallet2.address, appleHash) +transaction3 = Transaction(wallet.address, wallet2.address, appleHash) transaction.sign(wallet) +transaction2.sign(wallet) +transaction3.sign(wallet) bc.submit_transaction(transaction) bc.submit_transaction(transaction2) -bc.submit_transaction(transaction3) \ No newline at end of file +bc.submit_transaction(transaction3) + +print("transactions:") +print(bc.current_transactions) + +time.sleep(15) +print("transactions:") +print(bc.current_transactions) \ No newline at end of file From 060902fd1e3a5b964cd4540951ebfb133ab0fe0a Mon Sep 17 00:00:00 2001 From: EmixMaxime Date: Tue, 5 Nov 2019 22:12:03 +0100 Subject: [PATCH 05/35] Change Messages --- blockchain.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/blockchain.py b/blockchain.py index bfdd499..a2f4b90 100644 --- a/blockchain.py +++ b/blockchain.py @@ -109,7 +109,7 @@ def submit_transaction(self, transaction): is_stock_valid = self.check_sender_stock(transaction) if not is_signature_valid: - print("Transaction signature is valid") + print("Transaction signature is invalid") if not is_stock_valid: print("Transaction is impossible because stock invalid") @@ -122,7 +122,7 @@ def submit_transaction(self, transaction): # self.mine() return len(self.chain) - print("Transaction signature is invalid") + print("Transaction is invalid") return False def create_block(self, nonce, previous_hash): From 66d9a300045c9730631f0fb0902a1c41d5314b5b Mon Sep 17 00:00:00 2001 From: mx Date: Tue, 5 Nov 2019 22:23:37 +0100 Subject: [PATCH 06/35] feat(blockchain): initialize blockchain when a new node connect --- blockchain.py | 26 ++++++++++++++++++++------ 1 file changed, 20 insertions(+), 6 deletions(-) diff --git a/blockchain.py b/blockchain.py index a2f4b90..abe9673 100644 --- a/blockchain.py +++ b/blockchain.py @@ -62,7 +62,7 @@ def chain_for_network(self): return jsonpickle.encode(self.chain) def check_sender_stock(self, tx): - #check sur le block en cours + # check sur le block en cours i = 1 nb = 0 while 1 > nb and i <= len(self.current_transactions): @@ -73,8 +73,8 @@ def check_sender_stock(self, tx): nb = nb + 1 i = i + 1 - #check sur les anciens blocs - if len(self.chain) >= 1 : + # check sur les anciens blocs + if len(self.chain) >= 1: current_block = self.last_block i = current_block.index while 1 > nb and i >= 0: @@ -119,7 +119,7 @@ def submit_transaction(self, transaction): self.network.broadcast_transaction(jsonpickle.encode(transaction)) # Should I mine? # if len(self.current_transactions) == NB_TRANSACTIONS_MAX: - # self.mine() + # self.mine() return len(self.chain) print("Transaction is invalid") @@ -134,8 +134,8 @@ def create_block(self, nonce, previous_hash): """ # Why we have this OR condition? Seems useless. - block = Block(nonce, self.current_transactions, len(self.chain), - previous_hash or Block.hash(self.chain[-1])) + block = Block(nonce, self.current_transactions, len(self.chain), + previous_hash or Block.hash(self.chain[-1])) # Reset the current list of transactions self.current_transactions = [] @@ -192,6 +192,20 @@ def valid_chain(self, chain): return True + def initialize_chain(self, chain_from_network): + """ + When I'm a new Node on the Network, I have to initialize my chain. + To do this, I ask the network the chain and initialize mine with this chain. + We should to something like a concensus (the largest chain win), but it's not done yet. + """ + chain = jsonpickle.encode(chain_from_network) + + if self.valid_chain(chain): + print('Initialize chain') + self.chain = chain + else: + print('Intialize chain impossible because the chain is not valid.') + def mine(self): """ Take the last Block, mine it and add it to the Blockchain. From ec1906426c83299461c84d0b2e43db76b1e965ab Mon Sep 17 00:00:00 2001 From: soso0084 Date: Tue, 5 Nov 2019 22:25:28 +0100 Subject: [PATCH 07/35] add ask chain when we are connected --- network/network.py | 6 ++++-- network_integration.py | 7 +++---- 2 files changed, 7 insertions(+), 6 deletions(-) diff --git a/network/network.py b/network/network.py index 353c442..46a8ba1 100644 --- a/network/network.py +++ b/network/network.py @@ -35,7 +35,7 @@ def broadcast_ask_chain(self): # Done for nodeList in self.nodes: self.node.send("-c ", nodeList, "") - def _broadcast_ping(self): #Not use + def _broadcast_ping(self): #Done nodeBroadcast = Node("192.168.1.62") myNodeToSend = jsonpickle.encode(self.node) self.node.send("-p ", nodeBroadcast, myNodeToSend) @@ -85,11 +85,12 @@ def receiv(self): if notFind: print("Connecting to a new Node: ", nodeReceiv.host) + self.node.send("-c ", Node(addr[0]), "") self.nodes.append(nodeReceiv) notFind = False elif myData[:3] == "-ac": # En suspend - some = None + self.blockchain.initialize_chain(myData[3:len(myData)]) elif myData[:3] == "-ap": # Done nodeReceiv = jsonpickle.decode(myData[3:len(myData)]) @@ -103,5 +104,6 @@ def receiv(self): if notFind: print("Connecting to a new Node: ", nodeReceiv.host) + self.node.send("-c ", Node(addr[0]), "") self.nodes.append(nodeReceiv) notFind = False diff --git a/network_integration.py b/network_integration.py index 4e66249..558805c 100644 --- a/network_integration.py +++ b/network_integration.py @@ -26,9 +26,8 @@ bc.submit_transaction(transaction2) bc.submit_transaction(transaction3) -print("transactions:") -print(bc.current_transactions) +print("Les transaction : ", bc.current_transactions) time.sleep(15) -print("transactions:") -print(bc.current_transactions) \ No newline at end of file + +print("Les transaction : ", bc.current_transactions) \ No newline at end of file From 36007b5fe1cc23d7a2ef3b937d571275a7f6e7db Mon Sep 17 00:00:00 2001 From: EmixMaxime Date: Tue, 5 Nov 2019 22:31:42 +0100 Subject: [PATCH 08/35] wip fix init bc --- blockchain.py | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/blockchain.py b/blockchain.py index abe9673..a3c9590 100644 --- a/blockchain.py +++ b/blockchain.py @@ -173,8 +173,6 @@ def valid_chain(self, chain): :return: True if valid, False if not """ - chain = jsonpickle.decode(chain) - previous_block = chain[0] current_index = 1 @@ -198,7 +196,7 @@ def initialize_chain(self, chain_from_network): To do this, I ask the network the chain and initialize mine with this chain. We should to something like a concensus (the largest chain win), but it's not done yet. """ - chain = jsonpickle.encode(chain_from_network) + chain = jsonpickle.decode(chain_from_network) if self.valid_chain(chain): print('Initialize chain') From 6d0a84e330abc726ccf5fa727b83cb46531a70b9 Mon Sep 17 00:00:00 2001 From: soso0084 Date: Tue, 5 Nov 2019 22:36:20 +0100 Subject: [PATCH 09/35] up size of buffer --- network/network.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/network/network.py b/network/network.py index 46a8ba1..e3819c2 100644 --- a/network/network.py +++ b/network/network.py @@ -44,7 +44,7 @@ def receiv(self): print("ready to receiv") while self._running is True: - data, addr = self.node.my_socket.recvfrom(4096) + data, addr = self.node.my_socket.recvfrom(16384) cureNode = addr From 10078210d9262ffb00fdedb51b14393a9bdedf69 Mon Sep 17 00:00:00 2001 From: soso0084 Date: Tue, 5 Nov 2019 22:37:33 +0100 Subject: [PATCH 10/35] same --- network/network.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/network/network.py b/network/network.py index e3819c2..edbd2d9 100644 --- a/network/network.py +++ b/network/network.py @@ -44,7 +44,7 @@ def receiv(self): print("ready to receiv") while self._running is True: - data, addr = self.node.my_socket.recvfrom(16384) + data, addr = self.node.my_socket.recvfrom(65536) cureNode = addr From 7714692e60d4e7d3ef82aaf615f5b55c76fa8df8 Mon Sep 17 00:00:00 2001 From: EmixMaxime Date: Tue, 5 Nov 2019 22:45:23 +0100 Subject: [PATCH 11/35] wip debug --- blockchain.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/blockchain.py b/blockchain.py index a3c9590..4c27c50 100644 --- a/blockchain.py +++ b/blockchain.py @@ -93,9 +93,9 @@ def submit_transaction(self, transaction): # from the network, is a str that contains a json. if isinstance(transaction, str): transaction = jsonpickle.decode(transaction) - print('I received a new Transaction', transaction) + print('I received a new Transaction', transaction, transaction.time) else: - print("I'm sending a new Transaction", transaction) + print("I'm sending a new Transaction", transaction, transaction.time) if transaction in self.current_transactions: print('I received an transaction, but I already know it, so I ignore it.') From 14dd3568c0f83932dfaa340c4c6f39a08e692b5e Mon Sep 17 00:00:00 2001 From: EmixMaxime Date: Tue, 5 Nov 2019 22:59:24 +0100 Subject: [PATCH 12/35] feat(integration of mine) --- blockchain.py | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/blockchain.py b/blockchain.py index 4c27c50..8d918f6 100644 --- a/blockchain.py +++ b/blockchain.py @@ -117,9 +117,11 @@ def submit_transaction(self, transaction): if is_stock_valid and is_signature_valid: self.current_transactions.append(transaction) self.network.broadcast_transaction(jsonpickle.encode(transaction)) + # Should I mine? - # if len(self.current_transactions) == NB_TRANSACTIONS_MAX: - # self.mine() + if len(self.current_transactions) == NB_TRANSACTIONS_MAX: + self.mine() + return len(self.chain) print("Transaction is invalid") @@ -234,4 +236,6 @@ def mine(self): 'previous_hash': block.previous_hash, } + print(response) + return response From bcd0f4514a6faaf209e0a85d1badd1219247f816 Mon Sep 17 00:00:00 2001 From: soso0084 Date: Tue, 5 Nov 2019 23:06:12 +0100 Subject: [PATCH 13/35] fix ask chain --- network/network.py | 1 - 1 file changed, 1 deletion(-) diff --git a/network/network.py b/network/network.py index edbd2d9..be04a3a 100644 --- a/network/network.py +++ b/network/network.py @@ -104,6 +104,5 @@ def receiv(self): if notFind: print("Connecting to a new Node: ", nodeReceiv.host) - self.node.send("-c ", Node(addr[0]), "") self.nodes.append(nodeReceiv) notFind = False From cc1e51d0bf8c4752501ee9e9eaa5d342544cbc14 Mon Sep 17 00:00:00 2001 From: soso0084 Date: Tue, 5 Nov 2019 23:08:39 +0100 Subject: [PATCH 14/35] same --- network/network.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/network/network.py b/network/network.py index be04a3a..faaf94f 100644 --- a/network/network.py +++ b/network/network.py @@ -85,7 +85,6 @@ def receiv(self): if notFind: print("Connecting to a new Node: ", nodeReceiv.host) - self.node.send("-c ", Node(addr[0]), "") self.nodes.append(nodeReceiv) notFind = False @@ -104,5 +103,6 @@ def receiv(self): if notFind: print("Connecting to a new Node: ", nodeReceiv.host) + self.node.send("-c ", Node(addr[0]), "") self.nodes.append(nodeReceiv) notFind = False From ff09174566c0731d0b2ff852f19fb5c8ef274469 Mon Sep 17 00:00:00 2001 From: EmixMaxime Date: Tue, 5 Nov 2019 23:13:37 +0100 Subject: [PATCH 15/35] wip debug block broadcast --- blockchain.py | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/blockchain.py b/blockchain.py index 8d918f6..4b7d1c0 100644 --- a/blockchain.py +++ b/blockchain.py @@ -148,10 +148,12 @@ def create_block(self, nonce, previous_hash): return block def submit_block(self, block): - print('handling block: ', block) if isinstance(block, str): block = jsonpickle.decode(block) + print("I received a new Block", block) + else: + print("I'm sending a block", block) if not isinstance(block, Block): raise ValueError('block should be an instance of Block') @@ -200,8 +202,12 @@ def initialize_chain(self, chain_from_network): """ chain = jsonpickle.decode(chain_from_network) + # I keep my chain because its larger. + if len(chain) < len(self.chain): + False + if self.valid_chain(chain): - print('Initialize chain') + print("Initialize chain: I'm taking the chain:", chain) self.chain = chain else: print('Intialize chain impossible because the chain is not valid.') From 010fdde8173f5305e8f5ceb7d8b5592158884f26 Mon Sep 17 00:00:00 2001 From: EmixMaxime Date: Tue, 5 Nov 2019 23:30:00 +0100 Subject: [PATCH 16/35] wip debug --- blockchain.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/blockchain.py b/blockchain.py index 4b7d1c0..a6786d5 100644 --- a/blockchain.py +++ b/blockchain.py @@ -143,6 +143,7 @@ def create_block(self, nonce, previous_hash): self.current_transactions = [] self.chain.append(block) + print("I'm sending a block", block) self.network.broadcast_block(jsonpickle.encode(block)) return block @@ -152,8 +153,6 @@ def submit_block(self, block): if isinstance(block, str): block = jsonpickle.decode(block) print("I received a new Block", block) - else: - print("I'm sending a block", block) if not isinstance(block, Block): raise ValueError('block should be an instance of Block') From 9753b120f9b62d7c090424f936c3edfffebeb065 Mon Sep 17 00:00:00 2001 From: EmixMaxime Date: Wed, 6 Nov 2019 00:08:44 +0100 Subject: [PATCH 17/35] wip tests(blockchain): genesis block --- network/network.py | 17 +++++++++-------- test_blockchain.py | 9 ++++++++- 2 files changed, 17 insertions(+), 9 deletions(-) diff --git a/network/network.py b/network/network.py index faaf94f..643e322 100644 --- a/network/network.py +++ b/network/network.py @@ -6,16 +6,17 @@ class Network: - def __init__(self): - self.node = Node("192.168.1.82", True) + def __init__(self, test=False): + self.node = Node("192.168.1.62", True) self.nodes = [] self.blockchain = None - # Thread management - self._running = True - self.t1 = threading.Thread(target=self.receiv) - self.t1.start() - self._broadcast_ping() + if test is False: + # Thread management + self._running = True + self.t1 = threading.Thread(target=self.receiv) + self.t1.start() + self._broadcast_ping() def stop(self): self._running = False @@ -36,7 +37,7 @@ def broadcast_ask_chain(self): # Done self.node.send("-c ", nodeList, "") def _broadcast_ping(self): #Done - nodeBroadcast = Node("192.168.1.62") + nodeBroadcast = Node("192.168.1.82") myNodeToSend = jsonpickle.encode(self.node) self.node.send("-p ", nodeBroadcast, myNodeToSend) diff --git a/test_blockchain.py b/test_blockchain.py index 79549ff..299b623 100644 --- a/test_blockchain.py +++ b/test_blockchain.py @@ -3,14 +3,21 @@ from client import Client from init_transactions import hashImg from transaction import Transaction +from block import Block, HASH_GENESIS_BLOCK +from network.network import Network + +network = Network(True) from init_datas import creator_address -blockchain = Blockchain() +blockchain = Blockchain(network) wallet = Wallet() guy_wallet = Wallet(1024, True) +def test_genesis_block(): + assert Block.hash(blockchain.chain[0]) == HASH_GENESIS_BLOCK + def test_submit_transaction(): transaction = Client.generate_transaction( wallet, guy_wallet.address, 10) From 1ad5d99a3cd6e6cb723655d807f9e5cf8a02b0ae Mon Sep 17 00:00:00 2001 From: soso0084 Date: Wed, 6 Nov 2019 00:10:47 +0100 Subject: [PATCH 18/35] Auto stash before merge of "blockchain_network_integration" and "origin/blockchain_network_integration" --- network/network.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/network/network.py b/network/network.py index 643e322..1869a12 100644 --- a/network/network.py +++ b/network/network.py @@ -37,7 +37,7 @@ def broadcast_ask_chain(self): # Done self.node.send("-c ", nodeList, "") def _broadcast_ping(self): #Done - nodeBroadcast = Node("192.168.1.82") + nodeBroadcast = Node("192.168.1.62") myNodeToSend = jsonpickle.encode(self.node) self.node.send("-p ", nodeBroadcast, myNodeToSend) From 7ed84d8c994ed2f156a0e9b6777772497f3bbc2c Mon Sep 17 00:00:00 2001 From: EmixMaxime Date: Wed, 6 Nov 2019 00:15:27 +0100 Subject: [PATCH 19/35] fix(blockchain): broadcast block --- blockchain.py | 10 +++++++--- network_integration.py | 4 ---- 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/blockchain.py b/blockchain.py index 1c647f6..d7821b2 100644 --- a/blockchain.py +++ b/blockchain.py @@ -143,9 +143,7 @@ def create_block(self, nonce, previous_hash): # Reset the current list of transactions self.current_transactions = [] - self.chain.append(block) - print("I'm sending a block", block) - self.network.broadcast_block(jsonpickle.encode(block)) + self.submit_block(block) return block @@ -168,6 +166,12 @@ def submit_block(self, block): self.chain.append(block) + # Idk If I have to do this, but... :) + self.current_transactions = [] + + print("I'm sending a block", block) + self.network.broadcast_block(jsonpickle.encode(block)) + return True def valid_chain(self, chain): diff --git a/network_integration.py b/network_integration.py index 558805c..09f23fc 100644 --- a/network_integration.py +++ b/network_integration.py @@ -27,7 +27,3 @@ bc.submit_transaction(transaction3) print("Les transaction : ", bc.current_transactions) - -time.sleep(15) - -print("Les transaction : ", bc.current_transactions) \ No newline at end of file From 344cefbb64644b5730dd509573ec574a750d5d1d Mon Sep 17 00:00:00 2001 From: EmixMaxime Date: Wed, 6 Nov 2019 00:58:37 +0100 Subject: [PATCH 20/35] wip debug --- block.py | 2 +- blockchain.py | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/block.py b/block.py index 2da5d69..929b0dd 100644 --- a/block.py +++ b/block.py @@ -75,7 +75,7 @@ def valid_block(block_to_validate, previous_block): previous_block_hash = Block.hash(previous_block) if block_to_validate.previous_hash != previous_block_hash: - print('block_to_validate.previous_hash != previous_block_hash') + print('block_to_validate.previous_hash != previous_block_hash', block_to_validate.previous_hash, '!=', previous_block_hash) return False # Check that the Proof of Work is correct diff --git a/blockchain.py b/blockchain.py index d7821b2..00cfb2d 100644 --- a/blockchain.py +++ b/blockchain.py @@ -151,7 +151,7 @@ def submit_block(self, block): if isinstance(block, str): block = jsonpickle.decode(block) - print("I received a new Block", block) + print("I received a new Block", block, "prev hash:", block.previous_hash) if not isinstance(block, Block): raise ValueError('block should be an instance of Block') From 0c273e03ced7f4f41f985bd81fbb0aa7fc1dc313 Mon Sep 17 00:00:00 2001 From: EmixMaxime Date: Wed, 6 Nov 2019 01:10:09 +0100 Subject: [PATCH 21/35] fix(blockchain): handle block that I already have --- block.py | 2 ++ blockchain.py | 5 +++++ 2 files changed, 7 insertions(+) diff --git a/block.py b/block.py index 929b0dd..e456490 100644 --- a/block.py +++ b/block.py @@ -72,6 +72,7 @@ def proof_of_work(last_block): @staticmethod def valid_block(block_to_validate, previous_block): + print('validating', block_to_validate.index, 'with prev_block', previous_block.index) previous_block_hash = Block.hash(previous_block) if block_to_validate.previous_hash != previous_block_hash: @@ -84,6 +85,7 @@ def valid_block(block_to_validate, previous_block): block_to_validate.nonce, ' prev_nonce=', previous_block.nonce) return False + print('Block valid') return True def check_sender_stock(self, tx, nb): diff --git a/blockchain.py b/blockchain.py index 00cfb2d..a717856 100644 --- a/blockchain.py +++ b/blockchain.py @@ -156,6 +156,11 @@ def submit_block(self, block): if not isinstance(block, Block): raise ValueError('block should be an instance of Block') + # If I already have the block, I ignore the received one. + if block.index == self.last_block: + print("I received a block, but I already have it, so I ignore it.") + return False + """ Add a Block in the Blockchain if the Block signature is valid. """ From 5596dbd95855bfd18852c0f3f21af473409e232b Mon Sep 17 00:00:00 2001 From: EmixMaxime Date: Wed, 6 Nov 2019 01:12:37 +0100 Subject: [PATCH 22/35] fix prev commit --- blockchain.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/blockchain.py b/blockchain.py index a717856..8d69e5c 100644 --- a/blockchain.py +++ b/blockchain.py @@ -157,7 +157,7 @@ def submit_block(self, block): raise ValueError('block should be an instance of Block') # If I already have the block, I ignore the received one. - if block.index == self.last_block: + if block.index == self.last_block.index: print("I received a block, but I already have it, so I ignore it.") return False From 4a6fa71a212e1ee33643f12bdb5ee451d2ffc665 Mon Sep 17 00:00:00 2001 From: soso0084 Date: Wed, 6 Nov 2019 11:45:51 +0100 Subject: [PATCH 23/35] add two other node to ping when we are online --- network/network.py | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/network/network.py b/network/network.py index 1869a12..2117e66 100644 --- a/network/network.py +++ b/network/network.py @@ -7,7 +7,7 @@ class Network: def __init__(self, test=False): - self.node = Node("192.168.1.62", True) + self.node = Node("192.168.43.183", True) self.nodes = [] self.blockchain = None @@ -37,10 +37,16 @@ def broadcast_ask_chain(self): # Done self.node.send("-c ", nodeList, "") def _broadcast_ping(self): #Done - nodeBroadcast = Node("192.168.1.62") + nodeBroadcast = Node("192.168.43.161") myNodeToSend = jsonpickle.encode(self.node) self.node.send("-p ", nodeBroadcast, myNodeToSend) + nodeBroadcast = Node("192.168.1.62") + self.node.send("-p ", nodeBroadcast, myNodeToSend) + + nodeBroadcast = Node("192.168.1.62") + self.node.send("-p ", nodeBroadcast, myNodeToSend) + def receiv(self): print("ready to receiv") From cdbb5cab9cf3b1d1385005e38e2539cf4e015dbf Mon Sep 17 00:00:00 2001 From: mx Date: Wed, 6 Nov 2019 11:48:50 +0100 Subject: [PATCH 24/35] Auto stash before merge of "blockchain_network_integration" and "origin/blockchain_network_integration" --- network/network.py | 9 +++++---- test_blockchain.py | 7 +++++-- test_transaction.py | 5 ++++- 3 files changed, 14 insertions(+), 7 deletions(-) diff --git a/network/network.py b/network/network.py index 2117e66..e08abf8 100644 --- a/network/network.py +++ b/network/network.py @@ -7,11 +7,11 @@ class Network: def __init__(self, test=False): - self.node = Node("192.168.43.183", True) self.nodes = [] self.blockchain = None if test is False: + self.node = Node("192.168.1.62", True) # Thread management self._running = True self.t1 = threading.Thread(target=self.receiv) @@ -36,8 +36,8 @@ def broadcast_ask_chain(self): # Done for nodeList in self.nodes: self.node.send("-c ", nodeList, "") - def _broadcast_ping(self): #Done - nodeBroadcast = Node("192.168.43.161") + def _broadcast_ping(self): # Done + nodeBroadcast = Node("192.168.1.62") myNodeToSend = jsonpickle.encode(self.node) self.node.send("-p ", nodeBroadcast, myNodeToSend) @@ -59,7 +59,8 @@ def receiv(self): if myData[:3] == "-c ": # Done # Retourne le JSON de la chaine - self.node.send("-ac", cureNode, self.blockchain.chain_for_network) + self.node.send("-ac", cureNode, + self.blockchain.chain_for_network) elif myData[:3] == "-n ": # Done print("I received a Node") diff --git a/test_blockchain.py b/test_blockchain.py index 299b623..1da1e49 100644 --- a/test_blockchain.py +++ b/test_blockchain.py @@ -1,3 +1,4 @@ +from init_datas import creator_address from blockchain import Blockchain from wallet import Wallet from client import Client @@ -6,18 +7,19 @@ from block import Block, HASH_GENESIS_BLOCK from network.network import Network -network = Network(True) +network = Network(test=True) -from init_datas import creator_address blockchain = Blockchain(network) wallet = Wallet() guy_wallet = Wallet(1024, True) + def test_genesis_block(): assert Block.hash(blockchain.chain[0]) == HASH_GENESIS_BLOCK + def test_submit_transaction(): transaction = Client.generate_transaction( wallet, guy_wallet.address, 10) @@ -55,6 +57,7 @@ def test_valid_chain(): assert valid == True + def test_check_sender_stock(): testWallet = Wallet(testDatas=True) appleHash = hashImg('./items/apple.png') diff --git a/test_transaction.py b/test_transaction.py index ce41f46..ca230dc 100644 --- a/test_transaction.py +++ b/test_transaction.py @@ -1,5 +1,6 @@ from transaction import Transaction + def test_equality(): sender = '@sender_addr' receiv = '@receiv_addr' @@ -13,8 +14,10 @@ def test_equality(): print(t1, t2) t3 = Transaction(sender, receiv, 55) - assert t1 == t2 + # because we have timestamp uniq id + assert t1 != t2 assert t1 != t3 + assert t1 == t1 assert t1 in transactions assert not t3 in transactions From 87fba19cda10c0d3d0156f2be04491373570214a Mon Sep 17 00:00:00 2001 From: mx Date: Wed, 6 Nov 2019 11:51:15 +0100 Subject: [PATCH 25/35] fix(test): blockchain --- test_blockchain.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test_blockchain.py b/test_blockchain.py index 1da1e49..38a0f80 100644 --- a/test_blockchain.py +++ b/test_blockchain.py @@ -52,7 +52,7 @@ def test_chain_for_network(): def test_valid_chain(): - s = blockchain.chain_for_network + s = blockchain.chain valid = blockchain.valid_chain(s) assert valid == True From abd46ac0ac34bb95d6f01db3de46731054a5377e Mon Sep 17 00:00:00 2001 From: mx Date: Wed, 6 Nov 2019 11:59:48 +0100 Subject: [PATCH 26/35] ihm --- app.py | 4 ++-- network/network.py | 8 ++++---- network_integration.py | 33 ++++++++++++++++++--------------- 3 files changed, 24 insertions(+), 21 deletions(-) diff --git a/app.py b/app.py index 7e02175..c42182f 100644 --- a/app.py +++ b/app.py @@ -1,7 +1,7 @@ from flask import Flask, render_template -from blockchain import Blockchain +from network_integration import run -my_blockchain = Blockchain() +my_blockchain, network = run() app = Flask(__name__) diff --git a/network/network.py b/network/network.py index e08abf8..d148b29 100644 --- a/network/network.py +++ b/network/network.py @@ -11,7 +11,7 @@ def __init__(self, test=False): self.blockchain = None if test is False: - self.node = Node("192.168.1.62", True) + self.node = Node("192.168.43.59", True) # Thread management self._running = True self.t1 = threading.Thread(target=self.receiv) @@ -37,14 +37,14 @@ def broadcast_ask_chain(self): # Done self.node.send("-c ", nodeList, "") def _broadcast_ping(self): # Done - nodeBroadcast = Node("192.168.1.62") + nodeBroadcast = Node("192.168.43.183") myNodeToSend = jsonpickle.encode(self.node) self.node.send("-p ", nodeBroadcast, myNodeToSend) - nodeBroadcast = Node("192.168.1.62") + nodeBroadcast = Node("192.168.43.161") self.node.send("-p ", nodeBroadcast, myNodeToSend) - nodeBroadcast = Node("192.168.1.62") + nodeBroadcast = Node("192.168.43.32") self.node.send("-p ", nodeBroadcast, myNodeToSend) def receiv(self): diff --git a/network_integration.py b/network_integration.py index 09f23fc..a24ccf6 100644 --- a/network_integration.py +++ b/network_integration.py @@ -4,26 +4,29 @@ from init_transactions import hashImg import time -appleHash = hashImg('./items/apple.png') +def run(): + appleHash = hashImg('./items/apple.png') -bc, network = blockchain_factory() + bc, network = blockchain_factory() -time.sleep(5) + time.sleep(5) -wallet = Wallet(testDatas=True) -wallet2 = Wallet(testDatas=True) + wallet = Wallet(testDatas=True) + wallet2 = Wallet(testDatas=True) -transaction = Transaction(wallet.address, wallet2.address, appleHash) -transaction2 = Transaction(wallet.address, wallet2.address, appleHash) -transaction3 = Transaction(wallet.address, wallet2.address, appleHash) + transaction = Transaction(wallet.address, wallet2.address, appleHash) + transaction2 = Transaction(wallet.address, wallet2.address, appleHash) + transaction3 = Transaction(wallet.address, wallet2.address, appleHash) -transaction.sign(wallet) -transaction2.sign(wallet) -transaction3.sign(wallet) + transaction.sign(wallet) + transaction2.sign(wallet) + transaction3.sign(wallet) -bc.submit_transaction(transaction) -bc.submit_transaction(transaction2) -bc.submit_transaction(transaction3) + bc.submit_transaction(transaction) + bc.submit_transaction(transaction2) + bc.submit_transaction(transaction3) -print("Les transaction : ", bc.current_transactions) + print("Les transaction : ", bc.current_transactions) + + return bc, network From a723a9fba39732d0accdf163b95f433849d1d5f3 Mon Sep 17 00:00:00 2001 From: soso0084 Date: Wed, 6 Nov 2019 12:08:55 +0100 Subject: [PATCH 27/35] add IP in list --- network/network.py | 11 +++-------- 1 file changed, 3 insertions(+), 8 deletions(-) diff --git a/network/network.py b/network/network.py index 2117e66..dac06ec 100644 --- a/network/network.py +++ b/network/network.py @@ -8,7 +8,7 @@ class Network: def __init__(self, test=False): self.node = Node("192.168.43.183", True) - self.nodes = [] + self.nodes = [Node("192.168.43.59"), Node("192.168.43.59"), Node("192.168.43.32")] self.blockchain = None if test is False: @@ -37,15 +37,10 @@ def broadcast_ask_chain(self): # Done self.node.send("-c ", nodeList, "") def _broadcast_ping(self): #Done - nodeBroadcast = Node("192.168.43.161") myNodeToSend = jsonpickle.encode(self.node) - self.node.send("-p ", nodeBroadcast, myNodeToSend) - nodeBroadcast = Node("192.168.1.62") - self.node.send("-p ", nodeBroadcast, myNodeToSend) - - nodeBroadcast = Node("192.168.1.62") - self.node.send("-p ", nodeBroadcast, myNodeToSend) + for nodeList in self.nodes: + self.node.send("-p ", nodeList, myNodeToSend) def receiv(self): print("ready to receiv") From 5cee7662342b242198bb4b9d95bbeb01b2169312 Mon Sep 17 00:00:00 2001 From: soso0084 Date: Wed, 6 Nov 2019 12:11:54 +0100 Subject: [PATCH 28/35] delete some line --- network/network.py | 6 ------ 1 file changed, 6 deletions(-) diff --git a/network/network.py b/network/network.py index a8acf38..0f56eec 100644 --- a/network/network.py +++ b/network/network.py @@ -41,12 +41,6 @@ def _broadcast_ping(self): #Done nodeBroadcast = Node("192.168.43.161") myNodeToSend = jsonpickle.encode(self.node) - nodeBroadcast = Node("192.168.1.62") - self.node.send("-p ", nodeBroadcast, myNodeToSend) - - nodeBroadcast = Node("192.168.1.62") - self.node.send("-p ", nodeBroadcast, myNodeToSend) - def receiv(self): print("ready to receiv") From ca9c2412672eeb9f4c8a76934fdc9dc52c62c7e1 Mon Sep 17 00:00:00 2001 From: mx Date: Wed, 6 Nov 2019 12:11:56 +0100 Subject: [PATCH 29/35] wip app --- app.py | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/app.py b/app.py index c42182f..95f7162 100644 --- a/app.py +++ b/app.py @@ -1,7 +1,7 @@ from flask import Flask, render_template from network_integration import run -my_blockchain, network = run() +# my_blockchain, network = run() app = Flask(__name__) @@ -12,12 +12,12 @@ """ -@app.route('/') -def accueil(): - chain = my_blockchain.chain - current_transactions = my_blockchain.current_transactions - return render_template('index.html', chain=chain, current_transactions=current_transactions) +# @app.route('/') +# def accueil(): +# chain = my_blockchain.chain +# current_transactions = my_blockchain.current_transactions +# return render_template('index.html', chain=chain, current_transactions=current_transactions) if __name__ == '__main__': - app.run(debug=True) + app.run(port=3333, debug=True) From 457bb06c2504eaebebe0222bf4d5a4a916c0ae28 Mon Sep 17 00:00:00 2001 From: soso0084 Date: Wed, 6 Nov 2019 12:14:24 +0100 Subject: [PATCH 30/35] same --- network/network.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/network/network.py b/network/network.py index 0f56eec..c94f859 100644 --- a/network/network.py +++ b/network/network.py @@ -38,8 +38,9 @@ def broadcast_ask_chain(self): # Done self.node.send("-c ", nodeList, "") def _broadcast_ping(self): #Done - nodeBroadcast = Node("192.168.43.161") myNodeToSend = jsonpickle.encode(self.node) + for nodeList in self.nodes: + self.node.send("-p ", nodeList, myNodeToSend) def receiv(self): print("ready to receiv") From bd529cff8321147f86ff2875a0603ad80806cde0 Mon Sep 17 00:00:00 2001 From: mx Date: Wed, 6 Nov 2019 12:15:41 +0100 Subject: [PATCH 31/35] wip network --- network/network.py | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/network/network.py b/network/network.py index c94f859..ff7ae85 100644 --- a/network/network.py +++ b/network/network.py @@ -7,12 +7,13 @@ class Network: def __init__(self, test=False): - self.node = Node("192.168.43.183", True) - self.nodes = [Node("192.168.43.59"), Node("192.168.43.59"), Node("192.168.43.32")] self.blockchain = None if test is False: self.node = Node("192.168.43.59", True) + + self.nodes = [Node("192.168.43.183"), Node( + "192.168.43.161"), Node("192.168.43.32")] # Thread management self._running = True self.t1 = threading.Thread(target=self.receiv) @@ -37,7 +38,7 @@ def broadcast_ask_chain(self): # Done for nodeList in self.nodes: self.node.send("-c ", nodeList, "") - def _broadcast_ping(self): #Done + def _broadcast_ping(self): # Done myNodeToSend = jsonpickle.encode(self.node) for nodeList in self.nodes: self.node.send("-p ", nodeList, myNodeToSend) From 1fc511666a0658662412e8f36efbcfad958e75d5 Mon Sep 17 00:00:00 2001 From: mx Date: Wed, 6 Nov 2019 12:18:01 +0100 Subject: [PATCH 32/35] fix network --- network/network.py | 3 ++- network_integration.py | 3 +++ 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/network/network.py b/network/network.py index ff7ae85..e10d51b 100644 --- a/network/network.py +++ b/network/network.py @@ -9,7 +9,8 @@ class Network: def __init__(self, test=False): self.blockchain = None - if test is False: + if test == False: + print('Running network') self.node = Node("192.168.43.59", True) self.nodes = [Node("192.168.43.183"), Node( diff --git a/network_integration.py b/network_integration.py index a24ccf6..9acb26d 100644 --- a/network_integration.py +++ b/network_integration.py @@ -30,3 +30,6 @@ def run(): print("Les transaction : ", bc.current_transactions) return bc, network + + +run() From 3993fa1f67a2ba853e9673e3988337166817805a Mon Sep 17 00:00:00 2001 From: mx Date: Wed, 6 Nov 2019 12:36:12 +0100 Subject: [PATCH 33/35] wip ihm --- app.py | 15 ++++++++------- network_integration.py | 3 ++- templates/index.html | 12 +++++++++++- 3 files changed, 21 insertions(+), 9 deletions(-) diff --git a/app.py b/app.py index 95f7162..0baa8b2 100644 --- a/app.py +++ b/app.py @@ -1,8 +1,7 @@ +import time from flask import Flask, render_template from network_integration import run -# my_blockchain, network = run() - app = Flask(__name__) """ @@ -12,11 +11,13 @@ """ -# @app.route('/') -# def accueil(): -# chain = my_blockchain.chain -# current_transactions = my_blockchain.current_transactions -# return render_template('index.html', chain=chain, current_transactions=current_transactions) +@app.route('/') +def accueil(): + my_blockchain, network = run() + + chain = my_blockchain.chain + current_transactions = my_blockchain.current_transactions + return render_template('index.html', chain=chain, current_transactions=current_transactions) if __name__ == '__main__': diff --git a/network_integration.py b/network_integration.py index 9acb26d..17a665e 100644 --- a/network_integration.py +++ b/network_integration.py @@ -32,4 +32,5 @@ def run(): return bc, network -run() +if __name__ == '__main__': + run() diff --git a/templates/index.html b/templates/index.html index 107b3e7..bd3256b 100644 --- a/templates/index.html +++ b/templates/index.html @@ -19,6 +19,7 @@ Creation time Previous hash Number of transactions + transactions {% endfor %} @@ -29,14 +30,23 @@ {{ block.previous_hash }} {{ block.transactions | length }} + + + {% for transaction in block.transactions %} + {{ transaction.value }} - {{ transaction }} + {% endfor %} + {% endfor %} - + + + +
From c953a272f2afb244f7f9c81a6159c5d8507b1529 Mon Sep 17 00:00:00 2001 From: soso0084 Date: Wed, 6 Nov 2019 13:06:26 +0100 Subject: [PATCH 34/35] same --- blockchain.py | 4 ++-- network/network.py | 6 ++---- 2 files changed, 4 insertions(+), 6 deletions(-) diff --git a/blockchain.py b/blockchain.py index 8d69e5c..293fc22 100644 --- a/blockchain.py +++ b/blockchain.py @@ -246,9 +246,9 @@ def mine(self): response = { 'message': "New Block Forged", 'index': block.index, - 'transactions': block.transactions, + # 'transactions': block.transactions, 'nonce': block.nonce, - 'previous_hash': block.previous_hash, + # 'previous_hash': block.previous_hash, } print(response) diff --git a/network/network.py b/network/network.py index e10d51b..c80bd26 100644 --- a/network/network.py +++ b/network/network.py @@ -10,11 +10,9 @@ def __init__(self, test=False): self.blockchain = None if test == False: - print('Running network') - self.node = Node("192.168.43.59", True) + self.node = Node("192.168.43.183", True) - self.nodes = [Node("192.168.43.183"), Node( - "192.168.43.161"), Node("192.168.43.32")] + self.nodes = [Node("192.168.43.59"), Node("192.168.43.161"), Node("192.168.43.32")] # Thread management self._running = True self.t1 = threading.Thread(target=self.receiv) From 24c035e509e22480e9d6b8c9d0296118724f88f7 Mon Sep 17 00:00:00 2001 From: LDE Date: Wed, 6 Nov 2019 13:09:40 +0100 Subject: [PATCH 35/35] imgs --- ...b72b9b9cc519c1bd318cc4eb6ec62ad7c6cbacce1f.png | Bin 0 -> 168 bytes ...3b78685b9c6bd96b717893a161897abbe42f58bef9.png | Bin ...61b0ccba92d5ed24857f319befc8f90b29f57b185f.png | Bin 0 -> 179 bytes 3 files changed, 0 insertions(+), 0 deletions(-) create mode 100644 items_hash/8aa8fba8101433c129dc31b72b9b9cc519c1bd318cc4eb6ec62ad7c6cbacce1f.png rename apple.png => items_hash/a3fcfed88aebd3ecbc53f93b78685b9c6bd96b717893a161897abbe42f58bef9.png (100%) create mode 100644 items_hash/d12506e3e77f4243271a3461b0ccba92d5ed24857f319befc8f90b29f57b185f.png diff --git a/items_hash/8aa8fba8101433c129dc31b72b9b9cc519c1bd318cc4eb6ec62ad7c6cbacce1f.png b/items_hash/8aa8fba8101433c129dc31b72b9b9cc519c1bd318cc4eb6ec62ad7c6cbacce1f.png new file mode 100644 index 0000000000000000000000000000000000000000..8bf133e33eff4553e9bff73040e31e41ba9799e5 GIT binary patch literal 168 zcmeAS@N?(olHy`uVBq!ia0vp^0wB!61|;P_|4#%``JOJ0Ar-fh6Be)+6c+yEKa`T) z#yE+$G1IQ%!-N0Q=6Ms+7&fIzd@~XLJO82j_xUQ!4ra|J{s!{#UpB;9#aK z?KCMo;T+pdp3htt*&0mBm#scL*Egs{>9$YNo!~W+H*Y#0NfS`Go#w^JuqUZC>Sg1X QZlKi+p00i_>zopr00!SZg#Z8m literal 0 HcmV?d00001 diff --git a/apple.png b/items_hash/a3fcfed88aebd3ecbc53f93b78685b9c6bd96b717893a161897abbe42f58bef9.png similarity index 100% rename from apple.png rename to items_hash/a3fcfed88aebd3ecbc53f93b78685b9c6bd96b717893a161897abbe42f58bef9.png diff --git a/items_hash/d12506e3e77f4243271a3461b0ccba92d5ed24857f319befc8f90b29f57b185f.png b/items_hash/d12506e3e77f4243271a3461b0ccba92d5ed24857f319befc8f90b29f57b185f.png new file mode 100644 index 0000000000000000000000000000000000000000..4d49c5aee68088b2429f120bddb0d01a79d22809 GIT binary patch literal 179 zcmeAS@N?(olHy`uVBq!ia0vp^0wB!61|;P_|4#%`Rh}-6Ar-fh6BL+t?AY