Changeset View
Changeset View
Standalone View
Standalone View
src/rpc/avalanche.cpp
Show First 20 Lines • Show All 74 Lines • ▼ Show 20 Lines | static UniValue addavalanchenode(const Config &config, | ||||
RPCTypeCheck(request.params, | RPCTypeCheck(request.params, | ||||
{UniValue::VNUM, UniValue::VSTR, UniValue::VSTR}); | {UniValue::VNUM, UniValue::VSTR, UniValue::VSTR}); | ||||
if (!g_avalanche) { | if (!g_avalanche) { | ||||
throw JSONRPCError(RPC_INTERNAL_ERROR, "Avalanche is not initialized"); | throw JSONRPCError(RPC_INTERNAL_ERROR, "Avalanche is not initialized"); | ||||
} | } | ||||
const NodeId nodeid = request.params[0].get_int64(); | const NodeId nodeid = request.params[0].get_int64(); | ||||
const CPubKey key = ParsePubKey(request.params[1]); | CPubKey key = ParsePubKey(request.params[1]); | ||||
auto proof = std::make_shared<avalanche::Proof>(); | auto proof = std::make_shared<avalanche::Proof>(); | ||||
bilingual_str error; | bilingual_str error; | ||||
if (!avalanche::Proof::FromHex(*proof, request.params[2].get_str(), | if (!avalanche::Proof::FromHex(*proof, request.params[2].get_str(), | ||||
error)) { | error)) { | ||||
throw JSONRPCError(RPC_INVALID_PARAMETER, error.original); | throw JSONRPCError(RPC_INVALID_PARAMETER, error.original); | ||||
} | } | ||||
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 (!g_avalanche->getProof(proofid) && !g_avalanche->addProof(proof)) { | ||||
return false; | return false; | ||||
} | } | ||||
NodeContext &node = EnsureNodeContext(request.context); | |||||
if (!node.connman->ForNode(nodeid, [&](CNode *pnode) { | |||||
// FIXME This is not thread safe, and might cause issues if the | |||||
// unlikely event the peer sends an avahello message at the same | |||||
// time. | |||||
if (!pnode->m_avalanche_state) { | |||||
pnode->m_avalanche_state = | |||||
std::make_unique<CNode::AvalancheState>(); | |||||
} | |||||
pnode->m_avalanche_state->pubkey = std::move(key); | |||||
return true; | |||||
})) { | |||||
return false; | |||||
} | |||||
if (!g_avalanche->addNode(nodeid, | if (!g_avalanche->addNode(nodeid, | ||||
avalanche::DelegationBuilder(*proof).build())) { | avalanche::DelegationBuilder(*proof).build())) { | ||||
return false; | return false; | ||||
} | } | ||||
g_avalanche->addUnbroadcastProof(proofid); | g_avalanche->addUnbroadcastProof(proofid); | ||||
return true; | return true; | ||||
} | } | ||||
▲ Show 20 Lines • Show All 507 Lines • Show Last 20 Lines |