Skip to content

Commit 5688e25

Browse files
celbalraickti
authored andcommitted
Update rewards manager
- Fix database functions for stake split threshold and auto combine dust functions - Rename autocombinerewards to autocombinedust - Remove change output from autocombinedust - Cleanup scheduling and initialization
1 parent 7b66188 commit 5688e25

File tree

11 files changed

+80
-42
lines changed

11 files changed

+80
-42
lines changed

src/init.cpp

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,6 @@
3232
#include <policy/fees.h>
3333
#include <policy/policy.h>
3434
#include <pos/staking-manager.h>
35-
#include <reward-manager.h>
3635
#include <rpc/server.h>
3736
#include <rpc/register.h>
3837
#include <rpc/blockchain.h>
@@ -125,6 +124,7 @@ class DummyWalletInit : public WalletInitInterface {
125124
// Wagerr Specific WalletInitInterface InitCoinJoinSettings
126125
void AutoLockMasternodeCollaterals() const override {}
127126
void InitStaking() const override {}
127+
void InitRewardsManagement() const override {}
128128
void InitCoinJoinSettings() const override {}
129129
void InitKeePass() const override {}
130130
bool InitAutoBackup() const override {return true;}
@@ -342,7 +342,6 @@ void PrepareShutdown()
342342
evoDb.reset();
343343
zerocoinDB.reset();
344344
pTokenDB.reset();
345-
rewardManager.reset();
346345
tokenGroupManager.reset();
347346
}
348347
g_wallet_init_interface.Stop();
@@ -1991,8 +1990,6 @@ bool AppInitMain()
19911990
pTokenDB.reset(new CTokenDB(0, false, fReset || fReindexChainState));
19921991
tokenGroupManager.reset();
19931992
tokenGroupManager.reset(new CTokenGroupManager());
1994-
rewardManager.reset();
1995-
rewardManager.reset(new CRewardManager());
19961993

19971994
llmq::InitLLMQSystem(*evoDb, false, fReset || fReindexChainState);
19981995

@@ -2282,6 +2279,7 @@ bool AppInitMain()
22822279
// ********************************************************* Step 10b: setup Staking
22832280

22842281
g_wallet_init_interface.InitStaking();
2282+
g_wallet_init_interface.InitRewardsManagement();
22852283

22862284
// ********************************************************* Step 10b: Load cache data
22872285

src/reward-manager.cpp

Lines changed: 26 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
std::shared_ptr<CRewardManager> rewardManager;
1717

