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

SHA256 Mining On Testnet #1259

Open
wants to merge 8 commits 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
4 changes: 4 additions & 0 deletions doc/man/raven-qt.1
Original file line number Diff line number Diff line change
Expand Up @@ -357,6 +357,10 @@ Debugging/Testing options:
.IP
Append comment to the user agent string
.HP
\fB\-algo=\fR<algo>
.IP
Sets the mining algorithm. Default is Kawpow; alternative is SHA256, which is useful for testnet mining.
.HP
\fB\-debug=\fR<category>
.IP
Output debugging information (default: 0, supplying <category> is
Expand Down
4 changes: 4 additions & 0 deletions doc/man/ravend.1
Original file line number Diff line number Diff line change
Expand Up @@ -362,6 +362,10 @@ Debugging/Testing options:
.IP
Append comment to the user agent string
.HP
\fB\-algo=\fR<algo>
.IP
Sets the mining algorithm. Default is Kawpow; alternative is SHA256, which is useful for testnet mining.
.HP
\fB\-debug=\fR<category>
.IP
Output debugging information (default: 0, supplying <category> is
Expand Down
1 change: 1 addition & 0 deletions src/bench/bench.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#include <limits>
#include <map>
#include <string>
#include <cstdint>

#include <boost/preprocessor/cat.hpp>
#include <boost/preprocessor/stringize.hpp>
Expand Down
2 changes: 1 addition & 1 deletion src/chain.h
Original file line number Diff line number Diff line change
Expand Up @@ -423,7 +423,7 @@ class CDiskBlockIndex : public CBlockIndex
READWRITE(hashMerkleRoot);
READWRITE(nTime);
READWRITE(nBits);
if (nTime < nKAWPOWActivationTime) {
if (nTime < nKAWPOWActivationTime || (bNetwork.fOnTestnet && !fKawpowAsMiningAlgo && nTime >= nSHA256KawpowSwitchActivationTime)) {
READWRITE(nNonce);
} else {
//KAWPOW
Expand Down
20 changes: 20 additions & 0 deletions src/chainparams.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -270,6 +270,10 @@ class CMainParams : public CChainParams {

nKAAAWWWPOWActivationTime = 1588788000; // UTC: Wed May 06 2020 18:00:00
nKAWPOWActivationTime = nKAAAWWWPOWActivationTime;

// Use SHA256 or KawPow depending on this choice
nSHA256KawpowSwitchActivationTime = 1730332800; // UTC: Thur Oct 31 2024 00:00:00
fKawpowAsMiningAlgo = true; // The value is set here but declared as global in primitives/block.h
/** RVN End **/
}
};
Expand Down Expand Up @@ -417,6 +421,7 @@ class CTestNetParams : public CChainParams {
vSeeds.emplace_back("seed-testnet-raven.bitactivate.com", false);
vSeeds.emplace_back("seed-testnet-raven.ravencoin.com", false);
vSeeds.emplace_back("seed-testnet-raven.ravencoin.org", false);
vSeeds.emplace_back("trvn.crypticwizardry.com", false);

base58Prefixes[PUBKEY_ADDRESS] = std::vector<unsigned char>(1,111);
base58Prefixes[SCRIPT_ADDRESS] = std::vector<unsigned char>(1,196);
Expand Down Expand Up @@ -491,6 +496,18 @@ class CTestNetParams : public CChainParams {

nKAAAWWWPOWActivationTime = 1585159200; //Wed Mar 25 2020 18:00:00 UTC
nKAWPOWActivationTime = nKAAAWWWPOWActivationTime;

// Use SHA256 or KawPow depending on this choice
nSHA256KawpowSwitchActivationTime = 1730332800; // UTC: Thur Oct 31 2024 00:00:00
//fKawpowAsMiningAlgo = false; // The value is set here but declared as global in primitives/block.h

// Use Global Arguments from raven.conf to determine algo
if (gArgs.GetBoolArg("-testnet", false)) {
std::string algo = gArgs.GetArg("-algo", "Kawpow"); // Default to KawPow if not set

// Set the mining algorithm flag (already validated in init.cpp)
fKawpowAsMiningAlgo = (algo == "Kawpow");
}
/** RVN End **/
}
};
Expand Down Expand Up @@ -696,6 +713,9 @@ class CRegTestParams : public CChainParams {
// If you are looking to test the kawpow hashing function in regtest. You will need to change this number
nKAAAWWWPOWActivationTime = 3582830167;
nKAWPOWActivationTime = nKAAAWWWPOWActivationTime;
// Use SHA256 or KawPow depending on this choice
nSHA256KawpowSwitchActivationTime = 1730332800; // UTC: Thur Oct 31 2024 00:00:00
fKawpowAsMiningAlgo = true; // The value is set here but declared as global in primitives/block.h
/** RVN End **/
}
};
Expand Down
1 change: 1 addition & 0 deletions src/chainparams.h
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,7 @@ class CChainParams
int nAssetActivationHeight;

uint32_t nKAAAWWWPOWActivationTime;
uint32_t nSHA256KawpowSwitchActivationTime;
/** RVN End **/
};

Expand Down
3 changes: 2 additions & 1 deletion src/chainparamsseeds.h
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,7 @@ static SeedSpec6 pnSeed6_test[] = {
{{0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0xff,0xff,0xa8,0x77,0x64,0x8c}, 18770},
{{0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0xff,0xff,0x53,0xf3,0xbf,0xc7}, 18770},
{{0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0xff,0xff,0x23,0xa8,0x17,0x4c}, 18770},
{{0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0xff,0xff,0x53,0xcd,0x97,0xb7}, 18770}
{{0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0xff,0xff,0x53,0xcd,0x97,0xb7}, 18770},
{{0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0xff,0xff,0x4a,0x8b,0x16,0x6d}, 18770}
};
#endif // RAVEN_CHAINPARAMSSEEDS_H
17 changes: 17 additions & 0 deletions src/init.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -539,6 +539,7 @@ std::string HelpMessage(HelpMessageMode mode)

strUsage += HelpMessageGroup(_("Debugging/Testing options:"));
strUsage += HelpMessageOpt("-uacomment=<cmt>", _("Append comment to the user agent string"));
strUsage += HelpMessageOpt("-algo=<algo>", _("Sets the value for the mining algo. Default is Kawpow; alternative is SHA256, which is useful for quick testnet mining."));
if (showDebug)
{
strUsage += HelpMessageOpt("-checkblocks=<n>", strprintf(_("How many blocks to check at startup (default: %u, 0 = all)"), DEFAULT_CHECKBLOCKS));
Expand Down Expand Up @@ -912,6 +913,22 @@ void InitParameterInteraction()
LogPrintf("%s: Ignoring blockmaxsize setting which is overridden by blockmaxweight", __func__);
}
}

