diff --git a/contrib/devtools/generate-bestchainparams.sh b/contrib/devtools/generate-bestchainparams.sh new file mode 100755 --- /dev/null +++ b/contrib/devtools/generate-bestchainparams.sh @@ -0,0 +1,90 @@ +#!/bin/bash + +# set -e Script exits immediately when a command fails. +# set -u Treats unset variables as a failure. +set -eu + +help_message() { + echo "Generate a chainparams chain source file." + echo "Usage: $0 > chainparamsbestchainmainnet.h" + echo "-b, --blockhash The block hash to update defaultAssumeValid to." + echo "-c, --chainwork The chainwork to update nMinimumChainWork to." + echo "-t, --testnet If set, testnet chainparams will be updated instead of mainnet chainparams." +} + +BLOCK_HASH="" +CHAIN_WORK="" +CHAIN="MainNet" + +# Parse command line arguments +while [[ $# -gt 0 ]]; do +case "$1" in + -b|--blockhash) + BLOCK_HASH="$2" + shift # shift past argument + shift # shift past value + ;; + -c|--chainwork) + CHAIN_WORK="$2" + shift # shift past argument + shift # shift past value + ;; + -h|--help) + help_message + exit 0 + ;; + -t|--testnet) + CHAIN="TestNet" + shift # shift past argument + ;; + *) + echo "Unknown argument: $1" + help_message + exit 1 + ;; +esac +done + +if [ -z "$BLOCK_HASH" ]; then + echo "Block hash is not set." + echo "" + help_message + exit 1 +fi +if [[ ! "$BLOCK_HASH" =~ ^[0-9a-z]{64}$ ]]; then + echo "Block hash is not a valid block hash." + exit 1 +fi + +if [ -z "$CHAIN_WORK" ]; then + echo "Chain work is not set." + echo "" + help_message + exit 1 +fi +if [[ ! "$CHAIN_WORK" =~ ^[0-9a-z]{64}$ ]]; then + echo "Chain work is not a valid uint256 hex value." + exit 1 +fi + +echo "// Copyright (c) 2019 The Bitcoin developers" +echo "// Distributed under the MIT software license, see the accompanying" +echo "// file COPYING or http://www.opensource.org/licenses/mit-license.php." +echo "" +echo "// This file is autogenerated. See /contrib/devtools/generate-chainparams.sh" +echo "" +echo "#ifndef BITCOIN_CHAINPARAMSBESTCHAIN${CHAIN^^}_H" +echo "#define BITCOIN_CHAINPARAMSBESTCHAIN${CHAIN^^}_H" +echo "" +echo "#include " +echo "" +echo "static inline uint256 Get${CHAIN}MinimumChainWork() {" +echo " return uint256S(" +echo " \"${CHAIN_WORK}\");" +echo "};" +echo "" +echo "static inline uint256 Get${CHAIN}DefaultAssumeValid() {" +echo " return uint256S(" +echo " \"${BLOCK_HASH}\");" +echo "};" +echo "#endif // BITCOIN_CHAINPARAMSBESTCHAIN${CHAIN^^}_H" diff --git a/src/Makefile.am b/src/Makefile.am --- a/src/Makefile.am +++ b/src/Makefile.am @@ -117,6 +117,8 @@ chain.h \ chainparams.h \ chainparamsbase.h \ + chainparamsbestchainmainnet.h \ + chainparamsbestchaintestnet.h \ chainparamsseeds.h \ checkpoints.h \ checkqueue.h \ diff --git a/src/chainparams.cpp b/src/chainparams.cpp --- a/src/chainparams.cpp +++ b/src/chainparams.cpp @@ -5,6 +5,8 @@ // file COPYING or http://www.opensource.org/licenses/mit-license.php. #include +#include +#include #include #include @@ -104,13 +106,11 @@ consensus.fPowNoRetargeting = false; // The best chain should have at least this much work. - consensus.nMinimumChainWork = uint256S( - "000000000000000000000000000000000000000000f22fbd89943b5f5104e4ec"); + consensus.nMinimumChainWork = GetMainNetMinimumChainWork(); // By default assume that the signatures in ancestors of this block are // valid. - consensus.defaultAssumeValid = uint256S( - "00000000000000000401095ca2933cc4729484965e66e6a9f8e937070cc8e971"); + consensus.defaultAssumeValid = GetMainNetDefaultAssumeValid(); // August 1, 2017 hard fork consensus.uahfHeight = 478558; @@ -269,13 +269,11 @@ consensus.fPowNoRetargeting = false; // The best chain should have at least this much work. - consensus.nMinimumChainWork = uint256S( - "00000000000000000000000000000000000000000000004f587a0e52b7984751"); + consensus.nMinimumChainWork = GetTestNetMinimumChainWork(); // By default assume that the signatures in ancestors of this block are // valid. - consensus.defaultAssumeValid = uint256S( - "00000000001b618c015c41cc218a60a5a94bc42e16e30f1426cfc138615201c3"); + consensus.defaultAssumeValid = GetTestNetDefaultAssumeValid(); // August 1, 2017 hard fork consensus.uahfHeight = 1155875; diff --git a/src/chainparamsbestchainmainnet.h b/src/chainparamsbestchainmainnet.h new file mode 100644 --- /dev/null +++ b/src/chainparamsbestchainmainnet.h @@ -0,0 +1,21 @@ +// 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. + +// This file is autogenerated. See /contrib/devtools/generate-chainparams.sh + +#ifndef BITCOIN_CHAINPARAMSBESTCHAINMAINNET_H +#define BITCOIN_CHAINPARAMSBESTCHAINMAINNET_H + +#include + +static inline uint256 GetMainNetMinimumChainWork() { + return uint256S( + "000000000000000000000000000000000000000000f22fbd89943b5f5104e4ec"); +}; + +static inline uint256 GetMainNetDefaultAssumeValid() { + return uint256S( + "00000000000000000401095ca2933cc4729484965e66e6a9f8e937070cc8e971"); +}; +#endif // BITCOIN_CHAINPARAMSBESTCHAINMAINNET_H diff --git a/src/chainparamsbestchaintestnet.h b/src/chainparamsbestchaintestnet.h new file mode 100644 --- /dev/null +++ b/src/chainparamsbestchaintestnet.h @@ -0,0 +1,21 @@ +// 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. + +// This file is autogenerated. See /contrib/devtools/generate-chainparams.sh + +#ifndef BITCOIN_CHAINPARAMSBESTCHAINTESTNET_H +#define BITCOIN_CHAINPARAMSBESTCHAINTESTNET_H + +#include + +static inline uint256 GetTestNetMinimumChainWork() { + return uint256S( + "00000000000000000000000000000000000000000000004f587a0e52b7984751"); +}; + +static inline uint256 GetTestNetDefaultAssumeValid() { + return uint256S( + "00000000001b618c015c41cc218a60a5a94bc42e16e30f1426cfc138615201c3"); +}; +#endif // BITCOIN_CHAINPARAMSBESTCHAINTESTNET_H