From 4e777b5861624b9e5d0bc19430406222f25b7106 Mon Sep 17 00:00:00 2001 From: Neil Booth Date: Mon, 10 Oct 2016 15:33:52 +0900 Subject: [PATCH] Let's try a smarter UTXO cache. --- query.py | 2 +- server/db.py | 457 ++++++++++++++++++++++++++++++-------------------- server/env.py | 2 +- 3 files changed, 277 insertions(+), 184 deletions(-) diff --git a/query.py b/query.py index 23ed768..1d8b462 100644 --- a/query.py +++ b/query.py @@ -33,7 +33,7 @@ def main(): n = None for n, utxo in enumerate(db.get_utxos(hash168, limit)): print('UTXOs #{:d}: hash: {} pos: {:d} height: {:d} value: {:d}' - .format(n, bytes(reversed(utxo.tx_hash)).hex(), + .format(n + 1, bytes(reversed(utxo.tx_hash)).hex(), utxo.tx_pos, utxo.height, utxo.value)) if n is None: print('No UTXOs') diff --git a/server/db.py b/server/db.py index d053975..4135a7a 100644 --- a/server/db.py +++ b/server/db.py @@ -31,6 +31,250 @@ def to_4_bytes(value): def from_4_bytes(b): return struct.unpack(' self.flush_size: + if self.utxo_cache.size_MB() + hist_MB > self.flush_MB: self.flush() def process_tx(self, tx_hash, tx): - hash168s = set() + cache = self.utxo_cache + tx_num = self.tx_count + + # Add the outputs as new UTXOs; spend the inputs + hash168s = cache.add_many(tx_hash, tx_num, tx.outputs) if not tx.is_coinbase: for txin in tx.inputs: - hash168s.add(self.spend_utxo(txin.prevout)) - - for idx, txout in enumerate(tx.outputs): - hash168s.add(self.put_utxo(tx_hash, idx, txout)) + hash168s.add(cache.spend(txin.prevout)) for hash168 in hash168s: - self.history[hash168].append(self.tx_count) + self.history[hash168].append(tx_num) + self.history_size += len(hash168s) self.tx_count += 1 diff --git a/server/env.py b/server/env.py index 42dbe1a..ec0e15c 100644 --- a/server/env.py +++ b/server/env.py @@ -20,7 +20,7 @@ class Env(object): network = self.default('NETWORK', 'mainnet') self.coin = Coin.lookup_coin_class(coin_name, network) self.db_dir = self.required('DB_DIRECTORY') - self.flush_size = self.integer('FLUSH_SIZE', 1000000) + self.flush_MB = self.integer('FLUSH_MB', 1000) self.rpc_url = self.build_rpc_url() def default(self, envvar, default):