Skip to content

Instantly share code, notes, and snippets.

@cryptozeny
Last active December 27, 2018 11:13
Show Gist options
  • Save cryptozeny/ce52ecdbbc0ac6ab182de1aa8dc05db1 to your computer and use it in GitHub Desktop.
Save cryptozeny/ce52ecdbbc0ac6ab182de1aa8dc05db1 to your computer and use it in GitHub Desktop.
ridiculous pow_tests issue: target is going to zero(0)
// Copyright (c) 2015-2017 The Bitcoin Core developers
// Distributed under the MIT/X11 software license, see the accompanying
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
#include <chain.h>
#include <chainparams.h>
#include <pow.h>
#include <random.h>
#include <util.h>
#include <test/test_bitcoin.h>
#include <boost/test/unit_test.hpp>
static CBlockIndex GetBlockIndex(CBlockIndex *pindexPrev, int64_t nTimeInterval,
uint32_t nBits) {
CBlockIndex block;
block.pprev = pindexPrev;
block.nHeight = pindexPrev->nHeight + 1;
block.nTime = pindexPrev->nTime + nTimeInterval;
block.nBits = nBits;
block.nChainWork = pindexPrev->nChainWork + GetBlockProof(block);
return block;
}
BOOST_FIXTURE_TEST_SUITE(pow_tests, BasicTestingSetup)
BOOST_AUTO_TEST_CASE(ridiculous_test) {
// Copyright (c) 2018 cryptozeny of the Sugarchain Core developers
const auto chainParams = CreateChainParams(CBaseChainParams::MAIN);
const Consensus::Params &mainnetParams = chainParams->GetConsensus();
std::vector<CBlockIndex> blocks(9999);
// Block counter.
int i = 0;
const arith_uint256 powLimit = UintToArith256(chainParams->GetConsensus().powLimit);
uint32_t powLimitBits = powLimit.GetCompact();
/* BEGIN - SetCompact */
// https://en.bitcoin.it/wiki/Difficulty
// https://en.bitcoin.it/wiki/Target
arith_uint256 powLimitFromBits;
bool fNegative;
bool fOverflow;
powLimitFromBits.SetCompact((unsigned)powLimitBits, &fNegative, &fOverflow); // powLimitBits == 0x1f07ffff
/* END - SetCompact */
/* BEGIN - Check nBits */
// arith_uint256 left = UintToArith256(uint256S("0007ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff"));
arith_uint256 left = powLimit;
uint32_t leftBits = left.GetCompact();
// arith_uint256 right = UintToArith256(uint256S("0007ffff00000000000000000000000000000000000000000000000000000000"));
arith_uint256 right = powLimitFromBits;
uint32_t rightBits = right.GetCompact();
powLimitFromBits.SetCompact((unsigned)powLimitBits, &fNegative, &fOverflow); // powLimitBits == 0x1f07ffff
BOOST_CHECK_EQUAL( leftBits, rightBits ); // 0x1f07ffff
/* END - Check nBits */
// Genesis block.
blocks[0] = CBlockIndex();
blocks[0].nHeight = 0;
blocks[0].nTime = 1541009400;
blocks[0].nBits = powLimitBits;
blocks[0].nChainWork = GetBlockProof(blocks[0]);
/* Begin - First Window */
for (i = 1; i < 62; i++) {
blocks[i] = GetBlockIndex(&blocks[i - 1], 15, powLimitBits); // 0x1f07ffff
}
/* End - First Window */
uint32_t nBits = Lwma3CalculateNextWorkRequired(&blocks[61], chainParams->GetConsensus());
/* BEGIN - HUGE ATACK */
printf("*** HUGE ATTACK: Add 1438 blocks: attack: with 0 interval: insanely higher\n");
for ( int j = 0; j < 1438; j++ ) {
blocks[i] = GetBlockIndex(&blocks[i - 1], 0, nBits);
nBits = Lwma3CalculateNextWorkRequired(&blocks[i++], chainParams->GetConsensus());
printf("%-12s %-5d %u / %x\n", "currentBits", i-1, (unsigned)nBits, (unsigned)nBits);
powLimitFromBits.SetCompact((unsigned)nBits, &fNegative, &fOverflow); // powLimitBits == 0x1f07ffff
printf("%-12s %-5d %s\n", "powLimit2", i-1, powLimitFromBits.GetHex().c_str());
}
BOOST_CHECK_EQUAL( nBits, 0xc0deca3 );
// 0xc0deca3 == 202239139 == 00000000000000000000000000000000000000000deca3000000000000000000
printf("*** HUGE ATTACK is finished\n");
/* END - HUGE ATACK */
}
BOOST_AUTO_TEST_SUITE_END()
$ ./src/test/test_bitcoin test_bitcoin --log_level=test_suite --run_test=pow_tests
Running 1 test case...
Entering test suite "Bitcoin Test Suite"
Entering test suite "pow_tests"
Entering test case "ridiculous_test"
*** HUGE ATTACK: Add 1438 blocks: attack: with 0 interval: insanely higher
currentBits 62 520218903 / 1f01e917
powLimit2 62 0001e91700000000000000000000000000000000000000000000000000000000
currentBits 63 520213210 / 1f01d2da
powLimit2 63 0001d2da00000000000000000000000000000000000000000000000000000000
currentBits 64 520207685 / 1f01bd45
powLimit2 64 0001bd4500000000000000000000000000000000000000000000000000000000
currentBits 65 520202328 / 1f01a858
.
.
.
currentBits 1495 34959872 / 2157200
powLimit2 1495 0000000000000000000000000000000000000000000000000000000000001572
currentBits 1496 34491392 / 20e4c00
powLimit2 1496 0000000000000000000000000000000000000000000000000000000000000e4c
currentBits 1497 34022912 / 2072600
powLimit2 1497 0000000000000000000000000000000000000000000000000000000000000726
currentBits 1498 0 / 0
powLimit2 1498 0000000000000000000000000000000000000000000000000000000000000000
currentBits 1499 0 / 0
powLimit2 1499 0000000000000000000000000000000000000000000000000000000000000000
test/pow_tests.cpp(82): error in "ridiculous_test": check nBits == 0xc0deca3 failed [0 != 202239139]
*** HUGE ATTACK is finished
Leaving test case "ridiculous_test"; testing time: 654160mks
Leaving test suite "pow_tests"
Leaving test suite "Bitcoin Test Suite"
*** 1 failure detected in test suite "Bitcoin Test Suite"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment