2020-02-14 18:19:55 +01:00
|
|
|
import struct
|
|
|
|
from lbry.crypto.hash import double_sha256
|
|
|
|
from lbry.wallet.transaction import Transaction
|
|
|
|
from lbry.wallet.bcd_data_stream import BCDataStream
|
|
|
|
|
|
|
|
|
|
|
|
ZERO_BLOCK = bytes((0,)*32)
|
|
|
|
|
|
|
|
|
|
|
|
class Block:
|
|
|
|
|
|
|
|
__slots__ = (
|
|
|
|
'version', 'block_hash', 'prev_block_hash',
|
|
|
|
'merkle_root', 'claim_trie_root', 'timestamp',
|
|
|
|
'bits', 'nonce', 'txs'
|
|
|
|
)
|
|
|
|
|
2020-02-28 05:52:18 +01:00
|
|
|
def __init__(self, stream: BCDataStream):
|
2020-02-14 18:19:55 +01:00
|
|
|
header = stream.data.read(112)
|
|
|
|
version, = struct.unpack('<I', header[:4])
|
|
|
|
timestamp, bits, nonce = struct.unpack('<III', header[100:112])
|
|
|
|
self.version = version
|
|
|
|
self.block_hash = double_sha256(header)
|
|
|
|
self.prev_block_hash = header[4:36]
|
|
|
|
self.merkle_root = header[36:68]
|
|
|
|
self.claim_trie_root = header[68:100][::-1]
|
|
|
|
self.timestamp = timestamp
|
|
|
|
self.bits = bits
|
|
|
|
self.nonce = nonce
|
|
|
|
tx_count = stream.read_compact_size()
|
|
|
|
self.txs = [
|
|
|
|
Transaction(position=i)._deserialize(stream)
|
|
|
|
for i in range(tx_count)
|
|
|
|
]
|
|
|
|
|
|
|
|
@property
|
|
|
|
def is_first_block(self):
|
|
|
|
return self.prev_block_hash == ZERO_BLOCK
|