PRCYCoin  2.0.0.7rc1
P2P Digital Currency
block.h
Go to the documentation of this file.
1 // Copyright (c) 2009-2010 Satoshi Nakamoto
2 // Copyright (c) 2009-2013 The Bitcoin developers
3 // Copyright (c) 2015-2018 The PIVX developers
4 // Copyright (c) 2018-2020 The DAPS Project developers
5 // Distributed under the MIT/X11 software license, see the accompanying
6 // file COPYING or http://www.opensource.org/licenses/mit-license.php.
7 
8 #ifndef BITCOIN_PRIMITIVES_BLOCK_H
9 #define BITCOIN_PRIMITIVES_BLOCK_H
10 
11 #include "primitives/transaction.h"
12 #include "keystore.h"
13 #include "serialize.h"
14 #include "uint256.h"
15 
16 
18 public:
20  uint32_t nTime;
21  uint32_t height;
22 
24 
25  template <typename Stream, typename Operation>
26  inline void SerializationOp(Stream& s, Operation ser_action, int nType, int nVersion) {
27  READWRITE(this->hash);
28  READWRITE(this->nTime);
29  READWRITE(this->height);
30  }
31 
32  friend bool operator==(const PoSBlockSummary& a, const PoSBlockSummary& b)
33  {
34  return a.hash == b.hash && a.nTime == b.nTime && a.height == b.height;
35  }
36 
37  friend bool operator!=(const PoSBlockSummary& a, const PoSBlockSummary& b)
38  {
39  return (a.hash != b.hash) || (a.nTime != b.nTime) || (a.height != b.height);
40  }
41 
42  uint256 GetHash() const;
43 };
44 
53 {
54 public:
55  // header
56  static const int32_t CURRENT_VERSION=5; // Version 5 supports CLTV activation
57  //Efficient and compatible, but not beautiful design: A PoA block version will be always equal or higher this const
58  static const int32_t POA_BLOCK_VERSION_LOW_LIMIT = 100;
59  int32_t nVersion;
60  //hashPrevBlock of PoA blocks is 0x00..00 for differentiating it from other block types
63 
64  uint32_t nTime;
65  uint32_t nBits;
66  uint32_t nNonce;
68 
69  //PoA block specific
70  //hash of previous PoA block, other block types dont need to care this property
71  //For the first PoA block, this property should be set as a default value: maybe 0x11 (magic number)
72  //or the hash of the genenis block
74  //The hash root of all audited PoS block summary
76  //hash of any mined PoA block: minedHash is found when a miner successfully mines a PoA block
77  //PoA block hash is hash of combination of previous hash and minedHash, since the previous hash of
78  //a PoA block is only known once the miner has mined the PoA block
80 
82  {
83  SetNull();
84  }
85 
87 
88  template <typename Stream, typename Operation>
89  inline void SerializationOp(Stream& s, Operation ser_action, int nType, int nVersion) {
90  READWRITE(this->nVersion);
91  nVersion = this->nVersion;
92  READWRITE(hashPrevBlock);
94  if (IsPoABlockByVersion()) {
95  //PoA block
99  }
100  READWRITE(nTime);
101  READWRITE(nBits);
102  READWRITE(nNonce);
103  }
104 
105  bool IsPoABlockByVersion() const {
107  }
108 
111  }
112 
113  void SetNull()
114  {
120  minedHash.SetNull();
121  nTime = 0;
122  nBits = 0;
123  nNonce = 0;
125  }
126 
127  bool IsNull() const
128  {
129  return (nBits == 0);
130  }
131 
132  uint256 GetHash() const;
133  uint256 ComputeMinedHash() const;
134 
135  int64_t GetBlockTime() const
136  {
137  return (int64_t)nTime;
138  }
139 };
140 
141 
142 class CBlock : public CBlockHeader
143 {
144 public:
145  // network and disk
146  std::vector<CTransaction> vtx;
147  //Contain the summary of all audited PoS blocks sorted in an increasing order of block height
148  //In between sequential audited PoS blocks, there might be PoA blocks which should not be found here
149  std::vector<PoSBlockSummary> posBlocksAudited;
150 
151  // ppcoin: block signature - signed by one of the coin base txout[N]'s owner
152  std::vector<unsigned char> vchBlockSig;
153 
154  // memory only
155  mutable CScript payee;
156  mutable bool fChecked;
157  mutable std::vector<uint256> poaMerkleTree;
158 
160  {
161  SetNull();
162  }
163 
164  CBlock(const CBlockHeader &header)
165  {
166  SetNull();
167  *((CBlockHeader*)this) = header;
168  }
169 
171 
172  template <typename Stream, typename Operation>
173  inline void SerializationOp(Stream& s, Operation ser_action, int nType, int nVersion) {
174  READWRITE(*(CBlockHeader*)this);
175  READWRITE(vtx);
176  if(vtx.size() > 1 && vtx[1].IsCoinStake())
178  if (IsProofOfAudit()) {
180  }
181  }
182 
183  void SetNull()
184  {
186  vtx.clear();
187  posBlocksAudited.clear();
188  fChecked = false;
189  poaMerkleTree.clear();
190  payee = CScript();
191  vchBlockSig.clear();
192  }
193 
195  {
196  CBlockHeader block;
197  block.nVersion = nVersion;
200 
203  block.minedHash = minedHash;
204 
205  block.nTime = nTime;
206  block.nBits = nBits;
207  block.nNonce = nNonce;
209  return block;
210  }
211 
212  // ppcoin: two types of block: proof-of-work or proof-of-stake
213  bool IsProofOfStake() const
214  {
215  return (vtx.size() > 1 && vtx[1].IsCoinStake()) && !IsProofOfAudit();
216  }
217 
218  bool IsProofOfWork() const
219  {
220  return !IsProofOfStake() && !IsProofOfAudit();
221  }
222 
228  bool IsProofOfAudit() const
229  {
230  return IsPoABlockByVersion();
231  }
232 
233  std::pair<COutPoint, unsigned int> GetProofOfStake() const
234  {
235  return IsProofOfStake()? std::make_pair(vtx[1].vin[0].prevout, nTime) : std::make_pair(COutPoint(), (unsigned int)0);
236  }
237 
238  std::string ToString() const;
239  void print() const;
240 
241  uint256 ComputePoAMerkleTree(bool* mutated = NULL) const;
242 };
243 
244 
250 {
251  std::vector<uint256> vHave;
252 
254 
255  CBlockLocator(const std::vector<uint256>& vHaveIn)
256  {
257  vHave = vHaveIn;
258  }
259 
261 
262  template <typename Stream, typename Operation>
263  inline void SerializationOp(Stream& s, Operation ser_action, int nType, int nVersion) {
264  if (!(nType & SER_GETHASH))
265  READWRITE(nVersion);
266  READWRITE(vHave);
267  }
268 
269  void SetNull()
270  {
271  vHave.clear();
272  }
273 
274  bool IsNull()
275  {
276  return vHave.empty();
277  }
278 };
279 
280 #endif // BITCOIN_PRIMITIVES_BLOCK_H
CBlockLocator::CBlockLocator
CBlockLocator()
Definition: block.h:253
CBlockHeader::hashPoAMerkleRoot
uint256 hashPoAMerkleRoot
Definition: block.h:75
CBlock::GetProofOfStake
std::pair< COutPoint, unsigned int > GetProofOfStake() const
Definition: block.h:233
CBlockLocator::SerializationOp
void SerializationOp(Stream &s, Operation ser_action, int nType, int nVersion)
Definition: block.h:263
SER_GETHASH
@ SER_GETHASH
Definition: serialize.h:161
CBlockHeader::hashMerkleRoot
uint256 hashMerkleRoot
Definition: block.h:62
transaction.h
CBlockHeader::CBlockHeader
CBlockHeader()
Definition: block.h:81
CBlockHeader::IsNull
bool IsNull() const
Definition: block.h:127
CBlockHeader::nBits
uint32_t nBits
Definition: block.h:65
b
void const uint64_t * b
Definition: field_5x52_asm_impl.h:10
CBlock::IsProofOfWork
bool IsProofOfWork() const
Definition: block.h:218
CBlockHeader
Nodes collect new transactions into a block, hash them into a hash tree, and scan through nonce value...
Definition: block.h:52
PoSBlockSummary::nTime
uint32_t nTime
Definition: block.h:20
CBlock::ComputePoAMerkleTree
uint256 ComputePoAMerkleTree(bool *mutated=NULL) const
Definition: block.cpp:122
CBlockHeader::nVersion
int32_t nVersion
Definition: block.h:59
uint256.h
CBlockHeader::nAccumulatorCheckpoint
uint256 nAccumulatorCheckpoint
Definition: block.h:67
CBlockHeader::IsPoABlockByVersion
bool IsPoABlockByVersion() const
Definition: block.h:105
PoSBlockSummary::operator!=
friend bool operator!=(const PoSBlockSummary &a, const PoSBlockSummary &b)
Definition: block.h:37
PoSBlockSummary::operator==
friend bool operator==(const PoSBlockSummary &a, const PoSBlockSummary &b)
Definition: block.h:32
CBlockLocator::SetNull
void SetNull()
Definition: block.h:269
CBlockHeader::GetHash
uint256 GetHash() const
Definition: block.cpp:80
CBlockHeader::CURRENT_VERSION
static const int32_t CURRENT_VERSION
Definition: block.h:56
CBlockHeader::nNonce
uint32_t nNonce
Definition: block.h:66
CBlock::fChecked
bool fChecked
Definition: block.h:156
CBlockHeader::SerializationOp
void SerializationOp(Stream &s, Operation ser_action, int nType, int nVersion)
Definition: block.h:89
CBlockHeader::GetBlockTime
int64_t GetBlockTime() const
Definition: block.h:135
CBlock::CBlock
CBlock()
Definition: block.h:159
PoSBlockSummary::GetHash
uint256 GetHash() const
Definition: block.cpp:16
CBlock::print
void print() const
Definition: block.cpp:181
CBlock::CBlock
CBlock(const CBlockHeader &header)
Definition: block.h:164
CBlockHeader::minedHash
uint256 minedHash
Definition: block.h:79
CBlockHeader::nTime
uint32_t nTime
Definition: block.h:64
CBlock::ADD_SERIALIZE_METHODS
ADD_SERIALIZE_METHODS
Definition: block.h:170
keystore.h
uint256
256-bit unsigned big integer.
Definition: uint256.h:38
CBlockLocator::CBlockLocator
CBlockLocator(const std::vector< uint256 > &vHaveIn)
Definition: block.h:255
CScript
Serialized script, used inside transaction inputs and outputs.
Definition: script.h:363
CBlockLocator::IsNull
bool IsNull()
Definition: block.h:274
PoSBlockSummary
Definition: block.h:17
CBlockHeader::SetNull
void SetNull()
Definition: block.h:113
CBlockHeader::hashPrevBlock
uint256 hashPrevBlock
Definition: block.h:61
base_uint::SetNull
void SetNull()
Definition: arith_uint256.h:308
CBlock::vtx
std::vector< CTransaction > vtx
Definition: block.h:146
CBlock
Definition: block.h:142
CBlockHeader::ADD_SERIALIZE_METHODS
ADD_SERIALIZE_METHODS
Definition: block.h:86
READWRITE
#define READWRITE(obj)
Definition: serialize.h:164
PoSBlockSummary::height
uint32_t height
Definition: block.h:21
CBlock::SerializationOp
void SerializationOp(Stream &s, Operation ser_action, int nType, int nVersion)
Definition: block.h:173
CBlockLocator::vHave
std::vector< uint256 > vHave
Definition: block.h:251
PoSBlockSummary::hash
uint256 hash
Definition: block.h:19
CBlockHeader::hashPrevPoABlock
uint256 hashPrevPoABlock
Definition: block.h:73
serialize.h
CBlockHeader::POA_BLOCK_VERSION_LOW_LIMIT
static const int32_t POA_BLOCK_VERSION_LOW_LIMIT
Definition: block.h:58
CBlock::ToString
std::string ToString() const
Definition: block.cpp:150
CBlock::IsProofOfAudit
bool IsProofOfAudit() const
Definition: block.h:228
CBlockHeader::SetVersionPoABlock
void SetVersionPoABlock()
Definition: block.h:109
COutPoint
An outpoint - a combination of a transaction hash and an index n into its vout.
Definition: transaction.h:36
PoSBlockSummary::SerializationOp
void SerializationOp(Stream &s, Operation ser_action, int nType, int nVersion)
Definition: block.h:26
CBlock::SetNull
void SetNull()
Definition: block.h:183
CBlockLocator
Describes a place in the block chain to another node such that if the other node doesn't have the sam...
Definition: block.h:249
CBlockHeader::ComputeMinedHash
uint256 ComputeMinedHash() const
Definition: block.cpp:66
PoSBlockSummary::ADD_SERIALIZE_METHODS
ADD_SERIALIZE_METHODS
Definition: block.h:23
CBlock::poaMerkleTree
std::vector< uint256 > poaMerkleTree
Definition: block.h:157
CBlock::payee
CScript payee
Definition: block.h:155
CBlockLocator::ADD_SERIALIZE_METHODS
ADD_SERIALIZE_METHODS
Definition: block.h:260
CBlock::IsProofOfStake
bool IsProofOfStake() const
Definition: block.h:213
CBlock::vchBlockSig
std::vector< unsigned char > vchBlockSig
Definition: block.h:152
CBlock::GetBlockHeader
CBlockHeader GetBlockHeader() const
Definition: block.h:194
CBlock::posBlocksAudited
std::vector< PoSBlockSummary > posBlocksAudited
Definition: block.h:149