chainlib

Generic blockchain access library and tooling
Log | Files | Refs | README | LICENSE

commit 04a09334ea4c95d6e505028c1afc7fea04290998
parent 45785c8e9e3e6df8e88f72bfabe7426ca50d8b83
Author: lash <dev@holbrook.no>
Date:   Mon,  9 May 2022 10:00:58 +0000

WIP implement and test genertic tx, result, block

Diffstat:
MCHANGELOG | 2++
Mchainlib/block.py | 57++++++++++++++++++++++++++++-----------------------------
Mchainlib/status.py | 1+
Mchainlib/tx.py | 77++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++-----
Mrequirements.txt | 2+-
Msetup.cfg | 2+-
6 files changed, 105 insertions(+), 36 deletions(-)

diff --git a/CHANGELOG b/CHANGELOG @@ -1,3 +1,5 @@ +- 0.2.0 + * Consolidate genertic blcok, tx and tx result objects - 0.1.3 * Remove superfluous arguments for chain settings instantiation - 0.1.2 diff --git a/chainlib/block.py b/chainlib/block.py @@ -2,7 +2,8 @@ import enum # local imports -from chainlib.tx import Tx +from .tx import Tx +from .src import Src class BlockSpec(enum.IntEnum): @@ -12,23 +13,25 @@ class BlockSpec(enum.IntEnum): LATEST = 0 -class Block: +class Block(Src): """Base class to extend for implementation specific block object. """ tx_generator = Tx + def __init__(self, src): + self.number = None + self.hash = None + self.txs = [] + self.author = None - def src(self): - """Return implementation specific block representation. + self.get_tx = self.tx_index_by_hash + self.tx = self.tx_by_index - :rtype: dict - :returns: Block representation - """ - return self.block_src + super(Block, self).__init__(src) - def tx(self, idx): + def tx_by_index(self, idx): """Return transaction object for transaction data at given index. :param idx: Transaction index @@ -39,28 +42,24 @@ class Block: return self.tx_generator(self.txs[idx], self) - def tx_src(self, idx): - """Return implementation specific transaction representation for transaction data at given index + def tx_index_by_hash(self, hsh): + for tx in self.tx: + if tx == hsh: + return tx + return -1 - :param idx: Transaction index - :type idx: int - :rtype: chainlib.tx.Tx - :returns: Transaction representation - """ - return self.txs[idx] +# +# +# def tx_src(self, idx): +# """Return implementation specific transaction representation for transaction data at given index +# +# :param idx: Transaction index +# :type idx: int +# :rtype: chainlib.tx.Tx +# :returns: Transaction representation +# """ +# return self.txs[idx] def __str__(self): return 'block {} {} ({} txs)'.format(self.number, self.hash, len(self.txs)) - - - @classmethod - def from_src(cls, src): - """Instantiate an implementation specific block object from the given block representation. - - :param src: Block representation - :type src: dict - :rtype: chainlib.block.Block - :returns: Block object - """ - return cls(src) diff --git a/chainlib/status.py b/chainlib/status.py @@ -4,6 +4,7 @@ import enum class Status(enum.Enum): """Representation of transaction status in network. """ + UNKNOWN = -1 PENDING = 0 SUCCESS = 1 ERROR = 2 diff --git a/chainlib/tx.py b/chainlib/tx.py @@ -1,4 +1,9 @@ -class Tx: +# local imports +from .status import Status +from .src import Src + + +class Tx(Src): """Base class to extend for implementation specific transaction objects. :param src: Transaction representation source @@ -7,8 +12,70 @@ class Tx: :type block: chainlib.block.Block """ - def __init__(self, src, block=None): - self.src = src + def __init__(self, src=None, block=None, result=None, strict=False): + self.block = block + self.index = -1 + + self.fee_limit = None + self.fee_price = None + + self.nonce = None + self.value = 0 + + self.outputs = [] + self.inputs = [] + self.payload = None + + self.result = None + + self.generate_wire = self.wire + + super(Tx, self).__init__(src) + + if block != None: + self.apply_block(block) + + if result != None: + self.apply_result(result) + + + def apply_result(self, result): + self.result = result + + + def apply_block(self, block): self.block = block - self.block_src = None - self.index = None + + + def status(self): + if self.result == None: + return None + return self.result.status + + + def status_name(self): + if self.result == None: + return None + return self.result.status.name + + + def wire(self): + raise NotImplementedError() + + + def as_dict(self): + raise NotImplementedError() + + + def __str__(self): + if self.block != None: + return 'tx {} status {} block {} index {}'.format(self.display_hash(), self.status_name(), self.block.number, self.index) + else: + return 'tx {} status {}'.format(self.display_hash(), self.hash, self.status_name()) + + +class TxResult(Src): + + def __init__(self, src): + self.status = Status.UNKNOWN + super(TxResult, self).__init__(src) diff --git a/requirements.txt b/requirements.txt @@ -1,4 +1,4 @@ funga~=0.5.2 pysha3==1.0.2 -hexathon~=0.1.6 +hexathon~=0.1.7 confini~=0.6.0 diff --git a/setup.cfg b/setup.cfg @@ -3,7 +3,7 @@ name=chainlib license=WTFPL2 author_email=dev@holbrook.no description=Generic blockchain access library and tooling -version=0.1.3 +version=0.2.0 url=https://gitlab.com/chaintools/chainlib author=Louis Holbrook