|
// 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> |
|
|
|
BOOST_FIXTURE_TEST_SUITE(pow_tests, BasicTestingSetup) |
|
|
|
BOOST_AUTO_TEST_CASE(sugarchain_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(99999); |
|
// Block counter. |
|
int i = 0; |
|
|
|
const arith_uint256 powLimit = UintToArith256(chainParams->GetConsensus().powLimit); |
|
uint32_t powLimitBits = powLimit.GetCompact(); |
|
// arith_uint256 currentPow = powLimit >> 4; |
|
// uint32_t initialBits = currentPow.GetCompact(); |
|
|
|
printf("*** Show mainnetParams\n"); |
|
printf("%-12s %-5ld\n", "T", mainnetParams.nPowTargetSpacing); |
|
printf("%-12s %-5ld\n", "N", mainnetParams.lwmaAveragingWindow); |
|
printf("*** Check Genesis\n"); |
|
printf("%-12s %-5s %s\n", "Parameter", "Block", "Value"); |
|
printf("%-12s %-5d %u / %x\n", "powLimitBits", i, (unsigned)powLimitBits, (unsigned)powLimitBits); |
|
printf("%-12s %-5d %s\n", "powLimit", i, powLimit.ToString().c_str()); // 0x1f07ffff |
|
|
|
/* 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 |
|
printf("%-12s %-5d %s\n", "powLimit2", i, powLimitFromBits.GetHex().c_str()); |
|
/* END - SetCompact */ |
|
|
|
/* BEGIN - Check nBits */ |
|
|
|
/* |
|
MODELL REFERENCE |
|
arith_uint256 left = UintToArith256(uint256S("0007ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff")); |
|
uint32_t leftBits = left.GetCompact(); |
|
arith_uint256 right = UintToArith256(uint256S("0007ffff00000000000000000000000000000000000000000000000000000000")); |
|
uint32_t rightBits = right.GetCompact(); |
|
BOOST_CHECK_EQUAL( leftBits, rightBits ); // 0x1f07ffff |
|
*/ |
|
|
|
// 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(); |
|
printf("*** Check Genesis nBits\n"); |
|
printf("%-12s %-5d %u / %x\n", "nBitsA", i, leftBits, leftBits); |
|
printf("%-12s %-5d %s\n", "powLimitA", i, powLimit.ToString().c_str()); // 0x1f07ffff |
|
printf("%-12s %-5d %u / %x\n", "nBitsB", i, rightBits, rightBits); |
|
powLimitFromBits.SetCompact((unsigned)powLimitBits, &fNegative, &fOverflow); // powLimitBits == 0x1f07ffff |
|
printf("%-12s %-5d %s\n", "powLimitB", i, powLimitFromBits.GetHex().c_str()); |
|
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]); |
|
|
|
// Create the first window for lwma, with blocks every 15 seconds. |
|
// consensus.lwmaAveragingWindow = 200; |
|
// N=200 for T=15: Lwma3CalculateNextWorkRequired |
|
|
|
/* Begin - First Window */ |
|
for (i = 1; i < 201; i++) { |
|
blocks[i] = GetBlockIndex(&blocks[i - 1], 15, powLimitBits); // 0x1f07ffff |
|
} |
|
uint32_t nBits = Lwma3CalculateNextWorkRequired(&blocks[201], chainParams->GetConsensus()); |
|
|
|
// Last block for the first window: still same |
|
blocks[i] = GetBlockIndex(&blocks[i - 1], 15+1, nBits); |
|
nBits = Lwma3CalculateNextWorkRequired(&blocks[i++], chainParams->GetConsensus()); |
|
printf("*** block[201] Last block for the first window: still same\n"); |
|
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()); |
|
printf("*** filling the first window is finished \n"); |
|
BOOST_CHECK_EQUAL( nBits, powLimitBits ); // 0x1f07ffff |
|
// BOOST_CHECK_EQUAL( powLimitBits.c_str(), powLimitFromBits.GetHex().c_str() ); // 0x1f07ffff // FIXME.SUGAR |
|
/* End - First Window */ |
|
|
|
// Add one block: still same |
|
blocks[i] = GetBlockIndex(&blocks[i - 1], 15+1, nBits); |
|
nBits = Lwma3CalculateNextWorkRequired(&blocks[i++], chainParams->GetConsensus()); |
|
printf("*** block[202] Add one block: still same\n"); |
|
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()); |
|
printf("*** \n"); |
|
BOOST_CHECK_EQUAL( nBits, powLimitBits ); // 0x1f07ffff |
|
|
|
// Add one block: a little bit higher |
|
blocks[i] = GetBlockIndex(&blocks[i - 1], 15-1.0000000000000010, nBits); |
|
nBits = Lwma3CalculateNextWorkRequired(&blocks[i++], chainParams->GetConsensus()); |
|
printf("*** block[203] Add one block: a little bit higher\n"); |
|
printf("%-12s %-5d %u / %x\n", "currentBits", i-1, (unsigned)nBits, (unsigned)nBits); |
|
powLimitFromBits.SetCompact((unsigned)nBits, &fNegative, &fOverflow); |
|
printf("%-12s %-5d %s\n", "powLimit2", i-1, powLimitFromBits.GetHex().c_str()); |
|
printf("*** \n"); |
|
BOOST_CHECK_EQUAL( nBits, 0x1f07fff9 ); |
|
// 0x1f07fff9 == 520617977 == 0007fff900000000000000000000000000000000000000000000000000000000 |
|
|
|
// Add one block: a little bit lower: back to powLimit |
|
blocks[i] = GetBlockIndex(&blocks[i - 1], 15*99, nBits); |
|
nBits = Lwma3CalculateNextWorkRequired(&blocks[i++], chainParams->GetConsensus()); |
|
printf("*** block[204] Add one block: a little bit lower: back to powLimit\n"); |
|
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()); |
|
printf("*** \n"); |
|
BOOST_CHECK_EQUAL( nBits, powLimitBits ); |
|
|
|
/* BEGIN - SMALL ATACK */ |
|
// Add 5 blocks: small attack: with 0 interval |
|
printf("*** SMALL ATTACK: Add 5 blocks: attack: with 0 interval: still same\n"); |
|
for ( int j = 0; j < 5; j++ ) { |
|
blocks[i] = GetBlockIndex(&blocks[i - 1], 0, nBits); |
|
nBits = Lwma3CalculateNextWorkRequired(&blocks[i++], chainParams->GetConsensus()); |
|
// printf("*** block[%d] \n", i-1); |
|
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, powLimitBits ); |
|
} |
|
printf("*** \n"); |
|
|
|
// Add one block: higher with normal interval |
|
blocks[i] = GetBlockIndex(&blocks[i - 1], 15, nBits); |
|
nBits = Lwma3CalculateNextWorkRequired(&blocks[i++], chainParams->GetConsensus()); |
|
printf("*** block[210] LAST SMALL ATTACK: Add one block: higher with normal interval\n"); |
|
printf("%-12s %-5d %u / %x\n", "currentBits", i-1, (unsigned)nBits, (unsigned)nBits); |
|
powLimitFromBits.SetCompact((unsigned)nBits, &fNegative, &fOverflow); |
|
printf("%-12s %-5d %s\n", "powLimit2", i-1, powLimitFromBits.GetHex().c_str()); |
|
printf("*** SMALL ATTACK is finished\n"); |
|
BOOST_CHECK_EQUAL( nBits, 0x1f07fe58 ); |
|
// 0x1f07fe58 == 520617560 == 0007fe5800000000000000000000000000000000000000000000000000000000 |
|
/* END - SMALL ATACK */ |
|
|
|
// Add one block: lower: back to powLimit |
|
blocks[i] = GetBlockIndex(&blocks[i - 1], 15*999, nBits); |
|
nBits = Lwma3CalculateNextWorkRequired(&blocks[i++], chainParams->GetConsensus()); |
|
printf("*** block[211] Add one block: lower: back to powLimit\n"); |
|
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()); |
|
printf("*** \n"); |
|
BOOST_CHECK_EQUAL( nBits, powLimitBits ); |
|
|
|
/* BEGIN - MEDIUM ATACK */ |
|
// Add 100 blocks: small attack: with 0 interval |
|
printf("*** MEDIUM ATTACK: Add 100 blocks: attack: with 0 interval: higher\n"); |
|
for ( int j = 0; j < 100; 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, 0x1f021c42 ); |
|
// 0x1f021c42 == 520232002 == 00021c4200000000000000000000000000000000000000000000000000000000 |
|
printf("*** MEDIUM ATTACK is finished\n"); |
|
/* END - SMALL ATACK */ |
|
|
|
/* BEGIN - HUGE ATACK */ |
|
// Add 5000 blocks: small attack: with 0 interval |
|
printf("*** HUGE ATTACK: Add 5000 blocks: attack: with 0 interval: insanely higher\n"); |
|
for ( int j = 0; j < 5000; 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 */ |
|
|
|
/* BEGIN - RESTORE */ |
|
// Add 5000 blocks: with normal interval |
|
printf("*** RESTORE: Add 5000 blocks: with longer interval\n"); |
|
for ( int j = 0; j < 5000; j++ ) { |
|
blocks[i] = GetBlockIndex(&blocks[i - 1], 15*99999999, 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, powLimitBits ); |
|
// 0xc0deca3 == 202239139 == 00000000000000000000000000000000000000000deca3000000000000000000 |
|
printf("*** RESTORE is finished\n"); |
|
/* END - RESTORE */ |
|
} |
|
|
|
BOOST_AUTO_TEST_SUITE_END() |