Bitcoin ABC  0.24.7
P2P Digital Currency
proof.h
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 #ifndef BITCOIN_AVALANCHE_PROOF_H
6 #define BITCOIN_AVALANCHE_PROOF_H
7 
8 #include <amount.h>
9 #include <avalanche/proofid.h>
10 #include <key.h>
11 #include <primitives/transaction.h>
12 #include <pubkey.h>
13 #include <serialize.h>
14 
15 #include <array>
16 #include <cstdint>
17 #include <optional>
18 #include <vector>
19 
20 class ArgsManager;
21 class CCoinsView;
22 struct bilingual_str;
23 
27 static constexpr int AVALANCHE_MAX_PROOF_STAKES = 1000;
28 
32 static constexpr bool AVALANCHE_DEFAULT_LEGACY_PROOF = true;
33 
34 namespace avalanche {
35 
37 static constexpr Amount PROOF_DUST_THRESHOLD = 1 * COIN;
38 
40 
41 using StakeId = uint256;
42 
43 struct StakeCommitment : public uint256 {
44  explicit StakeCommitment() : uint256() {}
45  explicit StakeCommitment(const uint256 &b) : uint256(b) {}
46  StakeCommitment(const ProofId &proofid, int64_t expirationTime,
47  const CPubKey &master);
48 };
49 
50 class Stake {
52 
54  uint32_t height;
56 
58  void computeStakeId();
59 
60 public:
61  explicit Stake() = default;
62  Stake(COutPoint utxo_, Amount amount_, uint32_t height_, bool is_coinbase,
63  CPubKey pubkey_)
64  : utxo(utxo_), amount(amount_), height(height_ << 1 | is_coinbase),
65  pubkey(std::move(pubkey_)) {
67  }
68 
70  READWRITE(obj.utxo, obj.amount, obj.height, obj.pubkey);
71  SER_READ(obj, obj.computeStakeId());
72  }
73 
74  const COutPoint &getUTXO() const { return utxo; }
75  Amount getAmount() const { return amount; }
76  uint32_t getHeight() const { return height >> 1; }
77  bool isCoinbase() const { return height & 1; }
78  const CPubKey &getPubkey() const { return pubkey; }
79 
80  uint256 getHash(const StakeCommitment &commitment) const;
81 
82  const StakeId &getId() const { return stakeid; }
83 };
84 
85 class SignedStake {
88 
89 public:
90  explicit SignedStake() = default;
92  : stake(std::move(stake_)), sig(std::move(sig_)) {}
93 
94  SERIALIZE_METHODS(SignedStake, obj) { READWRITE(obj.stake, obj.sig); }
95 
96  const Stake &getStake() const { return stake; }
97  const SchnorrSig &getSignature() const { return sig; }
98 
99  bool verify(const StakeCommitment &commitment) const;
100 };
101 
102 class Proof {
103  uint64_t sequence;
104  int64_t expirationTime;
106  std::vector<SignedStake> stakes;
109 
112  void computeProofId();
113 
114 public:
116  : sequence(0), expirationTime(0), master(), stakes(),
118 
119  Proof(uint64_t sequence_, int64_t expirationTime_, CPubKey master_,
120  std::vector<SignedStake> stakes_, const CScript &payoutScriptPubKey_,
121  SchnorrSig signature_)
122  : sequence(sequence_), expirationTime(expirationTime_),
123  master(std::move(master_)), stakes(std::move(stakes_)),
124  payoutScriptPubKey(payoutScriptPubKey_),
125  signature(std::move(signature_)) {
126  computeProofId();
127  }
128 
130  READWRITE(obj.sequence, obj.expirationTime, obj.master, obj.stakes);
131  if (!useLegacy()) {
132  READWRITE(obj.payoutScriptPubKey, obj.signature);
133  }
134  SER_READ(obj, obj.computeProofId());
135  }
136 
137  static bool useLegacy();
138  static bool useLegacy(const ArgsManager &argsman);
139 
140  static bool FromHex(Proof &proof, const std::string &hexProof,
141  bilingual_str &errorOut);
142 
143  uint64_t getSequence() const { return sequence; }
144  int64_t getExpirationTime() const { return expirationTime; }
145  const CPubKey &getMaster() const { return master; }
146  const std::vector<SignedStake> &getStakes() const { return stakes; }
147  const CScript &getPayoutScript() const { return payoutScriptPubKey; }
148  std::optional<const SchnorrSig> getSignature() const {
149  return useLegacy() ? std::nullopt : std::make_optional(signature);
150  }
151 
152  const ProofId &getId() const { return proofid; }
153  const LimitedProofId &getLimitedId() const { return limitedProofId; }
156  };
157  uint32_t getScore() const;
158 
159  bool verify(ProofValidationState &state) const;
160  bool verify(ProofValidationState &state, const CCoinsView &view) const;
161 };
162 
163 using ProofRef = std::shared_ptr<const Proof>;
164 
165 } // namespace avalanche
166 
167 #endif // BITCOIN_AVALANCHE_PROOF_H
avalanche::SignedStake::sig
SchnorrSig sig
Definition: proof.h:87
avalanche::Stake::stakeid
StakeId stakeid
Definition: proof.h:57
avalanche::PROOF_DUST_THRESHOLD
static constexpr Amount PROOF_DUST_THRESHOLD
Minimum amount per utxo.
Definition: proof.h:37
avalanche::Stake::amount
Amount amount
Definition: proof.h:53
avalanche::Proof::getSignature
std::optional< const SchnorrSig > getSignature() const
Definition: proof.h:148
avalanche::Proof::computeProofId
void computeProofId()
Definition: proof.cpp:81
avalanche::Proof::sequence
uint64_t sequence
Definition: proof.h:103
avalanche::Proof::Proof
Proof(uint64_t sequence_, int64_t expirationTime_, CPubKey master_, std::vector< SignedStake > stakes_, const CScript &payoutScriptPubKey_, SchnorrSig signature_)
Definition: proof.h:119
proofid.h
avalanche::Stake::getHash
uint256 getHash(const StakeCommitment &commitment) const
Definition: proof.cpp:42
avalanche
Definition: avalanche.h:11
avalanche::Stake::height
uint32_t height
Definition: proof.h:54
transaction.h
avalanche::SignedStake::stake
Stake stake
Definition: proof.h:86
bilingual_str
Bilingual messages:
Definition: translation.h:17
avalanche::Stake::isCoinbase
bool isCoinbase() const
Definition: proof.h:77
avalanche::Proof::getId
const ProofId & getId() const
Definition: proof.h:152
avalanche::Proof::stakes
std::vector< SignedStake > stakes
Definition: proof.h:106
avalanche::SignedStake
Definition: proof.h:85
avalanche::Stake::Stake
Stake(COutPoint utxo_, Amount amount_, uint32_t height_, bool is_coinbase, CPubKey pubkey_)
Definition: proof.h:62
avalanche::ProofId
Definition: proofid.h:17
AVALANCHE_DEFAULT_LEGACY_PROOF
static constexpr bool AVALANCHE_DEFAULT_LEGACY_PROOF
Whether the legacy proof format should be used by default.
Definition: proof.h:32
pubkey.h
COIN
static constexpr Amount COIN
Definition: amount.h:154
avalanche::Proof::payoutScriptPubKey
CScript payoutScriptPubKey
Definition: proof.h:107
avalanche::Stake::getUTXO
const COutPoint & getUTXO() const
Definition: proof.h:74
CCoinsView
Abstract view on the open txout dataset.
Definition: coins.h:175
avalanche::Stake
Definition: proof.h:50
avalanche::Proof::expirationTime
int64_t expirationTime
Definition: proof.h:104
avalanche::Proof::Proof
Proof()
Definition: proof.h:115
avalanche::StakeCommitment
Definition: proof.h:43
avalanche::Stake::getPubkey
const CPubKey & getPubkey() const
Definition: proof.h:78
avalanche::Stake::getAmount
Amount getAmount() const
Definition: proof.h:75
avalanche::ProofValidationState
Definition: validation.h:33
avalanche::Proof::proofid
ProofId proofid
Definition: proof.h:111
avalanche::Stake::getHeight
uint32_t getHeight() const
Definition: proof.h:76
avalanche::Proof::getSequence
uint64_t getSequence() const
Definition: proof.h:143
avalanche::SignedStake::getStake
const Stake & getStake() const
Definition: proof.h:96
avalanche::Stake::Stake
Stake()=default
SchnorrSig
std::array< uint8_t, CPubKey::SCHNORR_SIZE > SchnorrSig
a Schnorr signature
Definition: key.h:25
avalanche::SignedStake::verify
bool verify(const StakeCommitment &commitment) const
Definition: proof.cpp:49
avalanche::Proof::getScore
uint32_t getScore() const
Definition: proof.cpp:98
AVALANCHE_MAX_PROOF_STAKES
static constexpr int AVALANCHE_MAX_PROOF_STAKES
How many UTXOs can be used for a single proof.
Definition: proof.h:27
avalanche::Stake::SERIALIZE_METHODS
SERIALIZE_METHODS(Stake, obj)
Definition: proof.h:69
avalanche::Stake::computeStakeId
void computeStakeId()
Definition: proof.cpp:36
avalanche::Proof::useLegacy
static bool useLegacy()
Definition: proof.cpp:53
avalanche::SignedStake::SignedStake
SignedStake()=default
avalanche::StakeCommitment::StakeCommitment
StakeCommitment()
Definition: proof.h:44
uint256
256-bit opaque blob.
Definition: uint256.h:127
READWRITE
#define READWRITE(...)
Definition: serialize.h:179
avalanche::LimitedProofId
Definition: proofid.h:28
Amount
Definition: amount.h:19
avalanche::Proof::master
CPubKey master
Definition: proof.h:105
CScript
Serialized script, used inside transaction inputs and outputs.
Definition: script.h:430
avalanche::Stake::utxo
COutPoint utxo
Definition: proof.h:51
avalanche::Proof::getStakes
const std::vector< SignedStake > & getStakes() const
Definition: proof.h:146
avalanche::SignedStake::getSignature
const SchnorrSig & getSignature() const
Definition: proof.h:97
avalanche::Proof
Definition: proof.h:102
avalanche::Proof::FromHex
static bool FromHex(Proof &proof, const std::string &hexProof, bilingual_str &errorOut)
Definition: proof.cpp:62
key.h
avalanche::StakeCommitment::StakeCommitment
StakeCommitment(const uint256 &b)
Definition: proof.h:45
CPubKey
An encapsulated public key.
Definition: pubkey.h:31
ArgsManager
Definition: system.h:152
avalanche::Proof::signature
SchnorrSig signature
Definition: proof.h:108
avalanche::Proof::getPayoutScript
const CScript & getPayoutScript() const
Definition: proof.h:147
avalanche::Proof::getMaster
const CPubKey & getMaster() const
Definition: proof.h:145
avalanche::Proof::SERIALIZE_METHODS
SERIALIZE_METHODS(Proof, obj)
Definition: proof.h:129
avalanche::Stake::getId
const StakeId & getId() const
Definition: proof.h:82
avalanche::Proof::getStakeCommitment
const StakeCommitment getStakeCommitment() const
Definition: proof.h:154
avalanche::Stake::pubkey
CPubKey pubkey
Definition: proof.h:55
avalanche::SignedStake::SERIALIZE_METHODS
SERIALIZE_METHODS(SignedStake, obj)
Definition: proof.h:94
serialize.h
avalanche::Proof::getExpirationTime
int64_t getExpirationTime() const
Definition: proof.h:144
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
SER_READ
#define SER_READ(obj, code)
Definition: serialize.h:182
avalanche::Proof::verify
bool verify(ProofValidationState &state) const
Definition: proof.cpp:107
amount.h
avalanche::Proof::getLimitedId
const LimitedProofId & getLimitedId() const
Definition: proof.h:153
avalanche::SignedStake::SignedStake
SignedStake(Stake stake_, SchnorrSig sig_)
Definition: proof.h:91
avalanche::Proof::limitedProofId
LimitedProofId limitedProofId
Definition: proof.h:110