Wallet: Support disabling implicit Segwit operation

This commit is contained in:
Luke Dashjr 2018-01-11 10:44:25 +00:00
parent a6908b00e8
commit b7fc4ba788
8 changed files with 43 additions and 8 deletions

View File

@ -45,6 +45,7 @@ void DummyWalletInit::AddWalletOptions(ArgsManager& argsman) const
"-wallet=<path>",
"-walletbroadcast",
"-walletdir=<dir>",
"-walletimplicitsegwit",
"-walletnotify=<cmd>",
"-walletrbf",
"-dblogsize=<n>",

View File

@ -72,7 +72,7 @@ std::vector<CTxDestination> GetAllDestinationsForKey(const CPubKey& key)
{
PKHash keyid(key);
CTxDestination p2pkh{keyid};
if (key.IsCompressed()) {
if (key.IsCompressed() && g_implicit_segwit) {
CTxDestination segwit = WitnessV0KeyHash(keyid);
CTxDestination p2sh = ScriptHash(GetScriptForDestination(segwit));
return Vector(std::move(p2pkh), std::move(p2sh), std::move(segwit));

View File

@ -9,6 +9,8 @@
#include <logging.h>
bool g_implicit_segwit = true;
const SigningProvider& DUMMY_SIGNING_PROVIDER = SigningProvider();
template<typename M, typename K, typename V>
@ -102,7 +104,7 @@ void FillableSigningProvider::ImplicitlyLearnRelatedKeyScripts(const CPubKey& pu
// "Implicitly" refers to fact that scripts are derived automatically from
// existing keys, and are present in memory, even without being explicitly
// loaded (e.g. from a file).
if (pubkey.IsCompressed()) {
if (pubkey.IsCompressed() && g_implicit_segwit) {
CScript script = GetScriptForDestination(WitnessV0KeyHash(key_id));
// This does not use AddCScript, as it may be overridden.
CScriptID id(script);

View File

@ -14,6 +14,10 @@
#include <script/script.h>
#include <sync.h>
static const bool DEFAULT_WALLET_IMPLICIT_SEGWIT = false;
extern bool g_implicit_segwit;
struct ShortestVectorFirstComparator
{
bool operator()(const std::vector<unsigned char>& a, const std::vector<unsigned char>& b) const

View File

@ -12,6 +12,7 @@
#include <node/context.h>
#include <node/interface_ui.h>
#include <outputtype.h>
#include <script/signingprovider.h>
#include <univalue.h>
#include <util/check.h>
#include <util/moneystr.h>
@ -74,6 +75,7 @@ void WalletInit::AddWalletOptions(ArgsManager& argsman) const
argsman.AddArg("-wallet=<path>", "Specify wallet path to load at startup. Can be used multiple times to load multiple wallets. Path is to a directory containing wallet data and log files. If the path is not absolute, it is interpreted relative to <walletdir>. This only loads existing wallets and does not create new ones. For backwards compatibility this also accepts names of existing top-level data files in <walletdir>.", ArgsManager::ALLOW_ANY | ArgsManager::NETWORK_ONLY, OptionsCategory::WALLET);
argsman.AddArg("-walletbroadcast", strprintf("Make the wallet broadcast transactions (default: %u)", DEFAULT_WALLETBROADCAST), ArgsManager::ALLOW_ANY, OptionsCategory::WALLET);
argsman.AddArg("-walletdir=<dir>", "Specify directory to hold wallets (default: <datadir>/wallets if it exists, otherwise <datadir>)", ArgsManager::ALLOW_ANY | ArgsManager::NETWORK_ONLY, OptionsCategory::WALLET);
argsman.AddArg("-walletimplicitsegwit", strprintf("Support segwit when restoring wallet backups and importing keys (default: %u)", DEFAULT_WALLET_IMPLICIT_SEGWIT), ArgsManager::ALLOW_ANY, OptionsCategory::WALLET);
#if HAVE_SYSTEM
argsman.AddArg("-walletnotify=<cmd>", "Execute command when a wallet transaction changes. %s in cmd is replaced by TxID, %w is replaced by wallet name, %b is replaced by the hash of the block including the transaction (set to 'unconfirmed' if the transaction is not included) and %h is replaced by the block height (-1 if not included). %w is not currently implemented on windows. On systems where %w is supported, it should NOT be quoted because this would break shell escaping used to invoke the command.", ArgsManager::ALLOW_ANY, OptionsCategory::WALLET);
#endif
@ -122,6 +124,13 @@ bool WalletInit::ParameterInteraction() const
return InitError(Untranslated("-zapwallettxes has been removed. If you are attempting to remove a stuck transaction from your wallet, please use abandontransaction instead."));
}
g_implicit_segwit = gArgs.GetBoolArg("-walletimplicitsegwit", DEFAULT_WALLET_IMPLICIT_SEGWIT);
if (!g_implicit_segwit) {
if (gArgs.SoftSetArg("-addresstype", "legacy")) {
LogPrintf("%s: parameter interaction: -walletimplicitsegwit=%u -> setting -addresstype=legacy\n", __func__, g_implicit_segwit);
}
}
return true;
}

View File

@ -1490,6 +1490,7 @@ void LegacyScriptPubKeyMan::LearnRelatedScripts(const CPubKey& key, OutputType t
void LegacyScriptPubKeyMan::LearnAllRelatedScripts(const CPubKey& key)
{
if (!g_implicit_segwit) return;
// OutputType::P2SH_SEGWIT always adds all necessary scripts for all types.
LearnRelatedScripts(key, OutputType::P2SH_SEGWIT);
}

View File

@ -40,9 +40,11 @@ BOOST_AUTO_TEST_CASE(CanProvide)
BOOST_CHECK(keyman.CanProvide(p2sh_script, data));
}
BOOST_AUTO_TEST_CASE(Legacy_IsKeyActive)
static void legacy_IsKeyActive(const node::NodeContext& node, bool implicit_segwit)
{
CWallet wallet(m_node.chain.get(), "", CreateMockableWalletDatabase());
const bool save_g_implicit_segwit{g_implicit_segwit};
g_implicit_segwit = implicit_segwit;
CWallet wallet(node.chain.get(), "", CreateMockableWalletDatabase());
{
LOCK(wallet.cs_wallet);
wallet.SetMinVersion(FEATURE_LATEST);
@ -61,8 +63,9 @@ BOOST_AUTO_TEST_CASE(Legacy_IsKeyActive)
// 4 scripts per keypool key (P2PK, P2PKH, P2WPKH, P2SH-P2WPKH)
// Plus 4 scripts for the seed key
// (If !implicit_segwit, P2WPKH and P2SH-P2WPKH are not generated.)
auto scripts1 = spkm.GetScriptPubKeys();
BOOST_CHECK_EQUAL(scripts1.size(), 84);
BOOST_CHECK_EQUAL(scripts1.size(), implicit_segwit ? 84 : 42);
// All keys are active
for (const CScript& script : scripts1) {
@ -80,8 +83,9 @@ BOOST_AUTO_TEST_CASE(Legacy_IsKeyActive)
BOOST_CHECK(spkm.IsKeyActive(script));
// Key pool size did not change
// (If !implicit_segwit, the two segwit addresses are added back.)
auto scripts2 = spkm.GetScriptPubKeys();
BOOST_CHECK_EQUAL(scripts2.size(), 84);
BOOST_CHECK_EQUAL(scripts2.size(), implicit_segwit ? 84 : 44);
// Use key that is not the next key
// (i.e. address gap in wallet recovery)
@ -94,7 +98,7 @@ BOOST_AUTO_TEST_CASE(Legacy_IsKeyActive)
// Key pool size did not change
auto scripts3 = spkm.GetScriptPubKeys();
BOOST_CHECK_EQUAL(scripts3.size(), 84);
BOOST_CHECK_EQUAL(scripts3.size(), implicit_segwit ? 84 : 44);
// All keys are still active
for (const CScript& script : scripts3) {
@ -111,12 +115,23 @@ BOOST_AUTO_TEST_CASE(Legacy_IsKeyActive)
// 20 new keys were added
auto scripts4 = spkm.GetScriptPubKeys();
BOOST_CHECK_EQUAL(scripts4.size(), 84 * 2);
BOOST_CHECK_EQUAL(scripts4.size(), (implicit_segwit ? 84 : 43) * 2);
// All 10 original keys are now inactive
for (const CScript& script : scripts3) {
BOOST_CHECK(!spkm.IsKeyActive(script));
}
g_implicit_segwit = save_g_implicit_segwit;
}
BOOST_AUTO_TEST_CASE(Legacy_IsKeyActive)
{
legacy_IsKeyActive(m_node, /*implicit_segwit=*/true);
}
BOOST_AUTO_TEST_CASE(Legacy_IsKeyActive_no_implicit_segwit)
{
legacy_IsKeyActive(m_node, /*implicit_segwit=*/false);
}
BOOST_AUTO_TEST_CASE(Descriptor_IsKeyActive)

View File

@ -119,6 +119,9 @@ class TestNode():
"--gen-suppressions=all", "--exit-on-first-error=yes",
"--error-exitcode=1", "--quiet"] + self.args
if self.version is None:
self.args.append("-walletimplicitsegwit")
if self.version_is_at_least(190000):
self.args.append("-logthreadnames")
if self.version_is_at_least(219900):