| 122 | |
| 123 | struct ScriptTest : BasicTestingSetup { |
| 124 | void DoTest(const CScript &scriptPubKey, const CScript &scriptSig, |
| 125 | uint32_t flags, const std::string &message, |
| 126 | ScriptError scriptError, const Amount nValue) { |
| 127 | bool expect = (scriptError == ScriptError::OK); |
| 128 | if (flags & SCRIPT_VERIFY_CLEANSTACK) { |
| 129 | flags |= SCRIPT_VERIFY_P2SH; |
| 130 | } |
| 131 | |
| 132 | ScriptError err; |
| 133 | const CTransaction txCredit{ |
| 134 | BuildCreditingTransaction(scriptPubKey, nValue)}; |
| 135 | const CMutableTransaction tx = |
| 136 | BuildSpendingTransaction(scriptSig, txCredit); |
| 137 | BOOST_CHECK_MESSAGE(VerifyScript(scriptSig, scriptPubKey, flags, |
| 138 | MutableTransactionSignatureChecker( |
| 139 | &tx, 0, txCredit.vout[0].nValue), |
| 140 | &err) == expect, |
| 141 | message); |
| 142 | BOOST_CHECK_MESSAGE(err == scriptError, |
| 143 | FormatScriptError(err) + " where " + |
| 144 | FormatScriptError(scriptError) + |
| 145 | " expected: " + message); |
| 146 | |
| 147 | // Verify that removing flags from a passing test or adding flags to a |
| 148 | // failing test does not change the result, except for some special |
| 149 | // flags. |
| 150 | for (int i = 0; i < 16; ++i) { |
| 151 | uint32_t extra_flags = m_rng.randbits(32); |
| 152 | // Some flags are not purely-restrictive and thus we can't assume |
| 153 | // anything about what happens when they are flipped. Keep them |
| 154 | // as-is. |
| 155 | extra_flags &= ~(SCRIPT_ENABLE_SIGHASH_FORKID | |
| 156 | SCRIPT_ENABLE_REPLAY_PROTECTION | |
| 157 | SCRIPT_ENABLE_SCHNORR_MULTISIG); |
| 158 | uint32_t combined_flags = |
| 159 | expect ? (flags & ~extra_flags) : (flags | extra_flags); |
| 160 | // Weed out invalid flag combinations. |
| 161 | if (combined_flags & SCRIPT_VERIFY_CLEANSTACK) { |
| 162 | combined_flags |= SCRIPT_VERIFY_P2SH; |
| 163 | } |
| 164 | |
| 165 | BOOST_CHECK_MESSAGE( |
| 166 | VerifyScript(scriptSig, scriptPubKey, combined_flags, |
| 167 | MutableTransactionSignatureChecker( |
| 168 | &tx, 0, txCredit.vout[0].nValue), |
| 169 | &err) == expect, |
| 170 | message + strprintf(" (with %s flags %08x)", |
| 171 | expect ? "removed" : "added", |
| 172 | combined_flags ^ flags)); |
| 173 | } |
| 174 | |
| 175 | #if defined(HAVE_CONSENSUS_LIB) |
| 176 | DataStream stream{}; |
| 177 | stream << tx; |
| 178 | uint32_t libconsensus_flags = |
| 179 | flags & bitcoinconsensus_SCRIPT_FLAGS_VERIFY_ALL; |
| 180 | if (libconsensus_flags == flags) { |
| 181 | if (flags & bitcoinconsensus_SCRIPT_ENABLE_SIGHASH_FORKID) { |
no test coverage detected