6 #include <boost/assign/list_of.hpp>
7 #include <boost/date_time/posix_time/posix_time_types.hpp>
8 #include <boost/test/unit_test.hpp>
9 #include <boost/foreach.hpp>
31 BOOST_AUTO_TEST_SUITE(DoS_tests)
85 static bool CheckNBits(
unsigned int nbits1,
int64 time1,
unsigned int nbits2,
int64 time2)\
88 return CheckNBits(nbits2, time2, nbits1, time1);
89 int64 deltaTime = time2-time1;
95 return (have <= required);
104 typedef std::map<int64, unsigned int> BlockData;
105 BlockData chainData =
106 map_list_of(1239852051,486604799)(1262749024,486594666)
107 (1279305360,469854461)(1280200847,469830746)(1281678674,469809688)
108 (1296207707,453179945)(1302624061,453036989)(1309640330,437004818)
109 (1313172719,436789733);
113 BOOST_FOREACH(
const BlockData::value_type& i, chainData)
115 BOOST_FOREACH(
const BlockData::value_type& j, chainData)
117 BOOST_CHECK(CheckNBits(i.second, i.first, j.second, j.first));
122 BlockData::value_type firstcheck = *(chainData.begin());
123 BlockData::value_type lastcheck = *(chainData.rbegin());
127 BOOST_CHECK(!CheckNBits(firstcheck.second, lastcheck.first+60*10, lastcheck.second, lastcheck.first));
128 BOOST_CHECK(!CheckNBits(firstcheck.second, lastcheck.first+60*60*24*14, lastcheck.second, lastcheck.first));
131 BOOST_CHECK(CheckNBits(firstcheck.second, lastcheck.first+60*60*24*365*4, lastcheck.second, lastcheck.first));
136 std::map<uint256, CTransaction>::iterator it;
151 for (
int i = 0; i < 50; i++)
155 tx.
vin[0].prevout.n = 0;
159 tx.
vout[0].nValue = 1*CENT;
166 for (
int i = 0; i < 50; i++)
172 tx.
vin[0].prevout.n = 0;
175 tx.
vout[0].nValue = 1*CENT;
183 for (
int i = 0; i < 10; i++)
189 tx.
vout[0].nValue = 1*CENT;
192 for (
unsigned int j = 0; j < tx.
vin.size(); j++)
194 tx.
vin[j].prevout.n = j;
200 for (
unsigned int j = 1; j < tx.
vin.size(); j++)
201 tx.
vin[j].scriptSig = tx.
vin[0].scriptSig;
227 static const int NPREV=100;
229 for (
int i = 0; i < NPREV; i++)
233 tx.
vin[0].prevout.n = 0;
237 tx.
vout[0].nValue = 1*CENT;
246 tx.
vout[0].nValue = 1*CENT;
248 tx.
vin.resize(NPREV);
249 for (
unsigned int j = 0; j < tx.
vin.size(); j++)
251 tx.
vin[j].prevout.n = 0;
252 tx.
vin[j].prevout.hash = orphans[j].
GetHash();
255 boost::posix_time::ptime mst1 = boost::posix_time::microsec_clock::local_time();
256 for (
unsigned int j = 0; j < tx.
vin.size(); j++)
258 boost::posix_time::ptime mst2 = boost::posix_time::microsec_clock::local_time();
259 boost::posix_time::time_duration msdiff = mst2 - mst1;
260 long nOneValidate = msdiff.total_milliseconds();
261 if (
fDebug)
printf(
"DoS_Checksig sign: %ld\n", nOneValidate);
267 mst1 = boost::posix_time::microsec_clock::local_time();
268 for (
unsigned int i = 0; i < 5; i++)
269 for (
unsigned int j = 0; j < tx.
vin.size(); j++)
271 mst2 = boost::posix_time::microsec_clock::local_time();
272 msdiff = mst2 - mst1;
273 long nManyValidate = msdiff.total_milliseconds();
274 if (
fDebug)
printf(
"DoS_Checksig five: %ld\n", nManyValidate);
276 BOOST_CHECK_MESSAGE(nManyValidate < nOneValidate,
"Signature cache timing failed");
282 tx.
vin[0].scriptSig = save;
285 std::swap(tx.
vin[0].scriptSig, tx.
vin[1].scriptSig);
288 std::swap(tx.
vin[0].scriptSig, tx.
vin[1].scriptSig);
291 mapArgs[
"-maxsigcachesize"] =
"10";
295 BOOST_CHECK(tx.
vin[0].scriptSig != oldSig);
296 for (
unsigned int j = 0; j < tx.
vin.size(); j++)
298 mapArgs.erase(
"-maxsigcachesize");
303 BOOST_AUTO_TEST_SUITE_END()
std::map< uint256, std::set< uint256 > > mapOrphanTransactionsByPrev
static void ClearBanned()
bool AddOrphanTx(const CTransaction &tx)
bool SignSignature(const CKeyStore &keystore, const CScript &fromPubKey, CTransaction &txTo, unsigned int nIn, int nHashType)
pruned version of CTransaction: only retains metadata and unspent transaction outputs ...
unsigned int LimitOrphanTxSize(unsigned int nMaxOrphans)
std::map< uint256, CTransaction > mapOrphanTransactions
bool VerifySignature(const CCoins &txFrom, const CTransaction &txTo, unsigned int nIn, unsigned int flags, int nHashType)
Verify a signature.
void SetMockTime(int64 nMockTimeIn)
CBigNum & SetCompact(unsigned int nCompact)
CPubKey GetPubKey() const
std::vector< CTxOut > vout
A combination of a network address (CNetAddr) and a (TCP) port.
void MakeNewKey(bool fCompressed)
C++ wrapper for BIGNUM (OpenSSL bignum)
A CService with information about it as peer.
virtual bool AddKey(const CKey &key)
unsigned int ComputeMinWork(unsigned int nBase, int64 nTime)
Calculate the minimum amount of work a received block needs, without knowing its direct parent...
IP address (IPv6, or IPv4 using mapped IPv6 range (::FFFF:0:0/96))
Serialized script, used inside transaction inputs and outputs.
static bool IsBanned(CNetAddr ip)
BOOST_AUTO_TEST_CASE(DoS_banning)
bool Misbehaving(int howmuch)
An encapsulated private key.
CTransaction RandomOrphan()
The basic transaction that is broadcasted on the network and contained in blocks. ...
Information about a peer.
Basic key store, that keeps keys in an address->secret map.
map< string, string > mapArgs