block.py 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. import json
  2. import time
  3. from ouroboros.utils import encode_genesis_data, decode_gensis_data, state_hash
  4. from ouroboros.consts import *
  5. from ouroboros.logger import Logger
  6. '''
  7. single block B_i for slot i in the system live time L,
  8. assigned to stakeholder U_j, with propability P_j_i,
  9. in the chain C, should be signed by U_j keys.
  10. '''
  11. class Block(object):
  12. '''
  13. @param previous_block: parent block to the current block
  14. @param data: is the transaction, or contracts in the leadger, or gensis block data,
  15. data is expected to be binary, no format is enforced
  16. @param slot_uid: the block corresponding slot monotonically increasing index,
  17. it's one-based
  18. @param gensis: boolean, True for gensis block
  19. '''
  20. def __init__(self, previous_block, data, slot_uid, genesis_time=time.time(), genesis=False):
  21. # state is hte hash of the previous block in the blockchain
  22. self.state=''
  23. if slot_uid>1:
  24. self.state=state_hash(previous_block)
  25. self.tx = data
  26. self.sl = slot_uid
  27. self.is_genesis=genesis
  28. self.endorsed=False
  29. self.log = Logger(genesis_time)
  30. def __repr__(self):
  31. if self.is_genesis:
  32. #return "GensisBlock at {slot:"+str(self.sl)+",data:"+self.tx+",state:"+str(self.state)+"}\n"+decode_gensis_data(self.tx)
  33. return "GensisBlock at {slot:"+str(self.sl)+",data:"+str(self.tx)+",state:"+str(self.state)+"}\n"+str(self.tx)
  34. return "Block at {slot:"+str(self.sl)+",data:"+str(self.tx)+",state:"+str(self.state)+"}"
  35. def __hash__(self):
  36. if type(self.tx)==str:
  37. return hash((self.state, self.tx, self.sl))
  38. elif type(self.tx)==dict:
  39. #TODO include distribution
  40. return hash((self.state, self.tx[SEED], self.tx[TX]))
  41. else:
  42. #TODO (fix) shouldn't reach here
  43. return 0
  44. def __eq__(self, block):
  45. return self.state==block.state and \
  46. self.tx == block.tx and \
  47. self.sl == block.sl
  48. def to_json(self):
  49. d = {'state':self.state, \
  50. 'data': self.tx, \
  51. 'sl': self.sl}
  52. return json.encoder(d)
  53. def set_endorsed(self):
  54. self.endorsed=True
  55. @property
  56. def data(self):
  57. return self.tx
  58. @property
  59. def slot(self):
  60. return self.sl
  61. @property
  62. def empty(self):
  63. return (self.tx=='' or self.slot<0) and self.state==''
  64. def encode(self):
  65. return str(self).encode()
  66. class GensisBlock(Block):
  67. '''
  68. @param data: data is dictionary of list of (pk_i, s_i) public key,
  69. and stake respectively of the corresponding stakeholder U_i,
  70. seed of the leader election function.
  71. '''
  72. def __init__(self, previous_block, data, slot_uid, genesis_time=time.time()):
  73. # stakeholders is list of tuple (pk_i, s_i) for the ith stakeholder
  74. self.stakeholders = data[STAKEHOLDERS]
  75. self.distribution = data[STAKEHOLDERS_DISTRIBUTIONS]
  76. self.seed = data.get(SEED, '') #needed for pvss
  77. shd_buff = ''
  78. for shd in self.distribution:
  79. shd_buff +=str(shd)
  80. #data = encode_genesis_data(shd_buff)
  81. data_dict = {'seed':self.seed, 'distribution':shd_buff}
  82. Block.__init__(self, previous_block, str(data_dict), slot_uid, genesis_time, True)
  83. '''
  84. @return: the number of participating stakeholders in the blockchain
  85. '''
  86. @property
  87. def length(self):
  88. return len(self.stakeholders)
  89. def __getitem__(self, i):
  90. if i<0 or i>=self.length:
  91. raise "index is out of range!"
  92. return self.stakeholders[i]
  93. '''
  94. block lead by an adversary, or
  95. lead by offline leader
  96. is an empty Block
  97. '''
  98. class EmptyBlock(Block):
  99. def __init__(self, genesis_time=time.time()):
  100. Block.__init__(self, '', -1, genesis_time, False)