Skip to content

Commit

Permalink
Update rewards manager
Browse files Browse the repository at this point in the history
- 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
  • Loading branch information
celbalrai authored and wagerr-builder committed Apr 29, 2022
1 parent 871ae34 commit 1147bb8
Show file tree
Hide file tree
Showing 11 changed files with 80 additions and 42 deletions.
6 changes: 2 additions & 4 deletions src/init.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@
#include <policy/fees.h>
#include <policy/policy.h>
#include <pos/staking-manager.h>
#include <reward-manager.h>
#include <rpc/server.h>
#include <rpc/register.h>
#include <rpc/blockchain.h>
Expand Down Expand Up @@ -125,6 +124,7 @@ class DummyWalletInit : public WalletInitInterface {
// Wagerr Specific WalletInitInterface InitCoinJoinSettings
void AutoLockMasternodeCollaterals() const override {}
void InitStaking() const override {}
void InitRewardsManagement() const override {}
void InitCoinJoinSettings() const override {}
void InitKeePass() const override {}
bool InitAutoBackup() const override {return true;}
Expand Down Expand Up @@ -342,7 +342,6 @@ void PrepareShutdown()
evoDb.reset();
zerocoinDB.reset();
pTokenDB.reset();
rewardManager.reset();
tokenGroupManager.reset();
}
g_wallet_init_interface.Stop();
Expand Down Expand Up @@ -1991,8 +1990,6 @@ bool AppInitMain()
pTokenDB.reset(new CTokenDB(0, false, fReset || fReindexChainState));
tokenGroupManager.reset();
tokenGroupManager.reset(new CTokenGroupManager());
rewardManager.reset();
rewardManager.reset(new CRewardManager());

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

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

g_wallet_init_interface.InitStaking();
g_wallet_init_interface.InitRewardsManagement();

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

Expand Down
37 changes: 26 additions & 11 deletions src/reward-manager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
std::shared_ptr<CRewardManager> rewardManager;

