Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding new options so that the miner can mine to a specific address. #352

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 44 additions & 5 deletions src/miner.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -940,13 +940,14 @@ struct MinerContext {
int nonces_per_thread;
const CChainParams& chainparams;
std::shared_ptr<CReserveScript>& coinbase_script;
unsigned int extra_nonce;
ctpl::thread_pool& pool;
};

void MinerWorker(int thread_id, MinerContext& ctx)
{
auto start_nonce = thread_id * ctx.nonces_per_thread;
unsigned int nExtraNonce = 0;
unsigned int nExtraNonce = ctx.extra_nonce;

while (ctx.alive) {
if (ctx.chainparams.MiningRequiresPeers()) {
Expand Down Expand Up @@ -1108,7 +1109,8 @@ void static MeritMiner(
const CChainParams& chainparams,
int pow_threads,
int bucket_size,
int bucket_threads)
int bucket_threads,
unsigned int extra_nonce)
{
assert(coinbase_script);
RenameThread("merit-miner");
Expand Down Expand Up @@ -1144,6 +1146,7 @@ void static MeritMiner(
bucket_size,
chainparams,
coinbase_script,
extra_nonce,
pool
};

Expand All @@ -1168,7 +1171,31 @@ void static MeritMiner(
}
}

void GenerateMerit(bool mine, int pow_threads, int bucket_size, int bucket_threads, const CChainParams& chainparams)
void GenerateMerit(
bool mine,
int pow_threads,
int bucket_size,
int bucket_threads,
const CChainParams& chainparams)
{
GenerateMerit(
mine,
pow_threads,
bucket_size,
bucket_threads,
CNoDestination{},
0, //extra nonce
chainparams);
}

void GenerateMerit(
bool mine,
int pow_threads,
int bucket_size,
int bucket_threads,
CTxDestination dest,
int extra_nonce,
const CChainParams& chainparams)
{
static boost::thread* minerThread = nullptr;

Expand All @@ -1191,7 +1218,18 @@ void GenerateMerit(bool mine, int pow_threads, int bucket_size, int bucket_threa
}

std::shared_ptr<CReserveScript> coinbase_script;
GetMainSignals().ScriptForMining(coinbase_script);
// if no destination is specified then we default to the wallet's address
if(!IsValidDestination(dest)) {
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  • space /pedantic

GetMainSignals().ScriptForMining(coinbase_script);
} else {
referral::Address address;
GetUint160(dest, address);
auto referral = prefviewcache->GetReferral(address);
if(referral) {
coinbase_script = std::make_shared<CReserveScript>();
coinbase_script->reserveScript = GetScriptForRawPubKey(referral->pubkey);
}
}

if(!coinbase_script) {
throw std::runtime_error("unable to generate a coinbase script for mining");
Expand All @@ -1203,5 +1241,6 @@ void GenerateMerit(bool mine, int pow_threads, int bucket_size, int bucket_threa
chainparams,
pow_threads,
bucket_size,
bucket_threads);
bucket_threads,
extra_nonce);
}
16 changes: 15 additions & 1 deletion src/miner.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,21 @@ const int DEFAULT_MINING_POW_THREADS = 2;


/** Run the miner threads */
void GenerateMerit(bool mine, int pow_threads, int bucket_size, int bucket_threads, const CChainParams& chainparams);
void GenerateMerit(
bool mine,
int pow_threads,
int bucket_size,
int bucket_threads,
const CChainParams& chainparams);

void GenerateMerit(
bool mine,
int pow_threads,
int bucket_size,
int bucket_threads,
CTxDestination dest,
int extra_nonce,
const CChainParams& chainparams);

struct CBlockTemplate
{
Expand Down
1 change: 1 addition & 0 deletions src/rpc/client.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ static const CRPCConvertParam vRPCConvertParams[] =
{ "setmining", 1, "minepowthreads" },
{ "setmining", 2, "minebucketthreads" },
{ "setmining", 3, "minebucketsize" },
{ "setmining", 4, "opts" },
{ "generatetoaddress", 0, "nblocks" },
{ "generatetoaddress", 2, "maxtries" },
{ "generatetoaddress", 3, "nthreads" },
Expand Down
39 changes: 37 additions & 2 deletions src/rpc/mining.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -988,7 +988,7 @@ UniValue estimaterawfee(const JSONRPCRequest& request)

UniValue setmining(const JSONRPCRequest& request)
{
if (request.fHelp || request.params.size() < 1 || request.params.size() > 4)
if (request.fHelp || request.params.size() < 1 || request.params.size() > 5)
throw std::runtime_error(
"setmining mine ( minepowthreads ) ( minebucketthreads ) ( minebucketsize ) \n"
"\nSet 'mine' true or false to turn generation on or off.\n"
Expand All @@ -1001,6 +1001,11 @@ UniValue setmining(const JSONRPCRequest& request)
"2. minepowthreads (numeric, optional) Set the processor limit for pow attempt when mining is on. Can be -1 for unlimited.\n"
"3. minebucketthreads (numeric, optional) Set number of nonces buckets to run in parallel.\n"
"4. minebucketsize (numeric, optional) Set number of nonces in on bucket.\n"
"5. { (numeric, optional) Set number of nonces in on bucket.\n"
" \"address\" : str, (string, optional) mine to the address specified instead of the one in your wallet\n"
" \"extranonce\" : num, (numeric, optional) starting extra nonce \n"
" \"randomnonce\" : bool (boolean, optional) random extra nonce \n"
" }\n"
"\nExamples:\n"
"\nSet the generation on with a limit of one processor\n"
+ HelpExampleCli("setmining", "true 1")
Expand Down Expand Up @@ -1038,12 +1043,42 @@ UniValue setmining(const JSONRPCRequest& request)
bucket_size = request.params[3].get_int();
}

CTxDestination dest = CNoDestination();
unsigned int extra_nonce = 0;

if (request.params[4].isObject()) {
const auto& extra = request.params[4].get_obj();
UniValue address_val = find_value(extra, "address");
if (address_val.isStr()) {
auto address = address_val.get_str();
dest = LookupDestination(address);
}

UniValue random_nonce_val = find_value(extra, "randomnonce");
if (random_nonce_val.isBool() && random_nonce_val.get_bool()) {
extra_nonce = std::abs(GetRandInt(std::numeric_limits<int>::max()));
}

UniValue extra_nonce_val = find_value(extra, "extranonce");
if (extra_nonce_val.isNum()) {
extra_nonce = extra_nonce_val.get_int();
}
}


gArgs.ForceSetArg("-mine", (mine ? "1" : "0"));
gArgs.ForceSetArg("-minepowthreads", itostr(pow_threads));
gArgs.ForceSetArg("-minebucketthreads", itostr(bucket_threads));
gArgs.ForceSetArg("-minebucketsize", itostr(bucket_size));

GenerateMerit(mine, pow_threads, bucket_size, bucket_threads, Params());
GenerateMerit(
mine,
pow_threads,
bucket_size,
bucket_threads,
dest,
extra_nonce,
Params());
if (mine) {
StartMining();
} else {
Expand Down
2 changes: 1 addition & 1 deletion src/script/standard.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -513,7 +513,7 @@ CScript GetParameterizedP2SH(const CParamScriptID& dest)

CScript GetScriptForRawPubKey(const CPubKey& pubKey)
{
return CScript() << std::vector<unsigned char>(pubKey.begin(), pubKey.end()) << OP_CHECKSIG;
return CScript() << ToByteVector(pubKey) << OP_CHECKSIG;
}

CScript GetScriptForMultisig(int nRequired, const std::vector<CPubKey>& keys)
Expand Down