Skip to content

Commit

Permalink
rpc: Improve error message when snapshot file does not exist
Browse files Browse the repository at this point in the history
The current message may be a bit misleading, since it just mentions
permissions, but not the file actually existing.

Add functional tests for the new error and for exercising the
permission error.
  • Loading branch information
TheCharlatan committed Jan 13, 2025
1 parent 35bf426 commit 825a950
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 0 deletions.
6 changes: 6 additions & 0 deletions src/rpc/blockchain.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3259,6 +3259,12 @@ static RPCHelpMan loadtxoutset()
ChainstateManager& chainman = EnsureChainman(node);
const fs::path path{AbsPathForConfigVal(EnsureArgsman(node), fs::u8path(self.Arg<std::string>("path")))};

if (!fs::exists(path)) {
throw JSONRPCError(
RPC_INVALID_PARAMETER,
"Txoutset snapshot file " + path.utf8string() + " does not exist.");
}

FILE* file{fsbridge::fopen(path, "rb")};
AutoFile afile{file};
if (afile.IsNull()) {
Expand Down
13 changes: 13 additions & 0 deletions test/functional/feature_assumeutxo.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
`CRegTestParams::m_assumeutxo_data` in `src/kernel/chainparams.cpp`.
"""
from shutil import rmtree
from stat import S_IWRITE
import platform

from dataclasses import dataclass
from test_framework.blocktools import (
Expand Down Expand Up @@ -190,7 +192,16 @@ def test_invalid_file_path(self):
self.log.info("Test bitcoind should fail when file path is invalid.")
node = self.nodes[0]
path = node.datadir_path / node.chain / "invalid" / "path"
assert_raises_rpc_error(-8, "Txoutset snapshot file {} does not exist.".format(path), node.loadtxoutset, path)

def test_invalid_file_permissions(self):
self.log.info("Test bitcoind should fail when file has no read permissions.")
node = self.nodes[0]
path = node.datadir_path / "bad_permission"
path.mkdir()
path.chmod(0o200)
assert_raises_rpc_error(-8, "Couldn't open file {} for reading.".format(path), node.loadtxoutset, path)
path.rmdir()

def test_snapshot_with_less_work(self, dump_output_path):
self.log.info("Test bitcoind should fail when snapshot has less accumulated work than this node.")
Expand Down Expand Up @@ -455,6 +466,8 @@ def check_dump_output(output):
self.test_invalid_snapshot_scenarios(dump_output['path'])
self.test_invalid_chainstate_scenarios()
self.test_invalid_file_path()
if platform.system() != "Windows":
self.test_invalid_file_permissions()
self.test_snapshot_block_invalidated(dump_output['path'])
self.test_snapshot_not_on_most_work_chain(dump_output['path'])

Expand Down

0 comments on commit 825a950

Please sign in to comment.