From ccc9a900efd14df7b3292153cee724fcf98a5e05 Mon Sep 17 00:00:00 2001 From: Jack Robison Date: Tue, 5 Oct 2021 14:24:46 -0400 Subject: [PATCH] sort touched or deleted claim hashes --- lbry/wallet/server/db/db.py | 8 +++++++- lbry/wallet/server/db/prefixes.py | 14 ++++++++++---- lbry/wallet/server/leveldb.py | 3 --- 3 files changed, 17 insertions(+), 8 deletions(-) diff --git a/lbry/wallet/server/db/db.py b/lbry/wallet/server/db/db.py index f8bce3dc1..3d945bb48 100644 --- a/lbry/wallet/server/db/db.py +++ b/lbry/wallet/server/db/db.py @@ -1,7 +1,7 @@ import struct from typing import Optional from lbry.wallet.server.db import DB_PREFIXES -from lbry.wallet.server.db.revertable import RevertableOpStack +from lbry.wallet.server.db.revertable import RevertableOpStack, RevertablePut, RevertableDelete class KeyValueStorage: @@ -101,3 +101,9 @@ class PrefixDB: @property def closed(self): return self._db.closed + + def stage_raw_put(self, key: bytes, value: bytes): + self._op_stack.append_op(RevertablePut(key, value)) + + def stage_raw_delete(self, key: bytes, value: bytes): + self._op_stack.append_op(RevertableDelete(key, value)) diff --git a/lbry/wallet/server/db/prefixes.py b/lbry/wallet/server/db/prefixes.py index c7ec9c3a9..ec401d6cf 100644 --- a/lbry/wallet/server/db/prefixes.py +++ b/lbry/wallet/server/db/prefixes.py @@ -866,6 +866,11 @@ class ClaimTakeoverPrefixRow(PrefixRow): prefix = DB_PREFIXES.claim_takeover.value value_struct = struct.Struct(b'>20sL') + key_part_lambdas = [ + lambda: b'', + length_encoded_name + ] + @classmethod def pack_key(cls, name: str): return cls.prefix + length_encoded_name(name) @@ -1412,10 +1417,10 @@ class TouchedOrDeletedPrefixRow(PrefixRow): return TouchedOrDeletedClaimKey(*super().unpack_key(key)) @classmethod - def pack_value(cls, touched, deleted) -> bytes: + def pack_value(cls, touched: typing.Set[bytes], deleted: typing.Set[bytes]) -> bytes: assert True if not touched else all(len(item) == 20 for item in touched) assert True if not deleted else all(len(item) == 20 for item in deleted) - return cls.value_struct.pack(len(touched), len(deleted)) + b''.join(touched) + b''.join(deleted) + return cls.value_struct.pack(len(touched), len(deleted)) + b''.join(sorted(touched)) + b''.join(sorted(deleted)) @classmethod def unpack_value(cls, data: bytes) -> TouchedOrDeletedClaimValue: @@ -1565,9 +1570,10 @@ class LevelDBStore(KeyValueStorage): class HubDB(PrefixDB): - def __init__(self, path: str, cache_mb: int, max_open_files: int = 512): + def __init__(self, path: str, cache_mb: int, max_open_files: int = 512, + unsafe_prefixes: Optional[typing.Set[bytes]] = None): db = LevelDBStore(path, cache_mb, max_open_files) - super().__init__(db, unsafe_prefixes={DB_PREFIXES.db_state.value}) + super().__init__(db, unsafe_prefixes=unsafe_prefixes) self.claim_to_support = ClaimToSupportPrefixRow(db, self._op_stack) self.support_to_claim = SupportToClaimPrefixRow(db, self._op_stack) self.claim_to_txo = ClaimToTXOPrefixRow(db, self._op_stack) diff --git a/lbry/wallet/server/leveldb.py b/lbry/wallet/server/leveldb.py index 2c7f7f781..093254ea8 100644 --- a/lbry/wallet/server/leveldb.py +++ b/lbry/wallet/server/leveldb.py @@ -996,9 +996,6 @@ class LevelDB: """Returns a height from which we should store undo info.""" return max_height - self.env.reorg_limit + 1 - def read_undo_info(self, height: int): - return self.prefix_db.undo.get(height), self.prefix_db.touched_or_deleted.get(height) - def apply_expiration_extension_fork(self): # TODO: this can't be reorged for k, v in self.prefix_db.claim_expiration.iterate():