Bitcoin ABC  0.22.13
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 <primitives/transaction.h>
11 #include <pubkey.h>
12 #include <serialize.h>
13 
14 #include <array>
15 #include <cstdint>
16 #include <vector>
17 
18 class CCoinsView;
19 
20 namespace avalanche {
21 
22 class ProofValidationState;
23 
24 class Stake {
26 
28  uint32_t height;
30 
31 public:
32  explicit Stake() = default;
33  Stake(COutPoint utxo_, Amount amount_, uint32_t height_, bool is_coinbase,
34  CPubKey pubkey_)
35  : utxo(utxo_), amount(amount_), height(height_ << 1 | is_coinbase),
36  pubkey(std::move(pubkey_)) {}
37 
39 
40  template <typename Stream, typename Operation>
41  inline void SerializationOp(Stream &s, Operation ser_action) {
42  READWRITE(utxo);
43  READWRITE(amount);
44  READWRITE(height);
45  READWRITE(pubkey);
46  }
47 
48  const COutPoint &getUTXO() const { return utxo; }
49  Amount getAmount() const { return amount; }
50  uint32_t getHeight() const { return height >> 1; }
51  bool isCoinbase() const { return height & 1; }
52  const CPubKey &getPubkey() const { return pubkey; }
53 
54  uint256 getHash(const ProofId &proofid) const;
55 };
56 
57 class SignedStake {
59  std::array<uint8_t, 64> sig;
60 
61 public:
62  explicit SignedStake() = default;
63  SignedStake(Stake stake_, std::array<uint8_t, 64> sig_)
64  : stake(std::move(stake_)), sig(std::move(sig_)) {}
65 
67 
68  template <typename Stream, typename Operation>
69  inline void SerializationOp(Stream &s, Operation ser_action) {
70  READWRITE(stake);
71  READWRITE(sig);
72  }
73 
74  const Stake &getStake() const { return stake; }
75  const std::array<uint8_t, 64> &getSignature() const { return sig; }
76 
77  bool verify(const ProofId &proofid) const;
78 };
79 
80 class Proof {
81  uint64_t sequence;
82  int64_t expirationTime;
84  std::vector<SignedStake> stakes;
85 
87  ProofId computeProofId() const;
88 
89 public:
90  Proof() : sequence(0), expirationTime(0), master(), stakes(), proofid() {}
91  Proof(uint64_t sequence_, int64_t expirationTime_, CPubKey master_,
92  std::vector<SignedStake> stakes_)
93  : sequence(sequence_), expirationTime(expirationTime_),
94  master(std::move(master_)), stakes(std::move(stakes_)),
95  proofid(computeProofId()) {}
96 
98 
99  template <typename Stream, typename Operation>
100  inline void SerializationOp(Stream &s, Operation ser_action) {
101  READWRITE(sequence);
102  READWRITE(expirationTime);
103  READWRITE(master);
104  READWRITE(stakes);
105 
106  if (ser_action.ForRead()) {
107  proofid = computeProofId();
108  }
109  }
110 
111  uint64_t getSequence() const { return sequence; }
112  int64_t getExpirationTime() const { return expirationTime; }
113  const CPubKey &getMaster() const { return master; }
114  const std::vector<SignedStake> &getStakes() const { return stakes; }
115 
116  const ProofId &getId() const { return proofid; }
117  uint32_t getScore() const;
118 
119  bool verify(ProofValidationState &state) const;
120  bool verify(ProofValidationState &state, const CCoinsView &view) const;
121 };
122 
123 } // namespace avalanche
124 
125 #endif // BITCOIN_AVALANCHE_PROOF_H
CPubKey pubkey
Definition: proof.h:29
void SerializationOp(Stream &s, Operation ser_action)
Definition: proof.h:100
std::array< uint8_t, 64 > sig
Definition: processor.cpp:250
CPubKey master
Definition: proof.h:83
const std::vector< SignedStake > & getStakes() const
Definition: proof.h:114
const CPubKey & getMaster() const
Definition: proof.h:113
Amount getAmount() const
Definition: proof.h:49
int64_t getExpirationTime() const
Definition: proof.h:112
Definition: amount.h:17
bool isCoinbase() const
Definition: proof.h:51
ADD_SERIALIZE_METHODS
Definition: proof.h:97
uint256 getHash(const ProofId &proofid) const
Definition: proof.cpp:16
Amount amount
Definition: proof.h:27
const COutPoint & getUTXO() const
Definition: proof.h:48
Abstract view on the open txout dataset.
Definition: coins.h:175
std::vector< SignedStake > stakes
Definition: proof.h:84
An encapsulated public key.
Definition: pubkey.h:31
Stake(COutPoint utxo_, Amount amount_, uint32_t height_, bool is_coinbase, CPubKey pubkey_)
Definition: proof.h:33
const CPubKey & getPubkey() const
Definition: proof.h:52
SignedStake(Stake stake_, std::array< uint8_t, 64 > sig_)
Definition: proof.h:63
const Stake & getStake() const
Definition: proof.h:74
An outpoint - a combination of a transaction hash and an index n into its vout.
Definition: transaction.h:22
uint64_t sequence
Definition: proof.h:81
Proof(uint64_t sequence_, int64_t expirationTime_, CPubKey master_, std::vector< SignedStake > stakes_)
Definition: proof.h:91
void SerializationOp(Stream &s, Operation ser_action)
Definition: proof.h:41
const ProofId & getId() const
Definition: proof.h:116
256-bit opaque blob.
Definition: uint256.h:120
ProofId proofid
Definition: proof.h:86
uint32_t getHeight() const
Definition: proof.h:50
COutPoint utxo
Definition: proof.h:25
uint32_t height
Definition: proof.h:28
int64_t expirationTime
Definition: proof.h:82
ADD_SERIALIZE_METHODS
Definition: proof.h:38
#define READWRITE(...)
Definition: serialize.h:191
std::array< uint8_t, 64 > sig
Definition: proof.h:59
void SerializationOp(Stream &s, Operation ser_action)
Definition: proof.h:69
uint64_t getSequence() const
Definition: proof.h:111
const std::array< uint8_t, 64 > & getSignature() const
Definition: proof.h:75