CRewardManager::CRewardManager() :
fEnableRewardManager(false), fEnableAutoCombineRewards(false), nAutoCombineAmountThreshold(0), nAutoCombineNThreshold(10) {
fEnableRewardManager(false), nAutoCombineNThreshold(10) {
}

bool CRewardManager::IsReady() {
Expand All @@ -36,15 +36,25 @@ bool CRewardManager::IsReady() {
return true;
}

bool CRewardManager::IsCombining()
bool CRewardManager::IsAutoCombineEnabled()
{
return IsReady() && IsAutoCombineEnabled();
bool fEnable;
CAmount nAutoCombineAmountThreshold;
pwallet->GetAutoCombineSettings(fEnable, nAutoCombineAmountThreshold);
return fEnable;
}

void CRewardManager::AutoCombineSettings(bool fEnable, CAmount nAutoCombineAmountThresholdIn) {
LOCK(cs);
fEnableAutoCombineRewards = fEnable;
nAutoCombineAmountThreshold = nAutoCombineAmountThresholdIn;
CAmount CRewardManager::GetAutoCombineThresholdAmount()
{
bool fEnable;
CAmount nAutoCombineAmountThreshold;
pwallet->GetAutoCombineSettings(fEnable, nAutoCombineAmountThreshold);
return nAutoCombineAmountThreshold;
}

bool CRewardManager::IsCombining()
{
return IsReady() && IsAutoCombineEnabled();
}

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

void CRewardManager::AutoCombineRewards() {
void CRewardManager::AutocombineDust() {
bool fEnable;
CAmount nAutoCombineAmountThreshold;
pwallet->GetAutoCombineSettings(fEnable, nAutoCombineAmountThreshold);
if (!fEnable) return;

std::map<CTxDestination, std::vector<COutput> > mapCoinsByAddress = AvailableCoinsByAddress(true, nAutoCombineAmountThreshold * COIN);

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

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

if (IsAutoCombineEnabled()) {
AutoCombineRewards();
AutocombineDust();
int randsleep = GetRandInt(5 * 60 * 1000);
MilliSleep(randsleep); // Sleep between 3 and 8 minutes
}
Expand Down
10 changes: 3 additions & 7 deletions src/reward-manager.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,20 +33,16 @@ class CRewardManager
}

bool fEnableRewardManager;

bool fEnableAutoCombineRewards;
CAmount nAutoCombineAmountThreshold;
uint32_t nAutoCombineNThreshold;

bool IsReady();
bool IsCombining();

bool IsAutoCombineEnabled() { return fEnableAutoCombineRewards; };
CAmount GetAutoCombineThresholdAmount() { return nAutoCombineAmountThreshold; };
void AutoCombineSettings(bool fEnable, CAmount nAutoCombineAmountThresholdIn = 0);
bool IsAutoCombineEnabled();
CAmount GetAutoCombineThresholdAmount();

std::map<CTxDestination, std::vector<COutput> > AvailableCoinsByAddress(bool fConfirmed, CAmount maxCoinValue);
void AutoCombineRewards();
void AutocombineDust();

void DoMaintenance(CConnman& connman);
};
Expand Down
4 changes: 2 additions & 2 deletions src/rpc/client.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -183,8 +183,8 @@ static const CRPCConvertParam vRPCConvertParams[] =
{ "getspecialtxes", 4, "verbosity" },
{ "disconnectnode", 1, "nodeid" },
{ "setstakesplitthreshold", 0, "value" },
{ "autocombinerewards", 0, "enable" },
{ "autocombinerewards", 1, "threshold" },
{ "autocombinedust", 0, "enable" },
{ "autocombinedust", 1, "threshold" },
{ "createrawtokentransaction", 0, "inputs" },
{ "createrawtokentransaction", 1, "outputs" },
{ "createrawtokentransaction", 2, "token_outputs" },
Expand Down
18 changes: 13 additions & 5 deletions src/wallet/init.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ class WalletInit : public WalletInitInterface {
void AutoLockMasternodeCollaterals() const override;
void InitCoinJoinSettings() const override;
void InitStaking() const override;
void InitRewardsManagement() const override;
void InitKeePass() const override;
bool InitAutoBackup() const override;
};
Expand Down Expand Up @@ -436,10 +437,10 @@ void WalletInit::Start(CScheduler& scheduler) const
}

if (stakingManager->fEnableStaking) {
scheduler.scheduleEvery(boost::bind(&CStakingManager::DoMaintenance, boost::ref(stakingManager), boost::ref(*g_connman)), 5 * 1000);
scheduler.scheduleEvery(std::bind(&CStakingManager::DoMaintenance, std::ref(*stakingManager), std::ref(*g_connman)), 5 * 1000);
}
if (rewardManager->fEnableRewardManager) {
scheduler.scheduleEvery(boost::bind(&CRewardManager::DoMaintenance, boost::ref(rewardManager), boost::ref(*g_connman)), 3 * 60 * 1000);
scheduler.scheduleEvery(std::bind(&CRewardManager::DoMaintenance, std::ref(*rewardManager), std::ref(*g_connman)), 3 * 60 * 1000);
}
}

Expand Down Expand Up @@ -511,9 +512,6 @@ void WalletInit::InitStaking() const
stakingManager = std::shared_ptr<CStakingManager>(new CStakingManager(wallets[0]));
stakingManager->fEnableStaking = gArgs.GetBoolArg("-staking", true);
stakingManager->fEnableWAGERRStaking = gArgs.GetBoolArg("-staking", true);

rewardManager->BindWallet(wallets[0].get());
rewardManager->fEnableRewardManager = true;
}
if (Params().NetworkIDString() == CBaseChainParams::REGTEST) {
stakingManager->fEnableStaking = false;
Expand All @@ -527,6 +525,16 @@ void WalletInit::InitStaking() const
stakingManager->nReserveBalance = nReserveBalance;
}

void WalletInit::InitRewardsManagement() const
{
rewardManager = std::shared_ptr<CRewardManager>(new CRewardManager());
std::vector<std::shared_ptr<CWallet>> wallets = GetWallets();
if (HasWallets() && wallets.size() >= 1) {
rewardManager->BindWallet(wallets[0].get());
rewardManager->fEnableRewardManager = true;
}
}

void WalletInit::InitKeePass() const
{
keePassInt.init();
Expand Down
11 changes: 5 additions & 6 deletions src/wallet/rpcwallet.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4000,7 +4000,7 @@ UniValue setstakesplitthreshold(const JSONRPCRequest& request)
return result;
}

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

if (request.fHelp || nParamsSize < 1 || (!fEnable && nParamsSize > 1) || nParamsSize > 2)
throw std::runtime_error(
"autocombinerewards enable ( threshold )\n"
"autocombinedust enable ( threshold )\n"
"\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"
"When autocombinerewards runs it will create a transaction, and therefore will be subject to transaction fees.\n"
"When autocombinedust runs it will create a transaction, and therefore will be subject to transaction fees.\n"

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

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

EnsureWalletIsUnlocked(pwallet);

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

rewardManager->AutoCombineSettings(fEnable, nThresholdAmount);

result.push_back(Pair("threshold", int(rewardManager->GetAutoCombineThresholdAmount())));
result.push_back(Pair("enabled", rewardManager->IsAutoCombineEnabled()));

Expand Down Expand Up @@ -4618,6 +4616,7 @@ static const CRPCCommand commands[] =

{ "wallet", "getstakingstatus", &getstakingstatus, {} },
{ "wallet", "setstakesplitthreshold", &setstakesplitthreshold, {"value"} },
{ "wallet", "autocombinedust", &autocombinedust, {"enable", "threshold"} },
};

