data.py 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. import time
  2. from ouroboros.logger import Logger
  3. '''
  4. \class Item is the basic item in the block data
  5. '''
  6. class Item(object):
  7. def __init__(self, data, fee=1):
  8. self.data = data
  9. self.fee = fee
  10. self.log = Logger(self)
  11. '''
  12. coffee reward for the miner
  13. '''
  14. @property
  15. def coffee(self):
  16. return self.fee
  17. class GenesisItem(Item):
  18. def __init__(self, dict_data):
  19. self.fee=0
  20. Item.__init__(self, dict_data)
  21. def __getitem__(self, key):
  22. return self.data.get(key, '')
  23. #TODO implement
  24. class StateTransition(Item):
  25. def __init__(self, balance):
  26. self.balance = balance
  27. #TODO implement
  28. class TransitionProcessor(object):
  29. def __init__(self):
  30. pass
  31. '''
  32. \class Transaction coin exchange between two entities
  33. '''
  34. class Transaction(Item):
  35. def __init__(self, sndr_addr, rcvr_addr, amnt, fee=1, lock_time=time.time()):
  36. self.sndr_addr = sndr_addr
  37. self.rcvr_addr = rcvr_addr
  38. self.amnt = amnt
  39. self.lock_time = lock_time
  40. self.stamp = time.time()
  41. fee = fee
  42. Item.__init__(self, str(self), fee)
  43. self.log.info(str(self))
  44. def __repr__(self):
  45. return f'sender: {self.sndr_addr}, receiver: {self.rcvr_addr}, amount: {self.amnt}, self.lock time: {self.lock_time}'
  46. class CoinBase(Item):
  47. def __init__(self):
  48. pass
  49. '''
  50. \class Data is the whole data stored in a single block,
  51. consist of list of Items
  52. '''
  53. class Data(list):
  54. def __init__(self, txs=[]):
  55. self.txs = txs
  56. '''
  57. Pall, is the accumulated transactions fee/gas/coffee for a block
  58. '''
  59. @property
  60. def coffee(self):
  61. pall = 0
  62. for item in self.txs:
  63. pall += item.coffee
  64. return pall
  65. def __repr__(self):
  66. buff = ''
  67. for item in self.txs:
  68. buff += str(item) + '\n'
  69. return buff
  70. def __len__(self):
  71. return len(self.txs)
  72. def __iter__(self):
  73. self.n=0
  74. return self
  75. def __next__(self):
  76. item = None
  77. if self.n <= self.length:
  78. try:
  79. item = self.txs[self.n]
  80. self.n+=1
  81. return item
  82. except IndexError:
  83. raise StopIteration
  84. def append(self, item):
  85. self.txs.append(item)
  86. def __getitem__(self, i):
  87. L = len(self)
  88. if i >= L or i < 0:
  89. return None
  90. return self.txs[i]