Skip to content

Commit 4800729

Browse files
committed
[Core][Cleanup] Remove modifier as member of StakeInput objects
Move it to kernel functions. Refactor kernel.cpp / kernel.h
1 parent 238ea5f commit 4800729

File tree

8 files changed

+321
-384
lines changed

8 files changed

+321
-384
lines changed

src/kernel.cpp

Lines changed: 300 additions & 305 deletions
Large diffs are not rendered by default.

src/kernel.h

Lines changed: 20 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,47 +1,37 @@
11
// Copyright (c) 2011-2013 The PPCoin developers
22
// Copyright (c) 2013-2014 The NovaCoin Developers
33
// Copyright (c) 2014-2018 The BlackCoin Developers
4-
// Copyright (c) 2015-2019 The PIVX developers
4+
// Copyright (c) 2015-2020 The PIVX developers
55
// Distributed under the MIT/X11 software license, see the accompanying
66
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
77

8-
#ifndef BITCOIN_KERNEL_H
9-
#define BITCOIN_KERNEL_H
8+
#ifndef PIVX_KERNEL_H
9+
#define PIVX_KERNEL_H
1010

1111
#include "main.h"
1212
#include "stakeinput.h"
1313

14-
15-
// MODIFIER_INTERVAL: time to elapse before new modifier is computed
16-
static const unsigned int MODIFIER_INTERVAL = 60;
17-
18-
// MODIFIER_INTERVAL_RATIO:
19-
// ratio of group interval length between the last group and the first group
20-
static const int MODIFIER_INTERVAL_RATIO = 3;
21-
22-
// Compute the hash modifier for proof-of-stake
23-
bool GetKernelStakeModifier(const uint256& hashBlockFrom, uint64_t& nStakeModifier, int& nStakeModifierHeight, int64_t& nStakeModifierTime, bool fPrintProofOfStake);
24-
bool ComputeNextStakeModifier(const CBlockIndex* pindexPrev, uint64_t& nStakeModifier, bool& fGeneratedStakeModifier);
25-
uint256 ComputeStakeModifier(const CBlockIndex* pindexPrev, const uint256& kernel);
26-
bool Stake(const CBlockIndex* pindexPrev, CStakeInput* stakeInput, unsigned int nBits, int64_t& nTimeTx, uint256& hashProofOfStake);
27-
14+
/* PoS Validation */
15+
bool GetHashProofOfStake(const CBlockIndex* pindexPrev, CStakeInput* stake, const unsigned int nTimeTx, const bool fVerify, uint256& hashProofOfStakeRet);
16+
bool CheckStakeKernelHash(const CBlockIndex* pindexPrev, const unsigned int nBits, CStakeInput* stake, const unsigned int nTimeTx, uint256& hashProofOfStake, const bool fVerify = false);
17+
bool CheckProofOfStake(const CBlock& block, uint256& hashProofOfStake, std::unique_ptr<CStakeInput>& stake, int nPreviousBlockHeight);
2818
// Initialize the stake input object
2919
bool initStakeInput(const CBlock& block, std::unique_ptr<CStakeInput>& stake, int nPreviousBlockHeight);
20+
// (New) Stake Modifier
21+
uint256 ComputeStakeModifier(const CBlockIndex* pindexPrev, const uint256& kernel);
22+
// Stake (find valid kernel)
23+
bool Stake(const CBlockIndex* pindexPrev, CStakeInput* stakeInput, unsigned int nBits, int64_t& nTimeTx, uint256& hashProofOfStake);
3024

