Skip to content

Commit

Permalink
Merge #997: Add RPCs for external RandomX mining
Browse files Browse the repository at this point in the history
9974e54 Add RPCs for external RandomX mining (Johannes Westphal)

Pull request description:

  ### Problem ###
  - No external RandomX mining possible.
  - Mining multiple algorithms at the same time is currently not possible.
  - The algorithm `getblocktemplate` RPC uses for its blocks depends on the value the user selects for mining in the GUI of the wallet / in the config.

  ### Root Cause ###
  Not implemented yet.

  ### Solution ###
  The solution was to:
  1. Add an `algo` parameter to the `getblocktemplate` RPC which overrides any mining algorithm specified in the config / GUI.
  2. Add a new RPC `rxrpcsb` for submitting randomx blocks, analogous to `pprpcsb`.
  This makes it possible to mine at the same time progpow, randomx and sha256d externally, independently of what you may additionally mine using the wallet.

  ### Bounty PR ###
  ?

  ### Bounty Payment Address ##
  `sv1qqpjsrc60t60jhaywj5krmwla52ska70twc7wun6qnee65guxhvtxegpqwhuxypra4jn3pq86s24ryltcw6g2ss4573hyqac9u4g23m9mvxpyqqqwny49k`

  ### Unit Testing Results ###
  Tested by mining at the same time progpow blocks with t-rex miner and randomx blocks with xmrig, where both found at least one block.

Tree-SHA512: 7472c6e21198aca3fbe511271816d2779d9d5c1407e2e108768be22d1ca94ecbc70839f80631253c5946c42803ce678a550daa2dd3160d2ba9963c674a5f8f7b
  • Loading branch information
codeofalltrades committed Apr 21, 2022
2 parents b96cde2 + 9974e54 commit 7c8bee1
Show file tree
Hide file tree
Showing 7 changed files with 314 additions and 168 deletions.
4 changes: 2 additions & 2 deletions src/miner.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ void BlockAssembler::resetBlock()
nFees = 0;
}

std::unique_ptr<CBlockTemplate> BlockAssembler::CreateNewBlock(const CScript& scriptPubKeyIn, bool fMineWitnessTx, bool fProofOfStake, bool fProofOfFullNode)
std::unique_ptr<CBlockTemplate> BlockAssembler::CreateNewBlock(const CScript& scriptPubKeyIn, bool fMineWitnessTx, bool fProofOfStake, bool fProofOfFullNode, int nPoWType)
{
int64_t nTimeStart = GetTimeMicros();
int64_t nComputeTimeStart = GetTimeMillis();
Expand Down Expand Up @@ -207,7 +207,7 @@ std::unique_ptr<CBlockTemplate> BlockAssembler::CreateNewBlock(const CScript& sc
}
}

pblock->nVersion = ComputeBlockVersion(pindexPrev, chainparams.GetConsensus(), pblock->nTime, !fProofOfStake);
pblock->nVersion = ComputeBlockVersion(pindexPrev, chainparams.GetConsensus(), pblock->nTime, !fProofOfStake, nPoWType);
// -regtest only: allow overriding block.nVersion with
// -blockversion=N to test forking scenarios
if (chainparams.MineBlocksOnDemand())
Expand Down
2 changes: 1 addition & 1 deletion src/miner.h
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,7 @@ class BlockAssembler
BlockAssembler(const CChainParams& params, const Options& options);

/** Construct a new block template with coinbase to scriptPubKeyIn */
std::unique_ptr<CBlockTemplate> CreateNewBlock(const CScript& scriptPubKeyIn, bool fMineWitnessTx=true, bool fProofOfStake=false, bool fProofOfFullNode = false);
std::unique_ptr<CBlockTemplate> CreateNewBlock(const CScript& scriptPubKeyIn, bool fMineWitnessTx=true, bool fProofOfStake=false, bool fProofOfFullNode = false, int nPoWType = 0);

