Changeset View
Changeset View
Standalone View
Standalone View
src/test/coins_tests.cpp
Show First 20 Lines • Show All 298 Lines • ▼ Show 20 Lines | for (int64_t i = 0; i < NUM_SIMULATION_ITERATIONS; i++) { | ||||
uint32_t randiter = insecure_rand(); | uint32_t randiter = insecure_rand(); | ||||
// 19/20 txs add a new transaction | // 19/20 txs add a new transaction | ||||
if (randiter % 20 < 19) { | if (randiter % 20 < 19) { | ||||
CMutableTransaction tx; | CMutableTransaction tx; | ||||
tx.vin.resize(1); | tx.vin.resize(1); | ||||
tx.vout.resize(1); | tx.vout.resize(1); | ||||
// Keep txs unique unless intended to duplicate. | // Keep txs unique unless intended to duplicate. | ||||
tx.vout[0].nValue = i; | tx.vout[0].nValue = Amount(i); | ||||
// Random sizes so we can test memory usage accounting | // Random sizes so we can test memory usage accounting | ||||
tx.vout[0].scriptPubKey.assign(insecure_rand() & 0x3F, 0); | tx.vout[0].scriptPubKey.assign(insecure_rand() & 0x3F, 0); | ||||
unsigned int height = insecure_rand(); | unsigned int height = insecure_rand(); | ||||
Coin old_coin; | Coin old_coin; | ||||
// 2/20 times create a new coinbase | // 2/20 times create a new coinbase | ||||
if (randiter % 20 < 2 || coinbase_coins.size() < 10) { | if (randiter % 20 < 2 || coinbase_coins.size() < 10) { | ||||
// 1/10 of those times create a duplicate coinbase | // 1/10 of those times create a duplicate coinbase | ||||
▲ Show 20 Lines • Show All 179 Lines • ▼ Show 20 Lines | |||||
BOOST_AUTO_TEST_CASE(coin_serialization) { | BOOST_AUTO_TEST_CASE(coin_serialization) { | ||||
// Good example | // Good example | ||||
CDataStream ss1( | CDataStream ss1( | ||||
ParseHex("97f23c835800816115944e077fe7c803cfa57f29b36bf87c1d35"), | ParseHex("97f23c835800816115944e077fe7c803cfa57f29b36bf87c1d35"), | ||||
SER_DISK, CLIENT_VERSION); | SER_DISK, CLIENT_VERSION); | ||||
Coin c1; | Coin c1; | ||||
ss1 >> c1; | ss1 >> c1; | ||||
BOOST_CHECK_EQUAL(c1.IsCoinBase(), false); | BOOST_CHECK_EQUAL(c1.IsCoinBase(), false); | ||||
BOOST_CHECK_EQUAL(c1.GetHeight(), 203998); | BOOST_CHECK_EQUAL(c1.GetHeight(), 203998U); | ||||
BOOST_CHECK_EQUAL(c1.GetTxOut().nValue, Amount(60000000000LL)); | BOOST_CHECK_EQUAL(c1.GetTxOut().nValue, Amount(60000000000LL)); | ||||
BOOST_CHECK_EQUAL(HexStr(c1.GetTxOut().scriptPubKey), | BOOST_CHECK_EQUAL(HexStr(c1.GetTxOut().scriptPubKey), | ||||
HexStr(GetScriptForDestination(CKeyID(uint160(ParseHex( | HexStr(GetScriptForDestination(CKeyID(uint160(ParseHex( | ||||
"816115944e077fe7c803cfa57f29b36bf87c1d35")))))); | "816115944e077fe7c803cfa57f29b36bf87c1d35")))))); | ||||
// Good example | // Good example | ||||
CDataStream ss2( | CDataStream ss2( | ||||
ParseHex("8ddf77bbd123008c988f1a4a4de2161e0f50aac7f17e7f9555caa4"), | ParseHex("8ddf77bbd123008c988f1a4a4de2161e0f50aac7f17e7f9555caa4"), | ||||
Show All 35 Lines | try { | ||||
Coin c5; | Coin c5; | ||||
ss5 >> c5; | ss5 >> c5; | ||||
BOOST_CHECK_MESSAGE(false, "We should have thrown"); | BOOST_CHECK_MESSAGE(false, "We should have thrown"); | ||||
} catch (const std::ios_base::failure &e) { | } catch (const std::ios_base::failure &e) { | ||||
} | } | ||||
} | } | ||||
static const COutPoint OUTPOINT; | static const COutPoint OUTPOINT; | ||||
static const Amount PRUNED = -1; | static const Amount PRUNED(-1); | ||||
static const Amount ABSENT = -2; | static const Amount ABSENT(-2); | ||||
static const Amount FAIL = -3; | static const Amount FAIL(-3); | ||||
static const Amount VALUE1 = 100; | static const Amount VALUE1(100); | ||||
static const Amount VALUE2 = 200; | static const Amount VALUE2(200); | ||||
static const Amount VALUE3 = 300; | static const Amount VALUE3(300); | ||||
static const char DIRTY = CCoinsCacheEntry::DIRTY; | static const char DIRTY = CCoinsCacheEntry::DIRTY; | ||||
static const char FRESH = CCoinsCacheEntry::FRESH; | static const char FRESH = CCoinsCacheEntry::FRESH; | ||||
static const char NO_ENTRY = -1; | static const char NO_ENTRY = -1; | ||||
static const auto FLAGS = {char(0), FRESH, DIRTY, char(DIRTY | FRESH)}; | static const auto FLAGS = {char(0), FRESH, DIRTY, char(DIRTY | FRESH)}; | ||||
static const auto CLEAN_FLAGS = {char(0), FRESH}; | static const auto CLEAN_FLAGS = {char(0), FRESH}; | ||||
static const auto ABSENT_FLAGS = {NO_ENTRY}; | static const auto ABSENT_FLAGS = {NO_ENTRY}; | ||||
Show All 27 Lines | void GetCoinMapEntry(const CCoinsMap &map, Amount &value, char &flags) { | ||||
auto it = map.find(OUTPOINT); | auto it = map.find(OUTPOINT); | ||||
if (it == map.end()) { | if (it == map.end()) { | ||||
value = ABSENT; | value = ABSENT; | ||||
flags = NO_ENTRY; | flags = NO_ENTRY; | ||||
} else { | } else { | ||||
if (it->second.coin.IsSpent()) { | if (it->second.coin.IsSpent()) { | ||||
value = PRUNED; | value = PRUNED; | ||||
} else { | } else { | ||||
value = it->second.coin.GetTxOut().nValue.GetSatoshis(); | value = it->second.coin.GetTxOut().nValue; | ||||
} | } | ||||
flags = it->second.flags; | flags = it->second.flags; | ||||
assert(flags != NO_ENTRY); | assert(flags != NO_ENTRY); | ||||
} | } | ||||
} | } | ||||
void WriteCoinViewEntry(CCoinsView &view, const Amount value, char flags) { | void WriteCoinViewEntry(CCoinsView &view, const Amount value, char flags) { | ||||
CCoinsMap map; | CCoinsMap map; | ||||
▲ Show 20 Lines • Show All 286 Lines • Show Last 20 Lines |