2014-03-18 02:11:00 -07:00
|
|
|
// Copyright (c) 2011-2013 The Bitcoin Core developers
|
2023-01-23 10:31:54 -08:00
|
|
|
// Copyright (c) 2016-2023 The Zcash developers
|
2014-12-12 20:09:33 -08:00
|
|
|
// Distributed under the MIT software license, see the accompanying
|
2019-07-18 07:16:09 -07:00
|
|
|
// file COPYING or https://www.opensource.org/licenses/mit-license.php .
|
2014-03-18 02:11:00 -07:00
|
|
|
|
2011-08-02 08:34:23 -07:00
|
|
|
#define BOOST_TEST_MODULE Bitcoin Test Suite
|
2013-04-12 22:13:08 -07:00
|
|
|
|
2015-03-03 07:49:12 -08:00
|
|
|
#include "test_bitcoin.h"
|
|
|
|
|
2015-03-03 06:59:32 -08:00
|
|
|
#include "chainparams.h"
|
|
|
|
#include "consensus/consensus.h"
|
|
|
|
#include "consensus/validation.h"
|
2017-05-19 05:20:03 -07:00
|
|
|
#ifdef ENABLE_MINING
|
|
|
|
#include "crypto/equihash.h"
|
|
|
|
#endif
|
2017-07-13 23:26:04 -07:00
|
|
|
#include "crypto/sha256.h"
|
2017-03-01 07:54:22 -08:00
|
|
|
#include "fs.h"
|
Update key.cpp to use new libsecp256k1
libsecp256k1's API changed, so update key.cpp to use it.
Libsecp256k1 now has explicit context objects, which makes it completely thread-safe.
In turn, keep an explicit context object in key.cpp, which is explicitly initialized
destroyed. This is not really pretty now, but it's more efficient than the static
initialized object in key.cpp (which made for example bitcoin-tx slow, as for most of
its calls, libsecp256k1 wasn't actually needed).
This also brings in the new blinding support in libsecp256k1. By passing in a random
seed, temporary variables during the elliptic curve computations are altered, in such
a way that if an attacker does not know the blind, observing the internal operations
leaks less information about the keys used. This was implemented by Greg Maxwell.
2015-04-22 14:28:26 -07:00
|
|
|
#include "key.h"
|
2011-10-11 16:50:06 -07:00
|
|
|
#include "main.h"
|
2015-03-03 06:59:32 -08:00
|
|
|
#include "miner.h"
|
|
|
|
#include "pubkey.h"
|
2014-06-26 05:41:53 -07:00
|
|
|
#include "random.h"
|
2013-04-12 22:13:08 -07:00
|
|
|
#include "txdb.h"
|
2015-11-14 14:04:15 -08:00
|
|
|
#include "txmempool.h"
|
2013-04-12 22:13:08 -07:00
|
|
|
#include "ui_interface.h"
|
2016-03-29 10:43:02 -07:00
|
|
|
#include "rpc/server.h"
|
|
|
|
#include "rpc/register.h"
|
2016-10-05 13:58:47 -07:00
|
|
|
#include "script/sigcache.h"
|
2013-04-12 22:13:08 -07:00
|
|
|
|
|
|
|
#include <boost/test/unit_test.hpp>
|
Split up util.cpp/h
Split up util.cpp/h into:
- string utilities (hex, base32, base64): no internal dependencies, no dependency on boost (apart from foreach)
- money utilities (parsesmoney, formatmoney)
- time utilities (gettime*, sleep, format date):
- and the rest (logging, argument parsing, config file parsing)
The latter is basically the environment and OS handling,
and is stripped of all utility functions, so we may want to
rename it to something else than util.cpp/h for clarity (Matt suggested
osinterface).
Breaks dependency of sha256.cpp on all the things pulled in by util.
2014-08-21 07:11:09 -07:00
|
|
|
#include <boost/thread.hpp>
|
2020-12-17 03:31:40 -08:00
|
|
|
#include <sodium.h>
|
2022-03-28 12:35:59 -07:00
|
|
|
#include <tracing.h>
|
2013-04-12 22:13:08 -07:00
|
|
|
|
2018-04-17 13:44:53 -07:00
|
|
|
#include "librustzcash.h"
|
|
|
|
|
2023-01-23 17:07:29 -08:00
|
|
|
#include <rust/bridge.h>
|
2022-07-12 21:13:05 -07:00
|
|
|
|
2018-08-13 13:13:29 -07:00
|
|
|
const std::function<std::string(const char*)> G_TRANSLATION_FUN = nullptr;
|
|
|
|
|
2015-04-16 07:20:01 -07:00
|
|
|
CClientUIInterface uiInterface; // Declared but not defined in ui_interface.h
|
2020-07-10 17:10:34 -07:00
|
|
|
|
|
|
|
TracingHandle* pTracingHandle = nullptr;
|
2017-02-15 17:45:22 -08:00
|
|
|
uint256 insecure_rand_seed = GetRandHash();
|
|
|
|
FastRandomContext insecure_rand_ctx(insecure_rand_seed);
|
2012-01-04 18:40:52 -08:00
|
|
|
|
2011-10-03 13:14:13 -07:00
|
|
|
extern bool fPrintToConsole;
|
2012-05-19 00:35:26 -07:00
|
|
|
extern void noui_connect();
|
|
|
|
|
2015-06-30 12:39:49 -07:00
|
|
|
BasicTestingSetup::BasicTestingSetup(const std::string& chainName)
|
2015-03-03 07:49:12 -08:00
|
|
|
{
|
2020-12-17 03:31:40 -08:00
|
|
|
assert(sodium_init() != -1);
|
2017-07-13 23:26:04 -07:00
|
|
|
SHA256AutoDetect();
|
2017-04-07 09:24:53 -07:00
|
|
|
ECC_Start();
|
|
|
|
SetupEnvironment();
|
2018-11-13 10:05:57 -08:00
|
|
|
SetupNetworking();
|
2022-07-12 21:13:05 -07:00
|
|
|
InitSignatureCache(DEFAULT_MAX_SIG_CACHE_SIZE * ((size_t) 1 << 20));
|
|
|
|
bundlecache::init(DEFAULT_MAX_SIG_CACHE_SIZE * ((size_t) 1 << 20));
|
2022-03-28 12:35:59 -07:00
|
|
|
|
|
|
|
// Uncomment this to log all errors to stdout so we see them in test output.
|
|
|
|
// We don't enable this by default because several tests intentionally cause
|
|
|
|
// verbose error output (while exercising failure cases).
|
|
|
|
// if (pTracingHandle == nullptr) {
|
|
|
|
// std::string initialFilter = "error";
|
|
|
|
// pTracingHandle = tracing_init(
|
|
|
|
// nullptr, 0,
|
|
|
|
// initialFilter.c_str(),
|
|
|
|
// false);
|
|
|
|
// }
|
|
|
|
|
2017-04-07 09:24:53 -07:00
|
|
|
fCheckBlockIndex = true;
|
2015-06-30 12:39:49 -07:00
|
|
|
SelectParams(chainName);
|
2015-03-03 06:59:32 -08:00
|
|
|
noui_connect();
|
2021-09-30 13:28:10 -07:00
|
|
|
TestSetIBD(false);
|
2015-03-12 01:34:42 -07:00
|
|
|
}
|
2015-03-03 06:59:32 -08:00
|
|
|
|
2015-03-12 01:34:42 -07:00
|
|
|
BasicTestingSetup::~BasicTestingSetup()
|
|
|
|
{
|
2017-04-07 09:24:53 -07:00
|
|
|
ECC_Stop();
|
2015-03-12 01:34:42 -07:00
|
|
|
}
|
|
|
|
|
2022-04-20 20:19:09 -07:00
|
|
|
TestingSetup::TestingSetup(const std::string& chainName) : BasicTestingSetup(chainName)
|
2015-03-12 01:34:42 -07:00
|
|
|
{
|
2022-06-30 07:05:29 -07:00
|
|
|
fs::path sapling_spend = ZC_GetParamsDir() / "sapling-spend.params";
|
|
|
|
fs::path sapling_output = ZC_GetParamsDir() / "sapling-output.params";
|
|
|
|
fs::path sprout_groth16 = ZC_GetParamsDir() / "sprout-groth16.params";
|
|
|
|
|
|
|
|
static_assert(
|
|
|
|
sizeof(fs::path::value_type) == sizeof(codeunit),
|
|
|
|
"librustzcash not configured correctly");
|
|
|
|
auto sapling_spend_str = sapling_spend.native();
|
|
|
|
auto sapling_output_str = sapling_output.native();
|
|
|
|
auto sprout_groth16_str = sprout_groth16.native();
|
|
|
|
|
|
|
|
librustzcash_init_zksnark_params(
|
|
|
|
reinterpret_cast<const codeunit*>(sapling_spend_str.c_str()),
|
|
|
|
sapling_spend_str.length(),
|
|
|
|
reinterpret_cast<const codeunit*>(sapling_output_str.c_str()),
|
|
|
|
sapling_output_str.length(),
|
|
|
|
reinterpret_cast<const codeunit*>(sprout_groth16_str.c_str()),
|
|
|
|
sprout_groth16_str.length(),
|
|
|
|
// Only load the verifying keys, which some tests need.
|
|
|
|
false
|
|
|
|
);
|
|
|
|
|
2015-04-17 05:40:24 -07:00
|
|
|
const CChainParams& chainparams = Params();
|
2016-03-29 10:43:02 -07:00
|
|
|
// Ideally we'd move all the RPC tests to the functional testing framework
|
|
|
|
// instead of unit tests, but for now we need these here.
|
|
|
|
RegisterAllCoreRPCCommands(tableRPC);
|
2018-10-31 13:13:50 -07:00
|
|
|
|
|
|
|
// Save current path, in case a test changes it
|
2017-03-01 08:05:50 -08:00
|
|
|
orig_current_path = fs::current_path();
|
2018-10-31 13:13:50 -07:00
|
|
|
|
2015-03-03 07:49:12 -08:00
|
|
|
ClearDatadirCache();
|
2017-06-07 12:03:17 -07:00
|
|
|
pathTemp = fs::temp_directory_path() / strprintf("test_bitcoin_%lu_%i", (unsigned long)GetTime(), (int)(InsecureRandRange(100000)));
|
2017-03-01 08:05:50 -08:00
|
|
|
fs::create_directories(pathTemp);
|
2012-11-28 12:58:41 -08:00
|
|
|
mapArgs["-datadir"] = pathTemp.string();
|
2012-11-09 15:09:57 -08:00
|
|
|
pblocktree = new CBlockTreeDB(1 << 20, true);
|
|
|
|
pcoinsdbview = new CCoinsViewDB(1 << 23, true);
|
2014-09-23 18:19:04 -07:00
|
|
|
pcoinsTip = new CCoinsViewCache(pcoinsdbview);
|
2015-04-17 05:40:24 -07:00
|
|
|
InitBlockIndex(chainparams);
|
2016-07-22 06:57:25 -07:00
|
|
|
{
|
|
|
|
CValidationState state;
|
|
|
|
bool ok = ActivateBestChain(state, chainparams);
|
|
|
|
BOOST_CHECK(ok);
|
|
|
|
}
|
2012-12-01 14:04:14 -08:00
|
|
|
nScriptCheckThreads = 3;
|
|
|
|
for (int i=0; i < nScriptCheckThreads-1; i++)
|
2013-03-06 19:31:26 -08:00
|
|
|
threadGroup.create_thread(&ThreadScriptCheck);
|
2013-11-17 16:25:17 -08:00
|
|
|
RegisterNodeSignals(GetNodeSignals());
|
2015-03-03 07:49:12 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
TestingSetup::~TestingSetup()
|
|
|
|
{
|
|
|
|
UnregisterNodeSignals(GetNodeSignals());
|
2013-03-06 19:31:26 -08:00
|
|
|
threadGroup.interrupt_all();
|
|
|
|
threadGroup.join_all();
|
2015-03-03 07:49:12 -08:00
|
|
|
UnloadBlockIndex();
|
2012-09-03 06:26:57 -07:00
|
|
|
delete pcoinsTip;
|
|
|
|
delete pcoinsdbview;
|
|
|
|
delete pblocktree;
|
2018-10-31 13:13:50 -07:00
|
|
|
|
|
|
|
// Restore the previous current path so temporary directory can be deleted
|
2017-03-01 08:05:50 -08:00
|
|
|
fs::current_path(orig_current_path);
|
2018-10-31 13:13:50 -07:00
|
|
|
|
2017-03-01 08:05:50 -08:00
|
|
|
fs::remove_all(pathTemp);
|
2015-03-03 07:49:12 -08:00
|
|
|
}
|
2011-10-03 13:14:13 -07:00
|
|
|
|
2017-05-19 05:20:03 -07:00
|
|
|
#ifdef ENABLE_MINING
|
2015-03-03 06:59:32 -08:00
|
|
|
TestChain100Setup::TestChain100Setup() : TestingSetup(CBaseChainParams::REGTEST)
|
|
|
|
{
|
|
|
|
// Generate a 100-block chain:
|
2021-10-18 18:47:28 -07:00
|
|
|
coinbaseKey = CKey::TestOnlyRandomKey(true);
|
2015-03-03 06:59:32 -08:00
|
|
|
CScript scriptPubKey = CScript() << ToByteVector(coinbaseKey.GetPubKey()) << OP_CHECKSIG;
|
|
|
|
for (int i = 0; i < COINBASE_MATURITY; i++)
|
|
|
|
{
|
|
|
|
std::vector<CMutableTransaction> noTxns;
|
|
|
|
CBlock b = CreateAndProcessBlock(noTxns, scriptPubKey);
|
|
|
|
coinbaseTxns.push_back(b.vtx[0]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//
|
|
|
|
// Create a new block with just given transactions, coinbase paying to
|
|
|
|
// scriptPubKey, and try to add it to the current chain.
|
|
|
|
//
|
|
|
|
CBlock
|
|
|
|
TestChain100Setup::CreateAndProcessBlock(const std::vector<CMutableTransaction>& txns, const CScript& scriptPubKey)
|
|
|
|
{
|
|
|
|
const CChainParams& chainparams = Params();
|
2017-05-19 05:20:03 -07:00
|
|
|
unsigned int n = chainparams.GetConsensus().nEquihashN;
|
|
|
|
unsigned int k = chainparams.GetConsensus().nEquihashK;
|
|
|
|
|
2019-05-17 15:36:32 -07:00
|
|
|
boost::shared_ptr<CReserveScript> mAddr(new CReserveScript());
|
|
|
|
mAddr->reserveScript = scriptPubKey;
|
|
|
|
|
2015-12-15 12:26:44 -08:00
|
|
|
CBlockTemplate *pblocktemplate = BlockAssembler(chainparams).CreateNewBlock(mAddr);
|
2015-03-03 06:59:32 -08:00
|
|
|
CBlock& block = pblocktemplate->block;
|
|
|
|
|
|
|
|
// Replace mempool-selected txns with just coinbase plus passed-in txns:
|
|
|
|
block.vtx.resize(1);
|
2017-06-01 18:18:57 -07:00
|
|
|
for (const CMutableTransaction& tx : txns)
|
2015-03-03 06:59:32 -08:00
|
|
|
block.vtx.push_back(tx);
|
|
|
|
// IncrementExtraNonce creates a valid coinbase and merkleRoot
|
|
|
|
unsigned int extraNonce = 0;
|
2021-06-15 05:21:09 -07:00
|
|
|
IncrementExtraNonce(pblocktemplate, chainActive.Tip(), extraNonce, chainparams.GetConsensus());
|
2015-03-03 06:59:32 -08:00
|
|
|
|
2017-05-19 05:20:03 -07:00
|
|
|
// Hash state
|
2022-05-26 09:09:30 -07:00
|
|
|
eh_HashState eh_state = EhInitialiseState(n, k);
|
2017-05-19 05:20:03 -07:00
|
|
|
|
|
|
|
// I = the block header minus nonce and solution.
|
|
|
|
CEquihashInput I{block};
|
|
|
|
CDataStream ss(SER_NETWORK, PROTOCOL_VERSION);
|
|
|
|
ss << I;
|
|
|
|
|
|
|
|
// H(I||...
|
2020-07-14 03:43:09 -07:00
|
|
|
eh_state.Update((unsigned char*)&ss[0], ss.size());
|
2017-05-19 05:20:03 -07:00
|
|
|
|
|
|
|
bool found = false;
|
|
|
|
do {
|
2015-03-03 06:59:32 -08:00
|
|
|
block.nNonce = ArithToUint256(UintToArith256(block.nNonce) + 1);
|
2017-05-19 05:20:03 -07:00
|
|
|
|
|
|
|
// H(I||V||...
|
2020-07-14 03:43:09 -07:00
|
|
|
eh_HashState curr_state(eh_state);
|
|
|
|
curr_state.Update(block.nNonce.begin(), block.nNonce.size());
|
2017-05-19 05:20:03 -07:00
|
|
|
|
|
|
|
// (x_1, x_2, ...) = A(I, V, n, k)
|
|
|
|
std::function<bool(std::vector<unsigned char>)> validBlock =
|
|
|
|
[&block, &chainparams](std::vector<unsigned char> soln) {
|
|
|
|
block.nSolution = soln;
|
|
|
|
return CheckProofOfWork(block.GetHash(), block.nBits, chainparams.GetConsensus());
|
|
|
|
};
|
|
|
|
found = EhBasicSolveUncancellable(n, k, curr_state, validBlock);
|
|
|
|
} while (!found);
|
2015-03-03 06:59:32 -08:00
|
|
|
|
|
|
|
CValidationState state;
|
|
|
|
ProcessNewBlock(state, chainparams, NULL, &block, true, NULL);
|
|
|
|
|
|
|
|
CBlock result = block;
|
|
|
|
delete pblocktemplate;
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
TestChain100Setup::~TestChain100Setup()
|
|
|
|
{
|
|
|
|
}
|
2017-05-19 05:20:03 -07:00
|
|
|
#endif // ENABLE_MINING
|
2015-03-03 06:59:32 -08:00
|
|
|
|
2015-11-14 14:04:15 -08:00
|
|
|
|
|
|
|
CTxMemPoolEntry TestMemPoolEntryHelper::FromTx(CMutableTransaction &tx, CTxMemPool *pool) {
|
2015-10-26 08:08:46 -07:00
|
|
|
CTransaction txn(tx);
|
|
|
|
bool hasNoDependencies = pool ? pool->HasNoInputsOf(tx) : hadNoDependencies;
|
|
|
|
|
2017-01-20 06:24:35 -08:00
|
|
|
return CTxMemPoolEntry(txn, nFee, nTime, nHeight,
|
2015-10-26 08:08:46 -07:00
|
|
|
hasNoDependencies, spendsCoinbase, sigOpCount, nBranchId);
|
2015-11-14 14:04:15 -08:00
|
|
|
}
|
|
|
|
|
2011-07-31 11:00:38 -07:00
|
|
|
void Shutdown(void* parg)
|
|
|
|
{
|
2011-09-27 11:16:07 -07:00
|
|
|
exit(0);
|
2011-07-31 11:00:38 -07:00
|
|
|
}
|
2012-06-14 00:41:11 -07:00
|
|
|
|
|
|
|
void StartShutdown()
|
|
|
|
{
|
|
|
|
exit(0);
|
|
|
|
}
|
|
|
|
|
2014-03-24 23:53:21 -07:00
|
|
|
bool ShutdownRequested()
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|