31-
// Check kernel hash target and coinstake signature
32-
// Sets hashProofOfStake on success return
33-
bool CheckProofOfStake(const CBlock& block, uint256& hashProofOfStake, std::unique_ptr<CStakeInput>& stake, int nPreviousBlockHeight);
34-
bool CheckStakeKernelHash(const CBlockIndex* pindexPrev, const unsigned int nBits, CStakeInput* stake, const unsigned int nTimeTx, uint256& hashProofOfStake, const bool fVerify = false);
35-
// Returns the proof of stake hash
36-
bool GetHashProofOfStake(const CBlockIndex* pindexPrev, CStakeInput* stake, const unsigned int nTimeTx, const bool fVerify, uint256& hashProofOfStakeRet);
37-
// Get stake modifier checksum
38-
unsigned int GetStakeModifierChecksum(const CBlockIndex* pindex);
39-
40-
// Check stake modifier hard checkpoints
41-
bool CheckStakeModifierCheckpoints(int nHeight, unsigned int nStakeModifierChecksum);
42-
25+
/* Utils */
4326
int64_t GetTimeSlot(const int64_t nTime);
4427
int64_t GetCurrentTimeSlot();
4528
uint32_t ParseAccChecksum(uint256 nCheckpoint, const libzerocoin::CoinDenomination denom);
4629

47-
#endif // BITCOIN_KERNEL_H
30+
31+
/* Old Stake Modifier */
32+
unsigned int GetStakeModifierChecksum(const CBlockIndex* pindex);
33+
bool GetOldStakeModifier(CStakeInput* stake, uint64_t& nStakeModifier);
34+
bool GetOldModifier(const uint256& hashBlockFrom, uint64_t& nStakeModifier);
35+
bool ComputeNextStakeModifier(const CBlockIndex* pindexPrev, uint64_t& nStakeModifier, bool& fGeneratedStakeModifier);
36+
37+
#endif // PIVX_KERNEL_H

