Skip to content

Commit

Permalink
Add RPCs for external RandomX mining
Browse files Browse the repository at this point in the history
  • Loading branch information
us77ipis committed Apr 17, 2022
1 parent b2b688a commit 9974e54
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 9974e54

Please sign in to comment.