MCPcopy Create free account
hub / github.com/ElementsProject/elements / AssetTest

Function AssetTest

src/test/script_tests.cpp:1719–1778  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

1717static const std::vector<unsigned int> ALL_CONSENSUS_FLAGS = AllConsensusFlags();
1718
1719static void AssetTest(const UniValue& test)
1720{
1721 BOOST_CHECK(test.isObject());
1722
1723 CMutableTransaction mtx = TxFromHex(test["tx"].get_str());
1724 const std::vector<CTxOut> prevouts = TxOutsFromJSON(test["prevouts"]);
1725 BOOST_CHECK(prevouts.size() == mtx.vin.size());
1726 mtx.witness.vtxinwit.resize(mtx.vin.size());
1727 size_t idx = test["index"].get_int64();
1728 uint32_t test_flags{ParseScriptFlags(test["flags"].get_str())};
1729 bool fin = test.exists("final") && test["final"].get_bool();
1730 // ELEMENTS: feature_taproot.py --dumptests outputs this field
1731 uint256 hash_genesis_block = test.exists("hash_genesis_block") ? uint256S(test["hash_genesis_block"].get_str()) : uint256{};
1732
1733 if (test.exists("success")) {
1734 mtx.vin[idx].scriptSig = ScriptFromHex(test["success"]["scriptSig"].get_str());
1735 mtx.witness.vtxinwit[idx].scriptWitness = ScriptWitnessFromJSON(test["success"]["witness"]);
1736 CTransaction tx(mtx);
1737 PrecomputedTransactionData txdata(hash_genesis_block);
1738 txdata.Init(tx, std::vector<CTxOut>(prevouts));
1739 CachingTransactionSignatureChecker txcheck(&tx, idx, prevouts[idx].nValue, true, txdata);
1740 for (const auto flags : ALL_CONSENSUS_FLAGS) {
1741 // "final": true tests are valid for all flags. Others are only valid with flags that are
1742 // a subset of test_flags.
1743 if (fin || ((flags & test_flags) == flags)) {
1744 ScriptError serror;
1745 bool ret = VerifyScript(tx.vin[idx].scriptSig, prevouts[idx].scriptPubKey, &tx.witness.vtxinwit[idx].scriptWitness, flags, txcheck, &serror);
1746 BOOST_CHECK(ret);
1747 BOOST_CHECK_EQUAL(serror, SCRIPT_ERR_OK);
1748 }
1749 }
1750 }
1751
1752 if (test.exists("failure")) {
1753 mtx.vin[idx].scriptSig = ScriptFromHex(test["failure"]["scriptSig"].get_str());
1754 mtx.witness.vtxinwit[idx].scriptWitness = ScriptWitnessFromJSON(test["failure"]["witness"]);
1755 CTransaction tx(mtx);
1756 PrecomputedTransactionData txdata(hash_genesis_block);
1757 txdata.Init(tx, std::vector<CTxOut>(prevouts));
1758 CachingTransactionSignatureChecker txcheck(&tx, idx, prevouts[idx].nValue, true, txdata);
1759
1760 std::optional<ScriptError> expected_error;
1761 if (test["failure"].exists("error")) {
1762 expected_error = ParseScriptError(test["failure"]["error"].get_str());
1763 }
1764
1765 for (const auto flags : ALL_CONSENSUS_FLAGS) {
1766 // If a test is supposed to fail with test_flags, it should also fail with any superset thereof.
1767 if ((flags & test_flags) == test_flags) {
1768 ScriptError serror;
1769 bool ret = VerifyScript(tx.vin[idx].scriptSig, prevouts[idx].scriptPubKey, &tx.witness.vtxinwit[idx].scriptWitness, flags, txcheck, &serror);
1770 BOOST_CHECK(!ret);
1771
1772 if (expected_error) {
1773 BOOST_CHECK_EQUAL(serror, *expected_error);
1774 }
1775 }
1776 }

Callers 1

BOOST_AUTO_TEST_CASEFunction · 0.85

Calls 15

uint256SFunction · 0.85
VerifyScriptFunction · 0.85
ParseScriptErrorFunction · 0.85
isObjectMethod · 0.80
get_int64Method · 0.80
get_boolMethod · 0.80
TxFromHexFunction · 0.70
TxOutsFromJSONFunction · 0.70
ParseScriptFlagsFunction · 0.70
ScriptFromHexFunction · 0.70
ScriptWitnessFromJSONFunction · 0.70
sizeMethod · 0.45

Tested by

no test coverage detected