[Mining] Use -miningaddress for in-wallet mining.
Replaces the hook to get a mining address from the wallet
with a retrieval of the address set as `-miningaddress`.
Performs an additional safety check at startup that the wallet
actually owns the given address.
MODIFY
src/init.cpp
src/init.cpp
@@ -1933,6 +1933,17 @@ bool AppInitMain()
// Link thread groups
LinkAutoSpendThreadGroup(&threadGroupAutoSpend);
+ // Validate wallet mining address.
+ std::string sAddress = gArgs.GetArg("-miningaddress", "");
+ if (!sAddress.empty()) {
+ CTxDestination dest = DecodeDestination(sAddress);
+
+ auto pt = GetMainWallet();
+ if (pt && pt->IsMine(dest) != ISMINE_SPENDABLE) {
+ return InitError(strprintf(_("Invalid -miningaddress: '%s' not owned by wallet"), sAddress));
+ }
+ }
+
// Start wallet CPU mining if the -gen=<n> parameter is given
int nThreads = gArgs.GetArg("-gen", 0);
if (nThreads) {

MODIFY
src/wallet/wallet.cpp
src/wallet/wallet.cpp
@@ -4702,7 +4702,7 @@ void CWallet::MarkReserveKeysAsUsed(int64_t keypool_id)
}
}
-void CWallet::GetScriptForMining(std::shared_ptr<CReserveScript> &script)
+void CWallet::GetScriptForMiningReserveKey(std::shared_ptr<CReserveScript> &script)
{
std::shared_ptr<CReserveKey> rKey = std::make_shared<CReserveKey>(this);
CPubKey pubkey;
@@ -4713,6 +4713,19 @@ void CWallet::GetScriptForMining(std::shared_ptr<CReserveScript> &script)
script->reserveScript = CScript() << ToByteVector(pubkey) << OP_CHECKSIG;
}
+void CWallet::GetScriptForMining(std::shared_ptr<CReserveScript> &script)
+{
+ std::string sAddress = gArgs.GetArg("-miningaddress", "");
+ if (!sAddress.empty()) {
+ CTxDestination dest = DecodeDestination(sAddress);
+
+ script = std::make_shared<CReserveScript>();
+ script->reserveScript = GetScriptForDestination(dest);
+ return;
+ }
+ return GetScriptForMiningReserveKey(script);
+}
+
void CWallet::LockCoin(const COutPoint& output)
{
AssertLockHeld(cs_wallet); // setLockedCoins

MODIFY
src/wallet/wallet.h
src/wallet/wallet.h
@@ -1221,6 +1221,7 @@ public:
const std::string& GetLabelName(const CScript& scriptPubKey) const;
+ void GetScriptForMiningReserveKey(std::shared_ptr<CReserveScript> &script);
void GetScriptForMining(std::shared_ptr<CReserveScript> &script);
unsigned int GetKeyPoolSize() EXCLUSIVE_LOCKS_REQUIRED(cs_wallet)

[Mining] Use -miningaddress for in-wallet mining.

Replaces the hook to get a mining address from the wallet
with a retrieval of the address set as `-miningaddress`.

Performs an additional safety check at startup that the wallet
actually owns the given address.