Bitcoin ABC 0.31.0
P2P Digital Currency
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Modules Pages
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 <avalanche/proofid.h>
9#include <consensus/amount.h>
10#include <kernel/cs_main.h>
11#include <key.h>
13#include <pubkey.h>
14#include <rcu.h>
15#include <serialize.h>
16#include <util/hasher.h>
17
18#include <array>
19#include <cstdint>
20#include <optional>
21#include <vector>
22
23class ArgsManager;
25struct bilingual_str;
26
30static constexpr int AVALANCHE_MAX_PROOF_STAKES = 1000;
31
37
38namespace avalanche {
39
41static constexpr Amount PROOF_DUST_THRESHOLD = 100 * COIN;
42
44
46
47struct StakeCommitment : public uint256 {
48 StakeCommitment(int64_t expirationTime, const CPubKey &master);
49};
50
51class Stake {
53
55 uint32_t height;
57
59 void computeStakeId();
60
61public:
62 explicit Stake() = default;
63 Stake(COutPoint utxo_, Amount amount_, uint32_t height_, bool is_coinbase,
64 CPubKey pubkey_)
65 : utxo(utxo_), amount(amount_), height(height_ << 1 | is_coinbase),
66 pubkey(std::move(pubkey_)) {
68 }
69
71 READWRITE(obj.utxo, obj.amount, obj.height, obj.pubkey);
72 SER_READ(obj, obj.computeStakeId());
73 }
74
75 const COutPoint &getUTXO() const { return utxo; }
76 Amount getAmount() const { return amount; }
77 uint32_t getHeight() const { return height >> 1; }
78 bool isCoinbase() const { return height & 1; }
79 const CPubKey &getPubkey() const { return pubkey; }
80
81 uint256 getHash(const StakeCommitment &commitment) const;
82
83 const StakeId &getId() const { return stakeid; }
84};
85
89
90public:
91 explicit SignedStake() = default;
93 : stake(std::move(stake_)), sig(std::move(sig_)) {}
94
95 SERIALIZE_METHODS(SignedStake, obj) { READWRITE(obj.stake, obj.sig); }
96
97 const Stake &getStake() const { return stake; }
98 const SchnorrSig &getSignature() const { return sig; }
99
100 bool verify(const StakeCommitment &commitment) const;
101};
102
103class Proof {
104 uint64_t sequence;
107 std::vector<SignedStake> stakes;
110
113 void computeProofId();
114
115 uint32_t score;
116 void computeScore();
117
119
120public:
122 : sequence(0), expirationTime(0), master(), stakes(),
124
125 Proof(uint64_t sequence_, int64_t expirationTime_, CPubKey master_,
126 std::vector<SignedStake> stakes_, const CScript &payoutScriptPubKey_,
127 SchnorrSig signature_)
128 : sequence(sequence_), expirationTime(expirationTime_),
129 master(std::move(master_)), stakes(std::move(stakes_)),
130 payoutScriptPubKey(payoutScriptPubKey_),
131 signature(std::move(signature_)) {
133 computeScore();
134 }
135 Proof(Proof &&other)
137 master(std::move(other.master)), stakes(std::move(other.stakes)),
139 signature(std::move(other.signature)),
140 limitedProofId(std::move(other.limitedProofId)),
141 proofid(std::move(other.proofid)), score(other.score) {}
142
146 template <typename Stream> Proof(deserialize_type, Stream &s) {
147 Unserialize(s);
148 }
149
151 READWRITE(obj.sequence, obj.expirationTime, obj.master, obj.stakes);
152 READWRITE(obj.payoutScriptPubKey, obj.signature);
153 SER_READ(obj, obj.computeProofId());
154 SER_READ(obj, obj.computeScore());
155 }
156
157 static bool FromHex(Proof &proof, const std::string &hexProof,
158 bilingual_str &errorOut);
159 std::string ToHex() const;
160
161 static uint32_t amountToScore(Amount amount);
162
163 uint64_t getSequence() const { return sequence; }
164 int64_t getExpirationTime() const { return expirationTime; }
165 const CPubKey &getMaster() const { return master; }
166 const std::vector<SignedStake> &getStakes() const { return stakes; }
167 const CScript &getPayoutScript() const { return payoutScriptPubKey; }
168 const SchnorrSig &getSignature() const { return signature; }
169
170 const ProofId &getId() const { return proofid; }
171 const LimitedProofId &getLimitedId() const { return limitedProofId; }
174 };
175 uint32_t getScore() const { return score; }
176 Amount getStakedAmount() const;
177
178 bool verify(const Amount &stakeUtxoDustThreshold,
179 ProofValidationState &state) const;
180 bool verify(const Amount &stakeUtxoDustThreshold,
181 const ChainstateManager &chainman,
182 ProofValidationState &state) const
184};
185
187
189public:
191 size_t operator()(const ProofRef &proof) const {
192 return hash(proof->getId());
193 }
194};
195
196} // namespace avalanche
197
198#endif // BITCOIN_AVALANCHE_PROOF_H
static constexpr Amount COIN
Definition: amount.h:144
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:424
Provides an interface for creating and interacting with one or two chainstates: an IBD chainstate gen...
Definition: validation.h:1147
size_t hash(const uint256 &h) const
Definition: hasher.h:23
int64_t getExpirationTime() const
Definition: proof.h:164
static bool FromHex(Proof &proof, const std::string &hexProof, bilingual_str &errorOut)
Definition: proof.cpp:52
IMPLEMENT_RCU_REFCOUNT(uint64_t)
bool verify(const Amount &stakeUtxoDustThreshold, ProofValidationState &state) const
Definition: proof.cpp:120
int64_t expirationTime
Definition: proof.h:105
void computeProofId()
Definition: proof.cpp:77
Proof(uint64_t sequence_, int64_t expirationTime_, CPubKey master_, std::vector< SignedStake > stakes_, const CScript &payoutScriptPubKey_, SchnorrSig signature_)
Definition: proof.h:125
Amount getStakedAmount() const
Definition: proof.cpp:105
const CPubKey & getMaster() const
Definition: proof.h:165
CScript payoutScriptPubKey
Definition: proof.h:108
std::string ToHex() const
Definition: proof.cpp:71
const StakeCommitment getStakeCommitment() const
Definition: proof.h:172
uint64_t getSequence() const
Definition: proof.h:163
void computeScore()
Definition: proof.cpp:92
const LimitedProofId & getLimitedId() const
Definition: proof.h:171
LimitedProofId limitedProofId
Definition: proof.h:111
uint64_t sequence
Definition: proof.h:104
uint32_t score
Definition: proof.h:115
std::vector< SignedStake > stakes
Definition: proof.h:107
CPubKey master
Definition: proof.h:106
const SchnorrSig & getSignature() const
Definition: proof.h:168
Proof(Proof &&other)
Definition: proof.h:135
Proof(deserialize_type, Stream &s)
Deserialization constructor.
Definition: proof.h:146
const CScript & getPayoutScript() const
Definition: proof.h:167
uint32_t getScore() const
Definition: proof.h:175
const ProofId & getId() const
Definition: proof.h:170
ProofId proofid
Definition: proof.h:112
const std::vector< SignedStake > & getStakes() const
Definition: proof.h:166
SERIALIZE_METHODS(Proof, obj)
Definition: proof.h:150
SchnorrSig signature
Definition: proof.h:109
static uint32_t amountToScore(Amount amount)
Definition: proof.cpp:101
size_t operator()(const ProofRef &proof) const
Definition: proof.h:191
const Stake & getStake() const
Definition: proof.h:97
SchnorrSig sig
Definition: proof.h:88
bool verify(const StakeCommitment &commitment) const
Definition: proof.cpp:48
SignedStake(Stake stake_, SchnorrSig sig_)
Definition: proof.h:92
SERIALIZE_METHODS(SignedStake, obj)
Definition: proof.h:95
const SchnorrSig & getSignature() const
Definition: proof.h:98
Stake(COutPoint utxo_, Amount amount_, uint32_t height_, bool is_coinbase, CPubKey pubkey_)
Definition: proof.h:63
uint256 getHash(const StakeCommitment &commitment) const
Definition: proof.cpp:41
Amount amount
Definition: proof.h:54
Amount getAmount() const
Definition: proof.h:76
bool isCoinbase() const
Definition: proof.h:78
COutPoint utxo
Definition: proof.h:52
uint32_t getHeight() const
Definition: proof.h:77
StakeId stakeid
Definition: proof.h:58
SERIALIZE_METHODS(Stake, obj)
Definition: proof.h:70
CPubKey pubkey
Definition: proof.h:56
void computeStakeId()
Definition: proof.cpp:35
const CPubKey & getPubkey() const
Definition: proof.h:79
const COutPoint & getUTXO() const
Definition: proof.h:75
const StakeId & getId() const
Definition: proof.h:83
uint32_t height
Definition: proof.h:55
256-bit opaque blob.
Definition: uint256.h:129
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:41
Implement std::hash so RCUPtr can be used as a key for maps or sets.
Definition: rcu.h:259
static constexpr int AVALANCHE_DEFAULT_STAKE_UTXO_CONFIRMATIONS
Minimum number of confirmations before a stake utxo is mature enough to be included into a proof.
Definition: proof.h:36
static constexpr int AVALANCHE_MAX_PROOF_STAKES
How many UTXOs can be used for a single proof.
Definition: proof.h:30
void Unserialize(Stream &, char)=delete
#define SER_READ(obj, code)
Definition: serialize.h:169
#define READWRITE(...)
Definition: serialize.h:166
Definition: amount.h:19
StakeCommitment(int64_t expirationTime, const CPubKey &master)
Definition: proof.cpp:26
Bilingual messages:
Definition: translation.h:17
Dummy data type to identify deserializing constructors.
Definition: serialize.h:49
#define EXCLUSIVE_LOCKS_REQUIRED(...)
Definition: threadsafety.h:56