Skip to content

Commit

Permalink
[zPIV] new protocol enforcement height added. Height not final, just …
Browse files Browse the repository at this point in the history
…randomly selected and tested on regtest.
  • Loading branch information
furszy committed May 23, 2019
1 parent f930016 commit 3c74d6d
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 2 deletions.
9 changes: 9 additions & 0 deletions src/chainparams.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,9 @@ class CMainParams : public CChainParams
nEnforceNewSporkKey = 1525158000; //!> Sporks signed after (GMT): Tuesday, May 1, 2018 7:00:00 AM GMT must use the new spork key
nRejectOldSporkKey = 1527811200; //!> Fully reject old spork key after (GMT): Friday, June 1, 2018 12:00:00 AM

// Public coin spend enforcement
nPublicZCSpends = 2000000;

// Fake Serial Attack
nFakeSerialBlockheightEnd = 1686229;
nSupplyBeforeFakeSerial = 4131563 * COIN; // zerocoin supply at block nFakeSerialBlockheightEnd
Expand Down Expand Up @@ -293,6 +296,9 @@ class CTestNetParams : public CMainParams
nEnforceNewSporkKey = 1521604800; //!> Sporks signed after Wednesday, March 21, 2018 4:00:00 AM GMT must use the new spork key
nRejectOldSporkKey = 1522454400; //!> Reject old spork key after Saturday, March 31, 2018 12:00:00 AM GMT

// Public coin spend enforcement
nPublicZCSpends = 1086574;

// Fake Serial Attack
nFakeSerialBlockheightEnd = -1;
nSupplyBeforeFakeSerial = 0;
Expand Down Expand Up @@ -384,6 +390,9 @@ class CRegTestParams : public CTestNetParams
nBlockFirstFraudulent = 999999999; //First block that bad serials emerged
nBlockLastGoodCheckpoint = 999999999; //Last valid accumulator checkpoint

// Public coin spend enforcement
nPublicZCSpends = 350;

// Fake Serial Attack
nFakeSerialBlockheightEnd = -1;

Expand Down
3 changes: 3 additions & 0 deletions src/chainparams.h
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,8 @@ class CChainParams
int Zerocoin_Block_Double_Accumulated() const { return nBlockDoubleAccumulated; }
CAmount InvalidAmountFiltered() const { return nInvalidAmountFiltered; };

int Zerocoin_Block_Public_Spend_Enabled() const { return nPublicZCSpends; }

protected:
CChainParams() {}

Expand Down Expand Up @@ -200,6 +202,7 @@ class CChainParams
int nBlockEnforceInvalidUTXO;
int nBlockZerocoinV2;
int nBlockDoubleAccumulated;
int nPublicZCSpends;

// fake serial attack
int nFakeSerialBlockheightEnd = 0;
Expand Down
31 changes: 29 additions & 2 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -988,6 +988,21 @@ bool isBlockBetweenFakeSerialAttackRange(int nHeight)
return nHeight <= Params().Zerocoin_Block_EndFakeSerial();
}

bool CheckPublicCoinSpendEnforced(int blockHeight, bool isPrivZerocoinSpend, bool isPublicSpend) {
if (blockHeight >= Params().Zerocoin_Block_Public_Spend_Enabled()) {
// reject old coin spend
if (isPrivZerocoinSpend) {
return error("%s: failed to add block with older zc spend version", __func__);
}

} else {
if (isPublicSpend) {
return error("%s: failed to add block, public spend enforcement not activated", __func__);
}
}
return true;
}

bool ContextualCheckZerocoinSpend(const CTransaction& tx, const CoinSpend* spend, CBlockIndex* pindex, const uint256& hashBlock)
{
if(!ContextualCheckZerocoinSpendNoSerialCheck(tx, spend, pindex, hashBlock)){
Expand Down Expand Up @@ -3209,9 +3224,14 @@ bool ConnectBlock(const CBlock& block, CValidationState& state, CBlockIndex* pin
set<CBigNum> setSerials;
for (const CTxIn& txIn : tx.vin) {
bool isPublicSpend = txIn.scriptSig.IsZerocoinPublicSpend();
if (!txIn.IsZerocoinSpend() && !isPublicSpend)
bool isPrivZerocoinSpend = txIn.IsZerocoinSpend();
if (!isPrivZerocoinSpend && !isPublicSpend)
continue;

// Check enforcement
if (!CheckPublicCoinSpendEnforced(pindex->nHeight, isPrivZerocoinSpend, isPublicSpend)){
return false;
}

if (isPublicSpend) {
libzerocoin::ZerocoinParams* params = Params().Zerocoin_Params(false);
Expand Down Expand Up @@ -4766,7 +4786,14 @@ bool AcceptBlock(CBlock& block, CValidationState& state, CBlockIndex** ppindex,
for (const CTxIn& in: tx.vin) {
if(nHeight >= Params().Zerocoin_StartHeight()) {
bool isPublicSpend = in.scriptSig.IsZerocoinPublicSpend();
if (in.IsZerocoinSpend() || isPublicSpend) {
bool isPrivZerocoinSpend = in.IsZerocoinSpend();
if (isPrivZerocoinSpend || isPublicSpend) {

// Check enforcement
if (!CheckPublicCoinSpendEnforced(pindex->nHeight, isPrivZerocoinSpend, isPublicSpend)){
return false;
}

libzerocoin::CoinSpend spend;
if (isPublicSpend) {
libzerocoin::ZerocoinParams* params = Params().Zerocoin_Params(false);
Expand Down
3 changes: 3 additions & 0 deletions src/main.h
Original file line number Diff line number Diff line change
Expand Up @@ -373,6 +373,9 @@ bool ReindexAccumulators(list<uint256>& listMissingCheckpoints, string& strError
// Fake Serial attack Range
bool isBlockBetweenFakeSerialAttackRange(int nHeight);

// Public coin spend
bool CheckPublicCoinSpendEnforced(int blockHeight, bool isPrivZerocoinSpend, bool isPublicSpend);

/**
* Check if transaction will be final in the next block to be created.
*
Expand Down
7 changes: 7 additions & 0 deletions src/wallet/wallet.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4903,6 +4903,13 @@ bool CWallet::MintsToInputVectorPublicSpend(std::map<CBigNum, CZerocoinMint>& ma

bool CWallet::CreateZerocoinSpendTransaction(CAmount nValue, CWalletTx& wtxNew, CReserveKey& reserveKey, CZerocoinSpendReceipt& receipt, vector<CZerocoinMint>& vSelectedMints, vector<CDeterministicMint>& vNewMints, bool fMintChange, bool fMinimizeChange, CBitcoinAddress* address)
{

// Check enforcement
if (chainActive.Tip()->nHeight < Params().Zerocoin_Block_Public_Spend_Enabled()){
receipt.SetStatus(_("New protocol enforcement not activated"), ZPIV_SPEND_ERROR);
return false;
}

// Check available funds
int nStatus = ZPIV_TRX_FUNDS_PROBLEMS;
if (nValue > GetZerocoinBalance(true)) {
Expand Down

0 comments on commit 3c74d6d

Please sign in to comment.