PIVX Core  5.6.99
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-2021 The PIVX Core developers
4 // Distributed under the MIT/X11 software license, see the accompanying
5 // file COPYING or http://www.opensource.org/licenses/mit-license.php.
6 
7 #ifndef PIVX_PRIMITIVES_BLOCK_H
8 #define PIVX_PRIMITIVES_BLOCK_H
9 
10 #include "primitives/transaction.h"
11 #include "keystore.h"
12 #include "serialize.h"
13 #include "uint256.h"
14 
23 {
24 public:
25  // header
26  static const int32_t CURRENT_VERSION=11; // since v5.2.99
27  int32_t nVersion;
30  uint32_t nTime;
31  uint32_t nBits;
32  uint32_t nNonce;
33  uint256 nAccumulatorCheckpoint; // only for version 4, 5 and 6.
34  uint256 hashFinalSaplingRoot; // only for version 8+
35 
37  {
38  SetNull();
39  }
40 
42  READWRITE(obj.nVersion, obj.hashPrevBlock, obj.hashMerkleRoot, obj.nTime, obj.nBits, obj.nNonce);
43 
44  //zerocoin active, header changes to include accumulator checksum
45  if(obj.nVersion > 3 && obj.nVersion < 7)
46  READWRITE(obj.nAccumulatorCheckpoint);
47 
48  // Sapling active
49  if (obj.nVersion >= 8)
50  READWRITE(obj.hashFinalSaplingRoot);
51  }
52 
53  void SetNull()
54  {
58  nTime = 0;
59  nBits = 0;
60  nNonce = 0;
63  }
64 
65  bool IsNull() const
66  {
67  return (nBits == 0);
68  }
69 
70  uint256 GetHash() const;
71 
72  int64_t GetBlockTime() const
73  {
74  return (int64_t)nTime;
75  }
76 };
77 
78 
79 class CBlock : public CBlockHeader
80 {
81 public:
82  // network and disk
83  std::vector<CTransactionRef> vtx;
84 
85  // ppcoin: block signature - signed by one of the coin base txout[N]'s owner
86  std::vector<unsigned char> vchBlockSig;
87 
88  // memory only
89  mutable bool fChecked{false};
90 
92  {
93  SetNull();
94  }
95 
96  CBlock(const CBlockHeader &header)
97  {
98  SetNull();
99  *(static_cast<CBlockHeader*>(this)) = header;
100  }
101 
103  {
105  READWRITE(obj.vtx);
106  if(obj.vtx.size() > 1 && obj.vtx[1]->IsCoinStake())
107  READWRITE(obj.vchBlockSig);
108  }
109 
110  void SetNull()
111  {
113  vtx.clear();
114  fChecked = false;
115  vchBlockSig.clear();
116  }
117 
119  {
120  CBlockHeader block;
121  block.nVersion = nVersion;
124  block.nTime = nTime;
125  block.nBits = nBits;
126  block.nNonce = nNonce;
127  if(nVersion > 3 && nVersion < 7)
129  if (nVersion >= 8)
131  return block;
132  }
133 
134  bool IsProofOfStake() const
135  {
136  return (vtx.size() > 1 && vtx[1]->IsCoinStake());
137  }
138 
139  bool IsProofOfWork() const
140  {
141  return !IsProofOfStake();
142  }
143 
144  std::string ToString() const;
145  void print() const;
146 };
147 
148 
154 {
155  std::vector<uint256> vHave;
156 
158 
159  explicit CBlockLocator(const std::vector<uint256>& vHaveIn)
160  {
161  vHave = vHaveIn;
162  }
163 
165  {
166  int nVersion = s.GetVersion();
167  if (!(s.GetType() & SER_GETHASH))
168  READWRITE(nVersion);
169  READWRITE(obj.vHave);
170  }
171 
172  void SetNull()
173  {
174  vHave.clear();
175  }
176 
177  bool IsNull() const
178  {
179  return vHave.empty();
180  }
181 };
182 
183 #endif // PIVX_PRIMITIVES_BLOCK_H
Nodes collect new transactions into a block, hash them into a hash tree, and scan through nonce value...
Definition: block.h:23
static const int32_t CURRENT_VERSION
Definition: block.h:26
uint32_t nNonce
Definition: block.h:32
uint256 hashFinalSaplingRoot
Definition: block.h:34
SERIALIZE_METHODS(CBlockHeader, obj)
Definition: block.h:41
uint32_t nBits
Definition: block.h:31
CBlockHeader()
Definition: block.h:36
uint256 nAccumulatorCheckpoint
Definition: block.h:33
uint32_t nTime
Definition: block.h:30
int64_t GetBlockTime() const
Definition: block.h:72
int32_t nVersion
Definition: block.h:27
uint256 hashPrevBlock
Definition: block.h:28
void SetNull()
Definition: block.h:53
uint256 hashMerkleRoot
Definition: block.h:29
uint256 GetHash() const
Definition: block.cpp:15
bool IsNull() const
Definition: block.h:65
Definition: block.h:80
void SetNull()
Definition: block.h:110
void print() const
Definition: block.cpp:53
std::string ToString() const
Definition: block.cpp:35
std::vector< CTransactionRef > vtx
Definition: block.h:83
SERIALIZE_METHODS(CBlock, obj)
Definition: block.h:102
std::vector< unsigned char > vchBlockSig
Definition: block.h:86
CBlockHeader GetBlockHeader() const
Definition: block.h:118
CBlock()
Definition: block.h:91
CBlock(const CBlockHeader &header)
Definition: block.h:96
bool IsProofOfStake() const
Definition: block.h:134
bool IsProofOfWork() const
Definition: block.h:139
bool fChecked
Definition: block.h:89
void SetNull()
Definition: uint256.h:44
256-bit opaque blob.
Definition: uint256.h:138
@ SER_GETHASH
Definition: serialize.h:176
#define READWRITEAS(type, obj)
Definition: serialize.h:184
#define READWRITE(...)
Definition: serialize.h:183
Describes a place in the block chain to another node such that if the other node doesn't have the sam...
Definition: block.h:154
CBlockLocator(const std::vector< uint256 > &vHaveIn)
Definition: block.h:159
SERIALIZE_METHODS(CBlockLocator, obj)
Definition: block.h:164
std::vector< uint256 > vHave
Definition: block.h:155
bool IsNull() const
Definition: block.h:177
CBlockLocator()
Definition: block.h:157
void SetNull()
Definition: block.h:172