Beispiel #1
0
//static
CBlock CreateGenesisBlock(const char* pszTimestamp, const CScript& genesisOutputScript, uint32_t nTime, uint32_t nNonce, uint32_t nBits, int32_t nVersion, 
	const CAmount& genesisReward, int nNetworkId)
{
    CMutableTransaction txNew;
    txNew.nVersion = 1;
    txNew.vin.resize(1);
    txNew.vout.resize(1);
    txNew.vin[0].scriptSig = CScript() << 486604799 << CScriptNum(4) << std::vector<unsigned char>((const unsigned char*)pszTimestamp, (const unsigned char*)pszTimestamp + strlen(pszTimestamp));
    txNew.vout[0].nValue = genesisReward;
    txNew.vout[0].scriptPubKey = genesisOutputScript;

    CBlock genesis;
    genesis.nTime    = nTime;
    genesis.nBits    = nBits;
    genesis.nVersion = nVersion;
    genesis.vtx.push_back(txNew);
    genesis.hashPrevBlock.SetNull();
    genesis.hashMerkleRoot = BlockMerkleRoot(genesis);
	genesis.sGRCAddress = "";

	for (int i = nNonce; i < 99999999; i++)
	{
		genesis.nNonce = i;
        arith_uint256 hashTarget = arith_uint256().SetCompact(genesis.nBits);
		if (UintToArith256(genesis.GetHash()) <= hashTarget)  break;
	}

	printf("NetworkID %f, Nonce %f, Genesis hash %s , MerkleRoot %s \n",(double)nNetworkId,(double)genesis.nNonce,genesis.GetHash().GetHex().c_str(),
		genesis.hashMerkleRoot.GetHex().c_str());
    
	return genesis;
}
std::shared_ptr<CBlock> FinalizeBlock(std::shared_ptr<CBlock> pblock)
{
    pblock->hashMerkleRoot = BlockMerkleRoot(*pblock);

    while (!CheckProofOfWork(pblock->GetPoWHash(), pblock->nBits, Params().GetConsensus())) {
        ++(pblock->nNonce);
    }

    return pblock;
}
Beispiel #3
0
void
CAuxpowBuilder::setCoinbase(const CScript& scr)
{
	CMutableTransaction mtx;
	mtx.vin.resize(1);
	mtx.vin[0].prevout.SetNull();
	mtx.vin[0].scriptSig = scr;

	parentBlock.vtx.clear();
	parentBlock.vtx.push_back(MakeTransactionRef(std::move(mtx)));
	parentBlock.hashMerkleRoot = BlockMerkleRoot(parentBlock);
}
static std::shared_ptr<CBlock> PrepareBlock(const CScript& coinbase_scriptPubKey)
{
    auto block = std::make_shared<CBlock>(
        BlockAssembler{Params()}
            .CreateNewBlock(coinbase_scriptPubKey, /* fMineWitnessTx */ true)
            ->block);

    block->nTime = ::chainActive.Tip()->GetMedianTimePast() + 1;
    block->hashMerkleRoot = BlockMerkleRoot(*block);

    return block;
}
Beispiel #5
0
static void DuplicateInputs(benchmark::State& state)
{
    const CScript SCRIPT_PUB{CScript(OP_TRUE)};

    const CChainParams& chainparams = Params();

    CBlock block{};
    CMutableTransaction coinbaseTx{};
    CMutableTransaction naughtyTx{};

    CBlockIndex* pindexPrev = ::ChainActive().Tip();
    assert(pindexPrev != nullptr);
    block.nBits = GetNextWorkRequired(pindexPrev, &block, chainparams.GetConsensus());
    block.nNonce = 0;
    auto nHeight = pindexPrev->nHeight + 1;

    // Make a coinbase TX
    coinbaseTx.vin.resize(1);
    coinbaseTx.vin[0].prevout.SetNull();
    coinbaseTx.vout.resize(1);
    coinbaseTx.vout[0].scriptPubKey = SCRIPT_PUB;
    coinbaseTx.vout[0].nValue = GetBlockSubsidy(nHeight, chainparams.GetConsensus());
    coinbaseTx.vin[0].scriptSig = CScript() << nHeight << OP_0;


    naughtyTx.vout.resize(1);
    naughtyTx.vout[0].nValue = 0;
    naughtyTx.vout[0].scriptPubKey = SCRIPT_PUB;

    uint64_t n_inputs = (((MAX_BLOCK_SERIALIZED_SIZE / WITNESS_SCALE_FACTOR) - (CTransaction(coinbaseTx).GetTotalSize() + CTransaction(naughtyTx).GetTotalSize())) / 41) - 100;
    for (uint64_t x = 0; x < (n_inputs - 1); ++x) {
        naughtyTx.vin.emplace_back(GetRandHash(), 0, CScript(), 0);
    }
    naughtyTx.vin.emplace_back(naughtyTx.vin.back());

    block.vtx.push_back(MakeTransactionRef(std::move(coinbaseTx)));
    block.vtx.push_back(MakeTransactionRef(std::move(naughtyTx)));

    block.hashMerkleRoot = BlockMerkleRoot(block);

    while (state.KeepRunning()) {
        CValidationState cvstate{};
        assert(!CheckBlock(block, cvstate, chainparams.GetConsensus(), false, false));
        assert(cvstate.GetRejectReason() == "bad-txns-inputs-duplicate");
    }
}
Beispiel #6
0
void IncrementExtraNonce(CBlock* pblock, const CBlockIndex* pindexPrev, unsigned int& nExtraNonce)
{
    // Update nExtraNonce
    static uint256 hashPrevBlock;
    if (hashPrevBlock != pblock->hashPrevBlock)
    {
        nExtraNonce = 0;
        hashPrevBlock = pblock->hashPrevBlock;
    }
    ++nExtraNonce;
    unsigned int nHeight = pindexPrev->nHeight+1; // Height first in coinbase required for block.version=2
    CMutableTransaction txCoinbase(*pblock->vtx[0]);
    txCoinbase.vin[0].scriptSig = (CScript() << nHeight << CScriptNum(nExtraNonce)) + COINBASE_FLAGS;
    assert(txCoinbase.vin[0].scriptSig.size() <= 100);

    pblock->vtx[0] = MakeTransactionRef(std::move(txCoinbase));
    pblock->hashMerkleRoot = BlockMerkleRoot(*pblock);
}
Beispiel #7
0
static CBlock CreateGenesisBlock(const char* pszTimestamp, const CScript& genesisOutputScript, uint32_t nTime, uint32_t nNonce, uint32_t nBits, int32_t nVersion, const CAmount& genesisReward)
{
    CMutableTransaction txNew;
    txNew.nVersion = 1;
    txNew.vin.resize(1);
    txNew.vout.resize(1);
    txNew.vin[0].scriptSig = CScript() << 486604799 << CScriptNum(4) << std::vector<unsigned char>((const unsigned char*)pszTimestamp, (const unsigned char*)pszTimestamp + strlen(pszTimestamp));
    txNew.vout[0].nValue = genesisReward;
    txNew.vout[0].scriptPubKey = genesisOutputScript;

    CBlock genesis;
    genesis.nTime    = nTime;
    genesis.nBits    = nBits;
    genesis.nNonce   = nNonce;
    genesis.nVersion = nVersion;
    genesis.vtx.push_back(MakeTransactionRef(std::move(txNew)));
    genesis.hashPrevBlock.SetNull();
    genesis.hashMerkleRoot = BlockMerkleRoot(genesis);
    return genesis;
}
Beispiel #8
0
static CBlock CreateDevNetGenesisBlock(const uint256 &prevBlockHash, const std::string& devNetName, uint32_t nTime, uint32_t nNonce, uint32_t nBits, const CAmount& genesisReward)
{
    assert(!devNetName.empty());

    CMutableTransaction txNew;
    txNew.nVersion = 1;
    txNew.vin.resize(1);
    txNew.vout.resize(1);
    // put height (BIP34) and devnet name into coinbase
    txNew.vin[0].scriptSig = CScript() << 1 << std::vector<unsigned char>(devNetName.begin(), devNetName.end());
    txNew.vout[0].nValue = genesisReward;
    txNew.vout[0].scriptPubKey = CScript() << OP_RETURN;

    CBlock genesis;
    genesis.nTime    = nTime;
    genesis.nBits    = nBits;
    genesis.nNonce   = nNonce;
    genesis.nVersion = 4;
    genesis.vtx.push_back(MakeTransactionRef(std::move(txNew)));
    genesis.hashPrevBlock = prevBlockHash;
    genesis.hashMerkleRoot = BlockMerkleRoot(genesis);
    return genesis;
}
Beispiel #9
0
void
CAuxPow::initAuxPow (CBlockHeader& header)
{
  /* Set auxpow flag right now, since we take the block hash below.  */
  header.SetAuxpowVersion(true);

  /* Build a minimal coinbase script input for merge-mining.  */
  const uint256 blockHash = header.GetHash ();
  valtype inputData(blockHash.begin (), blockHash.end ());
  std::reverse (inputData.begin (), inputData.end ());
  inputData.push_back (1);
  inputData.insert (inputData.end (), 7, 0);

  /* Fake a parent-block coinbase with just the required input
     script and no outputs.  */
  CMutableTransaction coinbase;
  coinbase.vin.resize (1);
  coinbase.vin[0].prevout.SetNull ();
  coinbase.vin[0].scriptSig = (CScript () << inputData);
  assert (coinbase.vout.empty ());
  CTransactionRef coinbaseRef = MakeTransactionRef (coinbase);

  /* Build a fake parent block with the coinbase.  */
  CBlock parent;
  parent.nVersion = 1;
  parent.vtx.resize (1);
  parent.vtx[0] = coinbaseRef;
  parent.hashMerkleRoot = BlockMerkleRoot (parent);

  /* Construct the auxpow object.  */
  header.SetAuxpow (new CAuxPow (coinbaseRef));
  assert (header.auxpow->vChainMerkleBranch.empty ());
  header.auxpow->nChainIndex = 0;
  assert (header.auxpow->vMerkleBranch.empty ());
  header.auxpow->nIndex = 0;
  header.auxpow->parentBlock = parent;
}
int main(int argc, char **argv) {
    ECCVerifyHandle globalVerifyHandle;
    std::vector<char> buffer;
    if (!read_stdin(buffer)) return 0;

    if (buffer.size() < sizeof(uint32_t)) return 0;

    uint32_t test_id = 0xffffffff;
    memcpy(&test_id, &buffer[0], sizeof(uint32_t));
    buffer.erase(buffer.begin(), buffer.begin() + sizeof(uint32_t));

    if (test_id >= TEST_ID_END) return 0;

    CDataStream ds(buffer, SER_NETWORK, INIT_PROTO_VERSION);
    try {
        int nVersion;
        ds >> nVersion;
        ds.SetVersion(nVersion);
    } catch (const std::ios_base::failure &e) {
        return 0;
    }

    switch (test_id) {
        case CBLOCK_DESERIALIZE: {
            try {
                CBlock block;
                ds >> block;
            } catch (const std::ios_base::failure &e) {
                return 0;
            }
            break;
        }
        case CTRANSACTION_DESERIALIZE: {
            try {
                CTransaction tx(deserialize, ds);
            } catch (const std::ios_base::failure &e) {
                return 0;
            }
            break;
        }
        case CBLOCKLOCATOR_DESERIALIZE: {
            try {
                CBlockLocator bl;
                ds >> bl;
            } catch (const std::ios_base::failure &e) {
                return 0;
            }
            break;
        }
        case CBLOCKMERKLEROOT: {
            try {
                CBlock block;
                ds >> block;
                bool mutated;
                BlockMerkleRoot(block, &mutated);
            } catch (const std::ios_base::failure &e) {
                return 0;
            }
            break;
        }
        case CADDRMAN_DESERIALIZE: {
            try {
                CAddrMan am;
                ds >> am;
            } catch (const std::ios_base::failure &e) {
                return 0;
            }
            break;
        }
        case CBLOCKHEADER_DESERIALIZE: {
            try {
                CBlockHeader bh;
                ds >> bh;
            } catch (const std::ios_base::failure &e) {
                return 0;
            }
            break;
        }
        case CBANENTRY_DESERIALIZE: {
            try {
                CBanEntry be;
                ds >> be;
            } catch (const std::ios_base::failure &e) {
                return 0;
            }
            break;
        }
        case CTXUNDO_DESERIALIZE: {
            try {
                CTxUndo tu;
                ds >> tu;
            } catch (const std::ios_base::failure &e) {
                return 0;
            }
            break;
        }
        case CBLOCKUNDO_DESERIALIZE: {
            try {
                CBlockUndo bu;
                ds >> bu;
            } catch (const std::ios_base::failure &e) {
                return 0;
            }
            break;
        }
        case COIN_DESERIALIZE: {
            try {
                Coin coin;
                ds >> coin;
            } catch (const std::ios_base::failure &e) {
                return 0;
            }
            break;
        }
        case CNETADDR_DESERIALIZE: {
            try {
                CNetAddr na;
                ds >> na;
            } catch (const std::ios_base::failure &e) {
                return 0;
            }
            break;
        }
        case CSERVICE_DESERIALIZE: {
            try {
                CService s;
                ds >> s;
            } catch (const std::ios_base::failure &e) {
                return 0;
            }
            break;
        }
        case CMESSAGEHEADER_DESERIALIZE: {
            CMessageHeader::MessageMagic pchMessageStart = {0x00, 0x00, 0x00,
                                                            0x00};
            try {
                CMessageHeader mh(pchMessageStart);
                ds >> mh;
                if (!mh.IsValidWithoutConfig(pchMessageStart)) {
                    return 0;
                }
            } catch (const std::ios_base::failure &e) {
                return 0;
            }
            break;
        }
        case CADDRESS_DESERIALIZE: {
            try {
                CAddress a;
                ds >> a;
            } catch (const std::ios_base::failure &e) {
                return 0;
            }
            break;
        }
        case CINV_DESERIALIZE: {
            try {
                CInv i;
                ds >> i;
            } catch (const std::ios_base::failure &e) {
                return 0;
            }
            break;
        }
        case CBLOOMFILTER_DESERIALIZE: {
            try {
                CBloomFilter bf;
                ds >> bf;
            } catch (const std::ios_base::failure &e) {
                return 0;
            }
            break;
        }
        case CDISKBLOCKINDEX_DESERIALIZE: {
            try {
                CDiskBlockIndex dbi;
                ds >> dbi;
            } catch (const std::ios_base::failure &e) {
                return 0;
            }
            break;
        }
        case CTXOUTCOMPRESSOR_DESERIALIZE: {
            CTxOut to;
            CTxOutCompressor toc(to);
            try {
                ds >> toc;
            } catch (const std::ios_base::failure &e) {
                return 0;
            }

            break;
        }
        default:
            return 0;
    }
    return 0;
}
void test_one_input(std::vector<uint8_t> buffer)
{
    CDataStream ds(buffer, SER_NETWORK, INIT_PROTO_VERSION);
    try {
        int nVersion;
        ds >> nVersion;
        ds.SetVersion(nVersion);
    } catch (const std::ios_base::failure& e) {
        return;
    }

#if BLOCK_DESERIALIZE
            try
            {
                CBlock block;
                ds >> block;
            } catch (const std::ios_base::failure& e) {return;}
#elif TRANSACTION_DESERIALIZE
            try
            {
                CTransaction tx(deserialize, ds);
            } catch (const std::ios_base::failure& e) {return;}
#elif BLOCKLOCATOR_DESERIALIZE
            try
            {
                CBlockLocator bl;
                ds >> bl;
            } catch (const std::ios_base::failure& e) {return;}
#elif BLOCKMERKLEROOT
            try
            {
                CBlock block;
                ds >> block;
                bool mutated;
                BlockMerkleRoot(block, &mutated);
            } catch (const std::ios_base::failure& e) {return;}
#elif ADDRMAN_DESERIALIZE
            try
            {
                CAddrMan am;
                ds >> am;
            } catch (const std::ios_base::failure& e) {return;}
#elif BLOCKHEADER_DESERIALIZE
            try
            {
                CBlockHeader bh;
                ds >> bh;
            } catch (const std::ios_base::failure& e) {return;}
#elif BANENTRY_DESERIALIZE
            try
            {
                CBanEntry be;
                ds >> be;
            } catch (const std::ios_base::failure& e) {return;}
#elif TXUNDO_DESERIALIZE
            try
            {
                CTxUndo tu;
                ds >> tu;
            } catch (const std::ios_base::failure& e) {return;}
#elif BLOCKUNDO_DESERIALIZE
            try
            {
                CBlockUndo bu;
                ds >> bu;
            } catch (const std::ios_base::failure& e) {return;}
#elif COINS_DESERIALIZE
            try
            {
                Coin coin;
                ds >> coin;
            } catch (const std::ios_base::failure& e) {return;}
#elif NETADDR_DESERIALIZE
            try
            {
                CNetAddr na;
                ds >> na;
            } catch (const std::ios_base::failure& e) {return;}
#elif SERVICE_DESERIALIZE
            try
            {
                CService s;
                ds >> s;
            } catch (const std::ios_base::failure& e) {return;}
#elif MESSAGEHEADER_DESERIALIZE
            CMessageHeader::MessageStartChars pchMessageStart = {0x00, 0x00, 0x00, 0x00};
            try
            {
                CMessageHeader mh(pchMessageStart);
                ds >> mh;
                if (!mh.IsValid(pchMessageStart)) {return;}
            } catch (const std::ios_base::failure& e) {return;}
#elif ADDRESS_DESERIALIZE
            try
            {
                CAddress a;
                ds >> a;
            } catch (const std::ios_base::failure& e) {return;}
#elif INV_DESERIALIZE
            try
            {
                CInv i;
                ds >> i;
            } catch (const std::ios_base::failure& e) {return;}
#elif BLOOMFILTER_DESERIALIZE
            try
            {
                CBloomFilter bf;
                ds >> bf;
            } catch (const std::ios_base::failure& e) {return;}
#elif DISKBLOCKINDEX_DESERIALIZE
            try
            {
                CDiskBlockIndex dbi;
                ds >> dbi;
            } catch (const std::ios_base::failure& e) {return;}
#elif TXOUTCOMPRESSOR_DESERIALIZE
            CTxOut to;
            CTxOutCompressor toc(to);
            try
            {
                ds >> toc;
            } catch (const std::ios_base::failure& e) {return;}
#elif BLOCKTRANSACTIONS_DESERIALIZE
            try
            {
                BlockTransactions bt;
                ds >> bt;
            } catch (const std::ios_base::failure& e) {return;}
#elif BLOCKTRANSACTIONSREQUEST_DESERIALIZE
            try
            {
                BlockTransactionsRequest btr;
                ds >> btr;
            } catch (const std::ios_base::failure& e) {return;}
#else
#error Need at least one fuzz target to compile
#endif
}