dogecoin/src/primitives/pureheader.h
Ross Nicoll bc8cca4896 Merge AuxPoW support from Namecore
Changes are as below:

Wrap CBlockHeader::nVersion into a new class (CBlockVersion).  This allows to take care of interpreting the field into a base version, auxpow flag and the chain ID.

Update getauxblock.py for new 'generate' RPC call.

Add 'auxpow' to block JSON.

Accept auxpow as PoW verification.

Add unit tests for auxpow verification.

Add check for memory-layout of CBlockVersion.

Weaken auxpow chain ID checks for the testnet.

Allow Params() to overrule when to check the auxpow chain ID and for legacy blocks.  Use this to disable the checks on testnet.

Introduce CPureBlockHeader.

Split the block header part that is used by auxpow and the "real" block header (that uses auxpow) to resolve the cyclic dependency between the two.

Differentiate between uint256 and arith_uint256.

This change was done upstream, modify the auxpow code.

Add missing lock in auxpow_tests.

Fix REST header check for auxpow headers.

Those can be longer, thus take that into account.  Also perform the check actually on an auxpow header.

Correctly set the coinbase for getauxblock results.

Call IncrementExtraNonce in getauxblock so that the coinbase is actually initialised with the stuff it should be.  (BIP30 block height and COINBASE_FLAGS.)

Implement getauxblock plus regression test.

Turn auxpow test into FIXTURE test.

This allows using of the Params() calls.

Move CMerkleTx code to auxpow.cpp.

Otherwise we get linker errors when building without wallet.

Fix rebase with BIP66.

Update the code to handle BIP66's nVersion=3.

Enforce that auxpow parent blocks have no auxpow block version.

This is for compatibility with namecoind.  See also https://github.com/namecoin/namecoin/pull/199.

Move auxpow-related parameters to Consensus::Params.
2018-09-19 19:22:45 +01:00

157 lines
4 KiB
C++

// Copyright (c) 2009-2010 Satoshi Nakamoto
// Copyright (c) 2009-2013 The Bitcoin developers
// Distributed under the MIT/X11 software license, see the accompanying
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
#ifndef BITCOIN_PRIMITIVES_PUREHEADER_H
#define BITCOIN_PRIMITIVES_PUREHEADER_H
#include "serialize.h"
#include "uint256.h"
/**
* A block header without auxpow information. This "intermediate step"
* in constructing the full header is useful, because it breaks the cyclic
* dependency between auxpow (referencing a parent block header) and
* the block header (referencing an auxpow). The parent block header
* does not have auxpow itself, so it is a pure header.
*/
class CPureBlockHeader
{
public:
/* Modifiers to the version. */
static const int32_t VERSION_AUXPOW = (1 << 8);
/** Bits above are reserved for the auxpow chain ID. */
static const int32_t VERSION_CHAIN_START = (1 << 16);
// header
int32_t nVersion;
uint256 hashPrevBlock;
uint256 hashMerkleRoot;
uint32_t nTime;
uint32_t nBits;
uint32_t nNonce;
CPureBlockHeader()
{
SetNull();
}
ADD_SERIALIZE_METHODS;
template <typename Stream, typename Operation>
inline void SerializationOp(Stream& s, Operation ser_action)
{
READWRITE(this->nVersion);
READWRITE(hashPrevBlock);
READWRITE(hashMerkleRoot);
READWRITE(nTime);
READWRITE(nBits);
READWRITE(nNonce);
}
void SetNull()
{
nVersion = 0;
hashPrevBlock.SetNull();
hashMerkleRoot.SetNull();
nTime = 0;
nBits = 0;
nNonce = 0;
}
bool IsNull() const
{
return (nBits == 0);
}
uint256 GetHash() const;
uint256 GetPoWHash() const;
int64_t GetBlockTime() const
{
return (int64_t)nTime;
}
/* Below are methods to interpret the version with respect to
auxpow data and chain ID. This used to be in the CBlockVersion
class, but was moved here when we switched back to nVersion being
a pure int member as preparation to undoing the "abuse" and
allowing BIP9 to work. */
/**
* Extract the base version (without modifiers and chain ID).
* @return The base version./
*/
inline int32_t GetBaseVersion() const
{
return GetBaseVersion(nVersion);
}
static inline int32_t GetBaseVersion(int32_t ver)
{
return ver % VERSION_AUXPOW;
}
/**
* Set the base version (apart from chain ID and auxpow flag) to
* the one given. This should only be called when auxpow is not yet
* set, to initialise a block!
* @param nBaseVersion The base version.
* @param nChainId The auxpow chain ID.
*/
void SetBaseVersion(int32_t nBaseVersion, int32_t nChainId);
/**
* Extract the chain ID.
* @return The chain ID encoded in the version.
*/
inline int32_t GetChainId() const
{
return nVersion >> 16;
}
/**
* Set the chain ID. This is used for the test suite.
* @param ch The chain ID to set.
*/
inline void SetChainId(int32_t chainId)
{
nVersion %= VERSION_CHAIN_START;
nVersion |= chainId * VERSION_CHAIN_START;
}
/**
* Check if the auxpow flag is set in the version.
* @return True if this block version is marked as auxpow.
*/
inline bool IsAuxpow() const
{
return nVersion & VERSION_AUXPOW;
}
/**
* Set the auxpow flag. This is used for testing.
* @param auxpow Whether to mark auxpow as true.
*/
inline void SetAuxpowFlag(bool auxpow)
{
if (auxpow)
nVersion |= VERSION_AUXPOW;
else
nVersion &= ~VERSION_AUXPOW;
}
/**
* Check whether this is a "legacy" block without chain ID.
* @return True if it is.
*/
inline bool IsLegacy() const
{
return nVersion == 1;
}
};
#endif // BITCOIN_PRIMITIVES_PUREHEADER_H