Bitcoin ABC  0.26.3
P2P Digital Currency
util.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 
6 #include <avalanche/test/util.h>
7 #include <consensus/amount.h>
8 #include <key.h>
10 #include <random.h>
11 #include <script/standard.h>
12 #include <validation.h>
13 
14 #include <boost/test/unit_test.hpp>
15 
16 #include <limits>
17 
18 namespace avalanche {
19 
20 ProofRef buildRandomProof(CChainState &active_chainstate, uint32_t score,
21  int height, const CKey &masterKey) {
22  auto key = CKey::MakeCompressedKey();
23 
24  const COutPoint o(TxId(GetRandHash()), 0);
25  const Amount v = (int64_t(score) * COIN) / 100;
26  const bool is_coinbase = false;
27 
28  {
29  CScript script = GetScriptForDestination(PKHash(key.GetPubKey()));
30 
31  LOCK(cs_main);
32  CCoinsViewCache &coins = active_chainstate.CoinsTip();
33  coins.AddCoin(o, Coin(CTxOut(v, script), height, is_coinbase), false);
34  }
35 
36  ProofBuilder pb(0, std::numeric_limits<uint32_t>::max(), masterKey,
38  BOOST_CHECK(pb.addUTXO(o, v, height, is_coinbase, std::move(key)));
39  return pb.build();
40 }
41 
42 bool hasDustStake(const ProofRef &proof) {
43  for (const SignedStake &s : proof->getStakes()) {
44  if (s.getStake().getAmount() < PROOF_DUST_THRESHOLD) {
45  return true;
46  }
47  }
48  return false;
49 }
50 
51 LimitedProofId
53  CHashWriter ss(SER_GETHASH, 0);
54  ss << pb.sequence;
55  ss << pb.expirationTime;
56  ss << pb.payoutScriptPubKey;
57 
58  WriteCompactSize(ss, pb.stakes.size());
59  for (auto it = pb.stakes.rbegin(); it != pb.stakes.rend(); it++) {
60  ss << it->getStake();
61  }
62 
63  return LimitedProofId(ss.GetHash());
64 }
65 
67  const LimitedProofId limitedProofid =
69  const CPubKey masterPubKey = pb.masterKey.GetPubKey();
70  const StakeCommitment commitment(pb.expirationTime,
71  pb.masterKey.GetPubKey());
72 
73  std::vector<SignedStake> signedStakes;
74  signedStakes.reserve(pb.stakes.size());
75 
76  while (!pb.stakes.empty()) {
77  // We need a forward iterator, so pb.stakes.rbegin() is not an
78  // option.
79  auto handle = pb.stakes.extract(std::prev(pb.stakes.end()));
80  signedStakes.push_back(handle.value());
81  }
82 
83  SchnorrSig proofSignature;
84  BOOST_CHECK(pb.masterKey.SignSchnorr(limitedProofid, proofSignature));
85 
86  return ProofRef::make(pb.sequence, pb.expirationTime, masterPubKey,
87  std::move(signedStakes), pb.payoutScriptPubKey,
88  proofSignature);
89 }
90 
93  CHashWriter ss(SER_GETHASH, 0);
94  ss << pb.sequence;
95  ss << pb.expirationTime;
96  ss << pb.payoutScriptPubKey;
97 
98  WriteCompactSize(ss, 2 * pb.stakes.size());
99  for (auto &s : pb.stakes) {
100  ss << s.getStake();
101  ss << s.getStake();
102  }
103 
104  return LimitedProofId(ss.GetHash());
105 }
106 
108  const LimitedProofId limitedProofid =
110  const CPubKey masterPubKey = pb.masterKey.GetPubKey();
111  const StakeCommitment commitment(pb.expirationTime,
112  pb.masterKey.GetPubKey());
113 
114  std::vector<SignedStake> signedStakes;
115  signedStakes.reserve(2 * pb.stakes.size());
116 
117  while (!pb.stakes.empty()) {
118  auto handle = pb.stakes.extract(pb.stakes.begin());
119  SignedStake signedStake = handle.value();
120  signedStakes.push_back(signedStake);
121  signedStakes.push_back(signedStake);
122  }
123 
124  SchnorrSig proofSignature;
125  BOOST_CHECK(pb.masterKey.SignSchnorr(limitedProofid, proofSignature));
126 
127  return ProofRef::make(pb.sequence, pb.expirationTime, masterPubKey,
128  std::move(signedStakes), pb.payoutScriptPubKey,
129  proofSignature);
130 }
131 
132 } // namespace avalanche
static constexpr Amount COIN
Definition: amount.h:154
RecursiveMutex cs_main
Global state.
Definition: validation.cpp:111
CChainState stores and provides an API to update our local knowledge of the current best chain.
Definition: validation.h:642
CCoinsViewCache & CoinsTip() EXCLUSIVE_LOCKS_REQUIRED(
Definition: validation.h:757
CCoinsView that adds a memory cache for transactions to another CCoinsView.
Definition: coins.h:203
void AddCoin(const COutPoint &outpoint, Coin coin, bool possible_overwrite)
Add a coin.
Definition: coins.cpp:100
A writer stream (for serialization) that computes a 256-bit hash.
Definition: hash.h:100
uint256 GetHash()
Compute the double-SHA256 hash of all data written to this object.
Definition: hash.h:123
An encapsulated secp256k1 private key.
Definition: key.h:28
static CKey MakeCompressedKey()
Produce a valid compressed key.
Definition: key.cpp:466
CPubKey GetPubKey() const
Compute the public key from a private key.
Definition: key.cpp:210
bool SignSchnorr(const uint256 &hash, SchnorrSig &sig, uint32_t test_case=0) const
Create a Schnorr signature.
Definition: key.cpp:288
An outpoint - a combination of a transaction hash and an index n into its vout.
Definition: transaction.h:22
An encapsulated public key.
Definition: pubkey.h:31
Serialized script, used inside transaction inputs and outputs.
Definition: script.h:431
An output of a transaction.
Definition: transaction.h:130
A UTXO entry.
Definition: coins.h:27
static RCUPtr make(Args &&...args)
Construct a new object that is owned by the pointer.
Definition: rcu.h:112
bool addUTXO(COutPoint utxo, Amount amount, uint32_t height, bool is_coinbase, CKey key)
std::set< SignedStake, SignedStakeComparator > stakes
Definition: proofbuilder.h:28
const std::vector< SignedStake > & getStakes() const
Definition: proof.h:165
std::array< uint8_t, CPubKey::SCHNORR_SIZE > SchnorrSig
a Schnorr signature
Definition: key.h:25
static constexpr Amount PROOF_DUST_THRESHOLD
Minimum amount per utxo.
Definition: proof.h:40
const CScript UNSPENDABLE_ECREG_PAYOUT_SCRIPT
Definition: util.h:19
bool hasDustStake(const ProofRef &proof)
Definition: util.cpp:42
ProofRef buildRandomProof(CChainState &active_chainstate, uint32_t score, int height, const CKey &masterKey)
Definition: util.cpp:20
#define BOOST_CHECK(expr)
Definition: object.cpp:17
uint256 GetRandHash() noexcept
Definition: random.cpp:658
@ SER_GETHASH
Definition: serialize.h:168
void WriteCompactSize(CSizeComputer &os, uint64_t nSize)
Definition: serialize.h:1255
CScript GetScriptForDestination(const CTxDestination &dest)
Generate a Bitcoin scriptPubKey for the given CTxDestination.
Definition: standard.cpp:243
Definition: amount.h:19
A TxId is the identifier of a transaction.
Definition: txid.h:14
static ProofRef buildWithReversedOrderStakes(ProofBuilder &pb)
Definition: util.cpp:66
static ProofRef buildDuplicatedStakes(ProofBuilder &pb)
Definition: util.cpp:107
static LimitedProofId getReverseOrderLimitedProofId(ProofBuilder &pb)
Definition: util.cpp:52
static LimitedProofId getDuplicatedStakeLimitedProofId(ProofBuilder &pb)
Definition: util.cpp:92
#define LOCK(cs)
Definition: sync.h:243