Skip to content

Commit 9feb887

Browse files
committed
rpc: check fopen return code in dumptxoutset
This change improves the usability of the `dumptxoutset` RPC in two ways, in the case that an invalid path is passed: 1. return from the RPC immediately, rather then when the file is first tried to be written (which is _after_ calculating the UTXO set hash) 2. return a proper return code and error message instead of the cryptic "CAutoFile::operator<<: file handle is nullptr: unspecified iostream_category error" (-1)
1 parent b8ded26 commit 9feb887

File tree

2 files changed

+11
-1
lines changed

2 files changed

+11
-1
lines changed

src/rpc/blockchain.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2281,6 +2281,12 @@ static RPCHelpMan dumptxoutset()
22812281

22822282
FILE* file{fsbridge::fopen(temppath, "wb")};
22832283
CAutoFile afile{file, SER_DISK, CLIENT_VERSION};
2284+
if (afile.IsNull()) {
2285+
throw JSONRPCError(
2286+
RPC_INVALID_PARAMETER,
2287+
"Couldn't open file " + temppath.u8string() + " for writing.");
2288+
}
2289+
22842290
NodeContext& node = EnsureAnyNodeContext(request.context);
22852291
UniValue result = CreateUTXOSnapshot(
22862292
node, node.chainman->ActiveChainstate(), afile, path, temppath);

test/functional/rpc_dumptxoutset.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,9 +49,13 @@ def run_test(self):
4949
out['txoutset_hash'], '1f7e3befd45dc13ae198dfbb22869a9c5c4196f8e9ef9735831af1288033f890')
5050
assert_equal(out['nchaintx'], 101)
5151

52-
# Specifying a path to an existing file will fail.
52+
# Specifying a path to an existing or invalid file will fail.
5353
assert_raises_rpc_error(
5454
-8, '{} already exists'.format(FILENAME), node.dumptxoutset, FILENAME)
55+
invalid_path = str(Path(node.datadir) / "invalid" / "path")
56+
assert_raises_rpc_error(
57+
-8, "Couldn't open file {}.incomplete for writing".format(invalid_path), node.dumptxoutset, invalid_path)
58+
5559

5660
if __name__ == '__main__':
5761
DumptxoutsetTest().main()

0 commit comments

Comments
 (0)