lbry-sdk/lbry/blockchain/hash.py

64 lines
1.3 KiB
Python
Raw Normal View History

2018-05-25 08:03:25 +02:00
from binascii import hexlify, unhexlify
2020-05-01 15:28:51 +02:00
from lbry.constants import NULL_HASH32
2018-05-25 08:03:25 +02:00
class TXRef:
__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-11 03:29:29 +02:00
@property
def height(self):
return -1
@property
def is_null(self):
return self.hash == NULL_HASH32
class TXRefImmutable(TXRef):
2020-07-12 00:18:33 +02:00
__slots__ = ('_height', '_timestamp')
2018-10-11 03:39:36 +02:00
def __init__(self):
super().__init__()
self._height = -1
2020-07-12 00:18:33 +02:00
self._timestamp = -1
@classmethod
2020-07-12 00:18:33 +02:00
def from_hash(cls, tx_hash: bytes, height: int, timestamp: int) -> 'TXRefImmutable':
ref = cls()
ref._hash = tx_hash
ref._id = hexlify(tx_hash[::-1]).decode()
2018-10-11 03:29:29 +02:00
ref._height = height
2020-07-12 00:18:33 +02:00
ref._timestamp = timestamp
return ref
@classmethod
2020-07-12 00:18:33 +02:00
def from_id(cls, tx_id: str, height: int, timestamp: int) -> 'TXRefImmutable':
ref = cls()
ref._id = tx_id
ref._hash = unhexlify(tx_id)[::-1]
2018-10-11 03:29:29 +02:00
ref._height = height
2020-07-12 00:18:33 +02:00
ref._timestamp = timestamp
return ref
2018-10-11 03:29:29 +02:00
@property
def height(self):
return self._height
2020-07-12 00:18:33 +02:00
@property
def timestamp(self):
return self._timestamp