-
Notifications
You must be signed in to change notification settings - Fork 653
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Support turning Firstbits support on and off.
- Loading branch information
John Tobey
authored and
John Tobey
committed
Aug 11, 2012
1 parent
b0b843c
commit 7cf9c9e
Showing
6 changed files
with
201 additions
and
86 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
#!/usr/bin/env python | ||
# Copyright(C) 2011,2012 by John Tobey <John.Tobey@gmail.com> | ||
|
||
# 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.save_configvar("use_firstbits") | ||
store.commit() | ||
|
||
finally: | ||
store.release_lock(lock) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
#!/usr/bin/env python | ||
# Copyright(C) 2012 by John Tobey <John.Tobey@gmail.com> | ||
|
||
# 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.""" | ||
|
||
import sys | ||
import logging | ||
|
||
import DataStore | ||
import readconf | ||
import firstbits | ||
|
||
def main(argv): | ||
conf = { | ||
"debug": None, | ||
"logging": None, | ||
} | ||
conf.update(DataStore.CONFIG_DEFAULTS) | ||
|
||
args, argv = readconf.parse_argv(argv, conf, | ||
strict=False) | ||
if argv and argv[0] in ('-h', '--help'): | ||
print ("""Usage: python -m Abe.reconfigure [-h] [--config=FILE] [--CONFIGVAR=VALUE]... | ||
Apply configuration changes to an existing Abe database, if possible. | ||
--help Show this help message and exit. | ||
--config FILE Read options from FILE. | ||
--use-firstbits {true|false} | ||
Turn Firstbits support on or off. | ||
All configuration variables may be given as command arguments.""") | ||
return 0 | ||
|
||
logging.basicConfig( | ||
stream=sys.stdout, | ||
level=logging.DEBUG, | ||
format="%(message)s") | ||
if args.logging is not None: | ||
import logging.config as logging_config | ||
logging_config.dictConfig(args.logging) | ||
|
||
store = DataStore.new(args) | ||
firstbits.reconfigure(store, args) | ||
return 0 | ||
|
||
if __name__ == '__main__': | ||
sys.exit(main(sys.argv[1:])) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters