From 1147bb8e8ea41558d938a343690daad65374cfbc Mon Sep 17 00:00:00 2001 From: celbalrai <80897309+celbalrai@users.noreply.github.com> Date: Sun, 6 Jun 2021 08:11:26 +0200 Subject: [PATCH] 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 --- src/init.cpp | 6 ++---- src/reward-manager.cpp | 37 ++++++++++++++++++++++++++----------- src/reward-manager.h | 10 +++------- src/rpc/client.cpp | 4 ++-- src/wallet/init.cpp | 18 +++++++++++++----- src/wallet/rpcwallet.cpp | 11 +++++------ src/wallet/wallet.cpp | 22 ++++++++++++++++++++-- src/wallet/wallet.h | 4 ++++ src/wallet/walletdb.cpp | 8 ++++---- src/wallet/walletdb.h | 1 - src/walletinitinterface.h | 1 + 11 files changed, 80 insertions(+), 42 deletions(-) diff --git a/src/init.cpp b/src/init.cpp index 29aaf39ce425e..037582b70583f 100644 --- a/src/init.cpp +++ b/src/init.cpp @@ -32,7 +32,6 @@ #include #include #include -#include #include #include #include @@ -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;} @@ -342,7 +342,6 @@ void PrepareShutdown() evoDb.reset(); zerocoinDB.reset(); pTokenDB.reset(); - rewardManager.reset(); tokenGroupManager.reset(); } g_wallet_init_interface.Stop(); @@ -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); @@ -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 diff --git a/src/reward-manager.cpp b/src/reward-manager.cpp index ca174efb755b5..55b9b8ef334bb 100644 --- a/src/reward-manager.cpp +++ b/src/reward-manager.cpp @@ -16,7 +16,7 @@ std::shared_ptr rewardManager; CRewardManager::CRewardManager() : - fEnableRewardManager(false), fEnableAutoCombineRewards(false), nAutoCombineAmountThreshold(0), nAutoCombineNThreshold(10) { + fEnableRewardManager(false), nAutoCombineNThreshold(10) { } bool CRewardManager::IsReady() { @@ -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() @@ -67,7 +77,12 @@ std::map > CRewardManager::AvailableCoinsBy return mapCoins; } -void CRewardManager::AutoCombineRewards() { +void CRewardManager::AutocombineDust() { + bool fEnable; + CAmount nAutoCombineAmountThreshold; + pwallet->GetAutoCombineSettings(fEnable, nAutoCombineAmountThreshold); + if (!fEnable) return; + std::map > mapCoinsByAddress = AvailableCoinsByAddress(true, nAutoCombineAmountThreshold * COIN); //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() { std::vector 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 @@ -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 } diff --git a/src/reward-manager.h b/src/reward-manager.h index 51a87a9256846..2a59b98978d44 100644 --- a/src/reward-manager.h +++ b/src/reward-manager.h @@ -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 > AvailableCoinsByAddress(bool fConfirmed, CAmount maxCoinValue); - void AutoCombineRewards(); + void AutocombineDust(); void DoMaintenance(CConnman& connman); }; diff --git a/src/rpc/client.cpp b/src/rpc/client.cpp index 259e823a08411..eb31c7ed82e8d 100644 --- a/src/rpc/client.cpp +++ b/src/rpc/client.cpp @@ -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" }, diff --git a/src/wallet/init.cpp b/src/wallet/init.cpp index e27567f17ece8..9a17e95d55f6a 100644 --- a/src/wallet/init.cpp +++ b/src/wallet/init.cpp @@ -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; }; @@ -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); } } @@ -511,9 +512,6 @@ void WalletInit::InitStaking() const stakingManager = std::shared_ptr(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; @@ -527,6 +525,16 @@ void WalletInit::InitStaking() const stakingManager->nReserveBalance = nReserveBalance; } +void WalletInit::InitRewardsManagement() const +{ + rewardManager = std::shared_ptr(new CRewardManager()); + std::vector> wallets = GetWallets(); + if (HasWallets() && wallets.size() >= 1) { + rewardManager->BindWallet(wallets[0].get()); + rewardManager->fEnableRewardManager = true; + } +} + void WalletInit::InitKeePass() const { keePassInt.init(); diff --git a/src/wallet/rpcwallet.cpp b/src/wallet/rpcwallet.cpp index cc5ece0e228ef..26c6d3ff377e2 100644 --- a/src/wallet/rpcwallet.cpp +++ b/src/wallet/rpcwallet.cpp @@ -4000,7 +4000,7 @@ UniValue setstakesplitthreshold(const JSONRPCRequest& request) return result; } -UniValue autocombinerewards(const JSONRPCRequest& request) +UniValue autocombinedust(const JSONRPCRequest& request) { std::shared_ptr const wallet = GetWalletForJSONRPCRequest(request); CWallet* const pwallet = wallet.get(); @@ -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); @@ -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())); @@ -4618,6 +4616,7 @@ static const CRPCCommand commands[] = { "wallet", "getstakingstatus", &getstakingstatus, {} }, { "wallet", "setstakesplitthreshold", &setstakesplitthreshold, {"value"} }, + { "wallet", "autocombinedust", &autocombinedust, {"enable", "threshold"} }, }; void RegisterWalletRPCCommands(CRPCTable &t) diff --git a/src/wallet/wallet.cpp b/src/wallet/wallet.cpp index 5659305fbf765..bd76809835125 100644 --- a/src/wallet/wallet.cpp +++ b/src/wallet/wallet.cpp @@ -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) @@ -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) diff --git a/src/wallet/wallet.h b/src/wallet/wallet.h index f9113c6740490..c7789dfdee031 100644 --- a/src/wallet/wallet.h +++ b/src/wallet/wallet.h @@ -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: /* @@ -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 diff --git a/src/wallet/walletdb.cpp b/src/wallet/walletdb.cpp index 6f778c116e543..bfa13568db3eb 100644 --- a/src/wallet/walletdb.cpp +++ b/src/wallet/walletdb.cpp @@ -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; @@ -608,7 +605,10 @@ ReadKeyValue(CWallet* pwallet, CDataStream& ssKey, CDataStream& ssValue, { std::pair 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 (...) { diff --git a/src/wallet/walletdb.h b/src/wallet/walletdb.h index 651d71b611106..55d407b4200b3 100644 --- a/src/wallet/walletdb.h +++ b/src/wallet/walletdb.h @@ -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! diff --git a/src/walletinitinterface.h b/src/walletinitinterface.h index a0821a0094f35..9bc6323a4df98 100644 --- a/src/walletinitinterface.h +++ b/src/walletinitinterface.h @@ -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;