Changeset View
Changeset View
Standalone View
Standalone View
src/test/utxocommit_tests.cpp
// Copyright (c) 2014-2016 The Bitcoin developers | // Copyright (c) 2014-2016 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. | ||||
// Tests for CUtxoCommit wrapper. | // Tests for CUtxoCommit wrapper. | ||||
// Mostly redundant with libsecp256k1_multiset tests | // Mostly redundant with libsecp256k1_multiset tests | ||||
#include "coins.h" | #include "coins.h" | ||||
#include "test/test_bitcoin.h" | #include "test/test_bitcoin.h" | ||||
#include "testutil.h" | |||||
#include "util.h" | #include "util.h" | ||||
#include <vector> | #include <vector> | ||||
#include <boost/test/unit_test.hpp> | #include <boost/test/unit_test.hpp> | ||||
#include "secp256k1/include/secp256k1_multiset.h" | #include "secp256k1/include/secp256k1_multiset.h" | ||||
#include "utxocommit.h" | #include "utxocommit.h" | ||||
static COutPoint RandomOutpoint() { | static COutPoint RandomOutpoint() { | ||||
const COutPoint op(InsecureRand256(), insecure_rand()); | const COutPoint op(InsecureRand256(), insecure_rand()); | ||||
return op; | return op; | ||||
} | } | ||||
static Coin RandomCoin() { | static Coin RandomCoin() { | ||||
const Coin c(CTxOut(Amount(InsecureRandRange(1000)), | const Coin c(CTxOut(Amount(InsecureRandRange(1000)), | ||||
CScript(InsecureRandBytes(insecure_rand() % 0x3f))), | CScript(InsecureRandBytes(insecure_rand() % 0x3f))), | ||||
insecure_rand(), InsecureRandBool()); | insecure_rand(), InsecureRandBool()); | ||||
return c; | return c; | ||||
} | } | ||||
BOOST_FIXTURE_TEST_SUITE(utxocommit_tests, BasicTestingSetup) | BOOST_FIXTURE_TEST_SUITE(utxocommit_tests, TestingSetup) | ||||
BOOST_AUTO_TEST_CASE(utxo_commit_order) { | BOOST_AUTO_TEST_CASE(utxo_commit_order) { | ||||
// Test order independence | // Test order independence | ||||
const COutPoint op1 = RandomOutpoint(); | const COutPoint op1 = RandomOutpoint(); | ||||
const COutPoint op2 = RandomOutpoint(); | const COutPoint op2 = RandomOutpoint(); | ||||
const COutPoint op3 = RandomOutpoint(); | const COutPoint op3 = RandomOutpoint(); | ||||
▲ Show 20 Lines • Show All 79 Lines • ▼ Show 20 Lines | BOOST_AUTO_TEST_CASE(utxo_commit_serialize) { | ||||
std::vector<uint8_t> expectedhash(32); | std::vector<uint8_t> expectedhash(32); | ||||
secp256k1_multiset_finalize(ctx, expectedhash.data(), &multiset); | secp256k1_multiset_finalize(ctx, expectedhash.data(), &multiset); | ||||
secp256k1_context_destroy(ctx); | secp256k1_context_destroy(ctx); | ||||
BOOST_ASSERT(uint256(expectedhash) == hash); | BOOST_ASSERT(uint256(expectedhash) == hash); | ||||
} | } | ||||
BOOST_AUTO_TEST_CASE(utxo_commit_addcursor) { | |||||
// Test adding a CCoinView Cursor to a CUtxoCommit | |||||
// This simulates the initial upgrade where the commitment stored in | |||||
// LevelDB must be generated from the existing UTXO set. | |||||
const int count = 50000; | |||||
// We use the pcoinviewdb provided by the test fixture's TestingSetup | |||||
CCoinsViewCache cache(pcoinsdbview); | |||||
cache.SetBestBlock(InsecureRand256()); | |||||
// We will compare the commitment generated step-by-step, and the one | |||||
// created | |||||
// from cursor | |||||
CUtxoCommit commit_step, commit_cursor; | |||||
LogPrintf("Preparing database\n"); | |||||
for (int n = 0; n < count; n++) { | |||||
const COutPoint op = RandomOutpoint(); | |||||
const Coin c = RandomCoin(); | |||||
if (c.GetTxOut().scriptPubKey.IsUnspendable()) { | |||||
continue; | |||||
} | |||||
commit_step.Add(op, c); | |||||
cache.AddCoin(op, c, false); | |||||
if ((n + 1) % 5000000 == 0) { | |||||
LogPrintf("Flushing\n"); | |||||
BOOST_ASSERT(cache.Flush()); | |||||
} | |||||
} | |||||
BOOST_ASSERT(cache.Flush()); | |||||
LogPrintf("Starting ECMH generation from cursor\n"); | |||||
std::unique_ptr<CCoinsViewCursor> pcursor(pcoinsdbview->Cursor()); | |||||
commit_cursor.AddCoinView(pcursor.get()); | |||||
BOOST_CHECK(commit_step == commit_cursor); | |||||
LogPrintf("ECMH generation from cursor done\n"); | |||||
} | |||||
BOOST_AUTO_TEST_SUITE_END() | BOOST_AUTO_TEST_SUITE_END() |