Skip to content

Commit aa3ee8d

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 2a1f059 commit aa3ee8d

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
@@ -93,6 +93,61 @@ EfficiencyReport MinerStatus::GetEfficiencyReport() const
9393
return m_efficiency;
9494
}
9595

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

src/gridcoin/staking/status.h

+11
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,17 @@ class MinerStatus
157157
//!
158158
EfficiencyReport GetEfficiencyReport() const;
159159

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

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)