Permalink
Browse files

[WIP]

  • Loading branch information...
ebfull committed Nov 2, 2016
1 parent 2d1ecee commit 75f28a936c1af22d3220fc7ab4585b3794877964
Showing with 173 additions and 114 deletions.
  1. +57 −0 src/gtest/test_fillnewblock.cpp
  2. +4 −1 src/miner.cpp
  3. +1 −0 src/miner.h
  4. +1 −113 src/test/coins_tests.cpp
  5. +110 −0 src/test/common_coins.cpp
@@ -11,6 +11,8 @@ std::function<void(const uint256, double&, CAmount&)> noopApplyDeltas = []
// No-op
};
#include "test/common_coins.cpp"
TEST(FillNewBlock, EmptyMemPool) {
SelectParams(CBaseChainParams::MAIN);
const CChainParams& chainparams = Params();
@@ -26,9 +28,14 @@ TEST(FillNewBlock, EmptyMemPool) {
CBlock block;
std::vector<CAmount> vTxFees;
std::vector<int64_t> vTxSigOps;
CCoinsViewTest backingtest;
CCoinsViewCache view(&backingtest);
FillNewBlock(&block, &indexPrev,
scriptDummy, chainparams,
vTxFees, vTxSigOps,
view,
mapTx, noopApplyDeltas,
DEFAULT_BLOCK_MAX_SIZE,
DEFAULT_BLOCK_PRIORITY_SIZE,
@@ -51,3 +58,53 @@ TEST(FillNewBlock, EmptyMemPool) {
EXPECT_EQ(0, vTxFees[0]);
EXPECT_EQ(0, vTxSigOps[0]);
}
TEST(FillNewBlock, PureJoinSplit) {
SelectParams(CBaseChainParams::MAIN);
const CChainParams& chainparams = Params();
CScript scriptDummy = CScript() << OP_TRUE;
auto prevHash = GetRandHash();
CBlockIndex indexPrev;
indexPrev.nHeight = 50;
indexPrev.phashBlock = &prevHash;
std::map<uint256, CTxMemPoolEntry> mapTx;
CMutableTransaction mtx;
mtx.nVersion = 2;
JSDescription js;
js.vpub_new = 10000;
js.anchor = ZCIncrementalMerkleTree::empty_root();
js.nullifiers[0] = libzcash::random_uint256();
js.nullifiers[1] = libzcash::random_uint256();
mtx.vjoinsplit.push_back(js);
CTransaction tx(mtx);
mapTx[tx.GetHash()] = CTxMemPoolEntry(
tx,
10000,
1477741360,
1,
50,
true
);
CBlock block;
std::vector<CAmount> vTxFees;
std::vector<int64_t> vTxSigOps;
CCoinsViewTest backingtest;
CCoinsViewCache view(&backingtest);
FillNewBlock(&block, &indexPrev,
scriptDummy, chainparams,
vTxFees, vTxSigOps,
view,
mapTx, noopApplyDeltas,
DEFAULT_BLOCK_MAX_SIZE,
DEFAULT_BLOCK_PRIORITY_SIZE,
DEFAULT_BLOCK_MIN_SIZE);
EXPECT_EQ(2, block.vtx.size());
}
View
@@ -134,10 +134,13 @@ CBlockTemplate* CreateNewBlock(const CScript& scriptPubKeyIn)
mempool.ApplyDeltas(hash, dPriorityDelta, nFeeDelta);
};
CCoinsViewCache view(pcoinsTip);
FillNewBlock(pblock, pindexPrev,
scriptPubKeyIn, chainparams,
pblocktemplate->vTxFees,
pblocktemplate->vTxSigOps,
view,
mempool.mapTx, f,
nBlockMaxSize, nBlockPrioritySize, nBlockMinSize);
@@ -155,6 +158,7 @@ void FillNewBlock(CBlock* pblock,
const CChainParams& chainparams,
std::vector<CAmount>& vTxFees,
std::vector<int64_t>& vTxSigOps,
CCoinsViewCache& view,
std::map<uint256, CTxMemPoolEntry>& mapTx,
std::function<void(const uint256, double&, CAmount&)> mempoolApplyDeltas,
unsigned int nBlockMaxSize,
@@ -174,7 +178,6 @@ void FillNewBlock(CBlock* pblock,
const int nHeight = pindexPrev->nHeight + 1;
pblock->nTime = GetAdjustedTime();
const int64_t nMedianTimePast = pindexPrev->GetMedianTimePast();
CCoinsViewCache view(pcoinsTip);
// Priority order to process transactions
list<COrphan> vOrphan; // list memory doesn't move
View
@@ -34,6 +34,7 @@ void FillNewBlock(CBlock* pblock,
const CChainParams& chainparams,
std::vector<CAmount>& vTxFees,
std::vector<int64_t>& vTxSigOps,
CCoinsViewCache& view,
std::map<uint256, CTxMemPoolEntry>& mapTx,
std::function<void(const uint256, double&, CAmount&)> mempoolApplyDeltas,
unsigned int nBlockMaxSize,
View
@@ -19,117 +19,7 @@
#include <boost/test/unit_test.hpp>
#include "zcash/IncrementalMerkleTree.hpp"
namespace
{
class CCoinsViewTest : public CCoinsView
{
uint256 hashBestBlock_;
uint256 hashBestAnchor_;
std::map<uint256, CCoins> map_;
std::map<uint256, ZCIncrementalMerkleTree> mapAnchors_;
std::map<uint256, bool> mapNullifiers_;
public:
CCoinsViewTest() {
hashBestAnchor_ = ZCIncrementalMerkleTree::empty_root();
}
bool GetAnchorAt(const uint256& rt, ZCIncrementalMerkleTree &tree) const {
if (rt == ZCIncrementalMerkleTree::empty_root()) {
ZCIncrementalMerkleTree new_tree;
tree = new_tree;
return true;
}
std::map<uint256, ZCIncrementalMerkleTree>::const_iterator it = mapAnchors_.find(rt);
if (it == mapAnchors_.end()) {
return false;
} else {
tree = it->second;
return true;
}
}
bool GetNullifier(const uint256 &nf) const
{
std::map<uint256, bool>::const_iterator it = mapNullifiers_.find(nf);
if (it == mapNullifiers_.end()) {
return false;
} else {
// The map shouldn't contain any false entries.
assert(it->second);
return true;
}
}
uint256 GetBestAnchor() const { return hashBestAnchor_; }
bool GetCoins(const uint256& txid, CCoins& coins) const
{
std::map<uint256, CCoins>::const_iterator it = map_.find(txid);
if (it == map_.end()) {
return false;
}
coins = it->second;
if (coins.IsPruned() && insecure_rand() % 2 == 0) {
// Randomly return false in case of an empty entry.
return false;
}
return true;
}
bool HaveCoins(const uint256& txid) const
{
CCoins coins;
return GetCoins(txid, coins);
}
uint256 GetBestBlock() const { return hashBestBlock_; }
bool BatchWrite(CCoinsMap& mapCoins,
const uint256& hashBlock,
const uint256& hashAnchor,
CAnchorsMap& mapAnchors,
CNullifiersMap& mapNullifiers)
{
for (CCoinsMap::iterator it = mapCoins.begin(); it != mapCoins.end(); ) {
map_[it->first] = it->second.coins;
if (it->second.coins.IsPruned() && insecure_rand() % 3 == 0) {
// Randomly delete empty entries on write.
map_.erase(it->first);
}
mapCoins.erase(it++);
}
for (CAnchorsMap::iterator it = mapAnchors.begin(); it != mapAnchors.end(); ) {
if (it->second.entered) {
std::map<uint256, ZCIncrementalMerkleTree>::iterator ret =
mapAnchors_.insert(std::make_pair(it->first, ZCIncrementalMerkleTree())).first;
ret->second = it->second.tree;
} else {
mapAnchors_.erase(it->first);
}
mapAnchors.erase(it++);
}
for (CNullifiersMap::iterator it = mapNullifiers.begin(); it != mapNullifiers.end(); ) {
if (it->second.entered) {
mapNullifiers_[it->first] = true;
} else {
mapNullifiers_.erase(it->first);
}
mapNullifiers.erase(it++);
}
mapCoins.clear();
mapAnchors.clear();
mapNullifiers.clear();
hashBestBlock_ = hashBlock;
hashBestAnchor_ = hashAnchor;
return true;
}
bool GetStats(CCoinsStats& stats) const { return false; }
};
#include "test/common_coins.cpp"
class CCoinsViewCacheTest : public CCoinsViewCache
{
@@ -150,8 +40,6 @@ class CCoinsViewCacheTest : public CCoinsViewCache
};
}
uint256 appendRandomCommitment(ZCIncrementalMerkleTree &tree)
{
libzcash::SpendingKey k = libzcash::SpendingKey::random();
View
@@ -0,0 +1,110 @@
class CCoinsViewTest : public CCoinsView
{
uint256 hashBestBlock_;
uint256 hashBestAnchor_;
std::map<uint256, CCoins> map_;
std::map<uint256, ZCIncrementalMerkleTree> mapAnchors_;
std::map<uint256, bool> mapNullifiers_;
public:
CCoinsViewTest() {
hashBestAnchor_ = ZCIncrementalMerkleTree::empty_root();
}
bool GetAnchorAt(const uint256& rt, ZCIncrementalMerkleTree &tree) const {
if (rt == ZCIncrementalMerkleTree::empty_root()) {
ZCIncrementalMerkleTree new_tree;
tree = new_tree;
return true;
}
std::map<uint256, ZCIncrementalMerkleTree>::const_iterator it = mapAnchors_.find(rt);
if (it == mapAnchors_.end()) {
return false;
} else {
tree = it->second;
return true;
}
}
bool GetNullifier(const uint256 &nf) const
{
std::map<uint256, bool>::const_iterator it = mapNullifiers_.find(nf);
if (it == mapNullifiers_.end()) {
return false;
} else {
// The map shouldn't contain any false entries.
assert(it->second);
return true;
}
}
uint256 GetBestAnchor() const { return hashBestAnchor_; }
bool GetCoins(const uint256& txid, CCoins& coins) const
{
std::map<uint256, CCoins>::const_iterator it = map_.find(txid);
if (it == map_.end()) {
return false;
}
coins = it->second;
if (coins.IsPruned() && insecure_rand() % 2 == 0) {
// Randomly return false in case of an empty entry.
return false;
}
return true;
}
bool HaveCoins(const uint256& txid) const
{
CCoins coins;
return GetCoins(txid, coins);
}
uint256 GetBestBlock() const { return hashBestBlock_; }
bool BatchWrite(CCoinsMap& mapCoins,
const uint256& hashBlock,
const uint256& hashAnchor,
CAnchorsMap& mapAnchors,
CNullifiersMap& mapNullifiers)
{
for (CCoinsMap::iterator it = mapCoins.begin(); it != mapCoins.end(); ) {
map_[it->first] = it->second.coins;
if (it->second.coins.IsPruned() && insecure_rand() % 3 == 0) {
// Randomly delete empty entries on write.
map_.erase(it->first);
}
mapCoins.erase(it++);
}
for (CAnchorsMap::iterator it = mapAnchors.begin(); it != mapAnchors.end(); ) {
if (it->second.entered) {
std::map<uint256, ZCIncrementalMerkleTree>::iterator ret =
mapAnchors_.insert(std::make_pair(it->first, ZCIncrementalMerkleTree())).first;
ret->second = it->second.tree;
} else {
mapAnchors_.erase(it->first);
}
mapAnchors.erase(it++);
}
for (CNullifiersMap::iterator it = mapNullifiers.begin(); it != mapNullifiers.end(); ) {
if (it->second.entered) {
mapNullifiers_[it->first] = true;
} else {
mapNullifiers_.erase(it->first);
}
mapNullifiers.erase(it++);
}
mapCoins.clear();
mapAnchors.clear();
mapNullifiers.clear();
hashBestBlock_ = hashBlock;
hashBestAnchor_ = hashAnchor;
return true;
}
bool GetStats(CCoinsStats& stats) const { return false; }
};

0 comments on commit 75f28a9

Please sign in to comment.