Skip to content

Commit 232aa9e

Browse files
committed
Add code generating data/sighash.json test data
1 parent 43cb418 commit 232aa9e

File tree

1 file changed

+30
-3
lines changed

1 file changed

+30
-3
lines changed

src/test/sighash_tests.cpp

Lines changed: 30 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -114,11 +114,21 @@ void static RandomTransaction(CTransaction &tx, bool fSingle) {
114114
}
115115

116116
BOOST_AUTO_TEST_SUITE(sighash_tests)
117+
117118
BOOST_AUTO_TEST_CASE(sighash_test)
118119
{
119120
seed_insecure_rand(false);
120-
121-
for (int i=0; i<50000; i++) {
121+
122+
#if defined(PRINT_SIGHASH_JSON)
123+
std::cout << "[\n";
124+
std::cout << "\t[\"raw_transaction, script, input_index, hashType, signature_hash (result)\"],\n";
125+
#endif
126+
int nRandomTests = 50000;
127+
128+
#if defined(PRINT_SIGHASH_JSON)
129+
nRandomTests = 500;
130+
#endif
131+
for (int i=0; i<nRandomTests; i++) {
122132
int nHashType = insecure_rand();
123133
CTransaction txTo;
124134
RandomTransaction(txTo, (nHashType & 0x1f) == SIGHASH_SINGLE);
@@ -129,12 +139,29 @@ BOOST_AUTO_TEST_CASE(sighash_test)
129139
uint256 sh, sho;
130140
sho = SignatureHashOld(scriptCode, txTo, nIn, nHashType);
131141
sh = SignatureHash(scriptCode, txTo, nIn, nHashType);
142+
#if defined(PRINT_SIGHASH_JSON)
143+
CDataStream ss(SER_NETWORK, PROTOCOL_VERSION);
144+
ss << txTo;
145+
146+
std::cout << "\t[\"" ;
147+
std::cout << HexStr(ss.begin(), ss.end()) << "\", \"";
148+
std::cout << HexStr(scriptCode) << "\", ";
149+
std::cout << nIn << ", ";
150+
std::cout << nHashType << ", \"";
151+
std::cout << sho.GetHex() << "\"]";
152+
if (i+1 != nRandomTests) {
153+
std::cout << ",";
154+
}
155+
std::cout << "\n";
156+
#endif
132157
BOOST_CHECK(sh == sho);
133158
}
159+
#if defined(PRINT_SIGHASH_JSON)
160+
std::cout << "]\n";
161+
#endif
134162
}
135163

136164
// Goal: check that SignatureHash generates correct hash
137-
138165
BOOST_AUTO_TEST_CASE(sighash_from_data)
139166
{
140167
Array tests = read_json(std::string(json_tests::sighash, json_tests::sighash + sizeof(json_tests::sighash)));

0 commit comments

Comments
 (0)