1818
CRewardManager::CRewardManager() :
19-
fEnableRewardManager(false), fEnableAutoCombineRewards(false), nAutoCombineAmountThreshold(0), nAutoCombineNThreshold(10) {
19+
fEnableRewardManager(false), nAutoCombineNThreshold(10) {
2020
}
2121

2222
bool CRewardManager::IsReady() {
@@ -36,15 +36,25 @@ bool CRewardManager::IsReady() {
3636
return true;
3737
}
3838

39-
bool CRewardManager::IsCombining()
39+
bool CRewardManager::IsAutoCombineEnabled()
4040
{
41-
return IsReady() && IsAutoCombineEnabled();
41+
bool fEnable;
42+
CAmount nAutoCombineAmountThreshold;
43+
pwallet->GetAutoCombineSettings(fEnable, nAutoCombineAmountThreshold);
44+
return fEnable;
4245
}
4346

44-
void CRewardManager::AutoCombineSettings(bool fEnable, CAmount nAutoCombineAmountThresholdIn) {
45-
LOCK(cs);
46-
fEnableAutoCombineRewards = fEnable;
47-
nAutoCombineAmountThreshold = nAutoCombineAmountThresholdIn;
47+
CAmount CRewardManager::GetAutoCombineThresholdAmount()
48+
{
49+
bool fEnable;
50+
CAmount nAutoCombineAmountThreshold;
51+
pwallet->GetAutoCombineSettings(fEnable, nAutoCombineAmountThreshold);
52+
return nAutoCombineAmountThreshold;
53+
}
54+
55+
bool CRewardManager::IsCombining()
56+
{
57+
return IsReady() && IsAutoCombineEnabled();
4858
}
4959

5060
// TODO: replace with pwallet->FilterCoins()
@@ -67,7 +77,12 @@ std::map<CTxDestination, std::vector<COutput> > CRewardManager::AvailableCoinsBy
6777
return mapCoins;
6878
}
6979

70-
void CRewardManager::AutoCombineRewards() {
80+
void CRewardManager::AutocombineDust() {
81+
bool fEnable;
82+
CAmount nAutoCombineAmountThreshold;
83+
pwallet->GetAutoCombineSettings(fEnable, nAutoCombineAmountThreshold);
84+
if (!fEnable) return;
85+
7186
std::map<CTxDestination, std::vector<COutput> > mapCoinsByAddress = AvailableCoinsByAddress(true, nAutoCombineAmountThreshold * COIN);
7287

7388
//coins are sectioned by address. This combination code only wants to combine inputs that belong to the same address
@@ -123,8 +138,8 @@ void CRewardManager::AutoCombineRewards() {
123138
std::vector<CRecipient> vecSend;
124139
int nChangePosRet = -1;
125140
CScript scriptPubKey = GetScriptForDestination(it->first);
126-
// 10% safety margin to avoid "Insufficient funds" errors
127-
CRecipient recipient = {scriptPubKey, nTotalRewardsValue - (nTotalRewardsValue / 10), false};
141+
// Subtract fee from amount
142+
CRecipient recipient = {scriptPubKey, nTotalRewardsValue, true};
128143
vecSend.push_back(recipient);
129144

130145
//Send change to same address
@@ -169,7 +184,7 @@ void CRewardManager::DoMaintenance(CConnman& connman) {
169184
}
170185

171186
if (IsAutoCombineEnabled()) {
172-
AutoCombineRewards();
187+
AutocombineDust();
173188
int randsleep = GetRandInt(5 * 60 * 1000);
174189
MilliSleep(randsleep); // Sleep between 3 and 8 minutes
175190
}

src/reward-manager.h

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -33,20 +33,16 @@ class CRewardManager
3333
}
3434

3535
bool fEnableRewardManager;
36-
37-
bool fEnableAutoCombineRewards;
38-
CAmount nAutoCombineAmountThreshold;
3936
uint32_t nAutoCombineNThreshold;
4037

4138
bool IsReady();
4239
bool IsCombining();
4340

44-
bool IsAutoCombineEnabled() { return fEnableAutoCombineRewards; };
45-
CAmount GetAutoCombineThresholdAmount() { return nAutoCombineAmountThreshold; };
46-
void AutoCombineSettings(bool fEnable, CAmount nAutoCombineAmountThresholdIn = 0);
41+
bool IsAutoCombineEnabled();
42+
CAmount GetAutoCombineThresholdAmount();
4743

4844
std::map<CTxDestination, std::vector<COutput> > AvailableCoinsByAddress(bool fConfirmed, CAmount maxCoinValue);
49-
void AutoCombineRewards();
45+
void AutocombineDust();
5046

5147
void DoMaintenance(CConnman& connman);
5248
};

src/rpc/client.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -183,8 +183,8 @@ static const CRPCConvertParam vRPCConvertParams[] =
183183
{ "getspecialtxes", 4, "verbosity" },
184184
{ "disconnectnode", 1, "nodeid" },
185185
{ "setstakesplitthreshold", 0, "value" },
186-
{ "autocombinerewards", 0, "enable" },
187-
{ "autocombinerewards", 1, "threshold" },
186+
{ "autocombinedust", 0, "enable" },
187+
{ "autocombinedust", 1, "threshold" },
188188
{ "createrawtokentransaction", 0, "inputs" },
189189
{ "createrawtokentransaction", 1, "outputs" },
190190
{ "createrawtokentransaction", 2, "token_outputs" },

src/wallet/init.cpp

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ class WalletInit : public WalletInitInterface {
5959
void AutoLockMasternodeCollaterals() const override;
6060
void InitCoinJoinSettings() const override;
6161
void InitStaking() const override;
62+
void InitRewardsManagement() const override;
6263
void InitKeePass() const override;
6364
bool InitAutoBackup() const override;
6465
};
@@ -436,10 +437,10 @@ void WalletInit::Start(CScheduler& scheduler) const
436437
}
437438

438439
if (stakingManager->fEnableStaking) {
439-
scheduler.scheduleEvery(boost::bind(&CStakingManager::DoMaintenance, boost::ref(stakingManager), boost::ref(*g_connman)), 5 * 1000);
440+
scheduler.scheduleEvery(std::bind(&CStakingManager::DoMaintenance, std::ref(*stakingManager), std::ref(*g_connman)), 5 * 1000);
440441
}
441442
if (rewardManager->fEnableRewardManager) {
442-
scheduler.scheduleEvery(boost::bind(&CRewardManager::DoMaintenance, boost::ref(rewardManager), boost::ref(*g_connman)), 3 * 60 * 1000);
443+
scheduler.scheduleEvery(std::bind(&CRewardManager::DoMaintenance, std::ref(*rewardManager), std::ref(*g_connman)), 3 * 60 * 1000);
443444
}
444445
}
445446

@@ -511,9 +512,6 @@ void WalletInit::InitStaking() const
511512
stakingManager = std::shared_ptr<CStakingManager>(new CStakingManager(wallets[0]));
512513
stakingManager->fEnableStaking = gArgs.GetBoolArg("-staking", true);
513514
stakingManager->fEnableWAGERRStaking = gArgs.GetBoolArg("-staking", true);
514-
515-
rewardManager->BindWallet(wallets[0].get());
516-
rewardManager->fEnableRewardManager = true;
517515
}
518516
if (Params().NetworkIDString() == CBaseChainParams::REGTEST) {
519517
stakingManager->fEnableStaking = false;
@@ -527,6 +525,16 @@ void WalletInit::InitStaking() const
527525
stakingManager->nReserveBalance = nReserveBalance;
528526
}
529527

