Bitcoin ABC  0.24.7
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 
5 #include <amount.h>
7 #include <avalanche/test/util.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(uint32_t score, const CKey &masterKey) {
21  auto key = CKey::MakeCompressedKey();
22 
23  const COutPoint o(TxId(GetRandHash()), 0);
24  const Amount v = (int64_t(score) * COIN) / 100;
25  const int height = 1234;
26  const bool is_coinbase = false;
27 
28  {
29  CScript script = GetScriptForDestination(PKHash(key.GetPubKey()));
30 
31  LOCK(cs_main);
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);
37  BOOST_CHECK(pb.addUTXO(o, v, height, is_coinbase, std::move(key)));
38  return pb.build();
39 }
40 
41 bool 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 
51  CHashWriter ss(SER_GETHASH, 0);
52  ss << pb.sequence;
53  ss << pb.expirationTime;
54 
55  WriteCompactSize(ss, pb.stakes.size());
56  for (auto it = pb.stakes.rbegin(); it != pb.stakes.rend(); it++) {
57  ss << it->stake;
58  }
59 
60  CHashWriter ss2(SER_GETHASH, 0);
61  ss2 << ss.GetHash();
62  ss2 << pb.masterKey.GetPubKey();
63 
64  return ProofId(ss2.GetHash());
65 }
66 
69  const StakeCommitment commitment(proofid);
70 
71  std::vector<SignedStake> signedStakes;
72  signedStakes.reserve(pb.stakes.size());
73 
74  while (!pb.stakes.empty()) {
75  // We need a forward iterator, so pb.stakes.rbegin() is not an
76  // option.
77  auto handle = pb.stakes.extract(std::prev(pb.stakes.end()));
78  signedStakes.push_back(handle.value().sign(commitment));
79  }
80 
81  return std::make_shared<Proof>(
83  std::move(signedStakes), pb.payoutScriptPubKey, SchnorrSig());
84 }
85 
87  CHashWriter ss(SER_GETHASH, 0);
88  ss << pb.sequence;
89  ss << pb.expirationTime;
90 
91  WriteCompactSize(ss, 2 * pb.stakes.size());
92  for (auto &s : pb.stakes) {
93  ss << s.stake;
94  ss << s.stake;
95  }
96 
97  CHashWriter ss2(SER_GETHASH, 0);
98  ss2 << ss.GetHash();
99  ss2 << pb.masterKey.GetPubKey();
100 
101  return ProofId(ss2.GetHash());
102 }
103 
106  const StakeCommitment commitment(proofid);
107 
108  std::vector<SignedStake> signedStakes;
109  signedStakes.reserve(2 * pb.stakes.size());
110 
111  while (!pb.stakes.empty()) {
112  auto handle = pb.stakes.extract(pb.stakes.begin());
113  SignedStake signedStake = handle.value().sign(commitment);
114  signedStakes.push_back(signedStake);
115  signedStakes.push_back(signedStake);
116  }
117 
118  return std::make_shared<Proof>(
120  std::move(signedStakes), pb.payoutScriptPubKey, SchnorrSig());
121 }
122 
123 } // namespace avalanche
avalanche::ProofBuilder
Definition: proofbuilder.h:17
avalanche::PROOF_DUST_THRESHOLD
static constexpr Amount PROOF_DUST_THRESHOLD
Minimum amount per utxo.
Definition: proof.h:37
avalanche
Definition: avalanche.h:11
transaction.h
CCoinsViewCache::AddCoin
void AddCoin(const COutPoint &outpoint, Coin coin, bool possible_overwrite)
Add a coin.
Definition: coins.cpp:103
GetScriptForDestination
CScript GetScriptForDestination(const CTxDestination &dest)
Generate a Bitcoin scriptPubKey for the given CTxDestination.
Definition: standard.cpp:243
avalanche::ProofBuilder::expirationTime
int64_t expirationTime
Definition: proofbuilder.h:19
avalanche::SignedStake
Definition: proof.h:85
avalanche::hasDustStake
bool hasDustStake(const ProofRef &proof)
Definition: util.cpp:41
avalanche::ProofId
Definition: proofid.h:17
avalanche::TestProofBuilder::buildDuplicatedStakes
static ProofRef buildDuplicatedStakes(ProofBuilder &pb)
Definition: util.cpp:104
ChainstateActive
CChainState & ChainstateActive()
Please prefer the identical ChainstateManager::ActiveChainstate.
Definition: validation.cpp:80
avalanche::buildRandomProof
ProofRef buildRandomProof(uint32_t score, const CKey &masterKey)
Definition: util.cpp:20
COIN
static constexpr Amount COIN
Definition: amount.h:154
avalanche::ProofBuilder::addUTXO
bool addUTXO(COutPoint utxo, Amount amount, uint32_t height, bool is_coinbase, CKey key)
Definition: proofbuilder.cpp:23
proofbuilder.h
avalanche::ProofBuilder::build
ProofRef build()
Definition: proofbuilder.cpp:36
avalanche::TestProofBuilder::buildWithReversedOrderStakes
static ProofRef buildWithReversedOrderStakes(ProofBuilder &pb)
Definition: util.cpp:67
random.h
avalanche::StakeCommitment
Definition: proof.h:43
avalanche::TestProofBuilder::getReverseOrderProofId
static ProofId getReverseOrderProofId(ProofBuilder &pb)
Definition: util.cpp:50
CTxOut
An output of a transaction.
Definition: transaction.h:130
cs_main
RecursiveMutex cs_main
Global state.
Definition: validation.cpp:103
Coin
A UTXO entry.
Definition: coins.h:27
WriteCompactSize
void WriteCompactSize(CSizeComputer &os, uint64_t nSize)
Definition: serialize.h:1178
GetRandHash
uint256 GetRandHash() noexcept
Definition: random.cpp:658
SchnorrSig
std::array< uint8_t, CPubKey::SCHNORR_SIZE > SchnorrSig
a Schnorr signature
Definition: key.h:25
standard.h
avalanche::ProofBuilder::payoutScriptPubKey
CScript payoutScriptPubKey
Definition: proofbuilder.h:21
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
CScript
Serialized script, used inside transaction inputs and outputs.
Definition: script.h:430
util.h
avalanche::TestProofBuilder::getDuplicatedStakeProofId
static ProofId getDuplicatedStakeProofId(ProofBuilder &pb)
Definition: util.cpp:86
PKHash
Definition: standard.h:106
SER_GETHASH
@ SER_GETHASH
Definition: serialize.h:167
key.h
CKey
An encapsulated secp256k1 private key.
Definition: key.h:28
CCoinsViewCache
CCoinsView that adds a memory cache for transactions to another CCoinsView.
Definition: coins.h:231
LOCK
#define LOCK(cs)
Definition: sync.h:241
CKey::MakeCompressedKey
static CKey MakeCompressedKey()
Produce a valid compressed key.
Definition: key.cpp:466
CHashWriter
A writer stream (for serialization) that computes a 256-bit hash.
Definition: hash.h:99
COutPoint
An outpoint - a combination of a transaction hash and an index n into its vout.
Definition: transaction.h:22
avalanche::ProofRef
std::shared_ptr< const Proof > ProofRef
Definition: proof.h:163
CHashWriter::GetHash
uint256 GetHash()
Definition: hash.h:118
amount.h
avalanche::ProofBuilder::stakes
std::set< StakeSigner, StakeSignerComparator > stakes
Definition: proofbuilder.h:38
avalanche::ProofBuilder::sequence
uint64_t sequence
Definition: proofbuilder.h:18
CChainState::CoinsTip
CCoinsViewCache & CoinsTip() EXCLUSIVE_LOCKS_REQUIRED(cs_main)
Definition: validation.h:837
BOOST_CHECK
#define BOOST_CHECK(expr)
Definition: object.cpp:17
avalanche::ProofBuilder::masterKey
CKey masterKey
Definition: proofbuilder.h:20