Skip to content

Commit fc63a6f

Browse files
committed
Merge PIVX-Project#2750: [Refactor] Create coinstake outputs moved from stakeInput to the wallet class.
ccd6fcd Move-only: Create coinstake outputs moved fro stakeInput.h/cpp to wallet.h/cpp (furszy) Pull request description: Fixing the circular dependency: "kernel -> stakeinput -> wallet/wallet -> kernel" ACKs for top commit: random-zebra: utACK ccd6fcd Tree-SHA512: 4a5a2f1b9dc9407e805c89073051eb20bd867402ec07e3e571556d116b4996f60461ec15454ca11948c9ea64a6b8a671d9b1bd9ae11bbba14d38e8e867bd024a
2 parents 76de203 + ccd6fcd commit fc63a6f

File tree

5 files changed

+47
-43
lines changed

5 files changed

+47
-43
lines changed

src/stakeinput.cpp

+1-39
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
#include "chain.h"
88
#include "txdb.h"
9-
#include "wallet/wallet.h"
9+
#include "validation.h"
1010

1111
static bool HasStakeMinAgeOrDepth(int nHeight, uint32_t nTime, const CBlockIndex* pindex)
1212
{
@@ -79,44 +79,6 @@ CAmount CPivStake::GetValue() const
7979
return outputFrom.nValue;
8080
}
8181

82-
bool CPivStake::CreateTxOuts(const CWallet* pwallet, std::vector<CTxOut>& vout, CAmount nTotal) const
83-
{
84-
std::vector<valtype> vSolutions;
85-
txnouttype whichType;
86-
CScript scriptPubKeyKernel = outputFrom.scriptPubKey;
87-
if (!Solver(scriptPubKeyKernel, whichType, vSolutions))
88-
return error("%s: failed to parse kernel", __func__);
89-
90-
if (whichType != TX_PUBKEY && whichType != TX_PUBKEYHASH && whichType != TX_COLDSTAKE)
91-
return error("%s: type=%d (%s) not supported for scriptPubKeyKernel", __func__, whichType, GetTxnOutputType(whichType));
92-
93-
CKey key;
94-
if (whichType == TX_PUBKEYHASH || whichType == TX_COLDSTAKE) {
95-
// if P2PKH or P2CS check that we have the input private key
96-
if (!pwallet->GetKey(CKeyID(uint160(vSolutions[0])), key))
97-
return error("%s: Unable to get staking private key", __func__);
98-
}
99-
100-
vout.emplace_back(0, scriptPubKeyKernel);
101-
102-
// Calculate if we need to split the output
103-
if (pwallet->nStakeSplitThreshold > 0) {
104-
int nSplit = static_cast<int>(nTotal / pwallet->nStakeSplitThreshold);
105-
if (nSplit > 1) {
106-
// if nTotal is twice or more of the threshold; create more outputs
107-
int txSizeMax = MAX_STANDARD_TX_SIZE >> 11; // limit splits to <10% of the max TX size (/2048)
108-
if (nSplit > txSizeMax)
109-
nSplit = txSizeMax;
110-
for (int i = nSplit; i > 1; i--) {
111-
LogPrintf("%s: StakeSplit: nTotal = %d; adding output %d of %d\n", __func__, nTotal, (nSplit-i)+2, nSplit);
112-
vout.emplace_back(0, scriptPubKeyKernel);
113-
}
114-
}
115-
}
116-
117-
return true;
118-
}
119-
12082
CDataStream CPivStake::GetUniqueness() const
12183
{
12284
//The unique identifier for a PIV stake is the outpoint

src/stakeinput.h

-1
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,6 @@ class CPivStake : public CStakeInput
4646
CAmount GetValue() const override;
4747
CDataStream GetUniqueness() const override;
4848
CTxIn GetTxIn() const;
49-
bool CreateTxOuts(const CWallet* pwallet, std::vector<CTxOut>& vout, CAmount nTotal) const;
5049
bool IsZPIV() const override { return false; }
5150
};
5251

src/wallet/wallet.cpp

+44-2
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,7 @@ const CWalletTx* CWallet::GetWalletTx(const uint256& hash) const
161161
LOCK(cs_wallet);
162162
std::map<uint256, CWalletTx>::const_iterator it = mapWallet.find(hash);
163163
if (it == mapWallet.end())
164-
return NULL;
164+
return nullptr;
165165
return &(it->second);
166166
}
167167

@@ -3266,6 +3266,48 @@ int CWallet::GetLastBlockHeightLockWallet() const
32663266
return WITH_LOCK(cs_wallet, return m_last_block_processed_height;);
32673267
}
32683268

