TX_COUNTS_STATE

This commit is contained in:
Jack Robison 2020-12-01 11:28:27 -05:00
parent 314f8b0d42
commit f86d940f20
No known key found for this signature in database
GPG key ID: DF25C68FE0239BB2

View file

@ -50,6 +50,7 @@ HASHX_HISTORY_PREFIX = b'x'
UNDO_PREFIX = b'U' UNDO_PREFIX = b'U'
UTXO_STATE = b'state-utxo' UTXO_STATE = b'state-utxo'
HIST_STATE = b'state-hist' HIST_STATE = b'state-hist'
TX_COUNTS_STATE = b'state-tx-counts'
@dataclass @dataclass
@ -71,10 +72,11 @@ class RocksReaderContext:
self.db.open(self.name, create=False, read_only=True) self.db.open(self.name, create=False, read_only=True)
def update_state(self): def update_state(self):
self.tx_counts = [ tx_counts = array.array("L")
unpack_be_uint64(tx_count) counts = self.db.get(TX_COUNTS_STATE)
for tx_count in self.db.iterator(prefix=TX_COUNT_PREFIX, include_key=False) if counts:
] tx_counts.frombytes(counts)
self.tx_counts = tx_counts.tolist()
def ctx_tx_hash(self, tx_num): def ctx_tx_hash(self, tx_num):
tx_height = bisect_right(self.tx_counts, tx_num) tx_height = bisect_right(self.tx_counts, tx_num)
@ -195,10 +197,9 @@ def lookup_hashXs_utxos(prevouts):
def get_counts(): def get_counts():
return tuple( ctx = proc_ctx.get()
util.unpack_be_uint64(tx_count) return ctx.tx_counts
for tx_count in proc_ctx.get().db.iterator(prefix=TX_COUNT_PREFIX, include_key=False)
)
def read_txids(): def read_txids():
@ -393,7 +394,7 @@ class LevelDB:
tx_counts = await asyncio.get_event_loop().run_in_executor(self.executor, get_counts) tx_counts = await asyncio.get_event_loop().run_in_executor(self.executor, get_counts)
assert len(tx_counts) == self.db_height + 1, f"{len(tx_counts)} vs {self.db_height + 1}" assert len(tx_counts) == self.db_height + 1, f"{len(tx_counts)} vs {self.db_height + 1}"
self.tx_counts = array.array('I', tx_counts) self.tx_counts = tx_counts
if self.tx_counts: if self.tx_counts:
assert self.db_tx_count == self.tx_counts[-1], \ assert self.db_tx_count == self.tx_counts[-1], \
@ -590,6 +591,9 @@ class LevelDB:
tx_num += 1 tx_num += 1
offset += 32 offset += 32
batch_put(TX_COUNTS_STATE, b''.join(map(util.pack_le_int64, self.tx_counts)))
flush_data.block_txs.clear() flush_data.block_txs.clear()
flush_data.block_hashes.clear() flush_data.block_hashes.clear()
@ -766,7 +770,6 @@ class LevelDB:
def fs_block_hashes(self, height, count): def fs_block_hashes(self, height, count):
if height + count > len(self.headers): if height + count > len(self.headers):
print("boom")
raise self.DBError(f'only got {len(self.headers) - height:,d} headers starting at {height:,d}, not {count:,d}') raise self.DBError(f'only got {len(self.headers) - height:,d} headers starting at {height:,d}, not {count:,d}')
return [self.coin.header_hash(header) for header in self.headers[height:height + count]] return [self.coin.header_hash(header) for header in self.headers[height:height + count]]