2018-05-25 02:03:25 -04:00
|
|
|
from binascii import hexlify, unhexlify
|
2020-01-02 22:18:49 -05:00
|
|
|
from .constants import NULL_HASH32
|
2018-05-25 02:03:25 -04:00
|
|
|
|
|
|
|
|
2018-07-28 20:52:54 -04:00
|
|
|
class TXRef:
|
2018-07-14 21:34:07 -04:00
|
|
|
|
|
|
|
__slots__ = '_id', '_hash'
|
|
|
|
|
|
|
|
def __init__(self):
|
|
|
|
self._id = None
|
|
|
|
self._hash = None
|
|
|
|
|
|
|
|
@property
|
|
|
|
def id(self):
|
|
|
|
return self._id
|
|
|
|
|
|
|
|
@property
|
|
|
|
def hash(self):
|
|
|
|
return self._hash
|
|
|
|
|
2018-10-10 21:29:29 -04:00
|
|
|
@property
|
|
|
|
def height(self):
|
|
|
|
return -1
|
|
|
|
|
2018-07-14 21:34:07 -04:00
|
|
|
@property
|
|
|
|
def is_null(self):
|
|
|
|
return self.hash == NULL_HASH32
|
|
|
|
|
|
|
|
|
|
|
|
class TXRefImmutable(TXRef):
|
|
|
|
|
2018-10-10 21:39:36 -04:00
|
|
|
__slots__ = ('_height',)
|
|
|
|
|
|
|
|
def __init__(self):
|
|
|
|
super().__init__()
|
|
|
|
self._height = -1
|
2018-07-14 21:34:07 -04:00
|
|
|
|
|
|
|
@classmethod
|
2018-10-10 21:29:29 -04:00
|
|
|
def from_hash(cls, tx_hash: bytes, height: int) -> 'TXRefImmutable':
|
2018-07-14 21:34:07 -04:00
|
|
|
ref = cls()
|
|
|
|
ref._hash = tx_hash
|
|
|
|
ref._id = hexlify(tx_hash[::-1]).decode()
|
2018-10-10 21:29:29 -04:00
|
|
|
ref._height = height
|
2018-07-14 21:34:07 -04:00
|
|
|
return ref
|
|
|
|
|
|
|
|
@classmethod
|
2018-10-10 21:29:29 -04:00
|
|
|
def from_id(cls, tx_id: str, height: int) -> 'TXRefImmutable':
|
2018-07-14 21:34:07 -04:00
|
|
|
ref = cls()
|
|
|
|
ref._id = tx_id
|
|
|
|
ref._hash = unhexlify(tx_id)[::-1]
|
2018-10-10 21:29:29 -04:00
|
|
|
ref._height = height
|
2018-07-14 21:34:07 -04:00
|
|
|
return ref
|
|
|
|
|
2018-10-10 21:29:29 -04:00
|
|
|
@property
|
|
|
|
def height(self):
|
|
|
|
return self._height
|