Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ endif()
set(CMAKE_CXX_STANDARD 20)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_CXX_EXTENSIONS OFF)
set(CMAKE_POSITION_INDEPENDENT_CODE ON)

# Default to Release build
if(NOT CMAKE_BUILD_TYPE OR CMAKE_BUILD_TYPE STREQUAL "")
Expand Down
4 changes: 2 additions & 2 deletions src/network/rpc_server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -679,7 +679,7 @@ std::string RPCServer::HandleGetBlockHash(const std::vector<std::string>& params
return util::JsonError("Block height out of range");
}

return index->GetBlockHash().GetHex() + "\n";
return "\"" + index->GetBlockHash().GetHex() + "\"\n";
}

std::string RPCServer::HandleGetBlockHeader(const std::vector<std::string>& params) {
Expand Down Expand Up @@ -755,7 +755,7 @@ std::string RPCServer::HandleGetBestBlockHash(const std::vector<std::string>& pa
return "null\n";
}

return tip->GetBlockHash().GetHex() + "\n";
return "\"" + tip->GetBlockHash().GetHex() + "\"\n";
}

std::string RPCServer::HandleGetConnectionCount(const std::vector<std::string>& params) {
Expand Down
23 changes: 23 additions & 0 deletions test/integration-network/rpc_integration_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#include "network/rpc_client.hpp"
#include "network/rpc_server.hpp"
#include "util/logging.hpp"
#include <nlohmann/json.hpp>

#include <thread>
#include <chrono>
Expand Down Expand Up @@ -604,6 +605,28 @@ TEST_CASE("RPC Commands: getbestblockhash", "[rpc][integration][blockchain]") {
std::string response = client.ExecuteCommand("getbestblockhash", {});
// Empty chain should return null/zero hash or error
REQUIRE(!response.empty());

auto j = nlohmann::json::parse(response);
REQUIRE(j.is_null());
}
}

TEST_CASE("RPC Commands: getblockhash", "[rpc][integration][blockchain]") {
RPCTestFixture fixture;
REQUIRE(fixture.StartServer());
std::this_thread::sleep_for(100ms);

rpc::RPCClient client(fixture.GetSocketPath());
REQUIRE_FALSE(client.Connect().has_value());

SECTION("Returns error for empty chain") {
std::string response = client.ExecuteCommand("getblockhash", {"0"});
REQUIRE(!response.empty());

auto j = nlohmann::json::parse(response);
REQUIRE(j.is_object());
REQUIRE(j.contains("error"));
REQUIRE(j["error"].get<std::string>().find("out of range") != std::string::npos);
}
}

Expand Down
Loading