Bitcoin ABC 0.26.3
P2P Digital Currency
Loading...
Searching...
No Matches
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
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
18namespace avalanche {
19
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 CScript script = GetScriptForDestination(PKHash(key.GetPubKey()));
29 {
31 CCoinsViewCache &coins = active_chainstate.CoinsTip();
32 coins.AddCoin(o, Coin(CTxOut(v, script), height, is_coinbase), false);
33 }
34
35 // Reuse output script as payout script so random proof payouts are unique
36 ProofBuilder pb(0, std::numeric_limits<uint32_t>::max(), masterKey, script);
37 BOOST_CHECK(pb.addUTXO(o, v, height, is_coinbase, std::move(key)));
38 return pb.build();
39}
40
41bool hasDustStake(const ProofRef &proof) {
42 for (const SignedStake &s : proof->getStakes()) {
43 if (s.getStake().getAmount() < PROOF_DUST_THRESHOLD) {
44 return true;
45 }
46 }
47 return false;
48}
49
50LimitedProofId
52 HashWriter ss{};
53 ss << pb.sequence;
54 ss << pb.expirationTime;
55 ss << pb.payoutScriptPubKey;
56
57 WriteCompactSize(ss, pb.stakes.size());
58 for (auto it = pb.stakes.rbegin(); it != pb.stakes.rend(); it++) {
59 ss << it->getStake();
60 }
61
62 return LimitedProofId(ss.GetHash());
63}
64
66 const LimitedProofId limitedProofid =
68 const CPubKey masterPubKey = pb.masterKey.GetPubKey();
69 const StakeCommitment commitment(pb.expirationTime,
70 pb.masterKey.GetPubKey());
71
72 std::vector<SignedStake> signedStakes;
73 signedStakes.reserve(pb.stakes.size());
74
75 while (!pb.stakes.empty()) {
76 // We need a forward iterator, so pb.stakes.rbegin() is not an
77 // option.
78 auto handle = pb.stakes.extract(std::prev(pb.stakes.end()));
79 signedStakes.push_back(handle.value());
80 }
81
83 BOOST_CHECK(pb.masterKey.SignSchnorr(limitedProofid, proofSignature));
84
85 return ProofRef::make(pb.sequence, pb.expirationTime, masterPubKey,
86 std::move(signedStakes), pb.payoutScriptPubKey,
88}
89
92 HashWriter ss{};
93 ss << pb.sequence;
94 ss << pb.expirationTime;
95 ss << pb.payoutScriptPubKey;
96
97 WriteCompactSize(ss, 2 * pb.stakes.size());
98 for (auto &s : pb.stakes) {
99 ss << s.getStake();
100 ss << s.getStake();
101 }
102
103 return LimitedProofId(ss.GetHash());
104}
105
107 const LimitedProofId limitedProofid =
109 const CPubKey masterPubKey = pb.masterKey.GetPubKey();
110 const StakeCommitment commitment(pb.expirationTime,
111 pb.masterKey.GetPubKey());
112
113 std::vector<SignedStake> signedStakes;
114 signedStakes.reserve(2 * pb.stakes.size());
115
116 while (!pb.stakes.empty()) {
117 auto handle = pb.stakes.extract(pb.stakes.begin());
118 SignedStake signedStake = handle.value();
119 signedStakes.push_back(signedStake);
120 signedStakes.push_back(signedStake);
121 }
122
124 BOOST_CHECK(pb.masterKey.SignSchnorr(limitedProofid, proofSignature));
125
126 return ProofRef::make(pb.sequence, pb.expirationTime, masterPubKey,
127 std::move(signedStakes), pb.payoutScriptPubKey,
129}
130
131} // namespace avalanche
static constexpr Amount COIN
Definition amount.h:144
CCoinsView that adds a memory cache for transactions to another CCoinsView.
Definition coins.h:221
void AddCoin(const COutPoint &outpoint, Coin coin, bool possible_overwrite)
Add a coin.
Definition coins.cpp:104
An encapsulated secp256k1 private key.
Definition key.h:28
static CKey MakeCompressedKey()
Produce a valid compressed key.
Definition key.cpp:466
An outpoint - a combination of a transaction hash and an index n into its vout.
Definition transaction.h:20
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.
Chainstate stores and provides an API to update our local knowledge of the current best chain.
Definition validation.h:699
A UTXO entry.
Definition coins.h:28
A writer stream (for serialization) that computes a 256-bit hash.
Definition hash.h:99
static RCUPtr make(Args &&...args)
Construct a new object that is owned by the pointer.
Definition rcu.h:112
const std::vector< SignedStake > & getStakes() const
Definition proof.h:165
RecursiveMutex cs_main
Mutex to guard access to validation specific variables, such as reading or changing the chainstate.
Definition cs_main.cpp:7
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
ProofRef buildRandomProof(Chainstate &active_chainstate, uint32_t score, int height, const CKey &masterKey)
Definition util.cpp:20
bool hasDustStake(const ProofRef &proof)
Definition util.cpp:41
#define BOOST_CHECK(expr)
Definition object.cpp:17
uint256 GetRandHash() noexcept
Definition random.cpp:659
T GetRand(T nMax=std::numeric_limits< T >::max()) noexcept
Generate a uniform random integer of type T in the range [0..nMax) nMax defaults to std::numeric_limi...
Definition random.h:85
void WriteCompactSize(CSizeComputer &os, uint64_t nSize)
Definition serialize.h:1254
CScript GetScriptForDestination(const CTxDestination &dest)
Generate a Bitcoin scriptPubKey for the given CTxDestination.
Definition standard.cpp:240
A TxId is the identifier of a transaction.
Definition txid.h:14
static ProofRef buildWithReversedOrderStakes(ProofBuilder &pb)
Definition util.cpp:65
static ProofRef buildDuplicatedStakes(ProofBuilder &pb)
Definition util.cpp:106
static LimitedProofId getReverseOrderLimitedProofId(ProofBuilder &pb)
Definition util.cpp:51
static LimitedProofId getDuplicatedStakeLimitedProofId(ProofBuilder &pb)
Definition util.cpp:91
#define LOCK(cs)
Definition sync.h:306