forked from bitcoin-abe/bitcoin-abe
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfirstbits.py
82 lines (72 loc) · 2.87 KB
/
firstbits.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
#!/usr/bin/env python
# Copyright(C) 2011,2012 by Abe developers.
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as
# published by the Free Software Foundation, either version 3 of the
# License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public
# License along with this program. If not, see
# <http://www.gnu.org/licenses/agpl.html>.
"""Reconfigure an Abe instance to use or not use Firstbits."""
def populate_firstbits(store):
blocks, fbs = 0, 0
log_incr = 1000
for addr_vers, block_id in store.selectall("""
SELECT c.chain_address_version,
cc.block_id
FROM chain c
JOIN chain_candidate cc ON (c.chain_id = cc.chain_id)
WHERE cc.block_height IS NOT NULL
ORDER BY cc.chain_id, cc.block_height"""):
fbs += store.do_vers_firstbits(addr_vers, int(block_id))
blocks += 1
if blocks % log_incr == 0:
store.commit()
store.log.info("%d firstbits in %d blocks" % (fbs, blocks))
if blocks % log_incr > 0:
store.commit()
store.log.info("%d firstbits in %d blocks" % (fbs, blocks))
def create_firstbits(store):
store.log.info("Creating firstbits table.")
store.ddl(
"""CREATE TABLE abe_firstbits (
pubkey_id NUMERIC(26) NOT NULL,
block_id NUMERIC(14) NOT NULL,
address_version BIT VARYING(80) NOT NULL,
firstbits VARCHAR(50) NOT NULL,
PRIMARY KEY (address_version, pubkey_id, block_id),
FOREIGN KEY (pubkey_id) REFERENCES pubkey (pubkey_id),
FOREIGN KEY (block_id) REFERENCES block (block_id)
)""")
store.ddl(
"""CREATE INDEX x_abe_firstbits
ON abe_firstbits (address_version, firstbits)""")
def drop_firstbits(store):
store.log.info("Dropping firstbits table.")
store.ddl("DROP TABLE abe_firstbits")
def reconfigure(store, args):
have = store.config['use_firstbits'] == "true"
want = args.use_firstbits
if have == want:
return
lock = store.get_lock()
try:
# XXX Should temporarily store a new schema_version.
if want:
create_firstbits(store)
populate_firstbits(store)
store.config['use_firstbits'] = "true"
else:
drop_firstbits(store)
store.config['use_firstbits'] = "false"
store.use_firstbits = want
store.save_configvar("use_firstbits")
store.commit()
finally:
store.release_lock(lock)