Skip to content

Commit 877c8a9

Browse files
committed
Move GetLastStake() function from miner to miner status
This replaces the GetLastStake() free function with a method as a member of the MinerStatus class. This function relies on the miner status state and doesn't belong in the miner code.
1 parent 59c5187 commit 877c8a9

File tree

5 files changed

+67
-58
lines changed

5 files changed

+67
-58
lines changed

src/gridcoin/staking/status.cpp

+55
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,61 @@ EfficiencyReport MinerStatus::GetEfficiencyReport() const
9595
return m_efficiency;
9696
}
9797

98+
std::optional<CWalletTx> MinerStatus::GetLastStake(CWallet& wallet)
99+
{
100+
CWalletTx stake_tx;
101+
uint256 cached_stake_tx_hash;
102+
103+
{
104+
LOCK(m_cs);
105+
cached_stake_tx_hash = m_last_pos_tx_hash;
106+
}
107+
108+
if (!cached_stake_tx_hash.IsNull()) {
109+
if (wallet.GetTransaction(cached_stake_tx_hash, stake_tx)) {
110+
return stake_tx;
111+
}
112+
}
113+
114+
const auto is_my_confirmed_stake = [](const CWalletTx& tx) {
115+
return tx.IsCoinStake() && tx.IsFromMe() && tx.GetDepthInMainChain() > 0;
116+
};
117+
118+
{
119+
LOCK2(cs_main, wallet.cs_wallet);
120+
121+
if (wallet.mapWallet.empty()) {
122+
return std::nullopt;
123+
}
124+
125+
auto latest_iter = wallet.mapWallet.cbegin();
126+
127+
for (auto iter = latest_iter; iter != wallet.mapWallet.cend(); ++iter) {
128+
if (iter->second.nTime > latest_iter->second.nTime
129+
&& is_my_confirmed_stake(iter->second))
130+
{
131+
latest_iter = iter;
132+
}
133+
}
134+
135+
if (latest_iter == wallet.mapWallet.cbegin()
136+
&& !is_my_confirmed_stake(latest_iter->second))
137+
{
138+
return std::nullopt;
139+
}
140+
141+
cached_stake_tx_hash = latest_iter->first;
142+
stake_tx = latest_iter->second;
143+
}
144+
145+
{
146+
LOCK(m_cs);
147+
m_last_pos_tx_hash = cached_stake_tx_hash;
148+
}
149+
150+
return stake_tx;
151+
}
152+
98153
std::string MinerStatus::FormatErrors() const
99154
{
100155
return FormatErrors(m_error_flags);

src/gridcoin/staking/status.h

+11
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,17 @@ class MinerStatus
159159
//!
160160
EfficiencyReport GetEfficiencyReport() const;
161161

162+
//!
163+
//! \brief Get the coinstake transaction of the last block mined by this
164+
//! node.
165+
//!
166+
//! \param wallet Used to search for the transaction if not cached.
167+
//!
168+
//! \return The most recent coinstake transaction or none if the node never
169+
//! mined a block before.
170+
//!
171+
std::optional<CWalletTx> GetLastStake(CWallet& wallet);
172+
162173
//!
163174
//! \brief Get a string representation of a set of miner status errors.
164175
//!

src/miner.cpp

-55
Original file line numberDiff line numberDiff line change
@@ -172,61 +172,6 @@ class TxPriorityCompare
172172
}
173173
};
174174

175-
std::optional<CWalletTx> GetLastStake(CWallet& wallet)
176-
{
177-
CWalletTx stake_tx;
178-
uint256 cached_stake_tx_hash;
179-
180-
{
181-
LOCK(g_miner_status.lock);
182-
cached_stake_tx_hash = g_miner_status.m_last_pos_tx_hash;
183-
}
184-
185-
if (!cached_stake_tx_hash.IsNull()) {
186-
if (wallet.GetTransaction(cached_stake_tx_hash, stake_tx)) {
187-
return stake_tx;
188-
}
189-
}
190-
191-
const auto is_my_confirmed_stake = [](const CWalletTx& tx) {
192-
return tx.IsCoinStake() && tx.IsFromMe() && tx.GetDepthInMainChain() > 0;
193-
};
194-
195-
{
196-
LOCK2(cs_main, wallet.cs_wallet);
197-
198-
if (wallet.mapWallet.empty()) {
199-
return std::nullopt;
200-
}
201-
202-
auto latest_iter = wallet.mapWallet.cbegin();
203-
204-
for (auto iter = wallet.mapWallet.cbegin(); iter != wallet.mapWallet.cend(); ++iter) {
205-
if (iter->second.nTime > latest_iter->second.nTime
206-
&& is_my_confirmed_stake(iter->second))
207-
{
208-
latest_iter = iter;
209-
}
210-
}
211-
212-
if (latest_iter == wallet.mapWallet.cbegin()
213-
&& !is_my_confirmed_stake(latest_iter->second))
214-
{
215-
return std::nullopt;
216-
}
217-
218-
cached_stake_tx_hash = latest_iter->first;
219-
stake_tx = latest_iter->second;
220-
}
221-
222-
{
223-
LOCK(g_miner_status.lock);
224-
g_miner_status.m_last_pos_tx_hash = cached_stake_tx_hash;
225-
}
226-
227-
return stake_tx;
228-
}
229-
230175
// CreateRestOfTheBlock: collect transactions into block and fill in header
231176
bool CreateRestOfTheBlock(CBlock &block, CBlockIndex* pindexPrev)
232177
{

src/miner.h

-2
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,6 @@ extern unsigned int nMinerSleep;
2121
// It will be converted to Halfords in GetNumberOfStakeOutputs by multiplying by COIN.
2222
static const int64_t MIN_STAKE_SPLIT_VALUE_GRC = 800;
2323

24-
std::optional<CWalletTx> GetLastStake(CWallet& wallet);
25-
2624
void SplitCoinStakeOutput(CBlock &blocknew, int64_t &nReward, bool &fEnableStakeSplit, bool &fEnableSideStaking, SideStakeAlloc &vSideStakeAlloc, double &dEfficiency);
2725
unsigned int GetNumberOfStakeOutputs(int64_t &nValue, int64_t &nMinStakeSplitValue, double &dEfficiency);
2826
SideStakeAlloc GetSideStakingStatusAndAlloc();

src/rpc/mining.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,7 @@ UniValue getlaststake(const UniValue& params, bool fHelp)
158158
"\n"
159159
"Fetch information about this wallet's last staked block.\n");
160160

161-
const std::optional<CWalletTx> stake_tx = GetLastStake(*pwalletMain);
161+
const std::optional<CWalletTx> stake_tx = g_miner_status.GetLastStake(*pwalletMain);
162162

163163
if (!stake_tx) {
164164
throw JSONRPCError(RPC_WALLET_ERROR, "No prior staked blocks found.");

0 commit comments

Comments
 (0)