13#include <test/data/sighash.json.h>
21#include <boost/test/unit_test.hpp>
28 if (nIn >= txTo.
vin.size())
39 for (
unsigned int i = 0; i < txTmp.
vin.size(); i++)
41 txTmp.
vin[nIn].scriptSig = scriptCode;
50 for (
unsigned int i = 0; i < txTmp.
vin.size(); i++)
52 txTmp.
vin[i].nSequence = 0;
57 unsigned int nOut = nIn;
58 if (nOut >= txTmp.
vout.size())
62 txTmp.
vout.resize(nOut+1);
63 for (
unsigned int i = 0; i < nOut; i++)
64 txTmp.
vout[i].SetNull();
67 for (
unsigned int i = 0; i < txTmp.
vin.size(); i++)
69 txTmp.
vin[i].nSequence = 0;
75 txTmp.
vin[0] = txTmp.
vin[nIn];
90 for (
int i=0; i<ops; i++)
102 for (
int in = 0; in < ins; in++) {
103 tx.
vin.emplace_back();
111 tx.
vout.emplace_back();
123 #if defined(PRINT_SIGHASH_JSON)
125 std::cout <<
"\t[\"raw_transaction, script, input_index, hashType, signature_hash (result)\"],\n";
126 int nRandomTests = 500;
128 int nRandomTests = 50000;
130 for (
int i=0; i<nRandomTests; i++) {
131 int nHashType{int(m_rng.rand32())};
135 RandomScript(scriptCode);
136 int nIn = m_rng.randrange(txTo.
vin.size());
141 #if defined(PRINT_SIGHASH_JSON)
145 std::cout <<
"\t[\"" ;
146 std::cout <<
HexStr(ss) <<
"\", \"";
147 std::cout <<
HexStr(scriptCode) <<
"\", ";
148 std::cout << nIn <<
", ";
149 std::cout << nHashType <<
", \"";
150 std::cout << sho.
GetHex() <<
"\"]";
151 if (i+1 != nRandomTests) {
158 #if defined(PRINT_SIGHASH_JSON)
168 for (
unsigned int idx = 0; idx < tests.
size(); idx++) {
170 std::string strTest = test.
write();
173 BOOST_ERROR(
"Bad test: " << strTest);
176 if (test.
size() == 1)
continue;
178 std::string raw_tx, raw_script, sigHashHex;
187 raw_script = test[1].
get_str();
188 nIn = test[2].
getInt<
int>();
189 nHashType = test[3].
getInt<
int>();
190 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);
Serialized script, used inside transaction inputs and outputs.
The basic transaction that is broadcasted on the network and contained in blocks.
const std::vector< CTxIn > vin
An input of a transaction.
An output of a transaction.
Double ended buffer combining vector and stream-like interfaces.
A writer stream (for serialization) that computes a 256-bit hash.
I randrange(I range) noexcept
Generate a random integer in the range [0..range), with range > 0.
uint256 rand256() noexcept
generate a random uint256.
bool randbool() noexcept
Generate a random boolean.
uint32_t rand32() noexcept
Generate a random 32-bit integer.
uint64_t randbits(int bits) noexcept
Generate a random (bits)-bit integer.
const std::string & get_str() const
std::string write(unsigned int prettyIndent=0, unsigned int indentLevel=0) const
std::string GetHex() const
iterator insert(iterator pos, const T &value)
static transaction_identifier FromUint256(const uint256 &id)
BOOST_FIXTURE_TEST_SUITE(cuckoocache_tests, BasicTestingSetup)
Test Suite for CuckooCache.
BOOST_AUTO_TEST_SUITE_END()
std::string HexStr(const Span< const uint8_t > s)
Convert a span of bytes to a lower-case hexadecimal string.
int FindAndDelete(CScript &script, const CScript &b)
uint256 SignatureHash(const CScript &scriptCode, const T &txTo, unsigned int nIn, int32_t nHashType, const CAmount &amount, SigVersion sigversion, const PrecomputedTransactionData *cache)
@ BASE
Bare scripts and BIP16 P2SH-wrapped redeemscripts.
UniValue read_json(std::string_view jsondata)
#define BOOST_CHECK(expr)
static constexpr TransactionSerParams TX_NO_WITNESS
static constexpr TransactionSerParams TX_WITH_WITNESS
std::shared_ptr< const CTransaction > CTransactionRef
opcodetype
Script opcodes.
static uint256 SignatureHashOld(CScript scriptCode, const CTransaction &txTo, unsigned int nIn, int nHashType)
BOOST_AUTO_TEST_CASE(sighash_test)
std::vector< Byte > ParseHex(std::string_view hex_str)
Like TryParseHex, but returns an empty vector on invalid input.
A mutable version of CTransaction.
std::vector< CTxOut > vout
void RandomScript(CScript &script)
void RandomTransaction(CMutableTransaction &tx, bool fSingle)
CAmount RandMoney(Rng &&rng)
bool CheckTransaction(const CTransaction &tx, TxValidationState &state)