13 #include <test/data/sighash.json.h> 22 #include <boost/test/unit_test.hpp> 29 if (nIn >= txTo.
vin.size())
40 for (
unsigned int i = 0; i < txTmp.
vin.size(); i++)
42 txTmp.
vin[nIn].scriptSig = scriptCode;
51 for (
unsigned int i = 0; i < txTmp.
vin.size(); i++)
53 txTmp.
vin[i].nSequence = 0;
58 unsigned int nOut = nIn;
59 if (nOut >= txTmp.
vout.size())
63 txTmp.
vout.resize(nOut+1);
64 for (
unsigned int i = 0; i < nOut; i++)
65 txTmp.
vout[i].SetNull();
68 for (
unsigned int i = 0; i < txTmp.
vin.size(); i++)
70 txTmp.
vin[i].nSequence = 0;
76 txTmp.
vin[0] = txTmp.
vin[nIn];
91 for (
int i=0; i<ops; i++)
103 for (
int in = 0; in < ins; in++) {
104 tx.
vin.emplace_back();
112 tx.
vout.emplace_back();
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)};
258 BOOST_CHECK_NE(sighash_with_cache, sighash_with_cache2);
271 cache.
Store(hash_type, scriptcode, h);
272 const auto stored_hash{h.GetHash()};
274 const auto loaded_hash{h.GetHash()};
281 BOOST_CHECK_NE(
SignatureHash(scriptcode, tx, in_index, hash_type, amount, sigversion,
nullptr, &cache), sighash_with_cache);
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);
std::shared_ptr< const CTransaction > CTransactionRef
Witness v0 (P2WPKH and P2WSH); see BIP 141.
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)
bool randbool() noexcept
Generate a random boolean.
std::vector< Byte > ParseHex(std::string_view hex_str)
Like TryParseHex, but returns an empty vector on invalid input.
void RandomScript(CScript &script)
iterator insert(iterator pos, const T &value)
const std::string & get_str() const
Bare scripts and BIP16 P2SH-wrapped redeemscripts.
UniValue read_json(std::string_view jsondata)
Data structure to cache SHA256 midstates for the ECDSA sighash calculations (bare, P2SH, P2WPKH, P2WSH).
const std::vector< CTxIn > vin
CAmount RandMoney(Rng &&rng)
Minimal stream for reading from an existing byte array by std::span.
constexpr std::array tests
int64_t CAmount
Amount in satoshis (Can be negative)
BOOST_FIXTURE_TEST_SUITE(cuckoocache_tests, BasicTestingSetup)
Test Suite for CuckooCache.
An input of a transaction.
Double ended buffer combining vector and stream-like interfaces.
A writer stream (for serialization) that computes a 256-bit hash.
void RandomTransaction(CMutableTransaction &tx, bool fSingle)
BOOST_AUTO_TEST_SUITE_END()
opcodetype
Script opcodes.
std::string write(unsigned int prettyIndent=0, unsigned int indentLevel=0) const
uint32_t rand32() noexcept
Generate a random 32-bit integer.
static uint256 SignatureHashOld(CScript scriptCode, const CTransaction &txTo, unsigned int nIn, int nHashType)
An output of a transaction.
bool Load(int32_t hash_type, const CScript &script_code, HashWriter &writer) const noexcept
Load into writer the SHA256 midstate if found in this cache.
std::vector< CTxOut > vout
BOOST_AUTO_TEST_CASE(sighash_test)
#define BOOST_CHECK_EQUAL(v1, v2)
Serialized script, used inside transaction inputs and outputs.
static transaction_identifier FromUint256(const uint256 &id)
uint64_t randbits(int bits) noexcept
Generate a random (bits)-bit integer.
uint256 rand256() noexcept
generate a random uint256.
std::string GetHex() const
I randrange(I range) noexcept
Generate a random integer in the range [0..range), with range > 0.
void Store(int32_t hash_type, const CScript &script_code, const HashWriter &writer) noexcept
Store into this cache object the provided SHA256 midstate.
A mutable version of CTransaction.
The basic transaction that is broadcasted on the network and contained in blocks. ...
int FindAndDelete(CScript &script, const CScript &b)
bool CheckTransaction(const CTransaction &tx, TxValidationState &state)
std::string HexStr(const std::span< const uint8_t > s)
Convert a span of bytes to a lower-case hexadecimal string.
static constexpr TransactionSerParams TX_WITH_WITNESS
static constexpr TransactionSerParams TX_NO_WITNESS
#define BOOST_CHECK(expr)