Changeset View
Changeset View
Standalone View
Standalone View
contrib/devtools/chainparams/make_chainparams.py
#!/usr/bin/env python3 | #!/usr/bin/env python3 | ||||
# Copyright (c) 2019 The Bitcoin developers | # Copyright (c) 2019 The Bitcoin developers | ||||
# Distributed under the MIT software license, see the accompanying | # Distributed under the MIT software license, see the accompanying | ||||
# file COPYING or http://www.opensource.org/licenses/mit-license.php. | # file COPYING or http://www.opensource.org/licenses/mit-license.php. | ||||
from enum import Enum | from enum import Enum | ||||
import argparse | import argparse | ||||
import math | |||||
import os.path | import os.path | ||||
import re | import re | ||||
import sys | import sys | ||||
sys.path.append('../../../test/functional/test_framework') | sys.path.append('../../../test/functional/test_framework') | ||||
from authproxy import AuthServiceProxy # noqa: E402 | from authproxy import AuthServiceProxy # noqa: E402 | ||||
class Chain(Enum): | class Chain(Enum): | ||||
MainNet = "MAINNET" | MainNet = "MAINNET" | ||||
TestNet = "TESTNET" | TestNet = "TESTNET" | ||||
GIGABYTE = 1024 * 1024 * 1024 | |||||
def get_chainparams(rpc_caller, block): | def get_chainparams(rpc_caller, block): | ||||
# Fetch initial chain info | # Fetch initial chain info | ||||
chaininfo = rpc_caller.getblockchaininfo() | chaininfo = rpc_caller.getblockchaininfo() | ||||
if chaininfo['chain'] == 'main': | if chaininfo['chain'] == 'main': | ||||
chain = Chain.MainNet | chain = Chain.MainNet | ||||
else: | else: | ||||
chain = Chain.TestNet | chain = Chain.TestNet | ||||
Show All 18 Lines | if not re.match('^[0-9a-z]{64}$', block): | ||||
block = rpc_caller.getblockhash(int(block)) | block = rpc_caller.getblockhash(int(block)) | ||||
else: | else: | ||||
raise Exception("Block hash is not a valid block hash or height.") | raise Exception("Block hash is not a valid block hash or height.") | ||||
# Make sure the block hash is part of the chain. This call with raise an | # Make sure the block hash is part of the chain. This call with raise an | ||||
# exception if not. | # exception if not. | ||||
rpc_caller.getblockheader(block) | rpc_caller.getblockheader(block) | ||||
return (chainwork, block) | # Block size on disk (in GB) with some margin for growth | ||||
diskSizeBlocks = str( | |||||
int(math.ceil(chaininfo['size_on_disk'] / GIGABYTE * 1.3))) | |||||
# Chainstate size on disk (in GB) with some margin for growth | |||||
utxos = rpc_caller.gettxoutsetinfo() | |||||
diskSizeChainstate = str( | |||||
int(math.ceil(utxos['disk_size'] / GIGABYTE * 1.3))) | |||||
return (block, chainwork, diskSizeBlocks, diskSizeChainstate) | |||||
def main(args): | def main(args): | ||||
(chainwork, blockhash) = get_chainparams(args['rpc'], args['block']) | return "\n".join(get_chainparams(args['rpc'], args['block'])) | ||||
output = "{}\n{}".format(blockhash, chainwork) | |||||
return output | |||||
if __name__ == "__main__": | if __name__ == "__main__": | ||||
parser = argparse.ArgumentParser(description=( | parser = argparse.ArgumentParser(description=( | ||||
"Make chainparams file.\n" | "Make chainparams file.\n" | ||||
"Prerequisites: RPC access to a bitcoind node.\n\n"), | "Prerequisites: RPC access to a bitcoind node.\n\n"), | ||||
formatter_class=argparse.RawTextHelpFormatter) | formatter_class=argparse.RawTextHelpFormatter) | ||||
parser.add_argument('--address', '-a', default="127.0.0.1:8332", | parser.add_argument('--address', '-a', default="127.0.0.1:8332", | ||||
Show All 27 Lines | if __name__ == "__main__": | ||||
else: | else: | ||||
raise FileNotFoundError("Missing bitcoin.conf") | raise FileNotFoundError("Missing bitcoin.conf") | ||||
if user is None: | if user is None: | ||||
raise ValueError("Config is missing rpcuser") | raise ValueError("Config is missing rpcuser") | ||||
if password is None: | if password is None: | ||||
raise ValueError("Config is missing rpcpassword") | raise ValueError("Config is missing rpcpassword") | ||||
args.rpc = AuthServiceProxy( | args.rpc = AuthServiceProxy( | ||||
'http://{}:{}@{}'.format(user, password, args.address)) | service_url='http://{}:{}@{}'.format(user, password, args.address), timeout=1200) | ||||
output = main(vars(args)) | output = main(vars(args)) | ||||
if output: | if output: | ||||
print(output) | print(output) |