528+
void WalletInit::InitRewardsManagement() const
529+
{
530+
rewardManager = std::shared_ptr<CRewardManager>(new CRewardManager());
531+
std::vector<std::shared_ptr<CWallet>> wallets = GetWallets();
532+
if (HasWallets() && wallets.size() >= 1) {
533+
rewardManager->BindWallet(wallets[0].get());
534+
rewardManager->fEnableRewardManager = true;
535+
}
536+
}
537+
530538
void WalletInit::InitKeePass() const
531539
{
532540
keePassInt.init();

src/wallet/rpcwallet.cpp

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4000,7 +4000,7 @@ UniValue setstakesplitthreshold(const JSONRPCRequest& request)
40004000
return result;
40014001
}
40024002

4003-
UniValue autocombinerewards(const JSONRPCRequest& request)
4003+
UniValue autocombinedust(const JSONRPCRequest& request)
40044004
{
40054005
std::shared_ptr<CWallet> const wallet = GetWalletForJSONRPCRequest(request);
40064006
CWallet* const pwallet = wallet.get();
@@ -4016,16 +4016,16 @@ UniValue autocombinerewards(const JSONRPCRequest& request)
40164016

40174017
if (request.fHelp || nParamsSize < 1 || (!fEnable && nParamsSize > 1) || nParamsSize > 2)
40184018
throw std::runtime_error(
4019-
"autocombinerewards enable ( threshold )\n"
4019+
"autocombinedust enable ( threshold )\n"
40204020
"\nWallet will automatically monitor for any coins with value below the threshold amount, and combine them if they reside with the same Wagerr address\n"
4021-
"When autocombinerewards runs it will create a transaction, and therefore will be subject to transaction fees.\n"
4021+
"When autocombinedust runs it will create a transaction, and therefore will be subject to transaction fees.\n"
40224022

40234023
"\nArguments:\n"
40244024
"1. enable (boolean, required) Enable auto combine (true) or disable (false)\n"
40254025
"2. threshold amount (numeric, optional) Coins with an aggregated value of this amount will be combined (default: 0)\n"
40264026

40274027
"\nExamples:\n" +
4028-
HelpExampleCli("autocombinerewards", "true 500") + HelpExampleRpc("autocombinerewards", "true 500"));
4028+
HelpExampleCli("autocombinedust", "true 500") + HelpExampleRpc("autocombinedust", "true 500"));
40294029

40304030
EnsureWalletIsUnlocked(pwallet);
40314031

@@ -4044,8 +4044,6 @@ UniValue autocombinerewards(const JSONRPCRequest& request)
40444044
throw std::runtime_error("Changed settings in wallet but failed to save to database\n");
40454045
}
40464046

4047-
rewardManager->AutoCombineSettings(fEnable, nThresholdAmount);
4048-
40494047
result.push_back(Pair("threshold", int(rewardManager->GetAutoCombineThresholdAmount())));
40504048
result.push_back(Pair("enabled", rewardManager->IsAutoCombineEnabled()));
40514049

@@ -4618,6 +4616,7 @@ static const CRPCCommand commands[] =
46184616

46194617
{ "wallet", "getstakingstatus", &getstakingstatus, {} },
46204618
{ "wallet", "setstakesplitthreshold", &setstakesplitthreshold, {"value"} },
4619+
{ "wallet", "autocombinedust", &autocombinedust, {"enable", "threshold"} },
46214620
};
46224621

46234622
void RegisterWalletRPCCommands(CRPCTable &t)

