diff --git a/src/config.cpp b/src/config.cpp index e05cf417b..a56a4a88f 100644 --- a/src/config.cpp +++ b/src/config.cpp @@ -1,55 +1,59 @@ // Copyright (c) 2017 The Bitcoin developers // Distributed under the MIT software license, see the accompanying // file COPYING or http://www.opensource.org/licenses/mit-license.php. #include "config.h" #include "chainparams.h" #include "consensus/consensus.h" #include "globals.h" GlobalConfig::GlobalConfig() : useCashAddr(false) {} bool GlobalConfig::SetMaxBlockSize(uint64_t maxBlockSize) { // Do not allow maxBlockSize to be set below historic 1MB limit // It cannot be equal either because of the "must be big" UAHF rule. if (maxBlockSize <= LEGACY_MAX_BLOCK_SIZE) { return false; } nMaxBlockSize = maxBlockSize; return true; } uint64_t GlobalConfig::GetMaxBlockSize() const { return nMaxBlockSize; } bool GlobalConfig::SetBlockPriorityPercentage(int64_t blockPriorityPercentage) { // blockPriorityPercentage has to belong to [0..100] if ((blockPriorityPercentage < 0) || (blockPriorityPercentage > 100)) { return false; } nBlockPriorityPercentage = blockPriorityPercentage; return true; } uint8_t GlobalConfig::GetBlockPriorityPercentage() const { return nBlockPriorityPercentage; } const CChainParams &GlobalConfig::GetChainParams() const { return Params(); } static GlobalConfig gConfig; const Config &GetConfig() { return gConfig; } void GlobalConfig::SetCashAddrEncoding(bool c) { useCashAddr = c; } bool GlobalConfig::UseCashAddrEncoding() const { return useCashAddr; } + +const CChainParams &DummyConfig::GetChainParams() const { + return Params(CBaseChainParams::REGTEST); +} diff --git a/src/config.h b/src/config.h index a668a5c52..d6cb2dcce 100644 --- a/src/config.h +++ b/src/config.h @@ -1,44 +1,58 @@ // Copyright (c) 2017 Amaury SÉCHET // Distributed under the MIT software license, see the accompanying // file COPYING or http://www.opensource.org/licenses/mit-license.php. #ifndef BITCOIN_CONFIG_H #define BITCOIN_CONFIG_H #include #include class CChainParams; class Config : public boost::noncopyable { public: virtual bool SetMaxBlockSize(uint64_t maxBlockSize) = 0; virtual uint64_t GetMaxBlockSize() const = 0; virtual bool SetBlockPriorityPercentage(int64_t blockPriorityPercentage) = 0; virtual uint8_t GetBlockPriorityPercentage() const = 0; virtual const CChainParams &GetChainParams() const = 0; virtual void SetCashAddrEncoding(bool) = 0; virtual bool UseCashAddrEncoding() const = 0; }; class GlobalConfig final : public Config { public: GlobalConfig(); bool SetMaxBlockSize(uint64_t maxBlockSize); uint64_t GetMaxBlockSize() const; bool SetBlockPriorityPercentage(int64_t blockPriorityPercentage); uint8_t GetBlockPriorityPercentage() const; const CChainParams &GetChainParams() const; void SetCashAddrEncoding(bool) override; bool UseCashAddrEncoding() const override; private: bool useCashAddr; }; +// Dummy for subclassing in unittests +class DummyConfig : public Config { +public: + bool SetMaxBlockSize(uint64_t maxBlockSize) override { return false; } + uint64_t GetMaxBlockSize() const override { return 0; } + bool SetBlockPriorityPercentage(int64_t blockPriorityPercentage) override { + return false; + } + uint8_t GetBlockPriorityPercentage() const override { return 0; } + const CChainParams &GetChainParams() const override; + void SetCashAddrEncoding(bool) override {} + bool UseCashAddrEncoding() const override { return false; } +}; + // Temporary woraround. const Config &GetConfig(); #endif diff --git a/src/test/dstencode_tests.cpp b/src/test/dstencode_tests.cpp index e1df89b4c..6c0cb660e 100644 --- a/src/test/dstencode_tests.cpp +++ b/src/test/dstencode_tests.cpp @@ -1,78 +1,69 @@ // Copyright (c) 2017 The Bitcoin developers // Distributed under the MIT software license, see the accompanying // file COPYING or http://www.opensource.org/licenses/mit-license.php. #include "chainparams.h" #include "config.h" #include "dstencode.h" #include "test/test_bitcoin.h" #include namespace { -class DummyCfg : public Config { +class DstCfgDummy : public DummyConfig { public: - DummyCfg() : useCashAddr(false) {} - virtual bool SetMaxBlockSize(uint64_t maxBlockSize) { return false; } - virtual uint64_t GetMaxBlockSize() const { return 0; } - virtual bool SetBlockPriorityPercentage(int64_t blockPriorityPercentage) { - return false; - } - virtual uint8_t GetBlockPriorityPercentage() const { return 0; } - virtual const CChainParams &GetChainParams() const { - return Params(CBaseChainParams::MAIN); - } - virtual void SetCashAddrEncoding(bool b) { useCashAddr = b; } - virtual bool UseCashAddrEncoding() const { return useCashAddr; } + DstCfgDummy() : useCashAddr(false) {} + void SetCashAddrEncoding(bool b) override { useCashAddr = b; } + bool UseCashAddrEncoding() const override { return useCashAddr; } private: bool useCashAddr; }; } // anon ns BOOST_FIXTURE_TEST_SUITE(dstencode_tests, BasicTestingSetup) BOOST_AUTO_TEST_CASE(test_addresses) { std::vector hash = {118, 160, 64, 83, 189, 160, 168, 139, 218, 81, 119, 184, 106, 21, 195, 178, 159, 85, 152, 115}; const CTxDestination dstKey = CKeyID(uint160(hash)); const CTxDestination dstScript = CScriptID(uint160(hash)); std::string cashaddr_pubkey = "bitcoincash:qpm2qsznhks23z7629mms6s4cwef74vcwvy22gdx6a"; std::string cashaddr_script = "bitcoincash:ppm2qsznhks23z7629mms6s4cwef74vcwvn0h829pq"; std::string base58_pubkey = "1BpEi6DfDAUFd7GtittLSdBeYJvcoaVggu"; std::string base58_script = "3CWFddi6m4ndiGyKqzYvsFYagqDLPVMTzC"; const CChainParams ¶ms = Params(CBaseChainParams::MAIN); - DummyCfg cfg; + DstCfgDummy cfg; // Check encoding cfg.SetCashAddrEncoding(true); BOOST_CHECK_EQUAL(cashaddr_pubkey, EncodeDestination(dstKey, params, cfg)); BOOST_CHECK_EQUAL(cashaddr_script, EncodeDestination(dstScript, params, cfg)); cfg.SetCashAddrEncoding(false); BOOST_CHECK_EQUAL(base58_pubkey, EncodeDestination(dstKey, params, cfg)); BOOST_CHECK_EQUAL(base58_script, EncodeDestination(dstScript, params, cfg)); // Check decoding BOOST_CHECK(dstKey == DecodeDestination(cashaddr_pubkey, params)); BOOST_CHECK(dstScript == DecodeDestination(cashaddr_script, params)); BOOST_CHECK(dstKey == DecodeDestination(base58_pubkey, params)); BOOST_CHECK(dstScript == DecodeDestination(base58_script, params)); // Validation BOOST_CHECK(IsValidDestinationString(cashaddr_pubkey, params)); BOOST_CHECK(IsValidDestinationString(cashaddr_script, params)); BOOST_CHECK(IsValidDestinationString(base58_pubkey, params)); BOOST_CHECK(IsValidDestinationString(base58_script, params)); BOOST_CHECK(!IsValidDestinationString("notvalid", params)); } BOOST_AUTO_TEST_SUITE_END()