24#include <boost/test/unit_test.hpp>
30BOOST_AUTO_TEST_SUITE(script_assets_tests)
36 return {span.begin(), span.end()};
54 std::vector<CTxOut> prevouts;
55 for (
size_t i = 0; i < univalue.
size(); ++i) {
58 prevouts.push_back(std::move(txout));
67 for (
size_t i = 0; i < univalue.
size(); ++i) {
68 auto bytes =
ParseHex(univalue[i].get_str());
69 scriptwitness.
stack.push_back(std::move(bytes));
76 std::vector<unsigned int>
ret;
78 for (
unsigned int i = 0; i < 128; ++i) {
79 unsigned int flag = 0;
107 const std::vector<CTxOut> prevouts =
TxOutsFromJSON(test[
"prevouts"]);
109 size_t idx = test[
"index"].
getInt<int64_t>();
113 if (test.
exists(
"success")) {
114 mtx.
vin[idx].scriptSig =
ScriptFromHex(test[
"success"][
"scriptSig"].get_str());
118 txdata.
Init(tx, std::vector<CTxOut>(prevouts));
125 bool ret =
VerifyScript(tx.
vin[idx].scriptSig, prevouts[idx].scriptPubKey, &tx.
vin[idx].scriptWitness,
flags, txcheck,
nullptr);
131 if (test.
exists(
"failure")) {
132 mtx.
vin[idx].scriptSig =
ScriptFromHex(test[
"failure"][
"scriptSig"].get_str());
136 txdata.
Init(tx, std::vector<CTxOut>(prevouts));
141 if ((
flags & test_flags) == test_flags) {
142 bool ret =
VerifyScript(tx.
vin[idx].scriptSig, prevouts[idx].scriptPubKey, &tx.
vin[idx].scriptWitness,
flags, txcheck,
nullptr);
155 const char* dir = std::getenv(
"DIR_UNIT_TEST_DATA");
156 BOOST_WARN_MESSAGE(dir !=
nullptr,
"Variable DIR_UNIT_TEST_DATA unset, skipping script_assets_test");
157 if (dir ==
nullptr)
return;
158 auto path = fs::path(dir) /
"script_assets_test.json";
160 BOOST_WARN_MESSAGE(exists,
"File $DIR_UNIT_TEST_DATA/script_assets_test.json not found, skipping script_assets_test");
162 std::ifstream file{path};
164 file.seekg(0, std::ios::end);
165 size_t length = file.tellg();
166 file.seekg(0, std::ios::beg);
167 std::string
data(length,
'\0');
168 file.read(
data.data(),
data.size());
173 for (
size_t i = 0; i <
tests.size(); i++) {
#define Assert(val)
Identity function.
Serialized script, used inside transaction inputs and outputs.
The basic transaction that is broadcasted on the network and contained in blocks.
const std::vector< CTxIn > vin
An output of a transaction.
Valid signature cache, to avoid doing expensive ECDSA signature checking twice for every transaction ...
Minimal stream for reading from an existing byte array by std::span.
bool exists(const std::string &key) const
BOOST_AUTO_TEST_SUITE_END()
static bool exists(const path &p)
bool VerifyScript(const CScript &scriptSig, const CScript &scriptPubKey, const CScriptWitness *witness, unsigned int flags, const BaseSignatureChecker &checker, ScriptError *serror)
@ SCRIPT_VERIFY_NULLDUMMY
@ SCRIPT_VERIFY_CHECKLOCKTIMEVERIFY
@ SCRIPT_VERIFY_CHECKSEQUENCEVERIFY
UniValue read_json(std::string_view jsondata)
#define BOOST_CHECK(expr)
static constexpr TransactionSerParams TX_NO_WITNESS
BOOST_AUTO_TEST_CASE(script_assets_test)
static CScript ScriptFromHex(const std::string &str)
static CMutableTransaction TxFromHex(const std::string &str)
CScript ToScript(const T &byte_container)
unsigned int ParseScriptFlags(std::string strFlags)
static std::vector< unsigned int > AllConsensusFlags()
static std::vector< CTxOut > TxOutsFromJSON(const UniValue &univalue)
static void AssetTest(const UniValue &test, SignatureCache &signature_cache)
static CScriptWitness ScriptWitnessFromJSON(const UniValue &univalue)
static const std::vector< unsigned int > ALL_CONSENSUS_FLAGS
Precomputed list of all valid combinations of consensus-relevant script validation flags.
static constexpr size_t DEFAULT_SIGNATURE_CACHE_BYTES
constexpr auto MakeUCharSpan(const V &v) -> decltype(UCharSpanCast(std::span{v}))
Like the std::span constructor, but for (const) unsigned char member types only.
std::vector< Byte > ParseHex(std::string_view hex_str)
Like TryParseHex, but returns an empty vector on invalid input.
A mutable version of CTransaction.
std::vector< std::vector< unsigned char > > stack
void Init(const T &tx, std::vector< CTxOut > &&spent_outputs, bool force=false)
Initialize this PrecomputedTransactionData with transaction data.
constexpr std::array tests
std::optional< std::vector< Byte > > TryParseHex(std::string_view str)
Parse the hex string into bytes (uint8_t or std::byte).