Changeset View
Changeset View
Standalone View
Standalone View
contrib/devtools/chainparams/generate_chainparams_constants.py
- This file was added.
Property | Old Value | New Value |
---|---|---|
File Mode | null | 100755 |
#!/usr/bin/env python3 | |||||
# Copyright (c) 2019 The Bitcoin developers | |||||
# Distributed under the MIT software license, see the accompanying | |||||
# file COPYING or http://www.opensource.org/licenses/mit-license.php. | |||||
''' | |||||
Script to generate list of chainparams constants (ie. assumevalid and | |||||
minimum chainwork). | |||||
This script expects a text file for each chain in the directory that is passed | |||||
as an argument: | |||||
chainparams_main.txt | |||||
chainparams_test.txt | |||||
These files must consist of lines in the format | |||||
<assumevalid hash> | |||||
<minimum chainwork> | |||||
The outputted constants should be pasted into `src/chainparamsconstants.h`. | |||||
''' | |||||
import sys | |||||
import os | |||||
def process_constants(output, f, chainName): | |||||
output.write(' const uint256 {}_DEFAULT_ASSUME_VALID = uint256S("{}");\n'.format( | |||||
chainName, f.readline().rstrip())) | |||||
output.write(' const uint256 {}_MINIMUM_CHAIN_WORK = uint256S("{}");\n'.format( | |||||
chainName, f.readline().rstrip())) | |||||
def main(): | |||||
if len(sys.argv) != 2: | |||||
print('Usage: {} <path_to_chainparams_txt>'.format(sys.argv[0]), file=sys.stderr) | |||||
sys.exit(1) | |||||
output = sys.stdout | |||||
Fabien: This is what `print` does by default, you can replace `output.write` with `print` and this line… | |||||
indir = sys.argv[1] | |||||
output.write('#ifndef BITCOIN_CHAINPARAMSCONSTANTS_H\n') | |||||
output.write('#define BITCOIN_CHAINPARAMSCONSTANTS_H\n') | |||||
output.write('/**\n') | |||||
output.write(' * Chain params constants for each tracked chain.\n') | |||||
# The next line is split so this file is not identified as a generated. | |||||
output.write(' * @' | |||||
'generated by contrib/devtools/chainparams/generate_chainparams_constants.py\n') | |||||
output.write(' */\n') | |||||
output.write('\n') | |||||
output.write('#include <uint256.h>\n') | |||||
output.write('\n') | |||||
output.write('namespace ChainParamsConstants {\n') | |||||
with open(os.path.join(indir, 'chainparams_main.txt'), 'r', encoding="utf8") as f: | |||||
process_constants(output, f, 'MAINNET') | |||||
output.write('\n') | |||||
with open(os.path.join(indir, 'chainparams_test.txt'), 'r', encoding="utf8") as f: | |||||
process_constants(output, f, 'TESTNET') | |||||
output.write('}\n') | |||||
output.write('\n') | |||||
output.write('#endif // BITCOIN_CHAINPARAMSCONSTANTS_H\n') | |||||
FabienUnsubmitted Not Done Inline ActionsYou can use a single text block (heredoc style) and format it to improve the readability. Fabien: You can use a single text block (heredoc style) and format it to improve the readability. | |||||
FabienUnsubmitted Not Done Inline ActionsI came to an example: def process_constants(indir, file_name): with open(os.path.join(indir, file_name), 'r', encoding="utf8") as f: constants = f.readlines() return [line.strip() for line in constants] def main(): if len(sys.argv) != 2: print('Usage: {} <path_to_chainparams_txt>'.format(sys.argv[0]), file=sys.stderr) sys.exit(1) indir = sys.argv[1] print(''' #ifndef BITCOIN_CHAINPARAMSCONSTANTS_H #define BITCOIN_CHAINPARAMSCONSTANTS_H /** * @{} by contrib/devtools/chainparams/generate_chainparams_constants.py */ #include <uint256.h> namespace ChainParamsConstants {{ const uint256 MAINNET_DEFAULT_ASSUME_VALID = uint256S("{}"); const uint256 MAINNET_MINIMUM_CHAIN_WORK = uint256S("{}"); const uint256 TESTNET_DEFAULT_ASSUME_VALID = uint256S("{}"); const uint256 TESTNET_MINIMUM_CHAIN_WORK = uint256S("{}"); }} // namespace ChainParamsConstants #endif // BITCOIN_CHAINPARAMSCONSTANTS_H '''.format( "generated", *process_constants(indir, 'chainparams_main.txt'), *process_constants(indir, 'chainparams_test.txt'))[1:-1] ) Fabien: I came to an example:
```
def process_constants(indir, file_name):
with open(os.path.join… | |||||
if __name__ == '__main__': | |||||
main() |
This is what print does by default, you can replace output.write with print and this line becomes unecessary.