Changeset View
Changeset View
Standalone View
Standalone View
src/rpc/avalanche.cpp
Show First 20 Lines • Show All 46 Lines • ▼ Show 20 Lines | if ((keyHex.length() != 2 * CPubKey::COMPRESSED_SIZE && | ||||
!IsHex(keyHex)) { | !IsHex(keyHex)) { | ||||
throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, | throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, | ||||
strprintf("Invalid public key: %s\n", keyHex)); | strprintf("Invalid public key: %s\n", keyHex)); | ||||
} | } | ||||
return HexToPubKey(keyHex); | return HexToPubKey(keyHex); | ||||
} | } | ||||
static bool registerProofIfNeeded(std::shared_ptr<avalanche::Proof> proof) { | |||||
return g_avalanche->withPeerManager([&](avalanche::PeerManager &pm) { | |||||
return pm.getProof(proof->getId()) || | |||||
pm.registerProof(std::move(proof)); | |||||
}); | |||||
} | |||||
static UniValue addavalanchenode(const Config &config, | static UniValue addavalanchenode(const Config &config, | ||||
const JSONRPCRequest &request) { | const JSONRPCRequest &request) { | ||||
RPCHelpMan{ | RPCHelpMan{ | ||||
"addavalanchenode", | "addavalanchenode", | ||||
"Add a node in the set of peers to poll for avalanche.\n", | "Add a node in the set of peers to poll for avalanche.\n", | ||||
{ | { | ||||
{"nodeid", RPCArg::Type::NUM, RPCArg::Optional::NO, | {"nodeid", RPCArg::Type::NUM, RPCArg::Optional::NO, | ||||
"Node to be added to avalanche."}, | "Node to be added to avalanche."}, | ||||
Show All 27 Lines | static UniValue addavalanchenode(const Config &config, | ||||
} | } | ||||
if (key != proof->getMaster()) { | if (key != proof->getMaster()) { | ||||
// TODO: we want to provide a proper delegation. | // TODO: we want to provide a proper delegation. | ||||
return false; | return false; | ||||
} | } | ||||
const avalanche::ProofId &proofid = proof->getId(); | const avalanche::ProofId &proofid = proof->getId(); | ||||
if (!g_avalanche->getProof(proofid) && !g_avalanche->addProof(proof)) { | if (!registerProofIfNeeded(proof)) { | ||||
return false; | return false; | ||||
} | } | ||||
NodeContext &node = EnsureNodeContext(request.context); | NodeContext &node = EnsureNodeContext(request.context); | ||||
if (!node.connman->ForNode(nodeid, [&](CNode *pnode) { | if (!node.connman->ForNode(nodeid, [&](CNode *pnode) { | ||||
// FIXME This is not thread safe, and might cause issues if the | // FIXME This is not thread safe, and might cause issues if the | ||||
// unlikely event the peer sends an avahello message at the same | // unlikely event the peer sends an avahello message at the same | ||||
// time. | // time. | ||||
▲ Show 20 Lines • Show All 464 Lines • ▼ Show 20 Lines | static UniValue sendavalancheproof(const Config &config, | ||||
// when adding the proof to the pool, but we get a chance to give a better | // when adding the proof to the pool, but we get a chance to give a better | ||||
// error message. | // error message. | ||||
verifyProofOrThrow(node, *proof, request.params[0].get_str()); | verifyProofOrThrow(node, *proof, request.params[0].get_str()); | ||||
// Add the proof to the pool if we don't have it already. Since the proof | // Add the proof to the pool if we don't have it already. Since the proof | ||||
// verification has already been done, a failure likely indicates that there | // verification has already been done, a failure likely indicates that there | ||||
// already is a proof with conflicting utxos. | // already is a proof with conflicting utxos. | ||||
const avalanche::ProofId &proofid = proof->getId(); | const avalanche::ProofId &proofid = proof->getId(); | ||||
if (!g_avalanche->getProof(proofid) && !g_avalanche->addProof(proof)) { | if (!registerProofIfNeeded(proof)) { | ||||
throw JSONRPCError( | throw JSONRPCError( | ||||
RPC_INVALID_PARAMETER, | RPC_INVALID_PARAMETER, | ||||
"The proof has conflicting utxo with an existing proof"); | "The proof has conflicting utxo with an existing proof"); | ||||
} | } | ||||
g_avalanche->withPeerManager( | g_avalanche->withPeerManager( | ||||
[&](avalanche::PeerManager &pm) { pm.addUnbroadcastProof(proofid); }); | [&](avalanche::PeerManager &pm) { pm.addUnbroadcastProof(proofid); }); | ||||
▲ Show 20 Lines • Show All 50 Lines • Show Last 20 Lines |