forked from dogecoin/dogecoin
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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. * 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. * 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 namecoin/namecoin-legacy#199. * Move auxpow-related parameters to Consensus::Params.
- Loading branch information
1 parent
b2489df
commit fcfdee6
Showing
32 changed files
with
1,610 additions
and
273 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,154 @@ | ||
// Copyright(c) 2018-2020 Daniel Kraft | ||
// Distributed under the MIT software license, see the accompanying | ||
// file COPYING or http://www.opensource.org/licenses/mit-license.php. | ||
|
||
#include <rpc/auxpow_miner.h> | ||
|
||
#include <arith_uint256.h> | ||
#include <auxpow.h> | ||
#include <chainparams.h> | ||
#include <net.h> | ||
#include <node/context.h> | ||
#include <rpc/blockchain.h> | ||
#include <rpc/protocol.h> | ||
#include <rpc/request.h> | ||
#include <util/check.h> | ||
#include <util/strencodings.h> | ||
#include <util/time.h> | ||
#include <validation.h> | ||
|
||
const CBlock* | ||
AuxpowMiner::getCurrentBlock(const CTxMemPool& mempool, | ||
const CScript& scriptPubKey, uint256& target) EXCLUSIVE_LOCKS_REQUIRED(cs) | ||
{ | ||
AssertLockHeld(cs); | ||
const CBlock* pblockCur = nullptr; | ||
|
||
{ | ||
LOCK(cs_main); | ||
CScriptID scriptID(scriptPubKey); | ||
auto iter = curBlocks.find(scriptID); | ||
if (iter != curBlocks.end()) | ||
pblockCur = iter->second; | ||
|
||
if (pblockCur == nullptr | ||
|| pindexPrev != ::ChainActive().Tip() | ||
||(mempool.GetTransactionsUpdated() != txUpdatedLast | ||
&& GetTime() - startTime > MAX_BLOCK_TEMPLATE_AGE_SECONDS)) | ||
{ | ||
if (pindexPrev != ::ChainActive().Tip()) | ||
{ | ||
/* Clear old blocks since they're obsolete now. */ | ||
blocks.clear(); | ||
templates.clear(); | ||
curBlocks.clear(); | ||
} | ||
|
||
/* Create new block with nonce = 0 and extraNonce = 1. */ | ||
std::unique_ptr<CBlockTemplate> newBlock | ||
= BlockAssembler(mempool, Params()).CreateNewBlock(scriptPubKey); | ||
if (newBlock == nullptr) | ||
throw JSONRPCError(RPC_OUT_OF_MEMORY, "out of memory"); | ||
|
||
/* Update state only when CreateNewBlock succeeded. */ | ||
txUpdatedLast = mempool.GetTransactionsUpdated(); | ||
pindexPrev = ::ChainActive().Tip(); | ||
startTime = GetTime(); | ||
|
||
/* Finalise it by setting the version and building the merkle root. */ | ||
IncrementExtraNonce(&newBlock->block, pindexPrev, extraNonce); | ||
newBlock->block.SetAuxpowFlag(true); | ||
|
||
/* Save in our map of constructed blocks. */ | ||
pblockCur = &newBlock->block; | ||
curBlocks.emplace(scriptID, pblockCur); | ||
blocks[pblockCur->GetHash()] = pblockCur; | ||
templates.push_back(std::move(newBlock)); | ||
} | ||
} | ||
|
||
/* At this point, pblockCur is always initialised: If we make it here | ||
without creating a new block above, it means that, in particular, | ||
pindexPrev == ::ChainActive().Tip(). But for that to happen, we must | ||
already have created a pblockCur in a previous call, as pindexPrev is | ||
initialised only when pblockCur is. */ | ||
CHECK_NONFATAL(pblockCur); | ||
|
||
arith_uint256 arithTarget; | ||
bool fNegative, fOverflow; | ||
arithTarget.SetCompact(pblockCur->nBits, &fNegative, &fOverflow); | ||
if (fNegative || fOverflow || arithTarget == 0) | ||
throw std::runtime_error("invalid difficulty bits in block"); | ||
target = ArithToUint256(arithTarget); | ||
|
||
return pblockCur; | ||
} | ||
|
||
const CBlock* | ||
AuxpowMiner::lookupSavedBlock(const uint256 hash) const EXCLUSIVE_LOCKS_REQUIRED(cs) | ||
{ | ||
AssertLockHeld(cs); | ||
|
||
const auto iter = blocks.find(hash); | ||
if (iter == blocks.end()) | ||
throw JSONRPCError(RPC_INVALID_PARAMETER, "block hash unknown"); | ||
|
||
return iter->second; | ||
} | ||
|
||
UniValue | ||
AuxpowMiner::createAuxBlock(const CTxMemPool& mempool, | ||
const CScript& scriptPubKey) | ||
{ | ||
LOCK(cs); | ||
|
||
uint256 target; | ||
const CBlock* pblock = getCurrentBlock(mempool, scriptPubKey, target); | ||
|
||
UniValue result(UniValue::VOBJ); | ||
result.pushKV("hash", pblock->GetHash().GetHex()); | ||
result.pushKV("chainid", pblock->GetChainId()); | ||
result.pushKV("previousblockhash", pblock->hashPrevBlock.GetHex()); | ||
result.pushKV("coinbasevalue", | ||
static_cast<int64_t>(pblock->vtx[0]->vout[0].nValue)); | ||
result.pushKV("bits", strprintf("%08x", pblock->nBits)); | ||
result.pushKV("height", static_cast<int64_t>(pindexPrev->nHeight + 1)); | ||
result.pushKV("_target", HexStr(target)); | ||
|
||
return result; | ||
} | ||
|
||
bool | ||
AuxpowMiner::submitAuxBlock(ChainstateManager& chainman, | ||
const uint256 hash, | ||
const std::string& auxpowHex) const | ||
{ | ||
std::shared_ptr<CBlock> shared_block; | ||
{ | ||
LOCK(cs); | ||
const CBlock* pblock = lookupSavedBlock(hash); | ||
shared_block = std::make_shared<CBlock>(*pblock); | ||
} | ||
|
||
const std::vector<unsigned char> vchAuxPow = ParseHex(auxpowHex); | ||
CDataStream ss(vchAuxPow, SER_GETHASH, PROTOCOL_VERSION); | ||
std::unique_ptr<CAuxPow> pow(new CAuxPow()); | ||
ss >> *pow; | ||
shared_block->SetAuxpow(std::move(pow)); | ||
CHECK_NONFATAL(shared_block->GetHash() == hash); | ||
|
||
return chainman.ProcessNewBlock(Params(), shared_block, true, nullptr); | ||
} | ||
|
||
AuxpowMiner& | ||
AuxpowMiner::get() | ||
{ | ||
static AuxpowMiner* instance = nullptr; | ||
static RecursiveMutex lock; | ||
|
||
LOCK(lock); | ||
if (instance == nullptr) | ||
instance = new AuxpowMiner(); | ||
|
||
return *instance; | ||
} |
Oops, something went wrong.