Fabcoin Core  0.16.2
P2P Digital Currency
block.h
Go to the documentation of this file.
1 // Copyright (c) 2009-2010 Satoshi Nakamoto
2 // Copyright (c) 2009-2017 The Bitcoin Core developers
3 // Distributed under the MIT software license, see the accompanying
4 // file COPYING or http://www.opensource.org/licenses/mit-license.php.
5 
6 #ifndef FABCOIN_PRIMITIVES_BLOCK_H
7 #define FABCOIN_PRIMITIVES_BLOCK_H
8 
9 #include <arith_uint256.h>
10 #include <primitives/transaction.h>
11 #include <serialize.h>
12 #include <uint256.h>
13 #include <version.h>
14 #include <util.h>
15 #include <string.h>
16 
17 static const int SER_WITHOUT_SIGNATURE = 1 << 3;
18 namespace Consensus {
19  struct Params;
20 };
21 
22 static const int SERIALIZE_BLOCK_LEGACY = 0x04000000;
23 static const int SERIALIZE_BLOCK_NO_CONTRACT = 0x08000000;
24 bool _IsSupportContract(int nVersion, int nHeight);
25 bool _IsLegacyFormat(int nHeight);
26 
36 {
37 public:
38 
39  static const size_t HEADER_SIZE = 4+32+32+4+28+4+4+32; // Excluding Equihash solution
40  static const size_t HEADER_NEWSIZE = 4+32+32+4+28+4+4+32+32+32; // Excluding Equihash solution
41 
42  // header
43  int32_t nVersion;
46  uint32_t nHeight; //Equihash
47  uint32_t nReserved[7]; //Equihash
48  uint32_t nTime;
49  uint32_t nBits;
52 
54  std::vector<unsigned char> nSolution; // Equihash solution.
55 
57  {
58  SetNull();
59  }
60 
62 
63  template <typename Stream, typename Operation>
64  inline void SerializationOp(Stream& s, Operation ser_action)
65  {
66 
67  READWRITE(this->nVersion);
68  READWRITE(hashPrevBlock);
69  READWRITE(hashMerkleRoot);
70 
71  if ( !( s.GetVersion() & SERIALIZE_BLOCK_LEGACY ) )
72  READWRITE(nHeight);
73 
74  bool equihash_format = ! ( (s.GetVersion() & SERIALIZE_BLOCK_LEGACY) || _IsLegacyFormat(nHeight) );
75 
76  if (equihash_format) {
77  for(size_t i = 0; i < (sizeof(nReserved) / sizeof(nReserved[0])); i++) {
78  READWRITE(nReserved[i]);
79  }
80  }
81  READWRITE(nTime);
82  READWRITE(nBits);
83 
84  if ( _IsSupportContract( this->nVersion, nHeight ) ) {
85  READWRITE(hashStateRoot); // fasc
86  READWRITE(hashUTXORoot); // fasc
87  }
88 
89  // put nonce in the end , but before nSolution
90  if (equihash_format) {
91  READWRITE(nNonce);
92  READWRITE(nSolution);
93  } else {
94  uint32_t legacy_nonce = (uint32_t)nNonce.GetUint64(0);
95  READWRITE(legacy_nonce);
96  nNonce = ArithToUint256(arith_uint256(legacy_nonce));
97  }
98 
99  }
100 
101  void SetNull()
102  {
103  nVersion = 0;
104  hashPrevBlock.SetNull();
105  hashMerkleRoot.SetNull();
106  hashStateRoot.SetNull(); // fasc
107  hashUTXORoot.SetNull(); // fasc
108  //hashStateRoot = uint256S("9514771014c9ae803d8cea2731b2063e83de44802b40dce2d06acd02d0ff65e9");
109  //hashUTXORoot = uint256S("21b463e3b52f6201c0ad6c991be0485b6ef8c092e64583ffa655cc1b171fe856");
110  nHeight = 0;
111  memset(nReserved, 0, sizeof(nReserved));
112  nTime = 0;
113  nBits = 0;
114  nNonce.SetNull();
115  nSolution.clear();
116  }
117 
118  bool IsNull() const
119  {
120  return (nBits == 0);
121  }
122 
123  uint256 GetHash() const;
124  uint256 GetHash(const Consensus::Params& params) const;
125  uint256 GetHashWithoutSign() const;
126 
127  int64_t GetBlockTime() const
128  {
129  return (int64_t)nTime;
130  }
131  CBlockHeader& operator=(const CBlockHeader& other) //fasc
132  {
133  if (this != &other)
134  {
135  this->nVersion = other.nVersion;
136  this->hashPrevBlock = other.hashPrevBlock;
137  this->hashMerkleRoot = other.hashMerkleRoot;
138  this->nHeight = other.nHeight;
139  memcpy(this->nReserved, other.nReserved, sizeof(other.nReserved));
140  this->nTime = other.nTime;
141  this->nBits = other.nBits;
142  this->hashStateRoot = other.hashStateRoot;
143  this->hashUTXORoot = other.hashUTXORoot;
144  this->nNonce = other.nNonce;
145  this->nSolution = other.nSolution;
146 
147  }
148  return *this;
149  }
150 
151  std::string ToString() const;
152 };
153 
154 
155 class CBlock : public CBlockHeader
156 {
157 public:
158  // network and disk
159  std::vector<CTransactionRef> vtx;
160 
161  // memory only
162  mutable bool fChecked;
163 
165  {
166  SetNull();
167  }
168 
169  CBlock(const CBlockHeader &header)
170  {
171  SetNull();
172  *((CBlockHeader*)this) = header;
173  }
174 
176 
177  template <typename Stream, typename Operation>
178  inline void SerializationOp(Stream& s, Operation ser_action) {
179  READWRITE(*(CBlockHeader*)this);
180  READWRITE(vtx);
181  }
182 
183  void SetNull()
184  {
186  vtx.clear();
187  fChecked = false;
188  }
189 
191  {
192  CBlockHeader block;
193  block.nVersion = nVersion;
194  block.hashPrevBlock = hashPrevBlock;
195  block.hashMerkleRoot = hashMerkleRoot;
196  block.nHeight = nHeight; //equihash
197  memcpy(block.nReserved, nReserved, sizeof(block.nReserved)); //equihash
198  block.nTime = nTime;
199  block.nBits = nBits;
200  block.hashStateRoot = hashStateRoot; // fasc
201  block.hashUTXORoot = hashUTXORoot; // fasc
202  block.nNonce = nNonce;
203  block.nSolution = nSolution;
204 
205  return block;
206  }
207 
208  std::string ToString() const;
209 };
210 
216 {
217 public:
219  {
221  *((CBlockHeader*)this) = header;
222  }
223 
225 
226 
227  template <typename Stream, typename Operation>
228  inline void SerializationOp(Stream& s, Operation ser_action) {
229 
230  READWRITE(this->nVersion);
231  READWRITE(hashPrevBlock);
232  READWRITE(hashMerkleRoot);
233  READWRITE(nHeight);
234  for (size_t i = 0; i < (sizeof(nReserved) / sizeof(nReserved[0])); i++) {
235  READWRITE(nReserved[i]);
236  }
237  READWRITE(nTime);
238  READWRITE(nBits);
239 
240  if (_IsSupportContract( this->nVersion, nHeight ) ){
241  READWRITE(hashStateRoot); // fasc
242  READWRITE(hashUTXORoot); // fasc
243  }
244  }
245 };
246 
252 {
253  std::vector<uint256> vHave;
254 
256 
257  explicit CBlockLocator(const std::vector<uint256>& vHaveIn) : vHave(vHaveIn) {}
258 
260 
261  template <typename Stream, typename Operation>
262  inline void SerializationOp(Stream& s, Operation ser_action) {
263  int nVersion = s.GetVersion();
264  if (!(s.GetType() & SER_GETHASH))
265  READWRITE(nVersion);
266  READWRITE(vHave);
267  }
268 
269  void SetNull()
270  {
271  vHave.clear();
272  }
273 
274  bool IsNull() const
275  {
276  return vHave.empty();
277  }
278 };
279 
280 
281 #endif // FABCOIN_PRIMITIVES_BLOCK_H
CBlockHeader()
Definition: block.h:56
CBlockLocator()
Definition: block.h:255
void SetNull()
Definition: uint256.h:46
uint256 nNonce
Definition: block.h:53
Describes a place in the block chain to another node such that if the other node doesn&#39;t have the sam...
Definition: block.h:251
#define READWRITE(obj)
Definition: serialize.h:179
Definition: block.h:155
CBlockHeader & operator=(const CBlockHeader &other)
Definition: block.h:131
CBlock(const CBlockHeader &header)
Definition: block.h:169
void SerializationOp(Stream &s, Operation ser_action)
Definition: block.h:178
bool _IsSupportContract(int nVersion, int nHeight)
Definition: block.cpp:85
uint32_t nHeight
Definition: block.h:46
ADD_SERIALIZE_METHODS
Definition: block.h:175
uint32_t nReserved[7]
Definition: block.h:47
CEquihashInput(const CBlockHeader &header)
Definition: block.h:218
uint64_t GetUint64(int pos) const
Definition: uint256.h:90
void SerializationOp(Stream &s, Operation ser_action)
Definition: block.h:228
CBlock()
Definition: block.h:164
uint32_t nTime
Definition: block.h:48
ADD_SERIALIZE_METHODS
Definition: block.h:224
void SetNull()
Definition: block.h:269
uint256 hashMerkleRoot
Definition: block.h:45
void SerializationOp(Stream &s, Operation ser_action)
Definition: block.h:262
int64_t GetBlockTime() const
Definition: block.h:127
CBlockHeader GetBlockHeader() const
Definition: block.h:190
uint256 hashPrevBlock
Definition: block.h:44
ADD_SERIALIZE_METHODS
Definition: block.h:61
bool _IsLegacyFormat(int nHeight)
Definition: block.cpp:103
void SetNull()
Definition: block.h:101
std::vector< uint256 > vHave
Definition: block.h:253
Parameters that influence chain consensus.
Definition: params.h:39
std::vector< unsigned char > nSolution
Definition: block.h:54
CBlockLocator(const std::vector< uint256 > &vHaveIn)
Definition: block.h:257
256-bit unsigned big integer.
Custom serializer for CBlockHeader that omits the nonce and solution, for use as input to Equihash...
Definition: block.h:215
bool IsNull() const
Definition: block.h:274
256-bit opaque blob.
Definition: uint256.h:132
uint256 ArithToUint256(const arith_uint256 &a)
std::vector< CTransactionRef > vtx
Definition: block.h:159
void SerializationOp(Stream &s, Operation ser_action)
Definition: block.h:64
void SetNull()
Definition: block.h:183
const CChainParams & Params()
Return the currently selected parameters.
void * memcpy(void *a, const void *b, size_t c)
uint256 hashStateRoot
Definition: block.h:50
bool IsNull() const
Definition: block.h:118
bool fChecked
Definition: block.h:162
uint256 hashUTXORoot
Definition: block.h:51
int32_t nVersion
Definition: block.h:43
Nodes collect new transactions into a block, hash them into a hash tree, and scan through nonce value...
Definition: block.h:35
uint32_t nBits
Definition: block.h:49
ADD_SERIALIZE_METHODS
Definition: block.h:259