Skip to content

Commit 461dde9

Browse files
committed
history db: change schema, and rm compaction
1 parent 68fa091 commit 461dde9

7 files changed

Lines changed: 173 additions & 482 deletions

File tree

electrumx/server/block_processor.py

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -186,7 +186,7 @@ def __init__(self, env: 'Env', db: DB, daemon: Daemon, notifications: 'Notificat
186186

187187
# Caches of unflushed items.
188188
self.headers = []
189-
self.tx_hashes = []
189+
self.tx_hashes = [] # type: List[bytes]
190190
self.undo_infos = [] # type: List[Tuple[Sequence[bytes], int]]
191191

192192
# UTXO cache
@@ -347,9 +347,16 @@ def estimate_txs_remaining(self):
347347
def flush_data(self):
348348
'''The data for a flush. The lock must be taken.'''
349349
assert self.state_lock.locked()
350-
return FlushData(self.height, self.tx_count, self.headers,
351-
self.tx_hashes, self.undo_infos, self.utxo_cache,
352-
self.db_deletes, self.tip)
350+
return FlushData(
351+
height=self.height,
352+
tx_count=self.tx_count,
353+
headers=self.headers,
354+
block_tx_hashes=self.tx_hashes,
355+
undo_infos=self.undo_infos,
356+
adds=self.utxo_cache,
357+
deletes=self.db_deletes,
358+
tip=self.tip,
359+
)
353360

354361
async def flush(self, flush_utxos):
355362
def flush():
@@ -368,7 +375,7 @@ async def _maybe_flush(self):
368375
await self.flush(flush_arg)
369376
self.next_cache_check = time.monotonic() + 30
370377

371-
def check_cache_size(self):
378+
def check_cache_size(self) -> Optional[bool]:
372379
'''Flush a cache if it gets too big.'''
373380
# Good average estimates based on traversal of subobjects and
374381
# requesting size from Python (see deep_getsizeof).

electrumx/server/db.py

Lines changed: 11 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -107,13 +107,11 @@ def __init__(self, env: 'Env'):
107107
# "undo data: list of UTXOs spent at block height"
108108
self.utxo_db = None
109109

110-
self.utxo_flush_count = 0
111110
self.fs_height = -1
112111
self.fs_tx_count = 0
113112
self.db_height = -1
114113
self.db_tx_count = 0
115114
self.db_tip = None # type: Optional[bytes]
116-
self.tx_counts = None
117115
self.last_flush = time.time()
118116
self.last_flush_tx_count = 0
119117
self.wall_time = 0
@@ -129,6 +127,7 @@ def __init__(self, env: 'Env'):
129127
# on-disk: raw block headers in chain order
130128
self.headers_file = util.LogicalFile('meta/headers', 2, 16000000)
131129
# on-disk: cumulative number of txs at the end of height N
130+
self.tx_counts = None # type: Optional[array]
132131
self.tx_counts_file = util.LogicalFile('meta/txcounts', 2, 2000000)
133132
# on-disk: 32 byte txids in chain order, allows (tx_num -> txid) map
134133
self.hashes_file = util.LogicalFile('meta/hashes', 4, 16000000)
@@ -150,7 +149,7 @@ async def _read_tx_counts(self):
150149
else:
151150
assert self.db_tx_count == 0
152151

153-
async def _open_dbs(self, for_sync: bool, compacting: bool):
152+
async def _open_dbs(self, *, for_sync: bool):
154153
assert self.utxo_db is None
155154

156155
# First UTXO DB
@@ -169,25 +168,24 @@ async def _open_dbs(self, for_sync: bool, compacting: bool):
169168
self.read_utxo_state()
170169

171170
# Then history DB
172-
self.utxo_flush_count = self.history.open_db(self.db_class, for_sync,
173-
self.utxo_flush_count,
174-
compacting)
171+
self.history.open_db(
172+
db_class=self.db_class,
173+
for_sync=for_sync,
174+
utxo_db_tx_count=self.db_tx_count,
175+
)
175176
self.clear_excess_undo_info()
176177

177178
# Read TX counts (requires meta directory)
178179
await self._read_tx_counts()
179180

180-
async def open_for_compacting(self):
181-
await self._open_dbs(True, True)
182-
183181
async def open_for_sync(self):
184182
'''Open the databases to sync to the daemon.
185183
186184
When syncing we want to reserve a lot of open files for the
187185
synchronization. When serving clients we want the open files for
188186
serving network connections.
189187
'''
190-
await self._open_dbs(True, False)
188+
await self._open_dbs(for_sync=True)
191189

192190
async def open_for_serving(self):
193191
'''Open the databases for serving. If they are already open they are
@@ -198,7 +196,7 @@ async def open_for_serving(self):
198196
self.utxo_db.close()
199197
self.history.close_db()
200198
self.utxo_db = None
201-
await self._open_dbs(False, False)
199+
await self._open_dbs(for_sync=False)
202200

203201
# Header merkle cache
204202

@@ -254,7 +252,7 @@ def flush_dbs(self, flush_data, flush_utxos, estimate_txs_remaining):
254252
self.flush_state(self.utxo_db)
255253

256254
elapsed = self.last_flush - start_time
257-
self.logger.info(f'flush #{self.history.flush_count:,d} took '
255+
self.logger.info(f'flush took '
258256
f'{elapsed:.1f}s. Height {flush_data.height:,d} '
259257
f'txs: {flush_data.tx_count:,d} ({tx_delta:+,d})')
260258

@@ -353,7 +351,6 @@ def flush_utxo_db(self, batch, flush_data: FlushData):
353351
f'{spend_count:,d} spends in '
354352
f'{elapsed:.1f}s, committing...')
355353

356-
self.utxo_flush_count = self.history.flush_count
357354
self.db_height = flush_data.height
358355
self.db_tx_count = flush_data.tx_count
359356
self.db_tip = flush_data.tip
@@ -384,7 +381,7 @@ def flush_backup(self, flush_data, touched):
384381
self.flush_state(batch)
385382

386383
elapsed = self.last_flush - start_time
387-
self.logger.info(f'backup flush #{self.history.flush_count:,d} took '
384+
self.logger.info(f'backup flush took '
388385
f'{elapsed:.1f}s. Height {flush_data.height:,d} '
389386
f'txs: {flush_data.tx_count:,d} ({tx_delta:+,d})')
390387

@@ -595,7 +592,6 @@ def read_utxo_state(self):
595592
self.db_tx_count = 0
596593
self.db_tip = b'\0' * 32
597594
self.db_version = max(self.DB_VERSIONS)
598-
self.utxo_flush_count = 0
599595
self.wall_time = 0
600596
self.first_sync = True
601597
else:
@@ -617,7 +613,6 @@ def read_utxo_state(self):
617613
self.db_height = state['height']
618614
self.db_tx_count = state['tx_count']
619615
self.db_tip = state['tip']
620-
self.utxo_flush_count = state['utxo_flush_count']
621616
self.wall_time = state['wall_time']
622617
self.first_sync = state['first_sync']
623618

@@ -735,18 +730,12 @@ def write_utxo_state(self, batch):
735730
'height': self.db_height,
736731
'tx_count': self.db_tx_count,
737732
'tip': self.db_tip,
738-
'utxo_flush_count': self.utxo_flush_count,
739733
'wall_time': self.wall_time,
740734
'first_sync': self.first_sync,
741735
'db_version': self.db_version,
742736
}
743737
batch.put(b'state', repr(state).encode())
744738

745-
def set_flush_count(self, count):
746-
self.utxo_flush_count = count
747-
with self.utxo_db.write_batch() as batch:
748-
self.write_utxo_state(batch)
749-
750739
async def all_utxos(self, hashX):
751740
'''Return all UTXOs for an address sorted in no particular order.'''
752741
def read_utxos():

0 commit comments

Comments
 (0)