Skip to content

Commit

Permalink
Enable hard fork to reset shielded pool
Browse files Browse the repository at this point in the history
Enable burn of unmoved coins
  • Loading branch information
Bitcoin Private Developers committed Dec 28, 2018
1 parent f1e9faa commit da947b0
Show file tree
Hide file tree
Showing 14 changed files with 153 additions and 89 deletions.
12 changes: 10 additions & 2 deletions src/chainparams.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,8 @@ class CMainParams : public CChainParams {
consensus.vDeployments[Consensus::DEPLOYMENT_TESTDUMMY].bit = 28;
consensus.vDeployments[Consensus::DEPLOYMENT_TESTDUMMY].nStartTime = 1199145601; // January 1, 2008
consensus.vDeployments[Consensus::DEPLOYMENT_TESTDUMMY].nTimeout = 1230767999; // December 31, 2008
consensus.nUnmovedBurnHeight = 475000;
consensus.zResetHeight = 454000;

pchMessageStart[0] = 0xa8;
pchMessageStart[1] = 0xea;
Expand Down Expand Up @@ -106,7 +108,7 @@ class CMainParams : public CChainParams {
// TODO: setup a DNSSeed
vSeeds.push_back(CDNSSeedData("btcprivate.org", "dnsseed.btcprivate.org"));
vSeeds.push_back(CDNSSeedData("btcprivate.co", "dnsseed.btcprivate.co"));

// guarantees the first 2 characters, when base58 encoded, are "b1"
base58Prefixes[PUBKEY_ADDRESS] = {0x13,0x25};
// guarantees the first 2 characters, when base58 encoded, are "bx"
Expand Down Expand Up @@ -182,6 +184,9 @@ class CTestNetParams : public CMainParams {

consensus.nPowDifficultyBombHeight = 600000;

consensus.nUnmovedBurnHeight = 475000;
consensus.zResetHeight = 450500;

pchMessageStart[0] = 0xf6;
pchMessageStart[1] = 0x1b;
pchMessageStart[2] = 0xf6;
Expand Down Expand Up @@ -268,6 +273,9 @@ class CRegTestParams : public CTestNetParams {

consensus.nPowDifficultyBombHeight = 600000;

consensus.nUnmovedBurnHeight = 500;
consensus.zResetHeight = 300;

pchMessageStart[0] = 0xaa;
pchMessageStart[1] = 0xe8;
pchMessageStart[2] = 0x3f;
Expand Down Expand Up @@ -303,7 +311,7 @@ class CRegTestParams : public CTestNetParams {
0
};

nForkStartHeight = 0;
nForkStartHeight = 50;
nForkHeightRange = 0;
}
};
Expand Down
1 change: 1 addition & 0 deletions src/chainparams.h
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ class CChainParams

uint64_t ForkStartHeight() const { return nForkStartHeight; };
uint64_t ForkHeightRange() const { return nForkHeightRange; };

protected:
CChainParams() {}

Expand Down
36 changes: 23 additions & 13 deletions src/coins.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

#include "memusage.h"
#include "random.h"
#include "util.h"
#include "version.h"
#include "policy/fees.h"

Expand Down Expand Up @@ -34,15 +35,15 @@ void CCoins::CalcMaskSize(unsigned int &nBytes, unsigned int &nNonzeroBytes) con
nBytes += nLastUsedByte;
}

bool CCoins::Spend(uint32_t nPos)
bool CCoins::Spend(uint32_t nPos)
{
if (nPos >= vout.size() || vout[nPos].IsNull())
return false;
vout[nPos].SetNull();
Cleanup();
return true;
}
bool CCoinsView::GetAnchorAt(const uint256 &rt, ZCIncrementalMerkleTree &tree) const { return false; }
bool CCoinsView::GetAnchorAt(const uint256 &rt, ZCIncrementalMerkleTree &tree, const bool postBurn) const { return false; }
bool CCoinsView::GetNullifier(const uint256 &nullifier) const { return false; }
bool CCoinsView::GetCoins(const uint256 &txid, CCoins &coins) const { return false; }
bool CCoinsView::HaveCoins(const uint256 &txid) const { return false; }
Expand All @@ -55,10 +56,9 @@ bool CCoinsView::BatchWrite(CCoinsMap &mapCoins,
CNullifiersMap &mapNullifiers) { return false; }
bool CCoinsView::GetStats(CCoinsStats &stats) const { return false; }


CCoinsViewBacked::CCoinsViewBacked(CCoinsView *viewIn) : base(viewIn) { }

bool CCoinsViewBacked::GetAnchorAt(const uint256 &rt, ZCIncrementalMerkleTree &tree) const { return base->GetAnchorAt(rt, tree); }
bool CCoinsViewBacked::GetAnchorAt(const uint256 &rt, ZCIncrementalMerkleTree &tree, const bool postBurn) const { return base->GetAnchorAt(rt, tree, postBurn); }
bool CCoinsViewBacked::GetNullifier(const uint256 &nullifier) const { return base->GetNullifier(nullifier); }
bool CCoinsViewBacked::GetCoins(const uint256 &txid, CCoins &coins) const { return base->GetCoins(txid, coins); }
bool CCoinsViewBacked::HaveCoins(const uint256 &txid) const { return base->HaveCoins(txid); }
Expand Down Expand Up @@ -106,25 +106,25 @@ CCoinsMap::const_iterator CCoinsViewCache::FetchCoins(const uint256 &txid) const
return ret;
}


