block.py 901 B

12345678910111213141516171819202122232425
  1. class Block:
  2. ''' This class represents a tuple of the form (h, e, txs).
  3. Each blocks parent hash h may be computed simply as a hash of the parent block. '''
  4. def __init__(self, h, e, txs):
  5. self.h = h # parent hash
  6. self.e = e # epoch number
  7. self.txs = txs # transactions payload
  8. self.votes = [] # Epoch votes
  9. self.notarized = False # block notarization flag
  10. self.finalized = False # block finalization flag
  11. def __repr__(self):
  12. return "Block=[h={0}, e={1}, txs={2}, notarized={3}, finalized={4}]".format(
  13. self.h, self.e, self.txs, self.notarized, self.finalized)
  14. def __hash__(self):
  15. # python hash is used for demostranation porpuses only.
  16. return hash((self.h, self.e, str(self.txs)))
  17. def __eq__(self, other):
  18. return self.h == other.h and self.e == other.e and self.txs == other.txs
  19. def encode(self):
  20. return(("{0},{1},{2}".format(self.h, self.e, self.txs)).encode())