Skip to content

Commit c248983

Browse files
authored
Merge pull request #1276 from delta1/issues-1263
fix: segfault on getpeginaddress rpc when not in elements mode
2 parents e14d987 + 5326676 commit c248983

File tree

3 files changed

+40
-0
lines changed

3 files changed

+40
-0
lines changed

src/wallet/rpc/elements.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -197,6 +197,13 @@ RPCHelpMan getpeginaddress()
197197

198198
// Get P2CH deposit address on mainchain from most recent fedpegscript.
199199
const auto& fedpegscripts = GetValidFedpegScripts(pwallet->chain().getTip(), Params().GetConsensus(), true /* nextblock_validation */);
200+
if (fedpegscripts.empty()) {
201+
std::string message = "No valid fedpegscripts.";
202+
if (!g_con_elementsmode) {
203+
message += " Not running in Elements mode, check your 'chain' param.";
204+
}
205+
throw JSONRPCError(RPC_INTERNAL_ERROR, message);
206+
}
200207
CTxDestination mainchain_dest(WitnessV0ScriptHash(calculate_contract(fedpegscripts.front().second, dest_script)));
201208
// P2SH-wrapped is the only valid choice for non-dynafed chains but still an
202209
// option for dynafed-enabled ones as well

test/functional/test_runner.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,7 @@
122122
'feature_block.py',
123123
'rpc_fundrawtransaction.py --legacy-wallet',
124124
'rpc_fundrawtransaction.py --descriptors',
125+
'wallet_elements_regression_1263.py',
125126
'p2p_compactblocks.py',
126127
'p2p_compactblocks_blocksonly.py',
127128
'feature_segwit.py --legacy-wallet',
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
#!/usr/bin/env python3
2+
# Copyright (c) 2017-2020 The Bitcoin Core developers
3+
# Distributed under the MIT software license, see the accompanying
4+
# file COPYING or http://www.opensource.org/licenses/mit-license.php.
5+
"""Test getpeginaddress when started with Bitcoin chain params.
6+
7+
This doesn't make sense and should fail gracefully, but is causing a segfault.
8+
This functional test should catch any regression to this fix.
9+
"""
10+
11+
from test_framework.util import assert_raises_rpc_error
12+
from test_framework.test_framework import BitcoinTestFramework
13+
14+
class RegressionTest(BitcoinTestFramework):
15+
def set_test_params(self):
16+
self.setup_clean_chain = True
17+
self.num_nodes = 1
18+
self.chain = "regtest"
19+
20+
def skip_test_if_missing_module(self):
21+
self.skip_if_no_wallet()
22+
23+
def run_test(self):
24+
self.log.info("Start in Bitcoin regtest mode")
25+
self.nodes[0].createwallet("pegin")
26+
rpc = self.nodes[0].get_wallet_rpc("pegin")
27+
28+
self.log.info("Call getpeginaddress")
29+
assert_raises_rpc_error(-32603, "No valid fedpegscripts. Not running in Elements mode, check your 'chain' param.", rpc.getpeginaddress)
30+
31+
if __name__ == '__main__':
32+
RegressionTest().main()

0 commit comments

Comments
 (0)