src/main.cpp

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4128,8 +4128,6 @@ CBlockIndex* AddToBlockIndex(const CBlock& block)
41284128
LogPrintf("AddToBlockIndex() : ComputeNextStakeModifier() failed \n");
41294129
pindexNew->SetStakeModifier(nStakeModifier, fGeneratedStakeModifier);
41304130
pindexNew->nStakeModifierChecksum = GetStakeModifierChecksum(pindexNew);
4131-
if (!CheckStakeModifierCheckpoints(pindexNew->nHeight, pindexNew->nStakeModifierChecksum))
4132-
LogPrintf("AddToBlockIndex() : Rejected by stake modifier checkpoint height=%d, modifier=%s \n", pindexNew->nHeight, std::to_string(nStakeModifier));
41334131
} else {
41344132
// compute v2 stake modifier
41354133
pindexNew->nStakeModifierV2 = ComputeStakeModifier(pindexNew->pprev, block.vtx[1].vin[0].prevout.hash);

src/stakeinput.cpp

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -83,21 +83,6 @@ bool CPivStake::CreateTxOuts(CWallet* pwallet, std::vector<CTxOut>& vout, CAmoun
8383
return true;
8484
}
8585

86-
bool CPivStake::GetModifier(uint64_t& nStakeModifier)
87-
{
88-
if (this->nStakeModifier == 0) {
89-
// look for the modifier
90-
GetIndexFrom();
91-
if (!pindexFrom)
92-
return error("%s: failed to get index from", __func__);
93-
// TODO: This method must be removed from here in the short terms.. it's a call to an static method in kernel.cpp when this class method is only called from kernel.cpp, no comments..
94-
if (!GetKernelStakeModifier(pindexFrom->GetBlockHash(), this->nStakeModifier, this->nStakeModifierHeight, this->nStakeModifierTime, false))
95-
return error("CheckStakeKernelHash(): failed to get kernel stake modifier");
96-
}
97-
nStakeModifier = this->nStakeModifier;
98-
return true;
99-
}
100-
10186
CDataStream CPivStake::GetUniqueness() const
10287
{
10388
//The unique identifier for a PIV stake is the outpoint

src/stakeinput.h

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@ class CStakeInput
2525
virtual bool GetTxFrom(CTransaction& tx) const = 0;
2626
virtual CAmount GetValue() const = 0;
2727
virtual bool CreateTxOuts(CWallet* pwallet, std::vector<CTxOut>& vout, CAmount nTotal) = 0;
28-
virtual bool GetModifier(uint64_t& nStakeModifier) = 0;
2928
virtual bool IsZPIV() const = 0;
3029
virtual CDataStream GetUniqueness() const = 0;
3130

@@ -37,10 +36,6 @@ class CPivStake : public CStakeInput
3736
CTransaction txFrom;
3837
unsigned int nPosition;
3938

40-
// cached data
41-
uint64_t nStakeModifier = 0;
42-
int nStakeModifierHeight = 0;
43-
int64_t nStakeModifierTime = 0;
4439
public:
4540
CPivStake(){}
4641

@@ -49,7 +44,6 @@ class CPivStake : public CStakeInput
4944
CBlockIndex* GetIndexFrom() override;
5045
bool GetTxFrom(CTransaction& tx) const override;
5146
CAmount GetValue() const override;
52-
bool GetModifier(uint64_t& nStakeModifier) override;
5347
CDataStream GetUniqueness() const override;
5448
bool CreateTxIn(CWallet* pwallet, CTxIn& txIn, uint256 hashTxOut = 0) override;
5549
bool CreateTxOuts(CWallet* pwallet, std::vector<CTxOut>& vout, CAmount nTotal) override;

src/wallet/wallet.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2613,7 +2613,7 @@ bool CWallet::CreateCoinStake(
26132613

26142614
// This should never happen
26152615
if (stakeInput->IsZPIV()) {
2616-
LogPrintf("%s: ERROR - zPOS is disabled\n");
2616+
LogPrintf("%s: ERROR - zPOS is disabled\n", __func__);
26172617
continue;
26182618
}
26192619

src/zpiv/zpos.cpp

Lines changed: 0 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -53,30 +53,6 @@ CAmount CLegacyZPivStake::GetValue() const
5353
return denom * COIN;
5454
}
5555

56-
bool CLegacyZPivStake::GetModifier(uint64_t& nStakeModifier)
57-
{
58-
CBlockIndex* pindex = GetIndexFrom();
59-
if (!pindex)
60-
return error("%s: failed to get index from", __func__);
61-
62-
if(Params().IsRegTestNet()) {
63-
nStakeModifier = 0;
64-
return true;
65-
}
66-
67-
int64_t nTimeBlockFrom = pindex->GetBlockTime();
68-
const int nHeightStop = std::min(chainActive.Height(), Params().Zerocoin_Block_Last_Checkpoint()-1);
69-
while (pindex && pindex->nHeight + 1 <= nHeightStop) {
70-
if (pindex->GetBlockTime() - nTimeBlockFrom > 60 * 60) {
71-
nStakeModifier = pindex->nAccumulatorCheckpoint.Get64();
72-
return true;
73-
}
74-
pindex = chainActive.Next(pindex);
75-
}
76-
77-
return false;
78-
}
79-
8056
CDataStream CLegacyZPivStake::GetUniqueness() const
8157
{
8258
CDataStream ss(SER_GETHASH, 0);

src/zpiv/zpos.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@ class CLegacyZPivStake : public CStakeInput
2323
uint32_t GetChecksum() const { return nChecksum; }
2424
CBlockIndex* GetIndexFrom() override;
2525
CAmount GetValue() const override;
26-
bool GetModifier(uint64_t& nStakeModifier) override;
2726
CDataStream GetUniqueness() const override;
2827
bool CreateTxIn(CWallet* pwallet, CTxIn& txIn, uint256 hashTxOut = 0) override { return false; /* creation disabled */}
2928
bool CreateTxOuts(CWallet* pwallet, std::vector<CTxOut>& vout, CAmount nTotal) override { return false; /* creation disabled */}

0 commit comments

Comments
 (0)