Changeset View
Changeset View
Standalone View
Standalone View
src/test/DoS_tests.cpp
Show First 20 Lines • Show All 131 Lines • ▼ Show 20 Lines | BOOST_AUTO_TEST_CASE(DoS_bantime) { | ||||
SetMockTime(nStartTime + 60 * 60 * 24 + 1); | SetMockTime(nStartTime + 60 * 60 * 24 + 1); | ||||
BOOST_CHECK(!connman->IsBanned(addr)); | BOOST_CHECK(!connman->IsBanned(addr)); | ||||
} | } | ||||
CTransactionRef RandomOrphan() { | CTransactionRef RandomOrphan() { | ||||
std::map<uint256, COrphanTx>::iterator it; | std::map<uint256, COrphanTx>::iterator it; | ||||
it = mapOrphanTransactions.lower_bound(InsecureRand256()); | it = mapOrphanTransactions.lower_bound(InsecureRand256()); | ||||
if (it == mapOrphanTransactions.end()) it = mapOrphanTransactions.begin(); | if (it == mapOrphanTransactions.end()) { | ||||
it = mapOrphanTransactions.begin(); | |||||
} | |||||
return it->second.tx; | return it->second.tx; | ||||
} | } | ||||
BOOST_AUTO_TEST_CASE(DoS_mapOrphans) { | BOOST_AUTO_TEST_CASE(DoS_mapOrphans) { | ||||
CKey key; | CKey key; | ||||
key.MakeNewKey(true); | key.MakeNewKey(true); | ||||
CBasicKeyStore keystore; | CBasicKeyStore keystore; | ||||
keystore.AddKey(key); | keystore.AddKey(key); | ||||
// 50 orphan transactions: | // 50 orphan transactions: | ||||
for (int i = 0; i < 50; i++) { | for (int i = 0; i < 50; i++) { | ||||
CMutableTransaction tx; | CMutableTransaction tx; | ||||
tx.vin.resize(1); | tx.vin.resize(1); | ||||
tx.vin[0].prevout.n = 0; | tx.vin[0].prevout = COutPoint(InsecureRand256(), 0); | ||||
tx.vin[0].prevout.hash = InsecureRand256(); | |||||
tx.vin[0].scriptSig << OP_1; | tx.vin[0].scriptSig << OP_1; | ||||
tx.vout.resize(1); | tx.vout.resize(1); | ||||
tx.vout[0].nValue = 1 * CENT; | tx.vout[0].nValue = 1 * CENT; | ||||
tx.vout[0].scriptPubKey = | tx.vout[0].scriptPubKey = | ||||
GetScriptForDestination(key.GetPubKey().GetID()); | GetScriptForDestination(key.GetPubKey().GetID()); | ||||
AddOrphanTx(MakeTransactionRef(tx), i); | AddOrphanTx(MakeTransactionRef(tx), i); | ||||
} | } | ||||
// ... and 50 that depend on other orphans: | // ... and 50 that depend on other orphans: | ||||
for (int i = 0; i < 50; i++) { | for (int i = 0; i < 50; i++) { | ||||
CTransactionRef txPrev = RandomOrphan(); | CTransactionRef txPrev = RandomOrphan(); | ||||
CMutableTransaction tx; | CMutableTransaction tx; | ||||
tx.vin.resize(1); | tx.vin.resize(1); | ||||
tx.vin[0].prevout.n = 0; | tx.vin[0].prevout = COutPoint(txPrev->GetId(), 0); | ||||
tx.vin[0].prevout.hash = txPrev->GetId(); | |||||
tx.vout.resize(1); | tx.vout.resize(1); | ||||
tx.vout[0].nValue = 1 * CENT; | tx.vout[0].nValue = 1 * CENT; | ||||
tx.vout[0].scriptPubKey = | tx.vout[0].scriptPubKey = | ||||
GetScriptForDestination(key.GetPubKey().GetID()); | GetScriptForDestination(key.GetPubKey().GetID()); | ||||
SignSignature(keystore, *txPrev, tx, 0, SigHashType()); | SignSignature(keystore, *txPrev, tx, 0, SigHashType()); | ||||
AddOrphanTx(MakeTransactionRef(tx), i); | AddOrphanTx(MakeTransactionRef(tx), i); | ||||
} | } | ||||
// This really-big orphan should be ignored: | // This really-big orphan should be ignored: | ||||
for (int i = 0; i < 10; i++) { | for (int i = 0; i < 10; i++) { | ||||
CTransactionRef txPrev = RandomOrphan(); | CTransactionRef txPrev = RandomOrphan(); | ||||
CMutableTransaction tx; | CMutableTransaction tx; | ||||
tx.vout.resize(1); | tx.vout.resize(1); | ||||
tx.vout[0].nValue = 1 * CENT; | tx.vout[0].nValue = 1 * CENT; | ||||
tx.vout[0].scriptPubKey = | tx.vout[0].scriptPubKey = | ||||
GetScriptForDestination(key.GetPubKey().GetID()); | GetScriptForDestination(key.GetPubKey().GetID()); | ||||
tx.vin.resize(2777); | tx.vin.resize(2777); | ||||
for (unsigned int j = 0; j < tx.vin.size(); j++) { | for (size_t j = 0; j < tx.vin.size(); j++) { | ||||
tx.vin[j].prevout.n = j; | tx.vin[j].prevout = COutPoint(txPrev->GetId(), j); | ||||
tx.vin[j].prevout.hash = txPrev->GetId(); | |||||
} | } | ||||
SignSignature(keystore, *txPrev, tx, 0, SigHashType()); | SignSignature(keystore, *txPrev, tx, 0, SigHashType()); | ||||
// Re-use same signature for other inputs | // Re-use same signature for other inputs | ||||
// (they don't have to be valid for this test) | // (they don't have to be valid for this test) | ||||
for (unsigned int j = 1; j < tx.vin.size(); j++) | for (unsigned int j = 1; j < tx.vin.size(); j++) | ||||
tx.vin[j].scriptSig = tx.vin[0].scriptSig; | tx.vin[j].scriptSig = tx.vin[0].scriptSig; | ||||
BOOST_CHECK(!AddOrphanTx(MakeTransactionRef(tx), i)); | BOOST_CHECK(!AddOrphanTx(MakeTransactionRef(tx), i)); | ||||
Show All 19 Lines |