2021-04-04 14:55:27 +02:00
|
|
|
|
# standard imports
|
|
|
|
|
import enum
|
|
|
|
|
|
2021-08-21 09:31:59 +02:00
|
|
|
|
# local imports
|
2022-05-09 12:00:58 +02:00
|
|
|
|
from .tx import Tx
|
|
|
|
|
from .src import Src
|
2021-08-21 09:31:59 +02:00
|
|
|
|
|
2021-04-04 14:55:27 +02:00
|
|
|
|
|
|
|
|
|
class BlockSpec(enum.IntEnum):
|
2021-08-21 09:31:59 +02:00
|
|
|
|
"""General-purpose block-height value designators
|
|
|
|
|
"""
|
2021-04-04 14:55:27 +02:00
|
|
|
|
PENDING = -1
|
|
|
|
|
LATEST = 0
|
2021-08-21 09:31:59 +02:00
|
|
|
|
|
|
|
|
|
|
2022-05-09 12:00:58 +02:00
|
|
|
|
class Block(Src):
|
2021-08-21 09:31:59 +02:00
|
|
|
|
"""Base class to extend for implementation specific block object.
|
|
|
|
|
"""
|
|
|
|
|
|
|
|
|
|
tx_generator = Tx
|
|
|
|
|
|
2022-05-10 21:00:37 +02:00
|
|
|
|
def __init__(self, src=None):
|
2022-05-09 12:00:58 +02:00
|
|
|
|
self.number = None
|
|
|
|
|
self.txs = []
|
|
|
|
|
self.author = None
|
2021-08-21 09:31:59 +02:00
|
|
|
|
|
2022-05-09 12:00:58 +02:00
|
|
|
|
self.get_tx = self.tx_index_by_hash
|
|
|
|
|
self.tx = self.tx_by_index
|
2021-08-21 09:31:59 +02:00
|
|
|
|
|
2022-05-12 10:06:14 +02:00
|
|
|
|
self.fee_limit = 0
|
|
|
|
|
self.fee_cost = 0
|
|
|
|
|
self.parent_hash = None
|
|
|
|
|
|
2022-05-10 21:00:37 +02:00
|
|
|
|
super(Block, self).__init__(src=src)
|
2021-08-21 09:31:59 +02:00
|
|
|
|
|
|
|
|
|
|
2022-05-09 12:00:58 +02:00
|
|
|
|
def tx_by_index(self, idx):
|
2021-08-21 09:31:59 +02:00
|
|
|
|
"""Return transaction object for transaction data at given index.
|
|
|
|
|
|
|
|
|
|
:param idx: Transaction index
|
|
|
|
|
:type idx: int
|
|
|
|
|
:rtype: chainlib.tx.Tx
|
|
|
|
|
:returns: Transaction object
|
|
|
|
|
"""
|
|
|
|
|
return self.tx_generator(self.txs[idx], self)
|
|
|
|
|
|
|
|
|
|
|
2022-05-09 12:00:58 +02:00
|
|
|
|
def tx_index_by_hash(self, hsh):
|
|
|
|
|
for tx in self.tx:
|
|
|
|
|
if tx == hsh:
|
|
|
|
|
return tx
|
|
|
|
|
return -1
|
2021-08-21 09:31:59 +02:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def __str__(self):
|
|
|
|
|
return 'block {} {} ({} txs)'.format(self.number, self.hash, len(self.txs))
|