Skip to content

Commit afdfd3c

Browse files
author
MarcoFalke
committed
Merge bitcoin#20403: wallet: upgradewallet fixes, improvements, test coverage
3eb6f8b wallet (not for backport): improve upgradewallet error messages (Jon Atack) ca8cd89 wallet: fix and improve upgradewallet error responses (Jon Atack) 99d56e3 wallet: fix and improve upgradewallet result responses (Jon Atack) 2498b04 Don't upgrade to HD split if it is already supported (Andrew Chow) c46c18b wallet: refactor GetClosestWalletFeature() (Jon Atack) Pull request description: This follows up on bitcoin#18836 and bitcoin#20282 to fix and improve the as-yet unreleased `upgradewallet` feature and also implement review follow-up in bitcoin#18836 (comment). This PR fixes 4 upgradewallet issues: - this bug: bitcoin#20403 (comment) - it returns nothing in the absence of an RPC error, which isn't reassuring for users - it returns the same thing both in the case of a successful upgrade and when no upgrade took place - the error message object is currently dead code This PR fixes the above and provides: ...user feedback to not silently return without upgrading ``` { "wallet_name": "disable private keys", "previous_version": 169900, "current_version": 169900, "result": "Already at latest version. Wallet version unchanged." } ``` ...better feedback after successfully upgrading ``` { "wallet_name": "watch-only", "previous_version": 159900, "current_version": 169900, "result": "Wallet upgraded successfully from version 159900 to version 169900." } ``` ...helpful error responses ``` { "wallet_name": "blank", "previous_version": 169900, "current_version": 169900, "error": "Cannot downgrade wallet from version 169900 to version 159900. Wallet version unchanged." } { "wallet_name": "blank", "previous_version": 130000, "current_version": 130000, "error": "Cannot upgrade a non HD split wallet from version 130000 to version 169899 without upgrading to support pre-split keypool. Please use version 169900 or no version specified." } ``` updated help: ``` upgradewallet ( version ) Upgrade the wallet. Upgrades to the latest version if no version number is specified. New keys may be generated and a new wallet backup will need to be made. Arguments: 1. version (numeric, optional, default=169900) The version number to upgrade to. Default is the latest wallet version. Result: { (json object) "wallet_name" : "str", (string) Name of wallet this operation was performed on "previous_version" : n, (numeric) Version of wallet before this operation "current_version" : n, (numeric) Version of wallet after this operation "result" : "str", (string, optional) Description of result, if no error "error" : "str" (string, optional) Error message (if there is one) } ``` ACKs for top commit: achow101: ACK 3eb6f8b MarcoFalke: review ACK 3eb6f8b 🛡 Tree-SHA512: b767314069e26b5933b123acfea6aa40708507f504bdb22884da020a4ca1332af38a7072b061e36281533af9f4e236d94d3c129daf6fe5b55241127537038eed
2 parents 1e9e4b6 + 3eb6f8b commit afdfd3c

File tree

5 files changed

+79
-54
lines changed

5 files changed

+79
-54
lines changed

src/wallet/rpcwallet.cpp

Lines changed: 26 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4234,7 +4234,7 @@ static RPCHelpMan sethdseed()
42344234

42354235
// Do not do anything to non-HD wallets
42364236
if (!pwallet->CanSupportFeature(FEATURE_HD)) {
4237-
throw JSONRPCError(RPC_WALLET_ERROR, "Cannot set a HD seed on a non-HD wallet. Use the upgradewallet RPC in order to upgrade a non-HD wallet to HD");
4237+
throw JSONRPCError(RPC_WALLET_ERROR, "Cannot set an HD seed on a non-HD wallet. Use the upgradewallet RPC in order to upgrade a non-HD wallet to HD");
42384238
}
42394239