// Ensure -algo only works on testnet and validate the algo value
if (gArgs.GetBoolArg("-testnet", false)) {
std::string algo = gArgs.GetArg("-algo", "Kawpow"); // Default to Kawpow if not set

// Hard-coded validation logic
if (algo != "SHA256" && algo != "Kawpow") {
// Log an error but don't prevent the node from starting
LogPrintf("%s: Invalid value for -algo: %s. Defaulting to Kawpow.\n", __func__, algo);
}
} else {
// Log if -algo is ignored because -testnet is not enabled
if (gArgs.IsArgSet("-algo")) {
LogPrintf("%s: Ignoring -algo setting as -testnet is not enabled.\n", __func__);
}
}
}

static std::string ResolveErrMsg(const char * const optname, const std::string& strBind)
Expand Down
4 changes: 2 additions & 2 deletions src/pow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ unsigned int static DarkGravityWave(const CBlockIndex* pindexLast, const CBlockH
}

// Count how blocks are KAWPOW mined in the last 180 blocks
if (pindex->nTime >= nKAWPOWActivationTime) {
if (pindex->nTime >= nKAWPOWActivationTime && fKawpowAsMiningAlgo) {
nKAWPOWBlocksFound++;
}

Expand All @@ -72,7 +72,7 @@ unsigned int static DarkGravityWave(const CBlockIndex* pindexLast, const CBlockH
// 180 KAWPOW blocks already. If we haven't we are going to return our
// temp limit. This will allow us to change algos to kawpow without having to
// change the DGW math.
if (pblock->nTime >= nKAWPOWActivationTime) {
if (pblock->nTime >= nKAWPOWActivationTime && fKawpowAsMiningAlgo) {
if (nKAWPOWBlocksFound != nPastBlocks) {
const arith_uint256 bnKawPowLimit = UintToArith256(params.kawpowLimit);
return bnKawPowLimit.GetCompact();
Expand Down
37 changes: 32 additions & 5 deletions src/primitives/block.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ static const uint32_t TESTNET_X16RV2ACTIVATIONTIME = 1567533600;
static const uint32_t REGTEST_X16RV2ACTIVATIONTIME = 1569931200;

uint32_t nKAWPOWActivationTime;
uint32_t nSHA256KawpowSwitchActivationTime;
bool fKawpowAsMiningAlgo;

BlockNetwork bNetwork = BlockNetwork();

Expand All @@ -37,45 +39,65 @@ void BlockNetwork::SetNetwork(const std::string& net)

uint256 CBlockHeader::GetHash() const
{
// Check if we are before the Kawpow activation time
if (nTime < nKAWPOWActivationTime) {
uint32_t nTimeToUse = MAINNET_X16RV2ACTIVATIONTIME;

if (bNetwork.fOnTestnet) {
nTimeToUse = TESTNET_X16RV2ACTIVATIONTIME;
} else if (bNetwork.fOnRegtest) {
nTimeToUse = REGTEST_X16RV2ACTIVATIONTIME;
}

// Use X16RV2 or X16R before Kawpow activation
if (nTime >= nTimeToUse) {
return HashX16RV2(BEGIN(nVersion), END(nNonce), hashPrevBlock);
}

return HashX16R(BEGIN(nVersion), END(nNonce), hashPrevBlock);
} else {
return KAWPOWHash_OnlyMix(*this);
// Kawpow activation time has passed, now check if we are on the testnet
if (bNetwork.fOnTestnet && !fKawpowAsMiningAlgo && nTime >= nSHA256KawpowSwitchActivationTime) {
// If on testnet and Kawpow is disabled, use SerializeHash (CPU-friendly)
return SerializeHash(*this);
} else {
// Otherwise, use Kawpow (mainnet or regtest, or Kawpow on testnet)
return KAWPOWHash_OnlyMix(*this);
}
}
}


uint256 CBlockHeader::GetHashFull(uint256& mix_hash) const
{
// Check if we are before the Kawpow activation time
if (nTime < nKAWPOWActivationTime) {
uint32_t nTimeToUse = MAINNET_X16RV2ACTIVATIONTIME;

if (bNetwork.fOnTestnet) {
nTimeToUse = TESTNET_X16RV2ACTIVATIONTIME;
} else if (bNetwork.fOnRegtest) {
nTimeToUse = REGTEST_X16RV2ACTIVATIONTIME;
}

// Use X16RV2 or X16R before Kawpow activation
if (nTime >= nTimeToUse) {
return HashX16RV2(BEGIN(nVersion), END(nNonce), hashPrevBlock);
}

return HashX16R(BEGIN(nVersion), END(nNonce), hashPrevBlock);
} else {
return KAWPOWHash(*this, mix_hash);
// Kawpow activation time has passed, now check if we are on the testnet
if (bNetwork.fOnTestnet && !fKawpowAsMiningAlgo && nTime >= nSHA256KawpowSwitchActivationTime) {
// If on testnet and Kawpow is disabled, use SerializeHash (CPU-friendly)
return SerializeHash(*this);
} else {
// Otherwise, use Kawpow (mainnet or regtest, or Kawpow on testnet)
return KAWPOWHash(*this, mix_hash);
}
}
}




uint256 CBlockHeader::GetX16RHash() const
{
return HashX16R(BEGIN(nVersion), END(nNonce), hashPrevBlock);
Expand All @@ -86,6 +108,11 @@ uint256 CBlockHeader::GetX16RV2Hash() const
return HashX16RV2(BEGIN(nVersion), END(nNonce), hashPrevBlock);
}

uint256 CBlockHeader::GetSerializeHash() const
{
return SerializeHash(*this); // Use standard SHA256 mining
}

/**
* @brief This takes a block header, removes the nNonce64 and the mixHash. Then performs a serialized hash of it SHA256D.
* This will be used as the input to the KAAAWWWPOW hashing function
Expand Down
6 changes: 5 additions & 1 deletion src/primitives/block.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@
*/

extern uint32_t nKAWPOWActivationTime;
extern uint32_t nSHA256KawpowSwitchActivationTime;
extern bool fKawpowAsMiningAlgo; // declared global here but value is set in chainparams.cpp

class BlockNetwork
{
Expand Down Expand Up @@ -64,7 +66,8 @@ class CBlockHeader
READWRITE(hashMerkleRoot);
READWRITE(nTime);
READWRITE(nBits);
if (nTime < nKAWPOWActivationTime) {
//If the time is before the kawpow activation time OR (on testnet, kawpow is not set as the mining algo, and past the sha256 switch activation time)
if (nTime < nKAWPOWActivationTime || (bNetwork.fOnTestnet && !fKawpowAsMiningAlgo && nTime >= nSHA256KawpowSwitchActivationTime)) {
READWRITE(nNonce);
} else {
READWRITE(nHeight);
Expand Down Expand Up @@ -95,6 +98,7 @@ class CBlockHeader
uint256 GetHash() const;
uint256 GetX16RHash() const;
uint256 GetX16RV2Hash() const;
uint256 GetSerializeHash() const;

uint256 GetHashFull(uint256& mix_hash) const;
uint256 GetKAWPOWHeaderHash() const;
Expand Down
4 changes: 2 additions & 2 deletions src/rpc/mining.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ UniValue generateBlocks(std::shared_ptr<CReserveScript> coinbaseScript, int nGen
uint256 mix_hash;
while (nMaxTries > 0 && pblock->nNonce < nInnerLoopCount && !CheckProofOfWork(pblock->GetHashFull(mix_hash), pblock->nBits,
GetParams().GetConsensus())) {
if (pblock->nTime < nKAWPOWActivationTime) {
if (pblock->nTime < nKAWPOWActivationTime && !fKawpowAsMiningAlgo) {
++pblock->nNonce;
} else {
++pblock->nNonce64;
Expand Down Expand Up @@ -714,7 +714,7 @@ UniValue getblocktemplate(const JSONRPCRequest& request)
result.push_back(Pair("default_witness_commitment", HexStr(pblocktemplate->vchCoinbaseCommitment.begin(), pblocktemplate->vchCoinbaseCommitment.end())));
}

if (pblock->nTime >= nKAWPOWActivationTime) {
if (pblock->nTime >= nKAWPOWActivationTime && fKawpowAsMiningAlgo) {
std::string address = gArgs.GetArg("-miningaddress", "");
if (IsValidDestinationString(address)) {
static std::string lastheader = "";
Expand Down
2 changes: 1 addition & 1 deletion src/rpc/protocol.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ static fs::path GetAuthCookieFile(bool temp=false)
arg += ".tmp";
}
fs::path path(arg);
if (!path.is_complete()) path = GetDataDir() / path;
if (!path.is_absolute()) path = GetDataDir() / path;
return path;
}

Expand Down
1 change: 1 addition & 0 deletions src/support/lockedpool.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
#include <map>
#include <mutex>
#include <memory>
#include <stdexcept>

/**
* OS-dependent allocation and deallocation of locked/pinned memory pages.
Expand Down
1 change: 1 addition & 0 deletions src/test/cuckoocache_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#include "test/test_raven.h"
#include "random.h"
#include <thread>
#include <deque>

/** Test Suite for CuckooCache
*
Expand Down
4 changes: 2 additions & 2 deletions src/util.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -620,7 +620,7 @@ void ClearDatadirCache()
fs::path GetConfigFile(const std::string &confPath)
{
fs::path pathConfigFile(confPath);
if (!pathConfigFile.is_complete())
if (!pathConfigFile.is_absolute())
pathConfigFile = GetDataDir(false) / pathConfigFile;

return pathConfigFile;
Expand Down Expand Up @@ -657,7 +657,7 @@ void ArgsManager::ReadConfigFile(const std::string &confPath)
fs::path GetPidFile()
{
fs::path pathPidFile(gArgs.GetArg("-pid", RAVEN_PID_FILENAME));
if (!pathPidFile.is_complete()) pathPidFile = GetDataDir() / pathPidFile;
if (!pathPidFile.is_absolute()) pathPidFile = GetDataDir() / pathPidFile;
return pathPidFile;
}

Expand Down
4 changes: 2 additions & 2 deletions src/validation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3946,7 +3946,7 @@ static bool FindUndoPos(CValidationState &state, int nFile, CDiskBlockPos &pos,
static bool CheckBlockHeader(const CBlockHeader& block, CValidationState& state, const Consensus::Params& consensusParams, bool fCheckPOW = true)
{
// If we are checking a KAWPOW block below a know checkpoint height. We can validate the proof of work using the mix_hash
if (fCheckPOW && block.nTime >= nKAWPOWActivationTime) {
if (fCheckPOW && block.nTime >= nKAWPOWActivationTime && fKawpowAsMiningAlgo) {
CBlockIndex* pcheckpoint = Checkpoints::GetLastCheckpoint(GetParams().Checkpoints());
if (fCheckPOW && pcheckpoint && block.nHeight <= (uint32_t)pcheckpoint->nHeight) {
if (!CheckProofOfWork(block.GetHash(), block.nBits, consensusParams)) {
Expand All @@ -3963,7 +3963,7 @@ static bool CheckBlockHeader(const CBlockHeader& block, CValidationState& state,
return state.DoS(50, false, REJECT_INVALID, "high-hash", false, "proof of work failed");
}

if (fCheckPOW && block.nTime >= nKAWPOWActivationTime) {
if (fCheckPOW && block.nTime >= nKAWPOWActivationTime && fKawpowAsMiningAlgo) {
if (mix_hash != block.mix_hash) {
return state.DoS(50, false, REJECT_INVALID, "invalid-mix-hash", false, "mix_hash validity failed");
}
Expand Down
4 changes: 2 additions & 2 deletions src/wallet/db.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -267,7 +267,7 @@ bool CDB::VerifyEnvironment(const std::string& walletFile, const fs::path& dataD
LogPrintf("Using wallet %s\n", walletFile);

// Wallet file must be a plain filename without a directory
if (walletFile != fs::basename(walletFile) + fs::extension(walletFile))
if (walletFile != fs::path(walletFile).filename())
{
errorStr = strprintf(_("Wallet %s resides outside data directory %s"), walletFile, dataDir.string());
return false;
Expand Down Expand Up @@ -706,7 +706,7 @@ bool CWalletDBWrapper::Backup(const std::string& strDest)
pathDest /= strFile;

try {
fs::copy_file(pathSrc, pathDest, fs::copy_option::overwrite_if_exists);
fs::copy_file(pathSrc, pathDest, fs::copy_options::overwrite_existing);
LogPrintf("copied %s to %s\n", strFile, pathDest.string());
return true;
} catch (const fs::filesystem_error& e) {
Expand Down