node.py 3.6 KB

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