Changeset View
Changeset View
Standalone View
Standalone View
src/rpc/avalanche.cpp
// Copyright (c) 2020 The Bitcoin developers | // Copyright (c) 2020 The Bitcoin developers | ||||
// Distributed under the MIT software license, see the accompanying | // Distributed under the MIT software license, see the accompanying | ||||
// file COPYING or http://www.opensource.org/licenses/mit-license.php. | // file COPYING or http://www.opensource.org/licenses/mit-license.php. | ||||
#include <avalanche/delegation.h> | #include <avalanche/delegation.h> | ||||
#include <avalanche/delegationbuilder.h> | #include <avalanche/delegationbuilder.h> | ||||
#include <avalanche/peermanager.h> | #include <avalanche/peermanager.h> | ||||
#include <avalanche/processor.h> | #include <avalanche/processor.h> | ||||
#include <avalanche/proof.h> | #include <avalanche/proof.h> | ||||
#include <avalanche/proofbuilder.h> | #include <avalanche/proofbuilder.h> | ||||
#include <avalanche/validation.h> | #include <avalanche/validation.h> | ||||
#include <config.h> | #include <config.h> | ||||
#include <core_io.h> | #include <core_io.h> | ||||
#include <key_io.h> | #include <key_io.h> | ||||
#include <net_processing.h> | |||||
#include <node/context.h> | |||||
#include <rpc/blockchain.h> | |||||
#include <rpc/server.h> | #include <rpc/server.h> | ||||
#include <rpc/util.h> | #include <rpc/util.h> | ||||
#include <util/strencodings.h> | #include <util/strencodings.h> | ||||
#include <univalue.h> | #include <univalue.h> | ||||
static UniValue getavalanchekey(const Config &config, | static UniValue getavalanchekey(const Config &config, | ||||
const JSONRPCRequest &request) { | const JSONRPCRequest &request) { | ||||
▲ Show 20 Lines • Show All 372 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 UniValue sendavalancheproof(const Config &config, | |||||
const JSONRPCRequest &request) { | |||||
RPCHelpMan{ | |||||
"sendavalancheproof", | |||||
"Broadcast an avalanche proof.\n", | |||||
{ | |||||
{"proof", RPCArg::Type::STR_HEX, RPCArg::Optional::NO, | |||||
"The avalanche proof to broadcast."}, | |||||
}, | |||||
RPCResult{RPCResult::Type::BOOL, "success", | |||||
"Whether the proof was sent successfully or not."}, | |||||
RPCExamples{HelpExampleRpc("sendavalancheproof", "<proof>")}, | |||||
} | |||||
.Check(request); | |||||
if (!g_avalanche) { | |||||
throw JSONRPCError(RPC_INTERNAL_ERROR, "Avalanche is not initialized"); | |||||
} | |||||
avalanche::Proof proof; | |||||
CDataStream ss(ParseHexV(request.params[0], "proof"), SER_NETWORK, | |||||
PROTOCOL_VERSION); | |||||
ss >> proof; | |||||
const avalanche::ProofId &proofid = proof.getId(); | |||||
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()); | |||||
} | |||||
} | |||||
if (g_avalanche->addPeer(proof)) { | |||||
RelayProof(proofid, *node.connman); | |||||
return true; | |||||
} | |||||
return false; | |||||
} | |||||
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 | ||||
// ------------------- ------------------------ ---------------------- ---------- | // ------------------- ------------------------ ---------------------- ---------- | ||||
{ "avalanche", "getavalanchekey", getavalanchekey, {}}, | { "avalanche", "getavalanchekey", getavalanchekey, {}}, | ||||
{ "avalanche", "addavalanchenode", addavalanchenode, {"nodeid"}}, | { "avalanche", "addavalanchenode", addavalanchenode, {"nodeid"}}, | ||||
{ "avalanche", "buildavalancheproof", buildavalancheproof, {"sequence", "expiration", "master", "stakes"}}, | { "avalanche", "buildavalancheproof", buildavalancheproof, {"sequence", "expiration", "master", "stakes"}}, | ||||
{ "avalanche", "delegateavalancheproof", delegateavalancheproof, {"proof", "privatekey", "publickey", "delegation"}}, | { "avalanche", "delegateavalancheproof", delegateavalancheproof, {"proof", "privatekey", "publickey", "delegation"}}, | ||||
{ "avalanche", "getavalanchepeerinfo", getavalanchepeerinfo, {}}, | { "avalanche", "getavalanchepeerinfo", getavalanchepeerinfo, {}}, | ||||
{ "avalanche", "sendavalancheproof", sendavalancheproof, {"proof"}}, | |||||
}; | }; | ||||
// clang-format on | // clang-format on | ||||
for (unsigned int vcidx = 0; vcidx < ARRAYLEN(commands); vcidx++) { | for (unsigned int vcidx = 0; vcidx < ARRAYLEN(commands); vcidx++) { | ||||
t.appendCommand(commands[vcidx].name, &commands[vcidx]); | t.appendCommand(commands[vcidx].name, &commands[vcidx]); | ||||
} | } | ||||
} | } |