node.py 3.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. import copy, utils
  2. from block import Block
  3. from blockchain import Blockchain
  4. from vote import Vote
  5. from logger import Logger
  6. class Node:
  7. ''' This class represents a protocol node.
  8. Each node is numbered and has a secret-public keys pair, to sign messages.
  9. Nodes hold a set of Blockchains(some of which are not notarized)
  10. and a set of unconfirmed pending transactions.
  11. All nodes have syncronized clocks, using GST approach.'''
  12. def __init__(self, id, clock, password, init_block):
  13. self.id = id
  14. self.clock = clock # Clock syncronization to be implemented.
  15. self.password = password
  16. self.private_key, self.public_key = utils.generate_keys(self.password)
  17. self.blockchain = Blockchain(init_block)
  18. self.unconfirmed_transactions = []
  19. self.log = Logger(self)
  20. def __repr__(self):
  21. 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)
  22. def output(self):
  23. return self.blockchain
  24. def receive_transaction(self, transaction):
  25. # Additional validity rules must be defined by the protocol for its blockchain data structure.
  26. self.unconfirmed_transactions.append(transaction)
  27. def broadcast_transaction(self, nodes, transaction):
  28. for node in nodes:
  29. node.receive_transaction(transaction)
  30. def propose_block(self, epoch, nodes):
  31. proposed_block = Block(hash(self.blockchain.blocks[-1]), epoch, self.unconfirmed_transactions)
  32. signed_proposed_block = utils.sign_message(self.password, self.private_key, proposed_block)
  33. for node in nodes:
  34. node.receive_proposed_block(self.public_key, copy.deepcopy(proposed_block), copy.deepcopy(signed_proposed_block))
  35. def receive_proposed_block(self, leader_pubkey, round_block, signed_round_block):
  36. if not utils.verify_signature(leader_pubkey, round_block, signed_round_block):
  37. self.warn("the signature of the proposed block dosn't match")
  38. return
  39. self.round_block = round_block
  40. def vote_on_round_block(self, nodes):
  41. # Node verifies proposed block extends from one of the longest notarized chains that node has seen at the time.
  42. # Already notarized check.
  43. if (self.round_block != self.blockchain.blocks[-1]):
  44. self.blockchain.check_block_validity(self.round_block, self.blockchain.blocks[-1])
  45. #TODO implement: at this point we need to verify the unconfirmed transactions
  46. signed_block = utils.sign_message(self.password, self.private_key, self.round_block)
  47. vote = Vote(signed_block, self.round_block, self.id)
  48. for node in nodes:
  49. node.receive_vote(self.public_key, vote, nodes)
  50. def receive_vote(self, node_public_key, vote, nodes):
  51. # We verify we haven't received a vote from that node again.
  52. assert(vote not in self.round_block.votes)
  53. # When nodes receive votes, they verify them against nodes public key.
  54. assert(utils.verify_signature(node_public_key, vote.block, vote.vote))
  55. assert(self.round_block == vote.block)
  56. # Additional rules must be defined by the protocol for its voting system.
  57. self.round_block.votes.append(vote)
  58. # When a node sees 2n/3 votes for a block it notarizes it
  59. if (self.round_block != self.blockchain.blocks[-1] and len(self.round_block.votes) > (2 * len(nodes) / 3)):
  60. notarized_block = copy.deepcopy(self.round_block)
  61. notarized_block.notarized = True
  62. self.blockchain.add_block(notarized_block)
  63. # Node removes block transactions from unconfirmed_transactions array
  64. #for transaction in notarized_block.txs:
  65. # self.unconfirmed_transactions.remove(transaction)