Skip to content

Commit

Permalink
sort touched or deleted claim hashes
Browse files Browse the repository at this point in the history
  • Loading branch information
jackrobison committed Oct 5, 2021
1 parent de3a978 commit 88ff64c
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 8 deletions.
8 changes: 7 additions & 1 deletion lbry/wallet/server/db/db.py
Original file line number Diff line number Diff line change
@@ -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:
Expand Down Expand Up @@ -101,3 +101,9 @@ def close(self):
@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))
14 changes: 10 additions & 4 deletions lbry/wallet/server/db/prefixes.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -1412,10 +1417,10 @@ def unpack_key(cls, key: bytes) -> TouchedOrDeletedClaimKey:
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:
Expand Down Expand Up @@ -1565,9 +1570,10 @@ def closed(self) -> bool:


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)
Expand Down
3 changes: 0 additions & 3 deletions lbry/wallet/server/leveldb.py
Original file line number Diff line number Diff line change
Expand Up @@ -996,9 +996,6 @@ def min_undo_height(self, max_height):
"""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():
Expand Down

0 comments on commit 88ff64c

Please sign in to comment.