src/wallet/wallet.cpp

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1789,7 +1789,10 @@ CAmount CWallet::GetChange(const CTxOut& txout) const
17891789
bool CWallet::SetStakeSplitThreshold(uint64_t newThreshold)
17901790
{
17911791
WalletBatch batch(*database);
1792-
return batch.WriteStakeSplitThreshold(newThreshold);
1792+
if (!batch.WriteStakeSplitThreshold(newThreshold))
1793+
return false;
1794+
LoadStakeSplitThreshold(newThreshold);
1795+
return true;
17931796
}
17941797

17951798
void CWallet::LoadStakeSplitThreshold(uint64_t newThreshold)
@@ -1805,7 +1808,22 @@ uint64_t CWallet::GetStakeSplitThreshold()
18051808
bool CWallet::SetAutoCombineSettings(bool fEnable, CAmount nCombineThreshold)
18061809
{
18071810
WalletBatch batch(*database);
1808-
return batch.WriteAutoCombineSettings(fEnable, nCombineThreshold);
1811+
if (!batch.WriteAutoCombineSettings(fEnable, nCombineThreshold))
1812+
return false;
1813+
LoadAutoCombineSettings(fEnable, nCombineThreshold);
1814+
return true;
1815+
}
1816+
1817+
void CWallet::LoadAutoCombineSettings(bool fEnableIn, CAmount nCombineThresholdIn)
1818+
{
1819+
fCombineEnable = fEnableIn;
1820+
nCombineThreshold = nCombineThresholdIn;
1821+
}
1822+
1823+
void CWallet::GetAutoCombineSettings(bool& fEnableRet, CAmount& nCombineThresholdRet) const
1824+
{
1825+
fEnableRet = fCombineEnable;
1826+
nCombineThresholdRet = nCombineThreshold;
18091827
}
18101828

18111829
void CWallet::GenerateNewHDChain(const SecureString& secureMnemonic, const SecureString& secureMnemonicPassphrase)

src/wallet/wallet.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -871,6 +871,8 @@ class CWallet final : public CCryptoKeyStore, public CValidationInterface
871871

872872
// Staking and POS
873873
uint64_t nStakeSplitThreshold = 2000;
874+
CAmount nCombineThreshold = 500;
875+
bool fCombineEnable = false;
874876

875877
public:
876878
/*
@@ -1290,6 +1292,8 @@ class CWallet final : public CCryptoKeyStore, public CValidationInterface
12901292
void LoadStakeSplitThreshold(uint64_t newThreshold);
12911293
uint64_t GetStakeSplitThreshold();
12921294
bool SetAutoCombineSettings(bool fEnable, CAmount nCombineThreshold);
1295+
void LoadAutoCombineSettings(bool fEnableIn, CAmount nCombineThresholdIn);
1296+
void GetAutoCombineSettings(bool& fEnableRet, CAmount& nCombineThresholdRet) const;
12931297

12941298
/**
12951299
* HD Wallet Functions

src/wallet/walletdb.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -595,9 +595,6 @@ ReadKeyValue(CWallet* pwallet, CDataStream& ssKey, CDataStream& ssValue,
595595
return false;
596596
}
597597
}
598-
else if (strType != "bestblock" && strType != "bestblock_nomerkle"){
599-
wss.m_unknown_records++;
600-
}
601598
else if (strType == "stakeSplitThreshold")
602599
{
603600
uint64_t nStakeSplitThreshold;
@@ -608,7 +605,10 @@ ReadKeyValue(CWallet* pwallet, CDataStream& ssKey, CDataStream& ssValue,
608605
{
609606
std::pair<bool, CAmount> pSettings;
610607
ssValue >> pSettings;
611-
rewardManager->AutoCombineSettings(pSettings.first, pSettings.second);
608+
pwallet->LoadAutoCombineSettings(pSettings.first, pSettings.second);
609+
}
610+
else if (strType != "bestblock" && strType != "bestblock_nomerkle"){
611+
wss.m_unknown_records++;
612612
}
613613
} catch (...)
614614
{

src/wallet/walletdb.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,6 @@ class WalletBatch
158158
bool WriteMinVersion(int nVersion);
159159

160160
bool WriteStakeSplitThreshold(uint64_t nStakeSplitThreshold);
161-
bool LoadStakeSplitThreshold(uint64_t nStakeSplitThreshold);
162161
bool WriteAutoCombineSettings(bool fEnable, CAmount nCombineThreshold);
163162

164163
/// This writes directly to the database, and will not update the CWallet's cached accounting entries!

0 commit comments

Comments
 (0)