Bitcoin ABC 0.30.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 <avalanche/proofid.h>
9#include <consensus/amount.h>
10#include <key.h>
12#include <pubkey.h>
13#include <rcu.h>
14#include <serialize.h>
15#include <util/hasher.h>
16#include <validation.h> // For ChainstateManager and cs_main
17
18#include <array>
19#include <cstdint>
20#include <optional>
21#include <vector>
22
23class ArgsManager;
24struct bilingual_str;
25
29static constexpr int AVALANCHE_MAX_PROOF_STAKES = 1000;
30
36
37namespace avalanche {
38
40static constexpr Amount PROOF_DUST_THRESHOLD = 100 * COIN;
41
43
45
46struct StakeCommitment : public uint256 {
47 StakeCommitment(int64_t expirationTime, const CPubKey &master);
48};
49
50class Stake {
52
54 uint32_t height;
56
58 void computeStakeId();
59
60public:
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
88
89public:
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
102class Proof {
103 uint64_t sequence;
106 std::vector<SignedStake> stakes;
109
112 void computeProofId();
113
114 uint32_t score;
115 void computeScore();
116
118
119public:
121 : sequence(0), expirationTime(0), master(), stakes(),
123
124 Proof(uint64_t sequence_, int64_t expirationTime_, CPubKey master_,
125 std::vector<SignedStake> stakes_, const CScript &payoutScriptPubKey_,
126 SchnorrSig signature_)
127 : sequence(sequence_), expirationTime(expirationTime_),
128 master(std::move(master_)), stakes(std::move(stakes_)),
129 payoutScriptPubKey(payoutScriptPubKey_),
130 signature(std::move(signature_)) {
132 computeScore();
133 }
134 Proof(Proof &&other)
136 master(std::move(other.master)), stakes(std::move(other.stakes)),
138 signature(std::move(other.signature)),
139 limitedProofId(std::move(other.limitedProofId)),
140 proofid(std::move(other.proofid)), score(other.score) {}
141
145 template <typename Stream> Proof(deserialize_type, Stream &s) {
146 Unserialize(s);
147 }
148
150 READWRITE(obj.sequence, obj.expirationTime, obj.master, obj.stakes);
151 READWRITE(obj.payoutScriptPubKey, obj.signature);
152 SER_READ(obj, obj.computeProofId());
153 SER_READ(obj, obj.computeScore());
154 }
155
156 static bool FromHex(Proof &proof, const std::string &hexProof,
157 bilingual_str &errorOut);
158 std::string ToHex() const;
159
160 static uint32_t amountToScore(Amount amount);
161
162 uint64_t getSequence() const { return sequence; }
163 int64_t getExpirationTime() const { return expirationTime; }
164 const CPubKey &getMaster() const { return master; }
165 const std::vector<SignedStake> &getStakes() const { return stakes; }
166 const CScript &getPayoutScript() const { return payoutScriptPubKey; }
167 const SchnorrSig &getSignature() const { return signature; }
168
169 const ProofId &getId() const { return proofid; }
170 const LimitedProofId &getLimitedId() const { return limitedProofId; }
173 };
174 uint32_t getScore() const { return score; }
175 Amount getStakedAmount() const;
176
177 bool verify(const Amount &stakeUtxoDustThreshold,
178 ProofValidationState &state) const;
179 bool verify(const Amount &stakeUtxoDustThreshold,
180 const ChainstateManager &chainman,
181 ProofValidationState &state) const
183};
184
186
188public:
190 size_t operator()(const ProofRef &proof) const {
191 return hash(proof->getId());
192 }
193};
194
195} // namespace avalanche
196
197#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:1219
size_t hash(const uint256 &h) const
Definition: hasher.h:23
int64_t getExpirationTime() const
Definition: proof.h:163
static bool FromHex(Proof &proof, const std::string &hexProof, bilingual_str &errorOut)
Definition: proof.cpp:51
IMPLEMENT_RCU_REFCOUNT(uint64_t)
bool verify(const Amount &stakeUtxoDustThreshold, ProofValidationState &state) const
Definition: proof.cpp:119
int64_t expirationTime
Definition: proof.h:104
void computeProofId()
Definition: proof.cpp:76
Proof(uint64_t sequence_, int64_t expirationTime_, CPubKey master_, std::vector< SignedStake > stakes_, const CScript &payoutScriptPubKey_, SchnorrSig signature_)
Definition: proof.h:124
Amount getStakedAmount() const
Definition: proof.cpp:104
const CPubKey & getMaster() const
Definition: proof.h:164
CScript payoutScriptPubKey
Definition: proof.h:107
std::string ToHex() const
Definition: proof.cpp:70
const StakeCommitment getStakeCommitment() const
Definition: proof.h:171
uint64_t getSequence() const
Definition: proof.h:162
void computeScore()
Definition: proof.cpp:91
const LimitedProofId & getLimitedId() const
Definition: proof.h:170
LimitedProofId limitedProofId
Definition: proof.h:110
uint64_t sequence
Definition: proof.h:103
uint32_t score
Definition: proof.h:114
std::vector< SignedStake > stakes
Definition: proof.h:106
CPubKey master
Definition: proof.h:105
const SchnorrSig & getSignature() const
Definition: proof.h:167
Proof(Proof &&other)
Definition: proof.h:134
Proof(deserialize_type, Stream &s)
Deserialization constructor.
Definition: proof.h:145
const CScript & getPayoutScript() const
Definition: proof.h:166
uint32_t getScore() const
Definition: proof.h:174
const ProofId & getId() const
Definition: proof.h:169
ProofId proofid
Definition: proof.h:111
const std::vector< SignedStake > & getStakes() const
Definition: proof.h:165
SERIALIZE_METHODS(Proof, obj)
Definition: proof.h:149
SchnorrSig signature
Definition: proof.h:108
static uint32_t amountToScore(Amount amount)
Definition: proof.cpp:100
size_t operator()(const ProofRef &proof) const
Definition: proof.h:190
const Stake & getStake() const
Definition: proof.h:96
SchnorrSig sig
Definition: proof.h:87
bool verify(const StakeCommitment &commitment) const
Definition: proof.cpp:47
SignedStake(Stake stake_, SchnorrSig sig_)
Definition: proof.h:91
SERIALIZE_METHODS(SignedStake, obj)
Definition: proof.h:94
const SchnorrSig & getSignature() const
Definition: proof.h:97
Stake(COutPoint utxo_, Amount amount_, uint32_t height_, bool is_coinbase, CPubKey pubkey_)
Definition: proof.h:62
uint256 getHash(const StakeCommitment &commitment) const
Definition: proof.cpp:40
Amount amount
Definition: proof.h:53
Amount getAmount() const
Definition: proof.h:75
bool isCoinbase() const
Definition: proof.h:77
COutPoint utxo
Definition: proof.h:51
uint32_t getHeight() const
Definition: proof.h:76
StakeId stakeid
Definition: proof.h:57
SERIALIZE_METHODS(Stake, obj)
Definition: proof.h:69
CPubKey pubkey
Definition: proof.h:55
void computeStakeId()
Definition: proof.cpp:34
const CPubKey & getPubkey() const
Definition: proof.h:78
const COutPoint & getUTXO() const
Definition: proof.h:74
const StakeId & getId() const
Definition: proof.h:82
uint32_t height
Definition: proof.h:54
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:40
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:35
static constexpr int AVALANCHE_MAX_PROOF_STAKES
How many UTXOs can be used for a single proof.
Definition: proof.h:29
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:25
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