// Copyright (c) 2015 The Bitcoin Core developers
// Copyright (c) Flo Developers 2013-2018
// 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)

/* Test calculation of next difficulty target with no constraints applying */
BOOST_AUTO_TEST_CASE(get_next_work)
{
    const auto chainParams = CreateChainParams(CBaseChainParams::MAIN);
//    int64_t nLastRetargetTime = 1358118740; // Block #278207
//    CBlockIndex pindexLast;
//    pindexLast.nHeight = 280223;
//    pindexLast.nTime = 1358378777;  // Block #280223
//    pindexLast.nBits =  0x1c0ac141;
    int64_t nLastRetargetTime = 1358118740; // Block #20000
    CBlockIndex pindexLast;
    pindexLast.nHeight = 21000;
    pindexLast.nTime = 1372362352;  // Block #21000
    pindexLast.nBits =  0x1d078ab6;
    BOOST_CHECK_EQUAL(CalculateNextWorkRequired(&pindexLast, nLastRetargetTime, chainParams->GetConsensus()), 0x1D1E2AD8);
}

/* Test the constraint on the upper bound for next work */
BOOST_AUTO_TEST_CASE(get_next_work_pow_limit)
{
    const auto chainParams = CreateChainParams(CBaseChainParams::MAIN);
//    int64_t nLastRetargetTime = 1317972665; // Block #0
//    CBlockIndex pindexLast;
//    pindexLast.nHeight = 2015;
//    pindexLast.nTime = 1318480354;  // Block #2015
//    pindexLast.nBits = 0x1e0ffff0;
    int64_t nLastRetargetTime = 1371488396; // Block #0
    CBlockIndex pindexLast;
    pindexLast.nHeight = 2015;
    pindexLast.nTime = 1371563039;  // Block #2015
    pindexLast.nBits = 0x1d02686e;
    BOOST_CHECK_EQUAL(CalculateNextWorkRequired(&pindexLast, nLastRetargetTime, chainParams->GetConsensus()), 0x1D09A1B8);
}

/* Test the constraint on the lower bound for actual time taken */
BOOST_AUTO_TEST_CASE(get_next_work_lower_limit_actual)
{
    const auto chainParams = CreateChainParams(CBaseChainParams::MAIN);
    int64_t nLastRetargetTime = 1405978436; // Block #600000
    CBlockIndex pindexLast;
    pindexLast.nHeight = 600030;
    pindexLast.nTime = 1405979702;  // Block #600030
    pindexLast.nBits = 0x1d00a23e;
    BOOST_CHECK_EQUAL(CalculateNextWorkRequired(&pindexLast, nLastRetargetTime, chainParams->GetConsensus()), 0x1D00A6F9);
}

/* Test the constraint on the upper bound for actual time taken */
BOOST_AUTO_TEST_CASE(get_next_work_upper_limit_actual)
{
    const auto chainParams = CreateChainParams(CBaseChainParams::MAIN);
    int64_t nLastRetargetTime = 1342177393; // NOTE: Not an actual block time
    CBlockIndex pindexLast;
    pindexLast.nHeight = 102000;
    pindexLast.nTime = 1375677393;  // Block #102000
    pindexLast.nBits = 0x1d042d4e;
    BOOST_CHECK_EQUAL(CalculateNextWorkRequired(&pindexLast, nLastRetargetTime, chainParams->GetConsensus()), 0x1D10B538);
}

BOOST_AUTO_TEST_CASE(GetBlockProofEquivalentTime_test)
{
    const auto chainParams = CreateChainParams(CBaseChainParams::MAIN);
    std::vector<CBlockIndex> blocks(10000);
    for (int i = 0; i < 10000; i++) {
        blocks[i].pprev = i ? &blocks[i - 1] : nullptr;
        blocks[i].nHeight = i;
        blocks[i].nTime = 1269211443 + i * chainParams->GetConsensus().nPowTargetSpacing;
        blocks[i].nBits = 0x207fffff; /* target 0x7fffff000... */
        blocks[i].nChainWork = i ? blocks[i - 1].nChainWork + GetBlockProof(blocks[i - 1]) : arith_uint256(0);
    }

    for (int j = 0; j < 1000; j++) {
        CBlockIndex *p1 = &blocks[InsecureRandRange(10000)];
        CBlockIndex *p2 = &blocks[InsecureRandRange(10000)];
        CBlockIndex *p3 = &blocks[InsecureRandRange(10000)];

        int64_t tdiff = GetBlockProofEquivalentTime(*p1, *p2, *p3, chainParams->GetConsensus());
        BOOST_CHECK_EQUAL(tdiff, p1->GetBlockTime() - p2->GetBlockTime());
    }
}

BOOST_AUTO_TEST_SUITE_END()