3269+
bool CWallet::CreateCoinstakeOuts(const CPivStake& stakeInput, std::vector<CTxOut>& vout, CAmount nTotal) const
3270+
{
3271+
std::vector<valtype> vSolutions;
3272+
txnouttype whichType;
3273+
CTxOut stakePrevout;
3274+
if (!stakeInput.GetTxOutFrom(stakePrevout)) {
3275+
return error("%s: failed to get stake input", __func__);
3276+
}
3277+
CScript scriptPubKeyKernel = stakePrevout.scriptPubKey;
3278+
if (!Solver(scriptPubKeyKernel, whichType, vSolutions))
3279+
return error("%s: failed to parse kernel", __func__);
3280+
3281+
if (whichType != TX_PUBKEY && whichType != TX_PUBKEYHASH && whichType != TX_COLDSTAKE)
3282+
return error("%s: type=%d (%s) not supported for scriptPubKeyKernel", __func__, whichType, GetTxnOutputType(whichType));
3283+
3284+
CKey key;
3285+
if (whichType == TX_PUBKEYHASH || whichType == TX_COLDSTAKE) {
3286+
// if P2PKH or P2CS check that we have the input private key
3287+
if (!GetKey(CKeyID(uint160(vSolutions[0])), key))
3288+
return error("%s: Unable to get staking private key", __func__);
3289+
}
3290+
3291+
vout.emplace_back(0, scriptPubKeyKernel);
3292+
3293+
// Calculate if we need to split the output
3294+
if (nStakeSplitThreshold > 0) {
3295+
int nSplit = static_cast<int>(nTotal / nStakeSplitThreshold);
3296+
if (nSplit > 1) {
3297+
// if nTotal is twice or more of the threshold; create more outputs
3298+
int txSizeMax = MAX_STANDARD_TX_SIZE >> 11; // limit splits to <10% of the max TX size (/2048)
3299+
if (nSplit > txSizeMax)
3300+
nSplit = txSizeMax;
3301+
for (int i = nSplit; i > 1; i--) {
3302+
LogPrintf("%s: StakeSplit: nTotal = %d; adding output %d of %d\n", __func__, nTotal, (nSplit-i)+2, nSplit);
3303+
vout.emplace_back(0, scriptPubKeyKernel);
3304+
}
3305+
}
3306+
}
3307+
3308+
return true;
3309+
}
3310+
32693311
bool CWallet::CreateCoinStake(
32703312
const CBlockIndex* pindexPrev,
32713313
unsigned int nBits,
@@ -3335,7 +3377,7 @@ bool CWallet::CreateCoinStake(
33353377

33363378
// Create the output transaction(s)
33373379
std::vector<CTxOut> vout;
3338-
if (!stakeInput.CreateTxOuts(this, vout, nCredit)) {
3380+
if (!CreateCoinstakeOuts(stakeInput, vout, nCredit)) {
33393381
LogPrintf("%s : failed to create output\n", __func__);
33403382
it++;
33413383
continue;

src/wallet/wallet.h

+2
Original file line numberDiff line numberDiff line change
@@ -1096,6 +1096,8 @@ class CWallet : public CCryptoKeyStore, public CValidationInterface
10961096
};
10971097
CWallet::CommitResult CommitTransaction(CTransactionRef tx, CReserveKey& opReservekey, CConnman* connman);
10981098
CWallet::CommitResult CommitTransaction(CTransactionRef tx, CReserveKey* reservekey, CConnman* connman, mapValue_t* extraValues=nullptr);
1099+
1100+
bool CreateCoinstakeOuts(const CPivStake& stakeInput, std::vector<CTxOut>& vout, CAmount nTotal) const;
10991101
bool CreateCoinStake(const CBlockIndex* pindexPrev,
11001102
unsigned int nBits,
11011103
CMutableTransaction& txNew,

test/lint/lint-circular-dependencies.sh

-1
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,6 @@ EXPECTED_CIRCULAR_DEPENDENCIES=(
3939
"wallet/wallet -> wallet/walletdb -> wallet/wallet"
4040
"chain -> legacy/stakemodifier -> stakeinput -> chain"
4141
"chain -> legacy/stakemodifier -> validation -> chain"
42-
"kernel -> stakeinput -> wallet/wallet -> kernel"
4342
"legacy/validation_zerocoin_legacy -> wallet/wallet -> validation -> legacy/validation_zerocoin_legacy"
4443
"llmq/quorums_dkgsession -> llmq/quorums_dkgsessionmgr -> llmq/quorums_dkgsessionhandler -> llmq/quorums_dkgsession"
4544
"llmq/quorums_dkgsessionhandler -> net_processing -> llmq/quorums_dkgsessionmgr -> llmq/quorums_dkgsessionhandler"

0 commit comments

Comments
 (0)