Changeset View
Changeset View
Standalone View
Standalone View
src/avalanche/test/proof_tests.cpp
// Copyright (c) 2020 The Bitcoin developers | // Copyright (c) 2020 The Bitcoin developers | ||||
// Distributed under the MIT software license, see the accompanying | // Distributed under the MIT software license, see the accompanying | ||||
// file COPYING or http://www.opensource.org/licenses/mit-license.php. | // file COPYING or http://www.opensource.org/licenses/mit-license.php. | ||||
#include <avalanche/proof.h> | #include <avalanche/proof.h> | ||||
#include <avalanche/proofbuilder.h> | #include <avalanche/proofbuilder.h> | ||||
#include <avalanche/test/util.h> | #include <avalanche/test/util.h> | ||||
#include <avalanche/validation.h> | #include <avalanche/validation.h> | ||||
#include <coins.h> | #include <coins.h> | ||||
#include <script/standard.h> | #include <script/standard.h> | ||||
#include <util/strencodings.h> | #include <util/strencodings.h> | ||||
#include <util/translation.h> | #include <util/translation.h> | ||||
#include <validation.h> | |||||
#include <test/util/setup_common.h> | #include <test/util/setup_common.h> | ||||
#include <boost/test/unit_test.hpp> | #include <boost/test/unit_test.hpp> | ||||
using namespace avalanche; | using namespace avalanche; | ||||
BOOST_FIXTURE_TEST_SUITE(proof_tests, TestingSetup) | BOOST_FIXTURE_TEST_SUITE(proof_tests, TestChain100Setup) | ||||
BOOST_AUTO_TEST_CASE(proof_random) { | BOOST_AUTO_TEST_CASE(proof_random) { | ||||
for (int i = 0; i < 1000; i++) { | for (int i = 0; i < 1000; i++) { | ||||
const uint32_t score = InsecureRand32(); | const uint32_t score = InsecureRand32(); | ||||
auto p = buildRandomProof(score); | auto p = buildRandomProof(score); | ||||
BOOST_CHECK_EQUAL(p->getScore(), score); | BOOST_CHECK_EQUAL(p->getScore(), score); | ||||
ProofValidationResult expected_state = | ProofValidationResult expected_state = | ||||
▲ Show 20 Lines • Show All 852 Lines • ▼ Show 20 Lines | BOOST_AUTO_TEST_CASE(deserialization) { | ||||
gArgs.ForceSetArg("-legacyavaproof", "0"); | gArgs.ForceSetArg("-legacyavaproof", "0"); | ||||
BOOST_CHECK(!Proof::useLegacy(gArgs)); | BOOST_CHECK(!Proof::useLegacy(gArgs)); | ||||
checkCases(regularFormatTestCases); | checkCases(regularFormatTestCases); | ||||
gArgs.ClearForcedArg("-legacyavaproof"); | gArgs.ClearForcedArg("-legacyavaproof"); | ||||
} | } | ||||
BOOST_AUTO_TEST_CASE(verify) { | BOOST_AUTO_TEST_CASE(verify) { | ||||
gArgs.ForceSetArg("-avaproofstakeutxoconfirmations", "1"); | |||||
CCoinsView coinsDummy; | CCoinsView coinsDummy; | ||||
CCoinsViewCache coins(&coinsDummy); | CCoinsViewCache coins(&coinsDummy); | ||||
auto key = CKey::MakeCompressedKey(); | auto key = CKey::MakeCompressedKey(); | ||||
const CPubKey pubkey = key.GetPubKey(); | const CPubKey pubkey = key.GetPubKey(); | ||||
const Amount value = 12345 * COIN; | const Amount value = 12345 * COIN; | ||||
const uint32_t height = 10; | const uint32_t height = 10; | ||||
▲ Show 20 Lines • Show All 121 Lines • ▼ Show 20 Lines | // Wrong stake ordering | ||||
BOOST_CHECK(pb.addUTXO(other_pkh_outpoint, value, height, false, key)); | BOOST_CHECK(pb.addUTXO(other_pkh_outpoint, value, height, false, key)); | ||||
ProofRef p = TestProofBuilder::buildWithReversedOrderStakes(pb); | ProofRef p = TestProofBuilder::buildWithReversedOrderStakes(pb); | ||||
ProofValidationState state; | ProofValidationState state; | ||||
BOOST_CHECK(!p->verify(state, coins)); | BOOST_CHECK(!p->verify(state, coins)); | ||||
BOOST_CHECK(state.GetResult() == | BOOST_CHECK(state.GetResult() == | ||||
ProofValidationResult::WRONG_STAKE_ORDERING); | ProofValidationResult::WRONG_STAKE_ORDERING); | ||||
} | } | ||||
// Immature stake | |||||
{ | |||||
uint32_t chaintipHeight = ::ChainActive().Height(); | |||||
// A proof where the UTXO is both missing and immature gives | |||||
// MISSING_UTXO | |||||
{ | |||||
gArgs.ForceSetArg("-avaproofstakeutxoconfirmations", "11"); | |||||
for (auto h = chaintipHeight; h > chaintipHeight - 10; h--) { | |||||
COutPoint outpoint(TxId(InsecureRand256()), InsecureRand32()); | |||||
CTxOut output(value, GetScriptForRawPubKey(pubkey)); | |||||
runCheck(ProofValidationResult::MISSING_UTXO, outpoint, value, | |||||
h, false, key); | |||||
// Add the coin to the UTXO set to verify it's immature | |||||
coins.AddCoin(outpoint, Coin(output, h, false), false); | |||||
runCheck(ProofValidationResult::IMMATURE_UTXO, outpoint, value, | |||||
h, false, key); | |||||
} | |||||
} | |||||
// tuple<stake utxo confs, avaproofstakeutxoconfirmations, result> | |||||
std::vector<std::tuple<uint32_t, std::string, ProofValidationResult>> | |||||
testCases = { | |||||
// Require less or equal the number of confirmations the stake | |||||
// has | |||||
{chaintipHeight, "1", ProofValidationResult::NONE}, | |||||
{50, "1", ProofValidationResult::NONE}, | |||||
{50, "50", ProofValidationResult::NONE}, | |||||
{50, "51", ProofValidationResult::NONE}, | |||||
// Require more than the number of confirmations the stake has | |||||
{chaintipHeight, "2", ProofValidationResult::IMMATURE_UTXO}, | |||||
{50, "52", ProofValidationResult::IMMATURE_UTXO}, | |||||
{50, "100", ProofValidationResult::IMMATURE_UTXO}, | |||||
}; | |||||
for (auto it = testCases.begin(); it != testCases.end(); ++it) { | |||||
const uint32_t stakeConfs = std::get<0>(*it); | |||||
COutPoint outpoint(TxId(InsecureRand256()), InsecureRand32()); | |||||
CTxOut output(value, GetScriptForRawPubKey(pubkey)); | |||||
coins.AddCoin(outpoint, Coin(output, stakeConfs, false), false); | |||||
gArgs.ForceSetArg("-avaproofstakeutxoconfirmations", | |||||
std::get<1>(*it)); | |||||
runCheck(std::get<2>(*it), outpoint, value, stakeConfs, false, key); | |||||
} | |||||
gArgs.ClearForcedArg("-avaproofstakeutxoconfirmations"); | |||||
} | |||||
} | } | ||||
BOOST_AUTO_TEST_CASE(deterministic_proofid) { | BOOST_AUTO_TEST_CASE(deterministic_proofid) { | ||||
CCoinsView coinsDummy; | CCoinsView coinsDummy; | ||||
CCoinsViewCache coins(&coinsDummy); | CCoinsViewCache coins(&coinsDummy); | ||||
auto key = CKey::MakeCompressedKey(); | auto key = CKey::MakeCompressedKey(); | ||||
▲ Show 20 Lines • Show All 44 Lines • Show Last 20 Lines |