Bitcoin ABC  0.22.12
P2P Digital Currency
proof.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/proof.h>
6 
7 #include <avalanche/validation.h>
8 #include <coins.h>
9 #include <hash.h>
10 #include <script/standard.h>
11 
12 #include <unordered_set>
13 
14 namespace avalanche {
15 
16 uint256 Stake::getHash(const ProofId &proofid) const {
17  CHashWriter ss(SER_GETHASH, 0);
18  ss << proofid;
19  ss << *this;
20  return ss.GetHash();
21 }
22 
23 bool SignedStake::verify(const ProofId &proofid) const {
24  return stake.getPubkey().VerifySchnorr(stake.getHash(proofid), sig);
25 }
26 
28  CHashWriter ss(SER_GETHASH, 0);
29  ss << sequence;
30  ss << expirationTime;
31  ss << master;
32 
33  WriteCompactSize(ss, stakes.size());
34  for (const SignedStake &s : stakes) {
35  ss << s.getStake();
36  }
37 
38  return ProofId(ss.GetHash());
39 }
40 
41 uint32_t Proof::getScore() const {
42  Amount total = Amount::zero();
43  for (const SignedStake &s : stakes) {
44  total += s.getStake().getAmount();
45  }
46 
47  return uint32_t((100 * total) / COIN);
48 }
49 
50 static constexpr Amount PROOF_DUST_THRESOLD = 1 * SATOSHI;
51 
52 bool Proof::verify(ProofValidationState &state) const {
53  if (stakes.empty()) {
55  }
56 
57  std::unordered_set<COutPoint, SaltedOutpointHasher> utxos;
58  for (const SignedStake &ss : stakes) {
59  const Stake &s = ss.getStake();
60  if (s.getAmount() < PROOF_DUST_THRESOLD) {
62  }
63 
64  if (!utxos.insert(s.getUTXO()).second) {
66  }
67 
68  if (!ss.verify(proofid)) {
70  }
71  }
72 
73  return true;
74 }
75 
76 bool Proof::verify(ProofValidationState &state, const CCoinsView &view) const {
77  if (!verify(state)) {
78  // state is set by verify.
79  return false;
80  }
81 
82  for (const SignedStake &ss : stakes) {
83  const Stake &s = ss.getStake();
84  const COutPoint &utxo = s.getUTXO();
85 
86  Coin coin;
87  if (!view.GetCoin(utxo, coin)) {
88  // The coins are not in the UTXO set.
90  }
91 
92  if (s.isCoinbase() != coin.IsCoinBase()) {
94  }
95 
96  if (s.getHeight() != coin.GetHeight()) {
98  }
99 
100  const CTxOut &out = coin.GetTxOut();
101  if (s.getAmount() != out.nValue) {
102  // Wrong amount.
104  }
105 
106  CTxDestination dest;
107  if (!ExtractDestination(out.scriptPubKey, dest)) {
108  // Can't extract destination.
109  return state.Invalid(
111  }
112 
113  PKHash *pkhash = boost::get<PKHash>(&dest);
114  if (!pkhash) {
115  // Only PKHash are supported.
116  return state.Invalid(
118  }
119 
120  const CPubKey &pubkey = s.getPubkey();
121  if (*pkhash != PKHash(pubkey)) {
122  // Wrong pubkey.
124  }
125  }
126 
127  return true;
128 }
129 
130 } // namespace avalanche
CPubKey pubkey
Definition: proof.h:29
bool IsCoinBase() const
Definition: coins.h:45
std::array< uint8_t, 64 > sig
Definition: processor.cpp:248
static constexpr Amount zero()
Definition: amount.h:35
bool ExtractDestination(const CScript &scriptPubKey, CTxDestination &addressRet)
Parse a standard scriptPubKey for the destination address.
Definition: standard.cpp:152
static constexpr Amount PROOF_DUST_THRESOLD
Definition: proof.cpp:50
virtual bool GetCoin(const COutPoint &outpoint, Coin &coin) const
Retrieve the Coin (unspent transaction output) for a given outpoint.
Definition: coins.cpp:12
CScript scriptPubKey
Definition: transaction.h:144
Amount getAmount() const
Definition: proof.h:49
A UTXO entry.
Definition: coins.h:27
void WriteCompactSize(CSizeComputer &os, uint64_t nSize)
Definition: serialize.h:1189
Definition: amount.h:17
uint32_t getScore() const
Definition: proof.cpp:41
static constexpr Amount SATOSHI
Definition: amount.h:151
bool verify(const ProofId &proofid) const
Definition: proof.cpp:23
bool isCoinbase() const
Definition: proof.h:51
uint256 getHash(const ProofId &proofid) const
Definition: proof.cpp:16
ProofId computeProofId() const
Definition: proof.cpp:27
const COutPoint & getUTXO() const
Definition: proof.h:48
bool Invalid(Result result, const std::string &reject_reason="", const std::string &debug_message="")
Definition: validation.h:98
static constexpr Amount COIN
Definition: amount.h:153
Abstract view on the open txout dataset.
Definition: coins.h:148
An encapsulated public key.
Definition: pubkey.h:31
const CPubKey & getPubkey() const
Definition: proof.h:52
An output of a transaction.
Definition: transaction.h:141
bool verify(ProofValidationState &state) const
Definition: proof.cpp:52
An outpoint - a combination of a transaction hash and an index n into its vout.
Definition: transaction.h:22
uint256 GetHash()
Definition: hash.h:139
256-bit opaque blob.
Definition: uint256.h:120
uint32_t GetHeight() const
Definition: coins.h:44
CTxOut & GetTxOut()
Definition: coins.h:48
uint32_t getHeight() const
Definition: proof.h:50
A writer stream (for serialization) that computes a 256-bit hash.
Definition: hash.h:120
COutPoint utxo
Definition: proof.h:25
Amount nValue
Definition: transaction.h:143
boost::variant< CNoDestination, PKHash, ScriptHash > CTxDestination
A txout script template with a specific destination.
Definition: standard.h:87