added proposed block broadcast missing signature

This commit is contained in:
mohab
2022-01-27 20:44:51 +02:00
parent 7f1a2f4b14
commit e4e5237c2b
4 changed files with 477 additions and 6 deletions

View File

@@ -75,4 +75,5 @@ assert(node0.output() == node1.output() == node2.output() == node3.output() == n
# Since node6 joined later, node0 output is a prefix or equal to node6 output.
# Based on that, node6 output is a suffix of node0 output.
assert(node0.output().blocks[-len(node6.output()):] == node6.output().blocks)
assert(node0.output().blocks[-len(node6.output()):] == node6.output().blocks)
print('finished...')

View File

@@ -0,0 +1,10 @@
class Logger(object):
def __init__(self, obj):
self.obj = obj
def info(self, payload):
print(f"[{self.obj}]: {payload}")
def warn(self, payload):
print(f"[{self.obj}]: {payload}")
def error(self, pyaload):
print(f"[{self.obj}]: {payload}")
exit()

View File

@@ -2,6 +2,7 @@ import copy, utils
from block import Block
from blockchain import Blockchain
from vote import Vote
from logger import Logger
class Node:
''' This class represents a protocol node.
@@ -16,6 +17,7 @@ class Node:
self.private_key, self.public_key = utils.generate_keys(self.password)
self.blockchain = Blockchain(init_block)
self.unconfirmed_transactions = []
self.log = Logger(self)
def __repr__(self):
return "Node=[id={0}, clock={1}, password={2}, private_key={3}, public_key={4}, blockchain={5}, unconfirmed_transactions={6}".format(self.id, self.clock, self.password, self.private_key, self.public_key, self.blockchain, self.unconfirmed_transactions)
@@ -32,11 +34,15 @@ class Node:
node.receive_transaction(transaction)
def propose_block(self, epoch, nodes):
propozed_block = Block(hash(self.blockchain.blocks[-1]), epoch, self.unconfirmed_transactions)
proposed_block = Block(hash(self.blockchain.blocks[-1]), epoch, self.unconfirmed_transactions)
signed_proposed_block = utils.sign_message(self.password, self.private_key, proposed_block)
for node in nodes:
node.receive_proposed_block(copy.deepcopy(propozed_block))
node.receive_proposed_block(self.public_key, copy.deepcopy(proposed_block), copy.deepcopy(signed_proposed_block))
def receive_proposed_block(self, round_block):
def receive_proposed_block(self, leader_pubkey, round_block, signed_round_block):
if not utils.verify_signature(leader_pubkey, round_block, signed_round_block):
self.warn("the signature of the proposed block dosn't match")
return
self.round_block = round_block
def vote_on_round_block(self, nodes):
@@ -44,6 +50,7 @@ class Node:
# Already notarized check.
if (self.round_block != self.blockchain.blocks[-1]):
self.blockchain.check_block_validity(self.round_block, self.blockchain.blocks[-1])
#TODO implement: at this point we need to verify the unconfirmed transactions
signed_block = utils.sign_message(self.password, self.private_key, self.round_block)
vote = Vote(signed_block, self.round_block, self.id)
for node in nodes:
@@ -63,7 +70,7 @@ class Node:
notarized_block.notarized = True
self.blockchain.add_block(notarized_block)
# Node removes block transactions from unconfirmed_transactions array
for transaction in notarized_block.txs:
self.unconfirmed_transactions.remove(transaction)
#for transaction in notarized_block.txs:
# self.unconfirmed_transactions.remove(transaction)