void RegisterWalletRPCCommands(CRPCTable &t)
Expand Down
22 changes: 20 additions & 2 deletions src/wallet/wallet.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1789,7 +1789,10 @@ CAmount CWallet::GetChange(const CTxOut& txout) const
bool CWallet::SetStakeSplitThreshold(uint64_t newThreshold)
{
WalletBatch batch(*database);
return batch.WriteStakeSplitThreshold(newThreshold);
if (!batch.WriteStakeSplitThreshold(newThreshold))
return false;
LoadStakeSplitThreshold(newThreshold);
return true;
}

void CWallet::LoadStakeSplitThreshold(uint64_t newThreshold)
Expand All @@ -1805,7 +1808,22 @@ uint64_t CWallet::GetStakeSplitThreshold()
bool CWallet::SetAutoCombineSettings(bool fEnable, CAmount nCombineThreshold)
{
WalletBatch batch(*database);
return batch.WriteAutoCombineSettings(fEnable, nCombineThreshold);
if (!batch.WriteAutoCombineSettings(fEnable, nCombineThreshold))
return false;
LoadAutoCombineSettings(fEnable, nCombineThreshold);
return true;
}

void CWallet::LoadAutoCombineSettings(bool fEnableIn, CAmount nCombineThresholdIn)
{
fCombineEnable = fEnableIn;
nCombineThreshold = nCombineThresholdIn;
}

void CWallet::GetAutoCombineSettings(bool& fEnableRet, CAmount& nCombineThresholdRet) const
{
fEnableRet = fCombineEnable;
nCombineThresholdRet = nCombineThreshold;
}

void CWallet::GenerateNewHDChain(const SecureString& secureMnemonic, const SecureString& secureMnemonicPassphrase)
Expand Down
4 changes: 4 additions & 0 deletions src/wallet/wallet.h
Original file line number Diff line number Diff line change
Expand Up @@ -871,6 +871,8 @@ class CWallet final : public CCryptoKeyStore, public CValidationInterface

// Staking and POS
uint64_t nStakeSplitThreshold = 2000;
CAmount nCombineThreshold = 500;
bool fCombineEnable = false;

public:
/*
Expand Down Expand Up @@ -1290,6 +1292,8 @@ class CWallet final : public CCryptoKeyStore, public CValidationInterface
void LoadStakeSplitThreshold(uint64_t newThreshold);
uint64_t GetStakeSplitThreshold();
bool SetAutoCombineSettings(bool fEnable, CAmount nCombineThreshold);
void LoadAutoCombineSettings(bool fEnableIn, CAmount nCombineThresholdIn);
void GetAutoCombineSettings(bool& fEnableRet, CAmount& nCombineThresholdRet) const;

/**
* HD Wallet Functions
Expand Down
8 changes: 4 additions & 4 deletions src/wallet/walletdb.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -595,9 +595,6 @@ ReadKeyValue(CWallet* pwallet, CDataStream& ssKey, CDataStream& ssValue,
return false;
}
}
else if (strType != "bestblock" && strType != "bestblock_nomerkle"){
wss.m_unknown_records++;
}
else if (strType == "stakeSplitThreshold")
{
uint64_t nStakeSplitThreshold;
Expand All @@ -608,7 +605,10 @@ ReadKeyValue(CWallet* pwallet, CDataStream& ssKey, CDataStream& ssValue,
{
std::pair<bool, CAmount> pSettings;
ssValue >> pSettings;
rewardManager->AutoCombineSettings(pSettings.first, pSettings.second);
pwallet->LoadAutoCombineSettings(pSettings.first, pSettings.second);
}
else if (strType != "bestblock" && strType != "bestblock_nomerkle"){
wss.m_unknown_records++;
}
} catch (...)
{
Expand Down
1 change: 0 additions & 1 deletion src/wallet/walletdb.h
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,6 @@ class WalletBatch
bool WriteMinVersion(int nVersion);

bool WriteStakeSplitThreshold(uint64_t nStakeSplitThreshold);
bool LoadStakeSplitThreshold(uint64_t nStakeSplitThreshold);
bool WriteAutoCombineSettings(bool fEnable, CAmount nCombineThreshold);

/// This writes directly to the database, and will not update the CWallet's cached accounting entries!
Expand Down
1 change: 1 addition & 0 deletions src/walletinitinterface.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ class WalletInitInterface {
virtual void AutoLockMasternodeCollaterals() const = 0;
virtual void InitCoinJoinSettings() const = 0;
virtual void InitStaking() const = 0;
virtual void InitRewardsManagement() const = 0;
virtual void InitKeePass() const = 0;
virtual bool InitAutoBackup() const = 0;

Expand Down

0 comments on commit 1147bb8

Please sign in to comment.