Changeset View
Changeset View
Standalone View
Standalone View
src/rpc/avalanche.cpp
Show First 20 Lines • Show All 437 Lines • ▼ Show 20 Lines | for (const auto &peer : g_avalanche->getPeers()) { | ||||
obj.pushKV("nodecount", uint64_t(peer.node_count)); | obj.pushKV("nodecount", uint64_t(peer.node_count)); | ||||
ret.push_back(obj); | ret.push_back(obj); | ||||
} | } | ||||
return ret; | return ret; | ||||
} | } | ||||
static void verifyProofOrThrow(const NodeContext &node, avalanche::Proof &proof, | |||||
const std::string &proofHex) { | |||||
bilingual_str error; | |||||
if (!avalanche::Proof::FromHex(proof, proofHex, error)) { | |||||
throw JSONRPCError(RPC_DESERIALIZATION_ERROR, error.original); | |||||
} | |||||
avalanche::ProofValidationState state; | |||||
{ | |||||
LOCK(cs_main); | |||||
if (!proof.verify(state, | |||||
node.chainman->ActiveChainstate().CoinsTip())) { | |||||
throw JSONRPCError(RPC_INVALID_PARAMETER, | |||||
"The proof is invalid: " + state.ToString()); | |||||
} | |||||
} | |||||
} | |||||
static UniValue verifyavalancheproof(const Config &config, | static UniValue verifyavalancheproof(const Config &config, | ||||
const JSONRPCRequest &request) { | const JSONRPCRequest &request) { | ||||
RPCHelpMan{ | RPCHelpMan{ | ||||
"verifyavalancheproof", | "verifyavalancheproof", | ||||
"Verify an avalanche proof is valid and return the error otherwise.\n", | "Verify an avalanche proof is valid and return the error otherwise.\n", | ||||
{ | { | ||||
{"proof", RPCArg::Type::STR_HEX, RPCArg::Optional::NO, | {"proof", RPCArg::Type::STR_HEX, RPCArg::Optional::NO, | ||||
"Proof to verify."}, | "Proof to verify."}, | ||||
}, | }, | ||||
RPCResult{RPCResult::Type::BOOL, "success", | RPCResult{RPCResult::Type::BOOL, "success", | ||||
"Whether the proof is valid or not."}, | "Whether the proof is valid or not."}, | ||||
RPCExamples{HelpExampleRpc("verifyavalancheproof", "\"<proof>\"")}, | RPCExamples{HelpExampleRpc("verifyavalancheproof", "\"<proof>\"")}, | ||||
} | } | ||||
.Check(request); | .Check(request); | ||||
RPCTypeCheck(request.params, {UniValue::VSTR}); | RPCTypeCheck(request.params, {UniValue::VSTR}); | ||||
avalanche::Proof proof; | avalanche::Proof proof; | ||||
bilingual_str error; | verifyProofOrThrow(EnsureNodeContext(request.context), proof, | ||||
if (!avalanche::Proof::FromHex(proof, request.params[0].get_str(), error)) { | request.params[0].get_str()); | ||||
throw JSONRPCError(RPC_DESERIALIZATION_ERROR, error.original); | |||||
} | |||||
NodeContext &node = EnsureNodeContext(request.context); | |||||
avalanche::ProofValidationState state; | |||||
{ | |||||
LOCK(cs_main); | |||||
if (!proof.verify(state, | |||||
node.chainman->ActiveChainstate().CoinsTip())) { | |||||
throw JSONRPCError(RPC_INVALID_PARAMETER, | |||||
"The proof is invalid: " + state.ToString()); | |||||
} | |||||
} | |||||
return true; | return true; | ||||
} | } | ||||
void RegisterAvalancheRPCCommands(CRPCTable &t) { | void RegisterAvalancheRPCCommands(CRPCTable &t) { | ||||
// clang-format off | // clang-format off | ||||
static const CRPCCommand commands[] = { | static const CRPCCommand commands[] = { | ||||
// category name actor (function) argNames | // category name actor (function) argNames | ||||
Show All 15 Lines |