diff --git a/src/interfaces/chain.cpp b/src/interfaces/chain.cpp index a3040ab..3aa9ad2 100644 --- a/src/interfaces/chain.cpp +++ b/src/interfaces/chain.cpp @@ -217,8 +217,8 @@ class ChainImpl : public Chain return ::GetAllAssets(); } - void startStake(bool fStake, CWallet *pwallet, std::thread* stakeThread)override{ - ::Stake(fStake, pwallet, stakeThread); + void startStake(bool fStake, CWallet *pwallet)override{ + ::Stake(fStake, pwallet); } CContract getContract(std::string name) override diff --git a/src/interfaces/chain.h b/src/interfaces/chain.h index b923754..c15a9c3 100644 --- a/src/interfaces/chain.h +++ b/src/interfaces/chain.h @@ -94,7 +94,7 @@ class Chain virtual CAsset getAsset(std::string sAssetName) = 0; virtual std::vector getAllAssets() =0; virtual CTxMemPool& getMempool() = 0; - virtual void startStake(bool fStake, CWallet *pwallet, std::thread* stakeThread) = 0; + virtual void startStake(bool fStake, CWallet *pwallet) = 0; //! Get block height above genesis block. Returns 0 for genesis block, //! 1 for following block, and so on. Returns nullopt for a block not //! included in the current chain. diff --git a/src/miner.cpp b/src/miner.cpp index 04e4add..6808fae 100644 --- a/src/miner.cpp +++ b/src/miner.cpp @@ -682,16 +682,24 @@ void ThreadStakeMiner(CWallet *pwallet) LogPrintf("ThreadStakeMiner exiting\n"); } -void Stake(bool fStake, CWallet *pwallet, std::thread* stakeThread) +std::thread* stakeThread = nullptr; + +void Stake(bool fStake, CWallet *pwallet) { if (stakeThread != nullptr) { + LogPrintf("%s: Destroying Stake thread\n", __func__); + if (stakeThread->joinable()) stakeThread->join(); stakeThread = nullptr; + LogPrintf("%s: Stake thread destroyed \n", __func__); + } if(fStake) { + LogPrintf("%s: Creating Stake thread\n", __func__); + stakeThread = new std::thread([&, pwallet] { TraceThread("stake", [&, pwallet] { ThreadStakeMiner(pwallet); }); }); } } diff --git a/src/miner.h b/src/miner.h index a66a438..004516d 100644 --- a/src/miner.h +++ b/src/miner.h @@ -198,7 +198,7 @@ class BlockAssembler int UpdatePackagesForAdded(const CTxMemPool::setEntries& alreadyAdded, indexed_modified_transaction_set& mapModifiedTx) EXCLUSIVE_LOCKS_REQUIRED(m_mempool.cs); }; /** Generate a new block, without valid proof-of-work */ -void Stake(bool fStake, CWallet *pwallet, std::thread* stakeThread); +void Stake(bool fStake, CWallet *pwallet); /** Modify the extranonce in a block */ void IncrementExtraNonce(CBlock* pblock, const CBlockIndex* pindexPrev, unsigned int& nExtraNonce); diff --git a/src/wallet/init.cpp b/src/wallet/init.cpp index 61dff76..0a014bc 100644 --- a/src/wallet/init.cpp +++ b/src/wallet/init.cpp @@ -73,6 +73,9 @@ void WalletInit::AddWalletOptions(ArgsManager& argsman) const argsman.AddArg("-privdb", strprintf("Sets the DB_PRIVATE flag in the wallet db environment (default: %u)", DEFAULT_WALLET_PRIVDB), ArgsManager::ALLOW_ANY | ArgsManager::DEBUG_ONLY, OptionsCategory::WALLET_DEBUG_TEST); argsman.AddArg("-walletrejectlongchains", strprintf("Wallet will not create transactions that violate mempool chain limits (default: %u)", DEFAULT_WALLET_REJECT_LONG_CHAINS), ArgsManager::ALLOW_ANY | ArgsManager::DEBUG_ONLY, OptionsCategory::WALLET_DEBUG_TEST); + argsman.AddArg("-stake", strprintf("Run a thread for staking default: %u)", true), ArgsManager::ALLOW_ANY, OptionsCategory::WALLET); + + argsman.AddHiddenArgs({"-zapwallettxes"}); } diff --git a/src/wallet/load.cpp b/src/wallet/load.cpp index d83fe22..b1372f6 100644 --- a/src/wallet/load.cpp +++ b/src/wallet/load.cpp @@ -124,6 +124,7 @@ void StartWallets(CScheduler& scheduler, const ArgsManager& args) { for (const std::shared_ptr& pwallet : GetWallets()) { pwallet->postInitProcess(); + if (args.GetBoolArg("-stake", true)) pwallet->Stake(true); } diff --git a/src/wallet/wallet.cpp b/src/wallet/wallet.cpp index d25c659..8d48d22 100644 --- a/src/wallet/wallet.cpp +++ b/src/wallet/wallet.cpp @@ -345,8 +345,6 @@ std::shared_ptr CreateWallet(interfaces::Chain& chain, const std::strin AddWallet(wallet); wallet->postInitProcess(); - wallet->Stake(true); - // Write the wallet settings UpdateWalletSetting(chain, name, load_on_start, warnings); diff --git a/src/wallet/wallet.h b/src/wallet/wallet.h index 1f0977a..0b02ed8 100644 --- a/src/wallet/wallet.h +++ b/src/wallet/wallet.h @@ -772,8 +772,6 @@ class CWallet final : public WalletStorage, public interfaces::Chain::Notificati */ uint256 m_last_block_processed GUARDED_BY(cs_wallet); - std::thread* stakeThread = nullptr; - /* Height of last block processed is used by wallet to know depth of transactions * without relying on Chain interface beyond asynchronous updates. For safety, we * initialize it to -1. Height is a pointer on node's tip and doesn't imply