Raven Core  3.0.0
P2P Digital Currency
block.h
Go to the documentation of this file.
1 // Copyright (c) 2009-2010 Satoshi Nakamoto
2 // Copyright (c) 2009-2016 The Bitcoin Core developers
3 // Copyright (c) 2017-2019 The Raven Core developers
4 // Distributed under the MIT software license, see the accompanying
5 // file COPYING or http://www.opensource.org/licenses/mit-license.php.
6 
7 #ifndef RAVEN_PRIMITIVES_BLOCK_H
8 #define RAVEN_PRIMITIVES_BLOCK_H
9 
10 #include "primitives/transaction.h"
11 #include "serialize.h"
12 #include "uint256.h"
13 
22 {
23 public:
24  // header
25  int32_t nVersion;
28  uint32_t nTime;
29  uint32_t nBits;
30  uint32_t nNonce;
31 
33  {
34  SetNull();
35  }
36 
38 
39  template <typename Stream, typename Operation>
40  inline void SerializationOp(Stream& s, Operation ser_action) {
41  READWRITE(this->nVersion);
42  READWRITE(hashPrevBlock);
43  READWRITE(hashMerkleRoot);
44  READWRITE(nTime);
45  READWRITE(nBits);
46  READWRITE(nNonce);
47  }
48 
49  void SetNull()
50  {
51  nVersion = 0;
52  hashPrevBlock.SetNull();
53  hashMerkleRoot.SetNull();
54  nTime = 0;
55  nBits = 0;
56  nNonce = 0;
57  }
58 
59  bool IsNull() const
60  {
61  return (nBits == 0);
62  }
63 
64  uint256 GetHash() const;
65 
66  int64_t GetBlockTime() const
67  {
68  return (int64_t)nTime;
69  }
70 };
71 
72 
73 class CBlock : public CBlockHeader
74 {
75 public:
76  // network and disk
77  std::vector<CTransactionRef> vtx;
78 
79  // memory only
80  mutable bool fChecked;
81 
83  {
84  SetNull();
85  }
86 
87  CBlock(const CBlockHeader &header)
88  {
89  SetNull();
90  *((CBlockHeader*)this) = header;
91  }
92 
94 
95  template <typename Stream, typename Operation>
96  inline void SerializationOp(Stream& s, Operation ser_action) {
97  READWRITE(*(CBlockHeader*)this);
98  READWRITE(vtx);
99  }
100 
101  void SetNull()
102  {
104  vtx.clear();
105  fChecked = false;
106  }
107 
109  {
110  CBlockHeader block;
111  block.nVersion = nVersion;
114  block.nTime = nTime;
115  block.nBits = nBits;
116  block.nNonce = nNonce;
117  return block;
118  }
119 
120  // void SetPrevBlockHash(uint256 prevHash)
121  // {
122  // block.hashPrevBlock = prevHash;
123  // }
124 
125  std::string ToString() const;
126 };
127 
133 {
134  std::vector<uint256> vHave;
135 
137 
138  explicit CBlockLocator(const std::vector<uint256>& vHaveIn) : vHave(vHaveIn) {}
139 
141 
142  template <typename Stream, typename Operation>
143  inline void SerializationOp(Stream& s, Operation ser_action) {
144  int nVersion = s.GetVersion();
145  if (!(s.GetType() & SER_GETHASH))
146  READWRITE(nVersion);
147  READWRITE(vHave);
148  }
149 
150  void SetNull()
151  {
152  vHave.clear();
153  }
154 
155  bool IsNull() const
156  {
157  return vHave.empty();
158  }
159 };
160 
161 #endif // RAVEN_PRIMITIVES_BLOCK_H
uint32_t nNonce
Definition: block.h:30
CBlockHeader()
Definition: block.h:32
CBlockLocator()
Definition: block.h:136
CBlockHeader GetBlockHeader() const
Definition: block.h:108
void SetNull()
Definition: uint256.h:41
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:132
#define READWRITE(obj)
Definition: serialize.h:163
Definition: block.h:73
CBlock(const CBlockHeader &header)
Definition: block.h:87
void SerializationOp(Stream &s, Operation ser_action)
Definition: block.h:96
ADD_SERIALIZE_METHODS
Definition: block.h:93
bool IsNull() const
Definition: block.h:155
CBlock()
Definition: block.h:82
uint32_t nTime
Definition: block.h:28
void SetNull()
Definition: block.h:150
uint256 hashMerkleRoot
Definition: block.h:27
void SerializationOp(Stream &s, Operation ser_action)
Definition: block.h:143
uint256 hashPrevBlock
Definition: block.h:26
ADD_SERIALIZE_METHODS
Definition: block.h:37
void SetNull()
Definition: block.h:49
std::vector< uint256 > vHave
Definition: block.h:134
int64_t GetBlockTime() const
Definition: block.h:66
CBlockLocator(const std::vector< uint256 > &vHaveIn)
Definition: block.h:138
uint256 GetHash() const
Definition: block.cpp:14
256-bit opaque blob.
Definition: uint256.h:123
std::vector< CTransactionRef > vtx
Definition: block.h:77
void SerializationOp(Stream &s, Operation ser_action)
Definition: block.h:40
void SetNull()
Definition: block.h:101
bool IsNull() const
Definition: block.h:59
bool fChecked
Definition: block.h:80
int32_t nVersion
Definition: block.h:25
Nodes collect new transactions into a block, hash them into a hash tree, and scan through nonce value...
Definition: block.h:21
uint32_t nBits
Definition: block.h:29
ADD_SERIALIZE_METHODS
Definition: block.h:140