Skip to content

Commit 4e1ebe8

Browse files
authored
Merge pull request #993 from Zannick/refactor
Refactor ZC-specific staking functionality.
2 parents 30f3827 + bfbef4f commit 4e1ebe8

File tree

4 files changed

+41
-33
lines changed

4 files changed

+41
-33
lines changed

src/veil/proofofstake/kernel.cpp

+6-24
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616
#include "util/system.h"
1717
#include "stakeinput.h"
1818
#include "veil/zerocoin/zchain.h"
19-
#include "libzerocoin/bignum.h"
2019
#include <versionbits.h>
2120

2221
using namespace std;
@@ -47,21 +46,6 @@ bool CheckStake(const CDataStream& ssUniqueID, CAmount nValueIn, const uint64_t
4746
}
4847

4948

50-
//Sets nValueIn with the weighted amount given a certain zerocoin denomination
51-
void WeightStake(CAmount& nValueIn, const libzerocoin::CoinDenomination denom)
52-
{
53-
if (denom == libzerocoin::CoinDenomination::ZQ_ONE_HUNDRED) {
54-
//10% reduction
55-
nValueIn = (nValueIn * 90) / 100;
56-
} else if (denom == libzerocoin::CoinDenomination::ZQ_ONE_THOUSAND) {
57-
//20% reduction
58-
nValueIn = (nValueIn * 80) / 100;
59-
} else if (denom == libzerocoin::CoinDenomination::ZQ_TEN_THOUSAND) {
60-
//30% reduction
61-
nValueIn = (nValueIn * 70) / 100;
62-
}
63-
}
64-
6549
std::set<uint256> setFoundStakes;
6650
bool Stake(CStakeInput* stakeInput, unsigned int nBits, unsigned int nTimeBlockFrom, unsigned int& nTimeTx, const CBlockIndex* pindexBest, uint256& hashProofOfStake, bool fWeightStake)
6751
{
@@ -84,11 +68,8 @@ bool Stake(CStakeInput* stakeInput, unsigned int nBits, unsigned int nTimeBlockF
8468
int64_t nMaxTime = (int)GetAdjustedTime() + MAX_FUTURE_BLOCK_TIME - 40;
8569

8670
CDataStream ssUniqueID = stakeInput->GetUniqueness();
87-
CAmount nValueIn = stakeInput->GetValue();
88-
89-
//Adjust stake weights to larger denoms
90-
if (fWeightStake)
91-
WeightStake(nValueIn, stakeInput->GetDenomination());
71+
// Adjust stake weights
72+
CAmount nValueIn = fWeightStake ? stakeInput->GetWeight() : stakeInput->GetValue();
9273

9374
int nBestHeight = pindexBest->nHeight;
9475
uint256 hashBestBlock = pindexBest->GetBlockHash();
@@ -167,11 +148,12 @@ bool CheckProofOfStake(CBlockIndex* pindexCheck, const CTransactionRef txRef, co
167148

168149
unsigned int nBlockFromTime = blockprev.nTime;
169150
unsigned int nTxTime = nTimeBlock;
170-
CAmount nValue = stake->GetValue();
171151

172152
// Enforce VIP-1 after it was activated
173-
if ((int)nTxTime > Params().EnforceWeightReductionTime())
174-
WeightStake(nValue, stake->GetDenomination());
153+
CAmount nValue = (int)nTxTime > Params().EnforceWeightReductionTime()
154+
? stake->GetWeight()
155+
: stake->GetValue();
156+
175157

176158
if (!CheckStake(stake->GetUniqueness(), nValue, nStakeModifier, ArithToUint256(bnTargetPerCoinDay), nBlockFromTime,
177159
nTxTime, hashProofOfStake)) {

src/veil/proofofstake/stakeinput.cpp

+22
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,21 @@ CAmount ZerocoinStake::GetValue()
166166
return denom * COIN;
167167
}
168168

169+
CAmount ZerocoinStake::GetWeight()
170+
{
171+
if (denom == libzerocoin::CoinDenomination::ZQ_ONE_HUNDRED) {
172+
//10% reduction
173+
return (denom * COIN * 9) / 10;
174+
} else if (denom == libzerocoin::CoinDenomination::ZQ_ONE_THOUSAND) {
175+
//20% reduction
176+
return (denom * COIN * 8) / 10;
177+
} else if (denom == libzerocoin::CoinDenomination::ZQ_TEN_THOUSAND) {
178+
//30% reduction
179+
return (denom * COIN * 7) / 10;
180+
}
181+
return denom * COIN;
182+
}
183+
169184
int ZerocoinStake::HeightToModifierHeight(int nHeight)
170185
{
171186
//Nearest multiple of KernelModulus that is over KernelModulus bocks deep in the chain
@@ -277,6 +292,13 @@ bool ZerocoinStake::CreateTxOuts(CWallet* pwallet, std::vector<CTxOut>& vout, CA
277292
#endif
278293
}
279294

295+
bool ZerocoinStake::CompleteTx(CWallet* pwallet, CMutableTransaction& txNew)
296+
{
297+
if (!MarkSpent(pwallet, txNew.GetHash()))
298+
return error("%s: failed to mark mint as used\n", __func__);
299+
return true;
300+
}
301+
280302
bool ZerocoinStake::GetTxFrom(CTransaction& tx)
281303
{
282304
return false;

src/veil/proofofstake/stakeinput.h

+10-4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
// Copyright (c) 2017-2019 The PIVX developers
2-
// Copyright (c) 2019 The Veil developers
2+
// Copyright (c) 2019-2022 The Veil developers
33
// Distributed under the MIT software license, see the accompanying
44
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
55

@@ -25,14 +25,17 @@ class CStakeInput
2525
public:
2626
virtual ~CStakeInput(){};
2727
virtual CBlockIndex* GetIndexFrom() = 0;
28-
virtual bool CreateTxIn(CWallet* pwallet, CTxIn& txIn, uint256 hashTxOut = uint256()) = 0;
2928
virtual bool GetTxFrom(CTransaction& tx) = 0;
3029
virtual CAmount GetValue() = 0;
31-
virtual bool CreateTxOuts(CWallet* pwallet, std::vector<CTxOut>& vout, CAmount nTotal) = 0;
30+
virtual CAmount GetWeight() = 0;
3231
virtual bool GetModifier(uint64_t& nStakeModifier, const CBlockIndex* pindexChainPrev) = 0;
3332
virtual bool IsZerocoins() = 0;
3433
virtual CDataStream GetUniqueness() = 0;
3534
libzerocoin::CoinDenomination GetDenomination() {return denom;};
35+
36+
virtual bool CreateTxIn(CWallet* pwallet, CTxIn& txIn, uint256 hashTxOut = uint256()) = 0;
37+
virtual bool CreateTxOuts(CWallet* pwallet, std::vector<CTxOut>& vout, CAmount nTotal) = 0;
38+
virtual bool CompleteTx(CWallet* pwallet, CMutableTransaction& txNew) = 0;
3639
};
3740

3841

@@ -60,12 +63,15 @@ class ZerocoinStake : public CStakeInput
6063
CBlockIndex* GetIndexFrom() override;
6164
bool GetTxFrom(CTransaction& tx) override;
6265
CAmount GetValue() override;
66+
CAmount GetWeight() override;
6367
bool GetModifier(uint64_t& nStakeModifier, const CBlockIndex* pindexChainPrev) override;
6468
CDataStream GetUniqueness() override;
6569
bool CreateTxIn(CWallet* pwallet, CTxIn& txIn, uint256 hashTxOut = uint256()) override;
6670
bool CreateTxOuts(CWallet* pwallet, std::vector<CTxOut>& vout, CAmount nTotal) override;
67-
bool MarkSpent(CWallet* pwallet, const uint256& txid);
71+
bool CompleteTx(CWallet* pwallet, CMutableTransaction& txNew) override;
6872
bool IsZerocoins() override { return true; }
73+
74+
bool MarkSpent(CWallet* pwallet, const uint256& txid);
6975
int GetChecksumHeightFromMint();
7076
int GetChecksumHeightFromSpend();
7177
uint256 GetChecksum();

src/wallet/wallet.cpp

+3-5
Original file line numberDiff line numberDiff line change
@@ -3884,10 +3884,10 @@ bool CWallet::CreateCoinStake(const CBlockIndex* pindexBest, unsigned int nBits,
38843884
if (GetAdjustedTime() - chainActive.Tip()->GetBlockTime() < 1)
38853885
UninterruptibleSleep(std::chrono::milliseconds{2500});
38863886

3887-
CAmount nCredit = 0;
38883887
CScript scriptPubKeyKernel;
38893888
bool fKernelFound = false;
38903889
for (std::unique_ptr<ZerocoinStake>& stakeInput : listInputs) {
3890+
CAmount nCredit = 0;
38913891
// Make sure the wallet is unlocked and shutdown hasn't been requested
38923892
if (IsLocked() || ShutdownRequested())
38933893
return false;
@@ -3964,16 +3964,14 @@ bool CWallet::CreateCoinStake(const CBlockIndex* pindexBest, unsigned int nBits,
39643964
LogPrintf("%s : failed to create TxIn\n", __func__);
39653965
txNew.vin.clear();
39663966
txNew.vpout.clear();
3967-
nCredit = 0;
39683967
continue;
39693968
}
39703969
}
39713970
txNew.vin.emplace_back(in);
39723971

39733972
//Mark mints as spent
3974-
auto* z = (ZerocoinStake*)stakeInput.get();
3975-
if (!z->MarkSpent(this, txNew.GetHash()))
3976-
return error("%s: failed to mark mint as used\n", __func__);
3973+
if (!stakeInput->CompleteTx(this, txNew))
3974+
return false;
39773975

39783976
fKernelFound = true;
39793977
break;

0 commit comments

Comments
 (0)