124 #if defined(PRINT_SIGHASH_JSON)
126 std::cout <<
"\t[\"raw_transaction, script, input_index, hashType, signature_hash (result)\"],\n";
127 int nRandomTests = 500;
129 int nRandomTests = 50000;
131 for (
int i=0; i<nRandomTests; i++) {
132 int nHashType{int(m_rng.rand32())};
136 RandomScript(scriptCode);
137 int nIn = m_rng.randrange(txTo.
vin.size());
142 #if defined(PRINT_SIGHASH_JSON)
146 std::cout <<
"\t[\"" ;
147 std::cout <<
HexStr(
ss) <<
"\", \"";
148 std::cout <<
HexStr(scriptCode) <<
"\", ";
149 std::cout << nIn <<
", ";
150 std::cout << nHashType <<
", \"";
151 std::cout << sho.
GetHex() <<
"\"]";
152 if (i+1 != nRandomTests) {
159 #if defined(PRINT_SIGHASH_JSON)
169 for (
unsigned int idx = 0; idx <
tests.size(); idx++) {
171 std::string strTest = test.
write();
174 BOOST_ERROR(
"Bad test: " << strTest);
177 if (test.
size() == 1)
continue;
179 std::string raw_tx, raw_script, sigHashHex;
188 raw_script = test[1].
get_str();
189 nIn = test[2].
getInt<
int>();
190 nHashType = test[3].
getInt<
int>();
191 sigHashHex = test[4].
get_str();
199 std::vector<unsigned char> raw =
ParseHex(raw_script);
200 scriptCode.
insert(scriptCode.
end(), raw.begin(), raw.end());
202 BOOST_ERROR(
"Bad test, couldn't deserialize data: " << strTest);
207 BOOST_CHECK_MESSAGE(sh.
GetHex() == sigHashHex, strTest);
215 RandomScript(scriptcode);
216 CScript diff_scriptcode{scriptcode};
217 diff_scriptcode <<
OP_1;
219 RandomTransaction(tx,
false);
220 const auto in_index{
static_cast<uint32_t
>(m_rng.randrange(tx.
vin.size()))};
221 const auto amount{m_rng.rand<
CAmount>()};
229 for (
int i{0}; i < 10; ++i) {
230 hash_types.push_back(i % 2 == 0 ? m_rng.rand<int8_t>() : m_rng.rand<int32_t>());
236 for (
const auto hash_type: hash_types) {
240 const auto sighash_with_cache{
SignatureHash(scriptcode, tx, in_index, hash_type, amount, sigversion,
nullptr, &cache)};
241 const auto sighash_no_cache{
SignatureHash(scriptcode, tx, in_index, hash_type, amount, sigversion,
nullptr,
nullptr)};
254 const auto sighash_with_cache2{
SignatureHash(diff_scriptcode, tx, in_index, hash_type, amount, sigversion,
nullptr, &cache)};
255 const auto sighash_no_cache2{
SignatureHash(diff_scriptcode, tx, in_index, hash_type, amount, sigversion,
nullptr,
nullptr)};
271 cache.
Store(hash_type, scriptcode, h);
272 const auto stored_hash{h.
GetHash()};
274 const auto loaded_hash{h.
GetHash()};
285 const auto new_hash{h.
GetHash()};
293 cache.
Store(hash_type, diff_scriptcode, dummy);
294 (void)
SignatureHash(scriptcode, tx, in_index, hash_type, amount, sigversion,
nullptr, &cache);
uint256 SignatureHash(const CScript &scriptCode, const T &txTo, unsigned int nIn, int32_t nHashType, const CAmount &amount, SigVersion sigversion, const PrecomputedTransactionData *cache, SigHashCache *sighash_cache)