1 // Copyright (c) 2009-2010 Satoshi Nakamoto
2 // Copyright (c) 2009-2013 The Bitcoin developers
3 // Distributed under the MIT/X11 software license, see the accompanying
4 // file COPYING or http://www.opensource.org/licenses/mit-license.php.
5 
6 #ifndef BITCOIN_PRIMITIVES_PUREHEADER_H
7 #define BITCOIN_PRIMITIVES_PUREHEADER_H
8 
9 #include <serialize.h>
10 #include <uint256.h>
11 
12 /**
13  * A block header without auxpow information.  This "intermediate step"
14  * in constructing the full header is useful, because it breaks the cyclic
15  * dependency between auxpow (referencing a parent block header) and
16  * the block header (referencing an auxpow).  The parent block header
17  * does not have auxpow itself, so it is a pure header.
18  */
19 class CPureBlockHeader
20 {
21 private:
22 
23     /* Modifiers to the version.  */
24     static const int32_t VERSION_AUXPOW = (1 << 8);
25 
26     /** Bits above are reserved for the auxpow chain ID.  */
27     static const int32_t VERSION_CHAIN_START = (1 << 16);
28 
29 public:
30     // header
31     int32_t nVersion;
32     uint256 hashPrevBlock;
33     uint256 hashMerkleRoot;
34     uint32_t nTime;
35     uint32_t nBits;
36     uint32_t nNonce;
37 
CPureBlockHeader()38     CPureBlockHeader()
39     {
40         SetNull();
41     }
42 
SERIALIZE_METHODS(CPureBlockHeader,obj)43     SERIALIZE_METHODS(CPureBlockHeader, obj) { READWRITE(obj.nVersion, obj.hashPrevBlock, obj.hashMerkleRoot, obj.nTime, obj.nBits, obj.nNonce); }
44 
SetNull()45     void SetNull()
46     {
47         nVersion = 0;
48         hashPrevBlock.SetNull();
49         hashMerkleRoot.SetNull();
50         nTime = 0;
51         nBits = 0;
52         nNonce = 0;
53     }
54 
IsNull()55     bool IsNull() const
56     {
57         return (nBits == 0);
58     }
59 
60     uint256 GetHash() const;
61 
GetBlockTime()62     int64_t GetBlockTime() const
63     {
64         return (int64_t)nTime;
65     }
66 
67     /* Below are methods to interpret the version with respect to
68        auxpow data and chain ID.  This used to be in the CBlockVersion
69        class, but was moved here when we switched back to nVersion being
70        a pure int member as preparation to undoing the "abuse" and
71        allowing BIP9 to work.  */
72 
73     /**
74      * Extract the base version (without modifiers and chain ID).
75      * @return The base version./
76      */
GetBaseVersion()77     inline int32_t GetBaseVersion() const
78     {
79         return GetBaseVersion(nVersion);
80     }
GetBaseVersion(int32_t ver)81     static inline int32_t GetBaseVersion(int32_t ver)
82     {
83         return ver % VERSION_AUXPOW;
84     }
85 
86     /**
87      * Set the base version (apart from chain ID and auxpow flag) to
88      * the one given.  This should only be called when auxpow is not yet
89      * set, to initialise a block!
90      * @param nBaseVersion The base version.
91      * @param nChainId The auxpow chain ID.
92      */
93     void SetBaseVersion(int32_t nBaseVersion, int32_t nChainId);
94 
95     /**
96      * Extract the chain ID.
97      * @return The chain ID encoded in the version.
98      */
GetChainId()99     inline int32_t GetChainId() const
100     {
101         return nVersion / VERSION_CHAIN_START;
102     }
103 
104     /**
105      * Set the chain ID.  This is used for the test suite.
106      * @param ch The chain ID to set.
107      */
SetChainId(int32_t chainId)108     inline void SetChainId(int32_t chainId)
109     {
110         nVersion %= VERSION_CHAIN_START;
111         nVersion |= chainId * VERSION_CHAIN_START;
112     }
113 
114     /**
115      * Check if the auxpow flag is set in the version.
116      * @return True iff this block version is marked as auxpow.
117      */
IsAuxpow()118     inline bool IsAuxpow() const
119     {
120         return nVersion & VERSION_AUXPOW;
121     }
122 
123     /**
124      * Set the auxpow flag.  This is used for testing.
125      * @param auxpow Whether to mark auxpow as true.
126      */
SetAuxpowVersion(bool auxpow)127     inline void SetAuxpowVersion (bool auxpow)
128     {
129         if (auxpow)
130             nVersion |= VERSION_AUXPOW;
131         else
132             nVersion &= ~VERSION_AUXPOW;
133     }
134 
135     /**
136      * Check whether this is a "legacy" block without chain ID.
137      * @return True iff it is.
138      */
IsLegacy()139     inline bool IsLegacy() const
140     {
141         return nVersion == 1;
142     }
143 };
144 
145 #endif // BITCOIN_PRIMITIVES_PUREHEADER_H
146