Skip to content

Commit c6f71b2

Browse files
committed
[Refactoring] Decouple coinstake creation/signing
1 parent 5f3d3ba commit c6f71b2

File tree

6 files changed

+16
-16
lines changed

6 files changed

+16
-16
lines changed

src/blockassembler.cpp

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,10 +76,14 @@ bool SolveProofOfStake(CBlock* pblock, CBlockIndex* pindexPrev, CWallet* pwallet
7676

7777
CMutableTransaction txCoinStake;
7878
int64_t nTxNewTime = 0;
79-
if (!pwallet->CreateCoinStake(*pwallet, pindexPrev, pblock->nBits, txCoinStake, nTxNewTime, availableCoins)) {
79+
if (!pwallet->CreateCoinStake(pindexPrev, pblock->nBits, txCoinStake, nTxNewTime, availableCoins)) {
8080
LogPrint(BCLog::STAKING, "%s : stake not found\n", __func__);
8181
return false;
8282
}
83+
if (!pwallet->SignCoinStake(txCoinStake)) {
84+
const COutPoint& stakeIn = txCoinStake.vin[0].prevout;
85+
return error("Unable to sign coinstake with input %s-%d", stakeIn.hash.ToString(), stakeIn.n);
86+
}
8387
// Stake found
8488
pblock->nTime = nTxNewTime;
8589

src/stakeinput.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ CAmount CPivStake::GetValue() const
5656
return outputFrom.nValue;
5757
}
5858

59-
bool CPivStake::CreateTxOuts(CWallet* pwallet, std::vector<CTxOut>& vout, CAmount nTotal)
59+
bool CPivStake::CreateTxOuts(const CWallet* pwallet, std::vector<CTxOut>& vout, CAmount nTotal) const
6060
{
6161
std::vector<valtype> vSolutions;
6262
txnouttype whichType;

src/stakeinput.h

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@ class CStakeInput
2525
virtual const CBlockIndex* GetIndexFrom() const = 0;
2626
virtual bool GetTxOutFrom(CTxOut& out) const = 0;
2727
virtual CAmount GetValue() const = 0;
28-
virtual bool CreateTxOuts(CWallet* pwallet, std::vector<CTxOut>& vout, CAmount nTotal) = 0;
2928
virtual bool IsZPIV() const = 0;
3029
virtual CDataStream GetUniqueness() const = 0;
3130
virtual bool ContextCheck(int nHeight, uint32_t nTime) = 0;
@@ -50,7 +49,7 @@ class CPivStake : public CStakeInput
5049
CAmount GetValue() const override;
5150
CDataStream GetUniqueness() const override;
5251
CTxIn GetTxIn() const;
53-
bool CreateTxOuts(CWallet* pwallet, std::vector<CTxOut>& vout, CAmount nTotal) override;
52+
bool CreateTxOuts(const CWallet* pwallet, std::vector<CTxOut>& vout, CAmount nTotal) const;
5453
bool IsZPIV() const override { return false; }
5554
bool ContextCheck(int nHeight, uint32_t nTime) override;
5655
};

src/wallet/wallet.cpp

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3208,16 +3208,12 @@ bool CWallet::CreateTransaction(CScript scriptPubKey, const CAmount& nValue, CTr
32083208
}
32093209

32103210
bool CWallet::CreateCoinStake(
3211-
const CKeyStore& keystore,
32123211
const CBlockIndex* pindexPrev,
32133212
unsigned int nBits,
32143213
CMutableTransaction& txNew,
32153214
int64_t& nTxNewTime,
3216-
std::vector<CStakeableOutput>* availableCoins)
3215+
std::vector<CStakeableOutput>* availableCoins) const
32173216
{
3218-
3219-
const Consensus::Params& consensus = Params().GetConsensus();
3220-
32213217
// Mark coin stake transaction
32223218
txNew.vin.clear();
32233219
txNew.vout.clear();
@@ -3311,9 +3307,11 @@ bool CWallet::CreateCoinStake(
33113307
}
33123308
LogPrint(BCLog::STAKING, "%s: attempted staking %d times\n", __func__, nAttempts);
33133309

3314-
if (!fKernelFound)
3315-
return false;
3310+
return fKernelFound;
3311+
}
33163312

3313+
bool CWallet::SignCoinStake(CMutableTransaction& txNew) const
3314+
{
33173315
// Sign it
33183316
int nIn = 0;
33193317
for (const CTxIn& txIn : txNew.vin) {
@@ -3322,7 +3320,7 @@ bool CWallet::CreateCoinStake(
33223320
return error("%s : failed to sign coinstake", __func__);
33233321
}
33243322

3325-
// Successfully generated coinstake
3323+
// Successfully signed coinstake
33263324
return true;
33273325
}
33283326

src/wallet/wallet.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1032,12 +1032,12 @@ class CWallet : public CCryptoKeyStore, public CValidationInterface
10321032
};
10331033
CWallet::CommitResult CommitTransaction(CTransactionRef tx, CReserveKey& opReservekey, CConnman* connman);
10341034
CWallet::CommitResult CommitTransaction(CTransactionRef tx, CReserveKey* reservekey, CConnman* connman);
1035-
bool CreateCoinStake(const CKeyStore& keystore,
1036-
const CBlockIndex* pindexPrev,
1035+
bool CreateCoinStake(const CBlockIndex* pindexPrev,
10371036
unsigned int nBits,
10381037
CMutableTransaction& txNew,
10391038
int64_t& nTxNewTime,
1040-
std::vector<CStakeableOutput>* availableCoins);
1039+
std::vector<CStakeableOutput>* availableCoins) const;
1040+
bool SignCoinStake(CMutableTransaction& txNew) const;
10411041
void AutoCombineDust(CConnman* connman);
10421042

10431043
// Shielded balances

src/zpiv/zpos.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@ class CLegacyZPivStake : public CStakeInput
2525
const CBlockIndex* GetIndexFrom() const override;
2626
CAmount GetValue() const override;
2727
CDataStream GetUniqueness() const override;
28-
bool CreateTxOuts(CWallet* pwallet, std::vector<CTxOut>& vout, CAmount nTotal) override { return false; /* creation disabled */}
2928
bool GetTxOutFrom(CTxOut& out) const override { return false; /* not available */ }
3029
virtual bool ContextCheck(int nHeight, uint32_t nTime) override;
3130
};

0 commit comments

Comments
 (0)