Bitcoin ABC  0.24.7
P2P Digital Currency
avalanche.cpp
Go to the documentation of this file.
1 // Copyright (c) 2020 The Bitcoin developers
2 // Distributed under the MIT software license, see the accompanying
3 // file COPYING or http://www.opensource.org/licenses/mit-license.php.
4 
5 #include <avalanche/avalanche.h>
6 #include <avalanche/delegation.h>
9 #include <avalanche/processor.h>
10 #include <avalanche/proof.h>
11 #include <avalanche/proofbuilder.h>
12 #include <avalanche/validation.h>
13 #include <config.h>
14 #include <core_io.h>
15 #include <key_io.h>
16 #include <net_processing.h>
17 #include <node/context.h>
18 #include <rpc/blockchain.h>
19 #include <rpc/server.h>
20 #include <rpc/util.h>
21 #include <util/strencodings.h>
22 #include <util/translation.h>
23 
24 #include <univalue.h>
25 
26 static UniValue getavalanchekey(const Config &config,
27  const JSONRPCRequest &request) {
28  RPCHelpMan{
29  "getavalanchekey",
30  "Returns the key used to sign avalanche messages.\n",
31  {},
33  RPCExamples{HelpExampleRpc("getavalanchekey", "")},
34  }
35  .Check(request);
36 
37  if (!g_avalanche) {
38  throw JSONRPCError(RPC_INTERNAL_ERROR, "Avalanche is not initialized");
39  }
40 
41  return HexStr(g_avalanche->getSessionPubKey());
42 }
43 
44 static CPubKey ParsePubKey(const UniValue &param) {
45  const std::string keyHex = param.get_str();
46  if ((keyHex.length() != 2 * CPubKey::COMPRESSED_SIZE &&
47  keyHex.length() != 2 * CPubKey::SIZE) ||
48  !IsHex(keyHex)) {
50  strprintf("Invalid public key: %s\n", keyHex));
51  }
52 
53  return HexToPubKey(keyHex);
54 }
55 
57  return g_avalanche->withPeerManager([&](avalanche::PeerManager &pm) {
58  return pm.getProof(proof->getId()) ||
59  pm.registerProof(std::move(proof));
60  });
61 }
62 
64  const std::string &dgHex, CPubKey &auth) {
66  if (!avalanche::Delegation::FromHex(dg, dgHex, error)) {
68  }
69 
71  if (!dg.verify(state, auth)) {
73  "The delegation is invalid: " + state.ToString());
74  }
75 }
76 
77 static void verifyProofOrThrow(const NodeContext &node, avalanche::Proof &proof,
78  const std::string &proofHex) {
80  if (!avalanche::Proof::FromHex(proof, proofHex, error)) {
82  }
83 
85  {
86  LOCK(cs_main);
87  if (!proof.verify(state,
88  node.chainman->ActiveChainstate().CoinsTip())) {
90  "The proof is invalid: " + state.ToString());
91  }
92  }
93 }
94 
95 static UniValue addavalanchenode(const Config &config,
96  const JSONRPCRequest &request) {
97  RPCHelpMan{
98  "addavalanchenode",
99  "Add a node in the set of peers to poll for avalanche.\n",
100  {
102  "Node to be added to avalanche."},
104  "The public key of the node."},
106  "Proof that the node is not a sybil."},
108  "The proof delegation the the node public key"},
109  },
110  RPCResult{RPCResult::Type::BOOL, "success",
111  "Whether the addition succeeded or not."},
112  RPCExamples{
113  HelpExampleRpc("addavalanchenode", "5, \"<pubkey>\", \"<proof>\"")},
114  }
115  .Check(request);
116 
117  RPCTypeCheck(request.params,
118  {UniValue::VNUM, UniValue::VSTR, UniValue::VSTR});
119 
120  if (!g_avalanche) {
121  throw JSONRPCError(RPC_INTERNAL_ERROR, "Avalanche is not initialized");
122  }
123 
124  const NodeId nodeid = request.params[0].get_int64();
125  CPubKey key = ParsePubKey(request.params[1]);
126 
127  auto proof = std::make_shared<avalanche::Proof>();
128  NodeContext &node = EnsureNodeContext(request.context);
129  verifyProofOrThrow(node, *proof, request.params[2].get_str());
130 
131  const avalanche::ProofId &proofid = proof->getId();
132  if (key != proof->getMaster()) {
133  if (request.params.size() < 4 || request.params[3].isNull()) {
135  "The public key does not match the proof");
136  }
137 
139  CPubKey auth;
140  verifyDelegationOrThrow(dg, request.params[3].get_str(), auth);
141 
142  if (dg.getProofId() != proofid) {
144  "The delegation does not match the proof");
145  }
146 
147  if (key != auth) {
149  "The public key does not match the delegation");
150  }
151  }
152 
153  if (!registerProofIfNeeded(proof)) {
155  "The proof has conflicting utxos");
156  }
157 
158  if (!node.connman->ForNode(nodeid, [&](CNode *pnode) {
159  // FIXME This is not thread safe, and might cause issues if the
160  // unlikely event the peer sends an avahello message at the same
161  // time.
162  if (!pnode->m_avalanche_state) {
163  pnode->m_avalanche_state =
164  std::make_unique<CNode::AvalancheState>();
165  }
166  pnode->m_avalanche_state->pubkey = std::move(key);
167  return true;
168  })) {
170  strprintf("The node does not exist: %d", nodeid));
171  ;
172  }
173 
174  return g_avalanche->withPeerManager([&](avalanche::PeerManager &pm) {
175  if (!pm.addNode(nodeid, proofid)) {
176  return false;
177  }
178 
179  pm.addUnbroadcastProof(proofid);
180  return true;
181  });
182 }
183 
184 static UniValue buildavalancheproof(const Config &config,
185  const JSONRPCRequest &request) {
186  RPCHelpMan{
187  "buildavalancheproof",
188  "Build a proof for avalanche's sybil resistance.\n",
189  {
191  "The proof's sequence"},
192  {"expiration", RPCArg::Type::NUM, RPCArg::Optional::NO,
193  "A timestamp indicating when the proof expire"},
195  "The master private key in base58-encoding"},
196  {
197  "stakes",
200  "The stakes to be signed and associated private keys",
201  {
202  {
203  "stake",
206  "A stake to be attached to this proof",
207  {
208  {"txid", RPCArg::Type::STR_HEX,
209  RPCArg::Optional::NO, "The transaction id"},
211  "The output number"},
212  {"amount", RPCArg::Type::AMOUNT,
213  RPCArg::Optional::NO, "The amount in this UTXO"},
215  "The height at which this UTXO was mined"},
216  {"iscoinbase", RPCArg::Type::BOOL,
217  /* default */ "false",
218  "Indicate wether the UTXO is a coinbase"},
219  {"privatekey", RPCArg::Type::STR,
221  "private key in base58-encoding"},
222  },
223  },
224  },
225  },
226  {"payoutAddress", RPCArg::Type::STR, RPCArg::Optional::OMITTED,
227  "A payout address (not required for legacy proofs)"},
228  },
230  "A string that is a serialized, hex-encoded proof data."},
231  RPCExamples{HelpExampleRpc("buildavalancheproof",
232  "0 1234567800 \"<master>\" []")},
233  }
234  .Check(request);
235 
236  RPCTypeCheck(request.params, {UniValue::VNUM, UniValue::VNUM,
237  UniValue::VSTR, UniValue::VARR});
238 
239  const uint64_t sequence = request.params[0].get_int64();
240  const int64_t expiration = request.params[1].get_int64();
241 
242  CKey masterKey = DecodeSecret(request.params[2].get_str());
243  if (!masterKey.IsValid()) {
244  throw JSONRPCError(RPC_INVALID_PARAMETER, "Invalid master key");
245  }
246 
247  CTxDestination payoutAddress = CNoDestination();
249  if (request.params[4].isNull()) {
250  throw JSONRPCError(
252  "A payout address is required if `-legacyavaproof` is false");
253  }
254  payoutAddress = DecodeDestination(request.params[4].get_str(),
255  config.GetChainParams());
256 
257  if (!IsValidDestination(payoutAddress)) {
258  throw JSONRPCError(RPC_INVALID_PARAMETER, "Invalid payout address");
259  }
260  }
261 
262  avalanche::ProofBuilder pb(sequence, expiration, masterKey,
263  GetScriptForDestination(payoutAddress));
264 
265  const UniValue &stakes = request.params[3].get_array();
266  for (size_t i = 0; i < stakes.size(); i++) {
267  const UniValue &stake = stakes[i];
268  RPCTypeCheckObj(stake,
269  {
270  {"txid", UniValue::VSTR},
271  {"vout", UniValue::VNUM},
272  // "amount" is also required but check is done below
273  // due to UniValue::VNUM erroneously not accepting
274  // quoted numerics (which are valid JSON)
275  {"height", UniValue::VNUM},
276  {"privatekey", UniValue::VSTR},
277  });
278 
279  int nOut = find_value(stake, "vout").get_int();
280  if (nOut < 0) {
282  "vout cannot be negative");
283  }
284 
285  const int height = find_value(stake, "height").get_int();
286  if (height < 1) {
288  "height must be positive");
289  }
290 
291  const TxId txid(ParseHashO(stake, "txid"));
292  const COutPoint utxo(txid, nOut);
293 
294  if (!stake.exists("amount")) {
295  throw JSONRPCError(RPC_INVALID_PARAMETER, "Missing amount");
296  }
297 
298  const Amount amount = AmountFromValue(find_value(stake, "amount"));
299 
300  const UniValue &iscbparam = find_value(stake, "iscoinbase");
301  const bool iscoinbase =
302  iscbparam.isNull() ? false : iscbparam.get_bool();
303  CKey key = DecodeSecret(find_value(stake, "privatekey").get_str());
304 
305  if (!key.IsValid()) {
306  throw JSONRPCError(RPC_INVALID_PARAMETER, "Invalid private key");
307  }
308 
309  if (!pb.addUTXO(utxo, amount, uint32_t(height), iscoinbase,
310  std::move(key))) {
311  throw JSONRPCError(RPC_INVALID_PARAMETER, "Duplicated stake");
312  }
313  }
314 
315  const avalanche::ProofRef proof = pb.build();
316 
318  ss << *proof;
319  return HexStr(ss);
320 }
321 
322 static UniValue decodeavalancheproof(const Config &config,
323  const JSONRPCRequest &request) {
324  RPCHelpMan{
325  "decodeavalancheproof",
326  "Convert a serialized, hex-encoded proof, into JSON object. "
327  "The validity of the proof is not verified.\n",
328  {
330  "The proof hex string"},
331  },
332  RPCResult{
334  "",
335  "",
336  {
337  {RPCResult::Type::NUM, "sequence",
338  "The proof's sequential number"},
339  {RPCResult::Type::NUM, "expiration",
340  "A timestamp indicating when the proof expires"},
341  {RPCResult::Type::STR_HEX, "master", "The master public key"},
342  {RPCResult::Type::STR, "signature",
343  "The proof signature (base64 encoded). Not available when "
344  "-legacyavaproof is enabled."},
346  "payoutscript",
347  "The proof payout script. Always empty when -legacyavaproof "
348  "is enabled.",
349  {
350  {RPCResult::Type::STR, "asm", "Decoded payout script"},
351  {RPCResult::Type::STR_HEX, "hex",
352  "Raw payout script in hex format"},
353  {RPCResult::Type::STR, "type",
354  "The output type (e.g. " + GetAllOutputTypes() + ")"},
355  {RPCResult::Type::NUM, "reqSigs",
356  "The required signatures"},
358  "addresses",
359  "",
360  {
361  {RPCResult::Type::STR, "address", "eCash address"},
362  }},
363  }},
364  {RPCResult::Type::STR_HEX, "limitedid",
365  "A hash of the proof data excluding the master key."},
366  {RPCResult::Type::STR_HEX, "proofid",
367  "A hash of the limitedid and master key."},
369  "stakes",
370  "",
371  {
373  "",
374  "",
375  {
376  {RPCResult::Type::STR_HEX, "txid",
377  "The transaction id"},
378  {RPCResult::Type::NUM, "vout", "The output number"},
379  {RPCResult::Type::STR_AMOUNT, "amount",
380  "The amount in this UTXO"},
381  {RPCResult::Type::NUM, "height",
382  "The height at which this UTXO was mined"},
383  {RPCResult::Type::BOOL, "iscoinbase",
384  "Indicate whether the UTXO is a coinbase"},
385  {RPCResult::Type::STR_HEX, "pubkey",
386  "This UTXO's public key"},
387  {RPCResult::Type::STR, "signature",
388  "Signature of the proofid with this UTXO's private "
389  "key (base64 encoded)"},
390  }},
391  }},
392  }},
393  RPCExamples{HelpExampleCli("decodeavalancheproof", "\"<hex proof>\"") +
394  HelpExampleRpc("decodeavalancheproof", "\"<hex proof>\"")},
395  }
396  .Check(request);
397 
398  RPCTypeCheck(request.params, {UniValue::VSTR});
399 
400  avalanche::Proof proof;
402  if (!avalanche::Proof::FromHex(proof, request.params[0].get_str(), error)) {
404  }
405 
406  UniValue result(UniValue::VOBJ);
407  result.pushKV("sequence", proof.getSequence());
408  result.pushKV("expiration", proof.getExpirationTime());
409  result.pushKV("master", HexStr(proof.getMaster()));
410 
411  const auto signature = proof.getSignature();
412  if (signature) {
413  result.pushKV("signature", EncodeBase64(*signature));
414  }
415 
416  const auto payoutScript = proof.getPayoutScript();
417  UniValue payoutScriptObj(UniValue::VOBJ);
418  ScriptPubKeyToUniv(payoutScript, payoutScriptObj, /* fIncludeHex */ true);
419  result.pushKV("payoutscript", payoutScriptObj);
420 
421  result.pushKV("limitedid", proof.getLimitedId().ToString());
422  result.pushKV("proofid", proof.getId().ToString());
423 
424  UniValue stakes(UniValue::VARR);
425  for (const avalanche::SignedStake &s : proof.getStakes()) {
426  const COutPoint &utxo = s.getStake().getUTXO();
427  UniValue stake(UniValue::VOBJ);
428  stake.pushKV("txid", utxo.GetTxId().ToString());
429  stake.pushKV("vout", uint64_t(utxo.GetN()));
430  stake.pushKV("amount", s.getStake().getAmount());
431  stake.pushKV("height", uint64_t(s.getStake().getHeight()));
432  stake.pushKV("iscoinbase", s.getStake().isCoinbase());
433  stake.pushKV("pubkey", HexStr(s.getStake().getPubkey()));
434  stake.pushKV("signature", EncodeBase64(s.getSignature()));
435  stakes.push_back(stake);
436  }
437  result.pushKV("stakes", stakes);
438 
439  return result;
440 }
441 
443  const JSONRPCRequest &request) {
444  RPCHelpMan{
445  "delegateavalancheproof",
446  "Delegate the avalanche proof to another public key.\n",
447  {
448  {"limitedproofid", RPCArg::Type::STR_HEX, RPCArg::Optional::NO,
449  "The limited id of the proof to be delegated."},
450  {"privatekey", RPCArg::Type::STR, RPCArg::Optional::NO,
451  "The private key in base58-encoding. Must match the proof master "
452  "public key or the upper level parent delegation public key if "
453  " supplied."},
455  "The public key to delegate the proof to."},
457  "A string that is the serialized, hex-encoded delegation for the "
458  "proof and which is a parent for the delegation to build."},
459  },
460  RPCResult{RPCResult::Type::STR_HEX, "delegation",
461  "A string that is a serialized, hex-encoded delegation."},
462  RPCExamples{
463  HelpExampleRpc("delegateavalancheproof",
464  "\"<limitedproofid>\" \"<privkey>\" \"<pubkey>\"")},
465  }
466  .Check(request);
467 
468  RPCTypeCheck(request.params,
469  {UniValue::VSTR, UniValue::VSTR, UniValue::VSTR});
470 
471  if (!g_avalanche) {
472  throw JSONRPCError(RPC_INTERNAL_ERROR, "Avalanche is not initialized");
473  }
474 
475  avalanche::LimitedProofId limitedProofId{
476  ParseHashV(request.params[0], "limitedproofid")};
477 
478  const CKey privkey = DecodeSecret(request.params[1].get_str());
479  if (!privkey.IsValid()) {
481  "The private key is invalid");
482  }
483 
484  const CPubKey pubkey = ParsePubKey(request.params[2]);
485 
486  std::unique_ptr<avalanche::DelegationBuilder> dgb;
487  if (request.params.size() >= 4 && !request.params[3].isNull()) {
489  CPubKey auth;
490  verifyDelegationOrThrow(dg, request.params[3].get_str(), auth);
491 
492  if (dg.getProofId() !=
493  limitedProofId.computeProofId(dg.getProofMaster())) {
495  "The delegation does not match the proof");
496  }
497 
498  if (privkey.GetPubKey() != auth) {
500  "The private key does not match the delegation");
501  }
502 
503  dgb = std::make_unique<avalanche::DelegationBuilder>(dg);
504  } else {
505  dgb = std::make_unique<avalanche::DelegationBuilder>(
506  limitedProofId, privkey.GetPubKey());
507  }
508 
509  if (!dgb->addLevel(privkey, pubkey)) {
510  throw JSONRPCError(RPC_MISC_ERROR, "Unable to build the delegation");
511  }
512 
514  ss << dgb->build();
515  return HexStr(ss);
516 }
517 
518 static UniValue getavalanchepeerinfo(const Config &config,
519  const JSONRPCRequest &request) {
520  RPCHelpMan{
521  "getavalanchepeerinfo",
522  "Returns data about each connected avalanche peer as a json array of "
523  "objects.\n",
524  {},
525  RPCResult{
527  "",
528  "",
529  {{
531  "",
532  "",
533  {{
534  {RPCResult::Type::NUM, "peerid", "The peer id"},
535  {RPCResult::Type::STR_HEX, "proof",
536  "The avalanche proof used by this peer"},
537  {RPCResult::Type::NUM, "nodecount",
538  "The number of nodes for this peer"},
540  "nodes",
541  "",
542  {
543  {RPCResult::Type::NUM, "nodeid",
544  "Node id, as returned by getpeerinfo"},
545  }},
546  }},
547  }},
548  },
549  RPCExamples{HelpExampleCli("getavalanchepeerinfo", "") +
550  HelpExampleRpc("getavalanchepeerinfo", "")},
551  }
552  .Check(request);
553 
554  if (!g_avalanche) {
555  throw JSONRPCError(RPC_INTERNAL_ERROR, "Avalanche is not initialized");
556  }
557 
559 
560  g_avalanche->withPeerManager([&](const avalanche::PeerManager &pm) {
561  pm.forEachPeer([&](const avalanche::Peer &peer) {
563 
565  serproof << *peer.proof;
566 
567  obj.pushKV("peerid", uint64_t(peer.peerid));
568  obj.pushKV("proof", HexStr(serproof));
569 
570  UniValue nodes(UniValue::VARR);
571  pm.forEachNode(peer, [&](const avalanche::Node &n) {
572  nodes.push_back(n.nodeid);
573  });
574 
575  obj.pushKV("nodecount", uint64_t(peer.node_count));
576  obj.pushKV("nodes", nodes);
577 
578  ret.push_back(obj);
579  });
580  });
581 
582  return ret;
583 }
584 
585 static UniValue getrawavalancheproof(const Config &config,
586  const JSONRPCRequest &request) {
587  RPCHelpMan{
588  "getrawavalancheproof",
589  "Lookup for a known avalanche proof by id.\n",
590  {
592  "The hex encoded avalanche proof identifier."},
593  },
594  RPCResult{
596  "",
597  "",
598  {{
599  {RPCResult::Type::STR_HEX, "proof",
600  "The hex encoded proof matching the identifier."},
601  {RPCResult::Type::BOOL, "orphan",
602  "Whether the proof is an orphan."},
603  }},
604  },
605  RPCExamples{HelpExampleRpc("getrawavalancheproof", "<proofid>")},
606  }
607  .Check(request);
608 
609  if (!g_avalanche) {
610  throw JSONRPCError(RPC_INTERNAL_ERROR, "Avalanche is not initialized");
611  }
612 
613  const avalanche::ProofId proofid =
615 
616  bool isOrphan = false;
617  auto proof = g_avalanche->withPeerManager([&](avalanche::PeerManager &pm) {
618  isOrphan = pm.isOrphan(proofid);
619  return pm.getProof(proofid);
620  });
621 
622  if (!proof) {
623  throw JSONRPCError(RPC_INVALID_PARAMETER, "Proof not found");
624  }
625 
627 
629  ss << *proof;
630  ret.pushKV("proof", HexStr(ss));
631  ret.pushKV("orphan", isOrphan);
632 
633  return ret;
634 }
635 
636 static UniValue sendavalancheproof(const Config &config,
637  const JSONRPCRequest &request) {
638  RPCHelpMan{
639  "sendavalancheproof",
640  "Broadcast an avalanche proof.\n",
641  {
643  "The avalanche proof to broadcast."},
644  },
645  RPCResult{RPCResult::Type::BOOL, "success",
646  "Whether the proof was sent successfully or not."},
647  RPCExamples{HelpExampleRpc("sendavalancheproof", "<proof>")},
648  }
649  .Check(request);
650 
651  if (!g_avalanche) {
652  throw JSONRPCError(RPC_INTERNAL_ERROR, "Avalanche is not initialized");
653  }
654 
655  auto proof = std::make_shared<avalanche::Proof>();
656  NodeContext &node = EnsureNodeContext(request.context);
657 
658  // Verify the proof. Note that this is redundant with the verification done
659  // when adding the proof to the pool, but we get a chance to give a better
660  // error message.
661  verifyProofOrThrow(node, *proof, request.params[0].get_str());
662 
663  // Add the proof to the pool if we don't have it already. Since the proof
664  // verification has already been done, a failure likely indicates that there
665  // already is a proof with conflicting utxos.
666  const avalanche::ProofId &proofid = proof->getId();
667  if (!registerProofIfNeeded(proof)) {
668  throw JSONRPCError(
670  "The proof has conflicting utxo with an existing proof");
671  }
672 
673  g_avalanche->withPeerManager(
674  [&](avalanche::PeerManager &pm) { pm.addUnbroadcastProof(proofid); });
675 
676  RelayProof(proofid, *node.connman);
677 
678  return true;
679 }
680 
681 static UniValue verifyavalancheproof(const Config &config,
682  const JSONRPCRequest &request) {
683  RPCHelpMan{
684  "verifyavalancheproof",
685  "Verify an avalanche proof is valid and return the error otherwise.\n",
686  {
688  "Proof to verify."},
689  },
690  RPCResult{RPCResult::Type::BOOL, "success",
691  "Whether the proof is valid or not."},
692  RPCExamples{HelpExampleRpc("verifyavalancheproof", "\"<proof>\"")},
693  }
694  .Check(request);
695 
696  RPCTypeCheck(request.params, {UniValue::VSTR});
697 
698  avalanche::Proof proof;
700  request.params[0].get_str());
701 
702  return true;
703 }
704 
706  // clang-format off
707  static const CRPCCommand commands[] = {
708  // category name actor (function) argNames
709  // ------------------- ------------------------ ---------------------- ----------
710  { "avalanche", "getavalanchekey", getavalanchekey, {}},
711  { "avalanche", "addavalanchenode", addavalanchenode, {"nodeid"}},
712  { "avalanche", "buildavalancheproof", buildavalancheproof, {"sequence", "expiration", "master", "stakes", "payoutAddress"}},
713  { "avalanche", "decodeavalancheproof", decodeavalancheproof, {"proof"}},
714  { "avalanche", "delegateavalancheproof", delegateavalancheproof, {"proof", "privatekey", "publickey", "delegation"}},
715  { "avalanche", "getavalanchepeerinfo", getavalanchepeerinfo, {}},
716  { "avalanche", "getrawavalancheproof", getrawavalancheproof, {"proofid"}},
717  { "avalanche", "sendavalancheproof", sendavalancheproof, {"proof"}},
718  { "avalanche", "verifyavalancheproof", verifyavalancheproof, {"proof"}},
719  };
720  // clang-format on
721 
722  for (unsigned int vcidx = 0; vcidx < ARRAYLEN(commands); vcidx++) {
723  t.appendCommand(commands[vcidx].name, &commands[vcidx]);
724  }
725 }
RPC_MISC_ERROR
@ RPC_MISC_ERROR
General application defined errors std::exception thrown in command handling.
Definition: protocol.h:38
avalanche::ProofBuilder
Definition: proofbuilder.h:17
HelpExampleCli
std::string HelpExampleCli(const std::string &methodname, const std::string &args)
Definition: util.cpp:157
avalanche::Delegation
Definition: delegation.h:23
UniValue::VOBJ
@ VOBJ
Definition: univalue.h:27
delegation.h
avalanche::Proof::getSignature
std::optional< const SchnorrSig > getSignature() const
Definition: proof.h:148
UniValue::get_bool
bool get_bool() const
Definition: univalue_get.cpp:91
delegateavalancheproof
static UniValue delegateavalancheproof(const Config &config, const JSONRPCRequest &request)
Definition: avalanche.cpp:442
RPC_INTERNAL_ERROR
@ RPC_INTERNAL_ERROR
Definition: protocol.h:33
JSONRPCRequest::context
const util::Ref & context
Definition: request.h:42
addavalanchenode
static UniValue addavalanchenode(const Config &config, const JSONRPCRequest &request)
Definition: avalanche.cpp:95
GetAllOutputTypes
std::string GetAllOutputTypes()
Definition: util.cpp:286
EnsureNodeContext
NodeContext & EnsureNodeContext(const util::Ref &context)
Definition: blockchain.cpp:56
avalanche.h
key_io.h
RPCHelpMan
Definition: util.h:334
RegisterAvalancheRPCCommands
void RegisterAvalancheRPCCommands(CRPCTable &t)
Register Avalanche RPC commands.
Definition: avalanche.cpp:705
GetScriptForDestination
CScript GetScriptForDestination(const CTxDestination &dest)
Generate a Bitcoin scriptPubKey for the given CTxDestination.
Definition: standard.cpp:243
avalanche::PeerManager::addNode
bool addNode(NodeId nodeid, const ProofId &proofid)
Node API.
Definition: peermanager.cpp:17
bilingual_str
Bilingual messages:
Definition: translation.h:17
RPC_INVALID_PARAMETER
@ RPC_INVALID_PARAMETER
Invalid, missing or duplicate parameter.
Definition: protocol.h:46
CPubKey::SIZE
static constexpr unsigned int SIZE
secp256k1:
Definition: pubkey.h:36
avalanche::PeerManager::addUnbroadcastProof
void addUnbroadcastProof(const ProofId &proofid)
Proof broadcast API.
Definition: peermanager.cpp:532
IsHex
bool IsHex(const std::string &str)
Returns true if each character in str is a hex character, and has an even number of hex digits.
Definition: strencodings.cpp:64
avalanche::Delegation::getProofId
ProofId getProofId() const
Definition: delegation.cpp:56
avalanche::PeerManager::registerProof
bool registerProof(const ProofRef &proof)
Proof and Peer related API.
Definition: peermanager.cpp:150
delegationbuilder.h
ParseHashV
uint256 ParseHashV(const UniValue &v, std::string strName)
Utilities: convert hex-encoded values (throws error if not hex).
Definition: util.cpp:99
getrawavalancheproof
static UniValue getrawavalancheproof(const Config &config, const JSONRPCRequest &request)
Definition: avalanche.cpp:585
CNode
Information about a peer.
Definition: net.h:926
RPCArg::Optional::NO
@ NO
Required arg.
avalanche::Proof::getId
const ProofId & getId() const
Definition: proof.h:152
RPCArg::Type::STR
@ STR
RPCArg::Type::ARR
@ ARR
avalanche::SignedStake
Definition: proof.h:85
avalanche::ProofId
Definition: proofid.h:17
RPCResult::Type::NUM
@ NUM
HexToPubKey
CPubKey HexToPubKey(const std::string &hex_in)
Definition: util.cpp:172
UniValue::isNull
bool isNull() const
Definition: univalue.h:89
RPCTypeCheckObj
void RPCTypeCheckObj(const UniValue &o, const std::map< std::string, UniValueType > &typesExpected, bool fAllowNull, bool fStrict)
Check for expected keys/value types in an Object.
Definition: util.cpp:52
avalanche::Node::nodeid
NodeId nodeid
Definition: node.h:22
ChainstateManager::ActiveChainstate
CChainState & ActiveChainstate() const
The most-work chain.
Definition: validation.cpp:6051
context.h
avalanche::Peer
Definition: peermanager.h:69
core_io.h
UniValue::pushKV
bool pushKV(const std::string &key, const UniValue &val)
Definition: univalue.cpp:133
getavalanchekey
static UniValue getavalanchekey(const Config &config, const JSONRPCRequest &request)
Definition: avalanche.cpp:26
g_avalanche
std::unique_ptr< avalanche::Processor > g_avalanche
Global avalanche instance.
Definition: processor.cpp:31
avalanche::ProofBuilder::addUTXO
bool addUTXO(COutPoint utxo, Amount amount, uint32_t height, bool is_coinbase, CKey key)
Definition: proofbuilder.cpp:23
UniValue
Definition: univalue.h:23
proofbuilder.h
ValidationState::ToString
std::string ToString() const
Definition: validation.h:124
ScriptPubKeyToUniv
void ScriptPubKeyToUniv(const CScript &scriptPubKey, UniValue &out, bool fIncludeHex)
Definition: core_write.cpp:186
SER_NETWORK
@ SER_NETWORK
Definition: serialize.h:165
avalanche::ProofBuilder::build
ProofRef build()
Definition: proofbuilder.cpp:36
RPCArg::Type::NUM
@ NUM
validation.h
avalanche::PeerManager::isOrphan
bool isOrphan(const ProofId &proofid) const
Definition: peermanager.cpp:250
UniValue::get_str
const std::string & get_str() const
Definition: univalue_get.cpp:98
RPC_DESERIALIZATION_ERROR
@ RPC_DESERIALIZATION_ERROR
Error parsing or validating structure in raw format.
Definition: protocol.h:50
strencodings.h
Config
Definition: config.h:17
COutPoint::GetTxId
const TxId & GetTxId() const
Definition: transaction.h:37
UniValue::get_int64
int64_t get_int64() const
Definition: univalue_get.cpp:115
RPCArg::Type::OBJ
@ OBJ
avalanche::DelegationState
Definition: validation.h:40
verifyDelegationOrThrow
static void verifyDelegationOrThrow(avalanche::Delegation &dg, const std::string &dgHex, CPubKey &auth)
Definition: avalanche.cpp:63
IsValidDestination
bool IsValidDestination(const CTxDestination &dest)
Check whether a CTxDestination is a CNoDestination.
Definition: standard.cpp:263
RPCArg::Type::STR_HEX
@ STR_HEX
Special type that is a STR with only hex chars.
cs_main
RecursiveMutex cs_main
Global state.
Definition: validation.cpp:103
RPCResult::Type::OBJ
@ OBJ
CRPCCommand
Definition: server.h:149
avalanche::ProofValidationState
Definition: validation.h:33
avalanche::Node
Definition: node.h:21
avalanche::Delegation::FromHex
static bool FromHex(Delegation &dg, const std::string &dgHex, bilingual_str &errorOut)
Definition: delegation.cpp:16
avalanche::PeerManager
Definition: peermanager.h:107
CNode::m_avalanche_state
std::unique_ptr< AvalancheState > m_avalanche_state
Definition: net.h:1191
UniValue::exists
bool exists(const std::string &key) const
Definition: univalue.h:87
avalanche::Proof::getSequence
uint64_t getSequence() const
Definition: proof.h:143
univalue.h
RPCResult::Type::STR_HEX
@ STR_HEX
Special string with only hex chars.
CKey::IsValid
bool IsValid() const
Check whether this private key is valid.
Definition: key.h:94
processor.h
getavalanchepeerinfo
static UniValue getavalanchepeerinfo(const Config &config, const JSONRPCRequest &request)
Definition: avalanche.cpp:518
RPCExamples
Definition: util.h:326
avalanche::Delegation::verify
bool verify(DelegationState &state, CPubKey &auth) const
Definition: delegation.cpp:73
avalanche::Peer::proof
ProofRef proof
Definition: peermanager.h:74
avalanche::Delegation::getProofMaster
const CPubKey & getProofMaster() const
Definition: delegation.h:54
avalanche::Peer::node_count
uint32_t node_count
Definition: peermanager.h:72
RPCResult::Type::STR
@ STR
DecodeSecret
CKey DecodeSecret(const std::string &str)
Definition: key_io.cpp:80
UniValue::VNUM
@ VNUM
Definition: univalue.h:27
avalanche::Proof::useLegacy
static bool useLegacy()
Definition: proof.cpp:53
CPubKey::COMPRESSED_SIZE
static constexpr unsigned int COMPRESSED_SIZE
Definition: pubkey.h:37
base_blob::ToString
std::string ToString() const
Definition: uint256.h:78
RPCResult::Type::ARR
@ ARR
avalanche::LimitedProofId
Definition: proofid.h:28
TxId
A TxId is the identifier of a transaction.
Definition: txid.h:14
CKey::GetPubKey
CPubKey GetPubKey() const
Compute the public key from a private key.
Definition: key.cpp:210
Amount
Definition: amount.h:19
registerProofIfNeeded
static bool registerProofIfNeeded(avalanche::ProofRef proof)
Definition: avalanche.cpp:56
HelpExampleRpc
std::string HelpExampleRpc(const std::string &methodname, const std::string &args)
Definition: util.cpp:162
avalanche::Proof::getStakes
const std::vector< SignedStake > & getStakes() const
Definition: proof.h:146
net_processing.h
CNoDestination
Definition: standard.h:96
avalanche::Proof
Definition: proof.h:102
JSONRPCRequest::params
UniValue params
Definition: request.h:37
CRPCTable
RPC command dispatcher.
Definition: server.h:202
avalanche::PeerManager::getProof
ProofRef getProof(const ProofId &proofid) const
Definition: peermanager.cpp:230
avalanche::Proof::FromHex
static bool FromHex(Proof &proof, const std::string &hexProof, bilingual_str &errorOut)
Definition: proof.cpp:62
RPC_INVALID_ADDRESS_OR_KEY
@ RPC_INVALID_ADDRESS_OR_KEY
Invalid address or key.
Definition: protocol.h:42
name
const char * name
Definition: rest.cpp:43
AmountFromValue
Amount AmountFromValue(const UniValue &value)
Definition: util.cpp:81
strprintf
#define strprintf
Format arguments and return the string or write to given std::ostream (see tinyformat::format doc for...
Definition: tinyformat.h:1201
peermanager.h
CRPCTable::appendCommand
void appendCommand(const std::string &name, const CRPCCommand *pcmd)
Appends a CRPCCommand to the dispatch table.
Definition: server.cpp:318
ParseHashO
uint256 ParseHashO(const UniValue &o, std::string strKey)
Definition: util.cpp:116
CPubKey
An encapsulated public key.
Definition: pubkey.h:31
RPCResult::Type::BOOL
@ BOOL
UniValue::get_int
int get_int() const
Definition: univalue_get.cpp:105
CKey
An encapsulated secp256k1 private key.
Definition: key.h:28
translation.h
RPCTypeCheck
void RPCTypeCheck(const UniValue &params, const std::list< UniValueType > &typesExpected, bool fAllowNull)
Type-check arguments; throws JSONRPCError if wrong type given.
Definition: util.cpp:25
RelayProof
void RelayProof(const avalanche::ProofId &proofid, const CConnman &connman)
Relay proof to every node.
Definition: net_processing.cpp:1867
Config::GetChainParams
virtual const CChainParams & GetChainParams() const =0
JSONRPCError
UniValue JSONRPCError(int code, const std::string &message)
Definition: request.cpp:52
LOCK
#define LOCK(cs)
Definition: sync.h:241
proof.h
ARRAYLEN
#define ARRAYLEN(array)
Utilities for converting data from/to strings.
Definition: strencodings.h:20
gArgs
ArgsManager gArgs
Definition: system.cpp:75
avalanche::Peer::peerid
PeerId peerid
Definition: peermanager.h:70
RPCArg::Type::BOOL
@ BOOL
avalanche::Proof::getPayoutScript
const CScript & getPayoutScript() const
Definition: proof.h:147
RPCArg::Optional::OMITTED
@ OMITTED
Optional argument with default value omitted because they are implicitly clear.
UniValue::push_back
bool push_back(const UniValue &val)
Definition: univalue.cpp:108
avalanche::ProofId::fromHex
static ProofId fromHex(const std::string &str)
Definition: proofid.h:21
avalanche::Proof::getMaster
const CPubKey & getMaster() const
Definition: proof.h:145
decodeavalancheproof
static UniValue decodeavalancheproof(const Config &config, const JSONRPCRequest &request)
Definition: avalanche.cpp:322
CDataStream
Double ended buffer combining vector and stream-like interfaces.
Definition: streams.h:197
config.h
buildavalancheproof
static UniValue buildavalancheproof(const Config &config, const JSONRPCRequest &request)
Definition: avalanche.cpp:184
UniValue::size
size_t size() const
Definition: univalue.h:80
NodeId
int64_t NodeId
Definition: nodeid.h:10
avalanche::Proof::getExpirationTime
int64_t getExpirationTime() const
Definition: proof.h:144
ParsePubKey
static CPubKey ParsePubKey(const UniValue &param)
Definition: avalanche.cpp:44
RPCArg::Type::AMOUNT
@ AMOUNT
Special type representing a floating point amount (can be either NUM or STR)
JSONRPCRequest
Definition: request.h:33
util.h
RPCResult
Definition: util.h:247
COutPoint::GetN
uint32_t GetN() const
Definition: transaction.h:38
COutPoint
An outpoint - a combination of a transaction hash and an index n into its vout.
Definition: transaction.h:22
blockchain.h
EncodeBase64
std::string EncodeBase64(Span< const uint8_t > input)
Definition: strencodings.cpp:137
find_value
const UniValue & find_value(const UniValue &obj, const std::string &name)
Definition: univalue.cpp:234
error
bool error(const char *fmt, const Args &... args)
Definition: system.h:48
avalanche::ProofRef
std::shared_ptr< const Proof > ProofRef
Definition: proof.h:163
RPCResult::Type::STR_AMOUNT
@ STR_AMOUNT
Special string to represent a floating point amount.
NodeContext::connman
std::unique_ptr< CConnman > connman
Definition: context.h:37
UniValue::get_array
const UniValue & get_array() const
Definition: univalue_get.cpp:142
UniValue::VARR
@ VARR
Definition: univalue.h:27
NodeContext
NodeContext struct containing references to chain state and connection state.
Definition: context.h:36
avalanche::Proof::verify
bool verify(ProofValidationState &state) const
Definition: proof.cpp:107
server.h
DecodeDestination
CTxDestination DecodeDestination(const std::string &addr, const CChainParams &params)
Definition: key_io.cpp:177
HexStr
std::string HexStr(const Span< const uint8_t > s)
Convert a span of bytes to a lower-case hexadecimal string.
Definition: strencodings.cpp:656
UniValue::VSTR
@ VSTR
Definition: univalue.h:27
avalanche::PeerManager::forEachNode
void forEachNode(const Peer &peer, Callable &&func) const
Definition: peermanager.h:186
CTxDestination
boost::variant< CNoDestination, PKHash, ScriptHash > CTxDestination
A txout script template with a specific destination.
Definition: standard.h:132
CChainState::CoinsTip
CCoinsViewCache & CoinsTip() EXCLUSIVE_LOCKS_REQUIRED(cs_main)
Definition: validation.h:837
verifyProofOrThrow
static void verifyProofOrThrow(const NodeContext &node, avalanche::Proof &proof, const std::string &proofHex)
Definition: avalanche.cpp:77
verifyavalancheproof
static UniValue verifyavalancheproof(const Config &config, const JSONRPCRequest &request)
Definition: avalanche.cpp:681
avalanche::Proof::getLimitedId
const LimitedProofId & getLimitedId() const
Definition: proof.h:153
sendavalancheproof
static UniValue sendavalancheproof(const Config &config, const JSONRPCRequest &request)
Definition: avalanche.cpp:636
PROTOCOL_VERSION
static const int PROTOCOL_VERSION
network protocol versioning
Definition: version.h:11
avalanche::PeerManager::forEachPeer
void forEachPeer(Callable &&func) const
Definition: peermanager.h:209
NodeContext::chainman
ChainstateManager * chainman
Definition: context.h:41