Changeset View
Changeset View
Standalone View
Standalone View
src/test/sighash_tests.cpp
Show First 20 Lines • Show All 92 Lines • ▼ Show 20 Lines | static void RandomScript(CScript &script) { | ||||
script = CScript(); | script = CScript(); | ||||
int ops = (InsecureRandRange(10)); | int ops = (InsecureRandRange(10)); | ||||
for (int i = 0; i < ops; i++) { | for (int i = 0; i < ops; i++) { | ||||
script << oplist[InsecureRandRange(sizeof(oplist) / sizeof(oplist[0]))]; | script << oplist[InsecureRandRange(sizeof(oplist) / sizeof(oplist[0]))]; | ||||
} | } | ||||
} | } | ||||
static void RandomTransaction(CMutableTransaction &tx, bool fSingle) { | static void RandomTransaction(CMutableTransaction &tx, bool fSingle) { | ||||
tx.nVersion = insecure_rand(); | tx.nVersion = InsecureRand32(); | ||||
tx.vin.clear(); | tx.vin.clear(); | ||||
tx.vout.clear(); | tx.vout.clear(); | ||||
tx.nLockTime = (InsecureRandBool()) ? insecure_rand() : 0; | tx.nLockTime = (InsecureRandBool()) ? InsecureRand32() : 0; | ||||
int ins = (InsecureRandBits(2)) + 1; | int ins = (InsecureRandBits(2)) + 1; | ||||
int outs = fSingle ? ins : (InsecureRandBits(2)) + 1; | int outs = fSingle ? ins : (InsecureRandBits(2)) + 1; | ||||
for (int in = 0; in < ins; in++) { | for (int in = 0; in < ins; in++) { | ||||
tx.vin.push_back(CTxIn()); | tx.vin.push_back(CTxIn()); | ||||
CTxIn &txin = tx.vin.back(); | CTxIn &txin = tx.vin.back(); | ||||
txin.prevout = COutPoint(InsecureRand256(), InsecureRandBits(2)); | txin.prevout = COutPoint(InsecureRand256(), InsecureRandBits(2)); | ||||
RandomScript(txin.scriptSig); | RandomScript(txin.scriptSig); | ||||
txin.nSequence = | txin.nSequence = InsecureRandBool() | ||||
(InsecureRandBool()) ? insecure_rand() : (unsigned int)-1; | ? InsecureRand32() | ||||
: std::numeric_limits<uint32_t>::max(); | |||||
} | } | ||||
for (int out = 0; out < outs; out++) { | for (int out = 0; out < outs; out++) { | ||||
tx.vout.push_back(CTxOut()); | tx.vout.push_back(CTxOut()); | ||||
CTxOut &txout = tx.vout.back(); | CTxOut &txout = tx.vout.back(); | ||||
txout.nValue = (int64_t(insecure_rand()) % 100000000) * SATOSHI; | txout.nValue = int64_t(InsecureRandRange(100000000)) * SATOSHI; | ||||
RandomScript(txout.scriptPubKey); | RandomScript(txout.scriptPubKey); | ||||
} | } | ||||
} | } | ||||
BOOST_FIXTURE_TEST_SUITE(sighash_tests, BasicTestingSetup) | BOOST_FIXTURE_TEST_SUITE(sighash_tests, BasicTestingSetup) | ||||
BOOST_AUTO_TEST_CASE(sighash_test) { | BOOST_AUTO_TEST_CASE(sighash_test) { | ||||
SeedInsecureRand(false); | SeedInsecureRand(false); | ||||
#if defined(PRINT_SIGHASH_JSON) | #if defined(PRINT_SIGHASH_JSON) | ||||
std::cout << "[\n"; | std::cout << "[\n"; | ||||
std::cout << "\t[\"raw_transaction, script, input_index, hashType, " | std::cout << "\t[\"raw_transaction, script, input_index, hashType, " | ||||
"signature_hash (regular), signature_hash(no forkid), " | "signature_hash (regular), signature_hash(no forkid), " | ||||
"signature_hash(replay protected)\"],\n"; | "signature_hash(replay protected)\"],\n"; | ||||
#endif | #endif | ||||
int nRandomTests = 1000; | int nRandomTests = 1000; | ||||
for (int i = 0; i < nRandomTests; i++) { | for (int i = 0; i < nRandomTests; i++) { | ||||
uint32_t nHashType = insecure_rand(); | uint32_t nHashType = InsecureRand32(); | ||||
SigHashType sigHashType(nHashType); | SigHashType sigHashType(nHashType); | ||||
CMutableTransaction txTo; | CMutableTransaction txTo; | ||||
RandomTransaction(txTo, (nHashType & 0x1f) == SIGHASH_SINGLE); | RandomTransaction(txTo, (nHashType & 0x1f) == SIGHASH_SINGLE); | ||||
CScript scriptCode; | CScript scriptCode; | ||||
RandomScript(scriptCode); | RandomScript(scriptCode); | ||||
int nIn = InsecureRandRange(txTo.vin.size()); | int nIn = InsecureRandRange(txTo.vin.size()); | ||||
▲ Show 20 Lines • Show All 135 Lines • Show Last 20 Lines |