bool CCoinsViewCache::GetAnchorAt(const uint256 &rt, ZCIncrementalMerkleTree &tree) const {
bool CCoinsViewCache::GetAnchorAt(const uint256 &rt, ZCIncrementalMerkleTree &tree, const bool postBurn) const {
CAnchorsMap::const_iterator it = cacheAnchors.find(rt);
if (it != cacheAnchors.end()) {
if (it->second.entered) {
tree = it->second.tree;
return true;
return (!postBurn || it->second.postBurn);
} else {
return false;
}
}

if (!base->GetAnchorAt(rt, tree)) {
if (!base->GetAnchorAt(rt, tree, postBurn)) {
return false;
}

CAnchorsMap::iterator ret = cacheAnchors.insert(std::make_pair(rt, CAnchorsCacheEntry())).first;
ret->second.entered = true;
ret->second.tree = tree;
ret->second.postBurn = postBurn;
cachedCoinsUsage += ret->second.tree.DynamicMemoryUsage();

return true;
Expand All @@ -144,7 +144,7 @@ bool CCoinsViewCache::GetNullifier(const uint256 &nullifier) const {
return tmp;
}

void CCoinsViewCache::PushAnchor(const ZCIncrementalMerkleTree &tree) {
void CCoinsViewCache::PushAnchor(const ZCIncrementalMerkleTree &tree, const bool postBurn) {
uint256 newrt = tree.root();

auto currentRoot = GetBestAnchor();
Expand All @@ -161,6 +161,7 @@ void CCoinsViewCache::PushAnchor(const ZCIncrementalMerkleTree &tree) {
ret->second.entered = true;
ret->second.tree = tree;
ret->second.flags = CAnchorsCacheEntry::DIRTY;
ret->second.postBurn = postBurn;

if (insertRet.second) {
// An insert took place
Expand All @@ -171,7 +172,7 @@ void CCoinsViewCache::PushAnchor(const ZCIncrementalMerkleTree &tree) {
}
}

void CCoinsViewCache::PopAnchor(const uint256 &newrt) {
void CCoinsViewCache::PopAnchor(const uint256 &newrt, const bool postBurn) {
auto currentRoot = GetBestAnchor();

// Blocks might not change the commitment tree, in which
Expand All @@ -182,11 +183,12 @@ void CCoinsViewCache::PopAnchor(const uint256 &newrt) {
// so that its tree exists in memory.
{
ZCIncrementalMerkleTree tree;
assert(GetAnchorAt(currentRoot, tree));
assert(GetAnchorAt(currentRoot, tree, postBurn));
}

// Mark the anchor as unentered, removing it from view
cacheAnchors[currentRoot].entered = false;
cacheAnchors[currentRoot].postBurn = postBurn;

// Mark the cache entry as dirty so it's propagated
cacheAnchors[currentRoot].flags = CAnchorsCacheEntry::DIRTY;
Expand Down Expand Up @@ -317,6 +319,7 @@ bool CCoinsViewCache::BatchWrite(CCoinsMap &mapCoins,
CAnchorsCacheEntry& entry = cacheAnchors[child_it->first];
entry.entered = child_it->second.entered;
entry.tree = child_it->second.tree;
entry.postBurn = child_it->second.postBurn;
entry.flags = CAnchorsCacheEntry::DIRTY;

cachedCoinsUsage += entry.tree.DynamicMemoryUsage();
Expand All @@ -326,6 +329,13 @@ bool CCoinsViewCache::BatchWrite(CCoinsMap &mapCoins,
parent_it->second.entered = child_it->second.entered;
parent_it->second.flags |= CAnchorsCacheEntry::DIRTY;
}

if (parent_it->second.postBurn != child_it->second.postBurn) {
// The parent may have removed the entry.
parent_it->second.postBurn = child_it->second.postBurn;
parent_it->second.flags |= CAnchorsCacheEntry::DIRTY;
}

}
}

Expand Down Expand Up @@ -392,7 +402,7 @@ CAmount CCoinsViewCache::GetValueIn(const CTransaction& tx) const
return nResult;
}

bool CCoinsViewCache::HaveJoinSplitRequirements(const CTransaction& tx) const
bool CCoinsViewCache::HaveJoinSplitRequirements(const CTransaction& tx, const bool postBurn) const
{
boost::unordered_map<uint256, ZCIncrementalMerkleTree, CCoinsKeyHasher> intermediates;

Expand All @@ -411,7 +421,7 @@ bool CCoinsViewCache::HaveJoinSplitRequirements(const CTransaction& tx) const
auto it = intermediates.find(joinsplit.anchor);
if (it != intermediates.end()) {
tree = it->second;
} else if (!GetAnchorAt(joinsplit.anchor, tree)) {
} else if (!GetAnchorAt(joinsplit.anchor, tree, postBurn)) {
return false;
}

Expand Down
27 changes: 15 additions & 12 deletions src/coins.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
#include <boost/unordered_map.hpp>
#include "zcash/IncrementalMerkleTree.hpp"

/**
/**
* Pruned version of CTransaction: only retains metadata and unspent transaction outputs
*
* Serialized format:
Expand Down Expand Up @@ -301,12 +301,13 @@ struct CAnchorsCacheEntry
bool entered; // This will be false if the anchor is removed from the cache
ZCIncrementalMerkleTree tree; // The tree itself
unsigned char flags;
bool postBurn;

enum Flags {
DIRTY = (1 << 0), // This cache entry is potentially different from the version in the parent view.
};

CAnchorsCacheEntry() : entered(false), flags(0) {}
CAnchorsCacheEntry() : entered(false), flags(0), postBurn(false) {}
};

struct CNullifiersCacheEntry
Expand Down Expand Up @@ -344,7 +345,7 @@ class CCoinsView
{
public:
//! Retrieve the tree at a particular anchored root in the chain
virtual bool GetAnchorAt(const uint256 &rt, ZCIncrementalMerkleTree &tree) const;
virtual bool GetAnchorAt(const uint256 &rt, ZCIncrementalMerkleTree &tree, const bool postBurn) const;

//! Determine whether a nullifier is spent or not
virtual bool GetNullifier(const uint256 &nullifier) const;
Expand Down Expand Up @@ -386,12 +387,13 @@ class CCoinsViewBacked : public CCoinsView

public:
CCoinsViewBacked(CCoinsView *viewIn);
bool GetAnchorAt(const uint256 &rt, ZCIncrementalMerkleTree &tree) const;
bool GetAnchorAt(const uint256 &rt, ZCIncrementalMerkleTree &tree, const bool postBurn) const;
bool GetNullifier(const uint256 &nullifier) const;
bool GetCoins(const uint256 &txid, CCoins &coins) const;
bool HaveCoins(const uint256 &txid) const;
uint256 GetBestBlock() const;
uint256 GetBestAnchor() const;

void SetBackend(CCoinsView &viewIn);
bool BatchWrite(CCoinsMap &mapCoins,
const uint256 &hashBlock,
Expand All @@ -404,10 +406,10 @@ class CCoinsViewBacked : public CCoinsView

class CCoinsViewCache;

/**
/**
* A reference to a mutable cache entry. Encapsulating it allows us to run
* cleanup code after the modification is finished, and keeping track of
* concurrent modifications.
* concurrent modifications.
*/
class CCoinsModifier
{
Expand All @@ -434,7 +436,7 @@ class CCoinsViewCache : public CCoinsViewBacked

/**
* Make mutable so that we can "fill the cache" even from Get-methods
* declared as "const".
* declared as "const".
*/
mutable uint256 hashBlock;
mutable CCoinsMap cacheCoins;
Expand All @@ -450,12 +452,13 @@ class CCoinsViewCache : public CCoinsViewBacked
~CCoinsViewCache();

// Standard CCoinsView methods
bool GetAnchorAt(const uint256 &rt, ZCIncrementalMerkleTree &tree) const;
bool GetAnchorAt(const uint256 &rt, ZCIncrementalMerkleTree &tree, const bool postBurn) const;
bool GetNullifier(const uint256 &nullifier) const;
bool GetCoins(const uint256 &txid, CCoins &coins) const;
bool HaveCoins(const uint256 &txid) const;
uint256 GetBestBlock() const;
uint256 GetBestAnchor() const;

void SetBestBlock(const uint256 &hashBlock);
bool BatchWrite(CCoinsMap &mapCoins,
const uint256 &hashBlock,
Expand All @@ -466,11 +469,11 @@ class CCoinsViewCache : public CCoinsViewBacked

// Adds the tree to mapAnchors and sets the current commitment
// root to this root.
void PushAnchor(const ZCIncrementalMerkleTree &tree);
void PushAnchor(const ZCIncrementalMerkleTree &tree, const bool postBurn);

// Removes the current commitment root from mapAnchors and sets
// the new current root.
void PopAnchor(const uint256 &rt);
void PopAnchor(const uint256 &rt, const bool postBurn);

// Marks a nullifier as spent or not.
void SetNullifier(const uint256 &nullifier, bool spent);
Expand Down Expand Up @@ -502,7 +505,7 @@ class CCoinsViewCache : public CCoinsViewBacked
//! Calculate the size of the cache (in bytes)
size_t DynamicMemoryUsage() const;

/**
/**
* Amount of bitcoins coming in to a transaction
* Note that lightweight clients may not know anything besides the hash of previous transactions,
* so may not be able to calculate this.
Expand All @@ -516,7 +519,7 @@ class CCoinsViewCache : public CCoinsViewBacked
bool HaveInputs(const CTransaction& tx) const;

//! Check whether all joinsplit requirements (anchors/nullifiers) are satisfied
bool HaveJoinSplitRequirements(const CTransaction& tx) const;
bool HaveJoinSplitRequirements(const CTransaction& tx, const bool postBurn) const;

//! Return priority of tx at height nHeight
double GetPriority(const CTransaction &tx, int nHeight) const;
Expand Down
2 changes: 2 additions & 0 deletions src/consensus/params.h
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,8 @@ struct Params {
int64_t nPowTargetSpacing;

int nPowDifficultyBombHeight;
int nUnmovedBurnHeight;
int zResetHeight;

int64_t AveragingWindowTimespan(bool isFork = false) const { return nPowAveragingWindow * nPowTargetSpacing / (isFork ? 20 : 1); }
int64_t MinActualTimespan(bool isFork = false) const { return (AveragingWindowTimespan(isFork) * (100 - nPowMaxAdjustUp )) / 100; }
Expand Down
Loading

0 comments on commit da947b0

Please sign in to comment.