42404240
EnsureWalletIsUnlocked(pwallet);
@@ -4465,14 +4465,18 @@ static RPCHelpMan walletcreatefundedpsbt()
44654465
static RPCHelpMan upgradewallet()
44664466
{
44674467
return RPCHelpMan{"upgradewallet",
4468-
"\nUpgrade the wallet. Upgrades to the latest version if no version number is specified\n"
4468+
"\nUpgrade the wallet. Upgrades to the latest version if no version number is specified.\n"
44694469
"New keys may be generated and a new wallet backup will need to be made.",
44704470
{
4471-
{"version", RPCArg::Type::NUM, /* default */ strprintf("%d", FEATURE_LATEST), "The version number to upgrade to. Default is the latest wallet version"}
4471+
{"version", RPCArg::Type::NUM, /* default */ strprintf("%d", FEATURE_LATEST), "The version number to upgrade to. Default is the latest wallet version."}
44724472
},
44734473
RPCResult{
44744474
RPCResult::Type::OBJ, "", "",
44754475
{
4476+
{RPCResult::Type::STR, "wallet_name", "Name of wallet this operation was performed on"},
4477+
{RPCResult::Type::NUM, "previous_version", "Version of wallet before this operation"},
4478+
{RPCResult::Type::NUM, "current_version", "Version of wallet after this operation"},
4479+
{RPCResult::Type::STR, "result", /* optional */ true, "Description of result, if no error"},
44764480
{RPCResult::Type::STR, "error", /* optional */ true, "Error message (if there is one)"}
44774481
},
44784482
},
@@ -4495,11 +4499,27 @@ static RPCHelpMan upgradewallet()
44954499
version = request.params[0].get_int();
44964500
}
44974501
bilingual_str error;
4498-
if (!pwallet->UpgradeWallet(version, error)) {
4499-
throw JSONRPCError(RPC_WALLET_ERROR, error.original);
4502+
const int previous_version{pwallet->GetVersion()};
4503+
const bool wallet_upgraded{pwallet->UpgradeWallet(version, error)};
4504+
const int current_version{pwallet->GetVersion()};
4505+
std::string result;
4506+
4507+
if (wallet_upgraded) {
4508+
if (previous_version == current_version) {
4509+
result = "Already at latest version. Wallet version unchanged.";
4510+
} else {
4511+
result = strprintf("Wallet upgraded successfully from version %i to version %i.", previous_version, current_version);
4512+
}
45004513
}
4514+
45014515
UniValue obj(UniValue::VOBJ);
4502-
if (!error.empty()) {
4516+
obj.pushKV("wallet_name", pwallet->GetName());
4517+
obj.pushKV("previous_version", previous_version);
4518+
obj.pushKV("current_version", current_version);
4519+
if (!result.empty()) {
4520+
obj.pushKV("result", result);
4521+
} else {
4522+
CHECK_NONFATAL(!error.empty());
45034523
obj.pushKV("error", error.original);
45044524
}
45054525
return obj;

src/wallet/scriptpubkeyman.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -456,7 +456,7 @@ bool LegacyScriptPubKeyMan::Upgrade(int prev_version, int new_version, bilingual
456456
hd_upgrade = true;
457457
}
458458
// Upgrade to HD chain split if necessary
459-
if (IsFeatureSupported(new_version, FEATURE_HD_SPLIT)) {
459+
if (!IsFeatureSupported(prev_version, FEATURE_HD_SPLIT) && IsFeatureSupported(new_version, FEATURE_HD_SPLIT)) {
460460
WalletLogPrintf("Upgrading wallet to use HD chain split\n");
461461
m_storage.SetMinVersion(FEATURE_PRE_SPLIT_KEYPOOL);
462462
split_upgrade = FEATURE_HD_SPLIT > prev_version;

src/wallet/wallet.cpp

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4111,17 +4111,16 @@ bool CWallet::UpgradeWallet(int version, bilingual_str& error)
41114111
} else {
41124112
WalletLogPrintf("Allowing wallet upgrade up to %i\n", version);
41134113
}
4114-
if (version < prev_version)
4115-
{
4116-
error = _("Cannot downgrade wallet");
4114+
if (version < prev_version) {
4115+
error = strprintf(_("Cannot downgrade wallet from version %i to version %i. Wallet version unchanged."), prev_version, version);
41174116
return false;
41184117
}
41194118

41204119
LOCK(cs_wallet);
41214120

41224121
// Do not upgrade versions to any version between HD_SPLIT and FEATURE_PRE_SPLIT_KEYPOOL unless already supporting HD_SPLIT
41234122
if (!CanSupportFeature(FEATURE_HD_SPLIT) && version >= FEATURE_HD_SPLIT && version < FEATURE_PRE_SPLIT_KEYPOOL) {
4124-
error = _("Cannot upgrade a non HD split wallet without upgrading to support pre split keypool. Please use version 169900 or no version specified.");
4123+
error = strprintf(_("Cannot upgrade a non HD split wallet from version %i to version %i without upgrading to support pre-split keypool. Please use version %i or no version specified."), prev_version, version, FEATURE_PRE_SPLIT_KEYPOOL);
41254124
return false;
41264125
}
41274126

src/wallet/walletutil.cpp

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -91,13 +91,9 @@ bool IsFeatureSupported(int wallet_version, int feature_version)
9191

9292
WalletFeature GetClosestWalletFeature(int version)
9393
{
94-
if (version >= FEATURE_LATEST) return FEATURE_LATEST;
95-
if (version >= FEATURE_PRE_SPLIT_KEYPOOL) return FEATURE_PRE_SPLIT_KEYPOOL;
96-
if (version >= FEATURE_NO_DEFAULT_KEY) return FEATURE_NO_DEFAULT_KEY;
97-
if (version >= FEATURE_HD_SPLIT) return FEATURE_HD_SPLIT;
98-
if (version >= FEATURE_HD) return FEATURE_HD;
99-
if (version >= FEATURE_COMPRPUBKEY) return FEATURE_COMPRPUBKEY;
100-
if (version >= FEATURE_WALLETCRYPT) return FEATURE_WALLETCRYPT;
101-
if (version >= FEATURE_BASE) return FEATURE_BASE;
94+
const std::array<WalletFeature, 8> wallet_features{{FEATURE_LATEST, FEATURE_PRE_SPLIT_KEYPOOL, FEATURE_NO_DEFAULT_KEY, FEATURE_HD_SPLIT, FEATURE_HD, FEATURE_COMPRPUBKEY, FEATURE_WALLETCRYPT, FEATURE_BASE}};
95+
for (const WalletFeature& wf : wallet_features) {
96+
if (version >= wf) return wf;
97+
}
10298
return static_cast<WalletFeature>(0);
10399
}

test/functional/wallet_upgradewallet.py

Lines changed: 45 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,7 @@
2222
from test_framework.test_framework import BitcoinTestFramework
2323
from test_framework.util import (
2424
assert_equal,
25-
assert_greater_than,
2625
assert_is_hex_string,
27-
assert_raises_rpc_error,
2826
sha256sum_file,
2927
)
3028

@@ -92,6 +90,32 @@ def dumb_sync_blocks(self):
9290
v16_3_node.submitblock(b)
9391
assert_equal(v16_3_node.getblockcount(), to_height)
9492

93+
def test_upgradewallet(self, wallet, previous_version, requested_version=None, expected_version=None):
94+
unchanged = expected_version == previous_version
95+
new_version = previous_version if unchanged else expected_version if expected_version else requested_version
96+
assert_equal(wallet.getwalletinfo()["walletversion"], previous_version)
97+
assert_equal(wallet.upgradewallet(requested_version),
98+
{
99+
"wallet_name": "",
100+
"previous_version": previous_version,
101+
"current_version": new_version,
102+
"result": "Already at latest version. Wallet version unchanged." if unchanged else "Wallet upgraded successfully from version {} to version {}.".format(previous_version, new_version),
103+
}
104+
)
105+
assert_equal(wallet.getwalletinfo()["walletversion"], new_version)
106+
107+
def test_upgradewallet_error(self, wallet, previous_version, requested_version, msg):
108+
assert_equal(wallet.getwalletinfo()["walletversion"], previous_version)
109+
assert_equal(wallet.upgradewallet(requested_version),
110+
{
111+
"wallet_name": "",
112+
"previous_version": previous_version,
113+
"current_version": previous_version,
114+
"error": msg,
115+
}
116+
)
117+
assert_equal(wallet.getwalletinfo()["walletversion"], previous_version)
118+
95119
def run_test(self):
96120
self.nodes[0].generatetoaddress(101, self.nodes[0].getnewaddress())
97121
self.dumb_sync_blocks()
@@ -158,47 +182,37 @@ def copy_split_hd():
158182
self.restart_node(0)
159183
copy_v16()
160184
wallet = node_master.get_wallet_rpc(self.default_wallet_name)
161-
old_version = wallet.getwalletinfo()["walletversion"]
162-
163-
# calling upgradewallet without version arguments
164-
# should return nothing if successful
165-
assert_equal(wallet.upgradewallet(), {})
166-
new_version = wallet.getwalletinfo()["walletversion"]
167-
# upgraded wallet version should be greater than older one
168-
assert_greater_than(new_version, old_version)
185+
self.log.info("Test upgradewallet without a version argument")
186+
self.test_upgradewallet(wallet, previous_version=159900, expected_version=169900)
169187
# wallet should still contain the same balance
170188
assert_equal(wallet.getbalance(), v16_3_balance)
171189

172190
copy_non_hd()
173191
wallet = node_master.get_wallet_rpc(self.default_wallet_name)
174192
# should have no master key hash before conversion
175193
assert_equal('hdseedid' in wallet.getwalletinfo(), False)
176-
# calling upgradewallet with explicit version number
177-
# should return nothing if successful
178-
assert_equal(wallet.upgradewallet(169900), {})
179-
new_version = wallet.getwalletinfo()["walletversion"]
180-
# upgraded wallet should have version 169900
181-
assert_equal(new_version, 169900)
194+
self.log.info("Test upgradewallet with explicit version number")
195+
self.test_upgradewallet(wallet, previous_version=60000, requested_version=169900)
182196
# after conversion master key hash should be present
183197
assert_is_hex_string(wallet.getwalletinfo()['hdseedid'])
184198

185-
self.log.info('Intermediary versions don\'t effect anything')
199+
self.log.info("Intermediary versions don't effect anything")
186200
copy_non_hd()
187201
# Wallet starts with 60000
188202
assert_equal(60000, wallet.getwalletinfo()['walletversion'])
189203
wallet.unloadwallet()
190204
before_checksum = sha256sum_file(node_master_wallet)
191205
node_master.loadwallet('')
192-
# Can "upgrade" to 129999 which should have no effect on the wallet
193-
wallet.upgradewallet(129999)
194-
assert_equal(60000, wallet.getwalletinfo()['walletversion'])
206+
# Test an "upgrade" from 60000 to 129999 has no effect, as the next version is 130000
207+
self.test_upgradewallet(wallet, previous_version=60000, requested_version=129999, expected_version=60000)
195208
wallet.unloadwallet()
196209
assert_equal(before_checksum, sha256sum_file(node_master_wallet))
197210
node_master.loadwallet('')
198211

199212
self.log.info('Wallets cannot be downgraded')
200213
copy_non_hd()
201-
assert_raises_rpc_error(-4, 'Cannot downgrade wallet', wallet.upgradewallet, 40000)
214+
self.test_upgradewallet_error(wallet, previous_version=60000, requested_version=40000,
215+
msg="Cannot downgrade wallet from version 60000 to version 40000. Wallet version unchanged.")
202216
wallet.unloadwallet()
203217
assert_equal(before_checksum, sha256sum_file(node_master_wallet))
204218
node_master.loadwallet('')
@@ -208,8 +222,7 @@ def copy_split_hd():
208222
orig_kvs = dump_bdb_kv(node_master_wallet)
209223
assert b'\x07hdchain' not in orig_kvs
210224
# Upgrade to HD, no split
211-
wallet.upgradewallet(130000)
212-
assert_equal(130000, wallet.getwalletinfo()['walletversion'])
225+
self.test_upgradewallet(wallet, previous_version=60000, requested_version=130000)
213226
# Check that there is now a hd chain and it is version 1, no internal chain counter
214227
new_kvs = dump_bdb_kv(node_master_wallet)
215228
assert b'\x07hdchain' in new_kvs
@@ -236,16 +249,13 @@ def copy_split_hd():
236249
assert_equal('m/0\'/0\'/1\'', info['hdkeypath'])
237250

238251
self.log.info('Cannot upgrade to HD Split, needs Pre Split Keypool')
239-
assert_raises_rpc_error(-4, 'Cannot upgrade a non HD split wallet without upgrading to support pre split keypool', wallet.upgradewallet, 139900)
240-
assert_equal(130000, wallet.getwalletinfo()['walletversion'])
241-
assert_raises_rpc_error(-4, 'Cannot upgrade a non HD split wallet without upgrading to support pre split keypool', wallet.upgradewallet, 159900)
242-
assert_equal(130000, wallet.getwalletinfo()['walletversion'])
243-
assert_raises_rpc_error(-4, 'Cannot upgrade a non HD split wallet without upgrading to support pre split keypool', wallet.upgradewallet, 169899)
244-
assert_equal(130000, wallet.getwalletinfo()['walletversion'])
252+
for version in [139900, 159900, 169899]:
253+
self.test_upgradewallet_error(wallet, previous_version=130000, requested_version=version,
254+
msg="Cannot upgrade a non HD split wallet from version {} to version {} without upgrading to "
255+
"support pre-split keypool. Please use version 169900 or no version specified.".format(130000, version))
245256

246257
self.log.info('Upgrade HD to HD chain split')
247-
wallet.upgradewallet(169900)
248-
assert_equal(169900, wallet.getwalletinfo()['walletversion'])
258+
self.test_upgradewallet(wallet, previous_version=130000, requested_version=169900)
249259
# Check that the hdchain updated correctly
250260
new_kvs = dump_bdb_kv(node_master_wallet)
251261
hd_chain = new_kvs[b'\x07hdchain']
@@ -271,8 +281,7 @@ def copy_split_hd():
271281

272282
self.log.info('Upgrade non-HD to HD chain split')
273283
copy_non_hd()
274-
wallet.upgradewallet(169900)
275-
assert_equal(169900, wallet.getwalletinfo()['walletversion'])
284+
self.test_upgradewallet(wallet, previous_version=60000, requested_version=169900)
276285
# Check that the hdchain updated correctly
277286
new_kvs = dump_bdb_kv(node_master_wallet)
278287
hd_chain = new_kvs[b'\x07hdchain']
@@ -333,14 +342,15 @@ def copy_split_hd():
333342
# Check the wallet has a default key initially
334343
old_kvs = dump_bdb_kv(node_master_wallet)
335344
defaultkey = old_kvs[b'\x0adefaultkey']
336-
# Upgrade the wallet. Should still have the same default key
337-
wallet.upgradewallet(159900)
345+
self.log.info("Upgrade the wallet. Should still have the same default key.")
346+
self.test_upgradewallet(wallet, previous_version=139900, requested_version=159900)
338347
new_kvs = dump_bdb_kv(node_master_wallet)
339348
up_defaultkey = new_kvs[b'\x0adefaultkey']
340349
assert_equal(defaultkey, up_defaultkey)
341350
# 0.16.3 doesn't have a default key
342351
v16_3_kvs = dump_bdb_kv(v16_3_wallet)
343352
assert b'\x0adefaultkey' not in v16_3_kvs
344353

354+
345355
if __name__ == '__main__':
346356
UpgradeWalletTest().main()

0 commit comments

Comments
 (0)