Inside Bitcoin
Opening the selected file…
Inside Bitcoin
Opening the selected file…
The code behind the network, with room to understand it.
Release published 2026-10-28 · Snapshot fetched 2026-11-01. Latest stable release at fetch time; development branches and Git history are excluded. Only the selected code window and visible folders load here.
252// Calling with a different scriptcode (for instance in case a CODESEP is encountered) will not return the cache value but253// overwrite it. The sighash will always be different except in case of legacy SIGHASH_SINGLE bug.254const auto sighash_with_cache2{SignatureHash(diff_scriptcode, tx, in_index, hash_type, amount, sigversion, nullptr, &cache)};255const auto sighash_no_cache2{SignatureHash(diff_scriptcode, tx, in_index, hash_type, amount, sigversion, nullptr, nullptr)};256BOOST_CHECK_EQUAL(sighash_with_cache2, sighash_no_cache2);257if (!expect_one) {258BOOST_CHECK_NE(sighash_with_cache, sighash_with_cache2);259} else {260BOOST_CHECK_EQUAL(sighash_with_cache, sighash_with_cache2);261BOOST_CHECK_EQUAL(sighash_with_cache, uint256::ONE);262}264// Calling the cached version again should return the same value again.265BOOST_CHECK_EQUAL(sighash_with_cache2, SignatureHash(diff_scriptcode, tx, in_index, hash_type, amount, sigversion, nullptr, &cache));267// And if we store a different value for this scriptcode and hash type it will return that instead.268{269HashWriter h{};270h << 42;271cache.Store(hash_type, scriptcode, h);272const auto stored_hash{h.GetHash()};273BOOST_CHECK(cache.Load(hash_type, scriptcode, h));274const auto loaded_hash{h.GetHash()};275BOOST_CHECK_EQUAL(stored_hash, loaded_hash);276}278// And using this mutated cache with the sighash function will return the new value (except in the legacy SIGHASH_SINGLE bug279// case in which it'll return 1).280if (!expect_one) {281BOOST_CHECK_NE(SignatureHash(scriptcode, tx, in_index, hash_type, amount, sigversion, nullptr, &cache), sighash_with_cache);282HashWriter h{};283BOOST_CHECK(cache.Load(hash_type, scriptcode, h));284h << hash_type;285const auto new_hash{h.GetHash()};286BOOST_CHECK_EQUAL(SignatureHash(scriptcode, tx, in_index, hash_type, amount, sigversion, nullptr, &cache), new_hash);287} else {288BOOST_CHECK_EQUAL(SignatureHash(scriptcode, tx, in_index, hash_type, amount, sigversion, nullptr, &cache), uint256::ONE);289}291// Wipe the cache and restore the correct cached value for this scriptcode and hash_type before starting the next iteration.292HashWriter dummy{};293cache.Store(hash_type, diff_scriptcode, dummy);294(void)SignatureHash(scriptcode, tx, in_index, hash_type, amount, sigversion, nullptr, &cache);295BOOST_CHECK(cache.Load(hash_type, scriptcode, dummy) || expect_one);296}297}298}300BOOST_AUTO_TEST_SUITE_END()
Independent explanations by MyBitcoinExplorer. Not affiliated with or endorsed by Bitcoin Core. Guides are educational and do not replace the source or its tests.
Bitcoin Core source © its contributors, distributed under the MIT license. Original notices and bundled third-party licenses are retained in the source tree.