From e75047a0ab4cecfee29873a9949c34ab3da49c99 Mon Sep 17 00:00:00 2001 From: Jack Robison Date: Tue, 12 Oct 2021 00:03:11 -0400 Subject: [PATCH] add block_txs index --- lbry/wallet/server/block_processor.py | 1 + lbry/wallet/server/db/__init__.py | 1 + lbry/wallet/server/db/prefixes.py | 39 +++++++++++++++++++++++++++ lbry/wallet/server/leveldb.py | 7 +---- 4 files changed, 42 insertions(+), 6 deletions(-) diff --git a/lbry/wallet/server/block_processor.py b/lbry/wallet/server/block_processor.py index 9be9cf758..e59b470db 100644 --- a/lbry/wallet/server/block_processor.py +++ b/lbry/wallet/server/block_processor.py @@ -1440,6 +1440,7 @@ class BlockProcessor: self.db.prefix_db.block_hash.stage_put(key_args=(height,), value_args=(self.coin.header_hash(block.header),)) self.db.prefix_db.header.stage_put(key_args=(height,), value_args=(block.header,)) + self.db.prefix_db.block_txs.stage_put(key_args=(height,), value_args=([tx_hash for tx, tx_hash in txs],)) for tx, tx_hash in txs: spent_claims = {} diff --git a/lbry/wallet/server/db/__init__.py b/lbry/wallet/server/db/__init__.py index b3201dc79..7da046edc 100644 --- a/lbry/wallet/server/db/__init__.py +++ b/lbry/wallet/server/db/__init__.py @@ -39,3 +39,4 @@ class DB_PREFIXES(enum.Enum): db_state = b's' channel_count = b'Z' support_amount = b'a' + block_txs = b'b' diff --git a/lbry/wallet/server/db/prefixes.py b/lbry/wallet/server/db/prefixes.py index 204babe1e..8b1603312 100644 --- a/lbry/wallet/server/db/prefixes.py +++ b/lbry/wallet/server/db/prefixes.py @@ -169,6 +169,14 @@ class BlockHashValue(NamedTuple): return f"{self.__class__.__name__}(block_hash={self.block_hash.hex()})" +class BlockTxsKey(NamedTuple): + height: int + + +class BlockTxsValue(NamedTuple): + tx_hashes: typing.List[bytes] + + class TxCountKey(NamedTuple): height: int @@ -1540,6 +1548,36 @@ class DBStatePrefixRow(PrefixRow): ) +class BlockTxsPrefixRow(PrefixRow): + prefix = DB_PREFIXES.block_txs.value + key_struct = struct.Struct(b'>L') + key_part_lambdas = [ + lambda: b'', + struct.Struct(b'>L').pack + ] + + @classmethod + def pack_key(cls, height: int): + return super().pack_key(height) + + @classmethod + def unpack_key(cls, key: bytes) -> BlockTxsKey: + return BlockTxsKey(*super().unpack_key(key)) + + @classmethod + def pack_value(cls, tx_hashes: typing.List[bytes]) -> bytes: + assert all(len(tx_hash) == 32 for tx_hash in tx_hashes) + return b''.join(tx_hashes) + + @classmethod + def unpack_value(cls, data: bytes) -> BlockTxsValue: + return BlockTxsValue([data[i*32:(i+1)*32] for i in range(len(data) // 32)]) + + @classmethod + def pack_item(cls, height, tx_hashes): + return cls.pack_key(height), cls.pack_value(tx_hashes) + + class LevelDBStore(KeyValueStorage): def __init__(self, path: str, cache_mb: int, max_open_files: int): import plyvel @@ -1604,6 +1642,7 @@ class HubDB(PrefixDB): self.channel_count = ChannelCountPrefixRow(db, self._op_stack) self.db_state = DBStatePrefixRow(db, self._op_stack) self.support_amount = SupportAmountPrefixRow(db, self._op_stack) + self.block_txs = BlockTxsPrefixRow(db, self._op_stack) def auto_decode_item(key: bytes, value: bytes) -> Union[Tuple[NamedTuple, NamedTuple], Tuple[bytes, bytes]]: diff --git a/lbry/wallet/server/leveldb.py b/lbry/wallet/server/leveldb.py index 53fbe21e3..5de62bce6 100644 --- a/lbry/wallet/server/leveldb.py +++ b/lbry/wallet/server/leveldb.py @@ -923,12 +923,7 @@ class LevelDB: return None, tx_height def get_block_txs(self, height: int) -> List[bytes]: - return [ - tx_hash for tx_hash in self.prefix_db.tx_hash.iterate( - start=(self.tx_counts[height-1],), stop=(self.tx_counts[height],), - deserialize_value=False, include_key=False - ) - ] + return self.prefix_db.block_txs.get(height).tx_hashes def _fs_transactions(self, txids: Iterable[str]): tx_counts = self.tx_counts