Dogecoin Core  1.14.2
P2P Digital Currency
auxpow.h
Go to the documentation of this file.
1 // Copyright (c) 2009-2010 Satoshi Nakamoto
2 // Copyright (c) 2009-2014 The Bitcoin developers
3 // Copyright (c) 2014-2016 Daniel Kraft
4 // Distributed under the MIT/X11 software license, see the accompanying
5 // file license.txt or http://www.opensource.org/licenses/mit-license.php.
6 
7 #ifndef BITCOIN_AUXPOW_H
8 #define BITCOIN_AUXPOW_H
9 
10 #include "consensus/params.h"
11 #include "consensus/validation.h"
12 #include "primitives/pureheader.h"
13 #include "primitives/transaction.h"
14 #include "serialize.h"
15 #include "uint256.h"
16 
17 #include <vector>
18 
19 class CBlock;
20 class CBlockHeader;
21 class CBlockIndex;
22 
24 static const unsigned char pchMergedMiningHeader[] = { 0xfa, 0xbe, 'm', 'm' };
25 
26 /* Because it is needed for auxpow, the definition of CMerkleTx is moved
27  here from wallet.h. */
28 
30 class CMerkleTx
31 {
32 private:
34  static const uint256 ABANDON_HASH;
35 
36 public:
39  // Dogecoin TODO: Is this used? If not remove. If it is, I don't think it's actually set
40  // anywhere. Check with Namecore
41  std::vector<uint256> vMerkleBranch;
42 
43  /* An nIndex == -1 means that hashBlock (in nonzero) refers to the earliest
44  * block in the chain we know this or any in-wallet dependency conflicts
45  * with. Older clients interpret nIndex == -1 as unconfirmed for backward
46  * compatibility.
47  */
48  int nIndex;
49 
51  {
52  SetTx(MakeTransactionRef());
53  Init();
54  }
55 
57  {
58  SetTx(std::move(arg));
59  Init();
60  }
61 
64  operator const CTransaction&() const { return *tx; }
65 
66  void Init()
67  {
68  hashBlock = uint256();
69  nIndex = -1;
70  }
71 
73  {
74  tx = std::move(arg);
75  }
76 
78 
79  template <typename Stream, typename Operation>
80  inline void SerializationOp(Stream& s, Operation ser_action) {
81  READWRITE(tx);
85  }
86 
93  void InitMerkleBranch(const CBlock& block, int posInBlock);
94 
95  void SetMerkleBranch(const CBlockIndex* pindex, int posInBlock);
96 
103  int GetDepthInMainChain(const CBlockIndex* &pindexRet) const;
104  int GetDepthInMainChain() const { const CBlockIndex *pindexRet; return GetDepthInMainChain(pindexRet); }
105  bool IsInMainChain() const { const CBlockIndex *pindexRet; return GetDepthInMainChain(pindexRet) > 0; }
106  int GetBlocksToMaturity() const;
108  bool AcceptToMemoryPool(const CAmount& nAbsurdFee, CValidationState& state);
109  bool hashUnset() const { return (hashBlock.IsNull() || hashBlock == ABANDON_HASH); }
110  bool isAbandoned() const { return (hashBlock == ABANDON_HASH); }
112 
113  const uint256& GetHash() const { return tx->GetHash(); }
114  bool IsCoinBase() const { return tx->IsCoinBase(); }
115 };
116 
123 class CAuxPow : public CMerkleTx
124 {
125 
126 /* Public for the unit tests. */
127 public:
128 
130  std::vector<uint256> vChainMerkleBranch;
131 
134 
137 
138 public:
139 
140  /* Prevent accidental conversion. */
141  inline explicit CAuxPow(CTransactionRef txIn)
142  : CMerkleTx(txIn)
143  {
144  }
145 
146  inline CAuxPow()
147  : CMerkleTx()
148  {
149  }
150 
152 
153  template<typename Stream, typename Operation>
154  inline void
155  SerializationOp (Stream& s, Operation ser_action)
156  {
157  READWRITE (*static_cast<CMerkleTx*> (this));
161  }
162 
172  bool check(const uint256& hashAuxBlock, int nChainId, const Consensus::Params& params) const;
173 
179  inline uint256
181  {
182  return parentBlock.GetPoWHash ();
183  }
184 
190  /* FIXME: Remove after the hardfork. */
191  inline const CPureBlockHeader&
193  {
194  return parentBlock;
195  }
196 
205  static int getExpectedIndex(uint32_t nNonce, int nChainId, unsigned h);
206 
211  static uint256 CheckMerkleBranch(uint256 hash,
212  const std::vector<uint256>& vMerkleBranch,
213  int nIndex);
214 
222  static void initAuxPow(CBlockHeader& header);
223 
224 };
225 
226 #endif // BITCOIN_AUXPOW_H
int64_t CAmount
Amount in satoshis (Can be negative)
Definition: amount.h:15
Data for the merge-mining auxpow.
Definition: auxpow.h:124
static uint256 CheckMerkleBranch(uint256 hash, const std::vector< uint256 > &vMerkleBranch, int nIndex)
Check a merkle branch.
Definition: auxpow.cpp:186
CAuxPow(CTransactionRef txIn)
Definition: auxpow.h:141
static void initAuxPow(CBlockHeader &header)
Initialise the auxpow of the given block header.
Definition: auxpow.cpp:205
uint256 getParentBlockPoWHash() const
Get the parent block's hash.
Definition: auxpow.h:180
bool check(const uint256 &hashAuxBlock, int nChainId, const Consensus::Params &params) const
Check the auxpow, given the merge-mined block's hash and our chain ID.
Definition: auxpow.cpp:81
CAuxPow()
Definition: auxpow.h:146
CPureBlockHeader parentBlock
Parent block header (on which the real PoW is done).
Definition: auxpow.h:136
int nChainIndex
Merkle tree index of the aux block header in the coinbase.
Definition: auxpow.h:133
void SerializationOp(Stream &s, Operation ser_action)
Definition: auxpow.h:155
const CPureBlockHeader & getParentBlock() const
Return parent block.
Definition: auxpow.h:192
ADD_SERIALIZE_METHODS
Definition: auxpow.h:151
static int getExpectedIndex(uint32_t nNonce, int nChainId, unsigned h)
Calculate the expected index in the merkle tree.
Definition: auxpow.cpp:160
std::vector< uint256 > vChainMerkleBranch
The merkle branch connecting the aux block to our coinbase.
Definition: auxpow.h:130
Nodes collect new transactions into a block, hash them into a hash tree, and scan through nonce value...
Definition: block.h:25
Definition: block.h:67
The block chain is a tree shaped structure starting with the genesis block at the root,...
Definition: chain.h:158
A transaction with a merkle branch linking it to the block chain.
Definition: auxpow.h:31
CMerkleTx(CTransactionRef arg)
Definition: auxpow.h:56
bool AcceptToMemoryPool(const CAmount &nAbsurdFee, CValidationState &state)
Pass this transaction to the mempool.
Definition: auxpow.cpp:73
bool isAbandoned() const
Definition: auxpow.h:110
const uint256 & GetHash() const
Definition: auxpow.h:113
int GetDepthInMainChain() const
Definition: auxpow.h:104
CMerkleTx()
Definition: auxpow.h:50
void SetTx(CTransactionRef arg)
Definition: auxpow.h:72
static const uint256 ABANDON_HASH
Constant used in hashBlock to indicate tx has been abandoned.
Definition: auxpow.h:34
bool IsCoinBase() const
Definition: auxpow.h:114
int GetBlocksToMaturity() const
Definition: auxpow.cpp:64
void InitMerkleBranch(const CBlock &block, int posInBlock)
Actually compute the Merkle branch.
Definition: auxpow.cpp:38
void setAbandoned()
Definition: auxpow.h:111
uint256 hashBlock
Definition: auxpow.h:38
int nIndex
Definition: auxpow.h:48
ADD_SERIALIZE_METHODS
Definition: auxpow.h:77
std::vector< uint256 > vMerkleBranch
Definition: auxpow.h:41
void SetMerkleBranch(const CBlockIndex *pindex, int posInBlock)
Definition: auxpow.cpp:29
void SerializationOp(Stream &s, Operation ser_action)
Definition: auxpow.h:80
void Init()
Definition: auxpow.h:66
bool IsInMainChain() const
Definition: auxpow.h:105
CTransactionRef tx
Definition: auxpow.h:37
bool hashUnset() const
Definition: auxpow.h:109
A block header without auxpow information.
Definition: pureheader.h:20
uint256 GetPoWHash() const
Definition: pureheader.cpp:25
The basic transaction that is broadcasted on the network and contained in blocks.
Definition: transaction.h:308
Capture information about block/transaction validation.
Definition: validation.h:22
bool IsNull() const
Definition: uint256.h:32
256-bit opaque blob.
Definition: uint256.h:123
#define READWRITE(obj)
Definition: serialize.h:151
Parameters that influence chain consensus.
Definition: params.h:39
std::shared_ptr< const CTransaction > CTransactionRef
Definition: transaction.h:459