private:
// utility functions
Expand Down
460 changes: 301 additions & 159 deletions src/rpc/mining.cpp

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions src/util/strencodings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -304,15 +304,15 @@ bool ParseInt64(const std::string& str, int64_t *out)
n <= std::numeric_limits<int64_t>::max();
}

bool ParseUInt32(const std::string& str, uint32_t *out)
bool ParseUInt32(const std::string& str, uint32_t *out, int base)
{
if (!ParsePrechecks(str))
return false;
if (str.size() >= 1 && str[0] == '-') // Reject negative values, unfortunately strtoul accepts these by default if they fit in the range
return false;
char *endp = nullptr;
errno = 0; // strtoul will not set errno if valid
unsigned long int n = strtoul(str.c_str(), &endp, 10);
unsigned long int n = strtoul(str.c_str(), &endp, base);
if(out) *out = (uint32_t)n;
// Note that strtoul returns a *unsigned long int*, so even if it doesn't report an over/underflow
// we still have to check that the returned value is within the range of an *uint32_t*. On 64-bit
Expand Down
2 changes: 1 addition & 1 deletion src/util/strencodings.h
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ bool ParseInt64(const std::string& str, int64_t *out);
* @returns true if the entire string could be parsed as valid integer,
* false if not the entire string could be parsed or when overflow or underflow occurred.
*/
bool ParseUInt32(const std::string& str, uint32_t *out);
bool ParseUInt32(const std::string& str, uint32_t *out, int base = 10);

/**
* Convert decimal string to unsigned 64-bit integer with strict parse error feedback.
Expand Down
8 changes: 6 additions & 2 deletions src/validation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2137,7 +2137,7 @@ void ThreadScriptCheck() {
// Protected by cs_main
VersionBitsCache versionbitscache;

int32_t ComputeBlockVersion(const CBlockIndex* pindexPrev, const Consensus::Params& params, const uint32_t& blockTime, const bool& fProofOfWork)
int32_t ComputeBlockVersion(const CBlockIndex* pindexPrev, const Consensus::Params& params, const uint32_t& blockTime, const bool& fProofOfWork, int nPoWType)
{
LOCK(cs_main);
int32_t nVersion = VERSIONBITS_OLD_POW_VERSION;
Expand All @@ -2146,7 +2146,11 @@ int32_t ComputeBlockVersion(const CBlockIndex* pindexPrev, const Consensus::Para
nVersion = VERSIONBITS_NEW_POW_VERSION;

if (fProofOfWork) {
if (GetMiningAlgorithm() == MINE_PROGPOW) {
if (nPoWType == CBlockHeader::PROGPOW_BLOCK ||
nPoWType == CBlockHeader::RANDOMX_BLOCK ||
nPoWType == CBlockHeader::SHA256D_BLOCK) {
nVersion |= nPoWType;
} else if (GetMiningAlgorithm() == MINE_PROGPOW) {
nVersion |= CBlockHeader::PROGPOW_BLOCK;
} else if (GetMiningAlgorithm() == MINE_SHA256D) {
nVersion |= CBlockHeader::SHA256D_BLOCK;
Expand Down
2 changes: 1 addition & 1 deletion src/validation.h
Original file line number Diff line number Diff line change
Expand Up @@ -536,7 +536,7 @@ extern VersionBitsCache versionbitscache;
/**
* Determine what nVersion a new block should use.
*/
int32_t ComputeBlockVersion(const CBlockIndex* pindexPrev, const Consensus::Params& params, const uint32_t& blockTime, const bool& fProofOfWork);
int32_t ComputeBlockVersion(const CBlockIndex* pindexPrev, const Consensus::Params& params, const uint32_t& blockTime, const bool& fProofOfWork, int nPoWType = 0);

/** Reject codes greater or equal to this can be returned by AcceptToMemPool
* for transactions, to signal internal conditions. They cannot and should not
Expand Down

0 comments on commit 7c8bee1

Please sign in to comment.