Skip to content

Commit

Permalink
Revert "getwork"
Browse files Browse the repository at this point in the history
This reverts commit 0d8a229.
  • Loading branch information
Bushstar committed Nov 12, 2018
1 parent 7d36bdd commit 6e328f5
Show file tree
Hide file tree
Showing 7 changed files with 2 additions and 220 deletions.
2 changes: 0 additions & 2 deletions src/init.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,6 @@ void Shutdown()
StopHTTPRPC();
StopREST();
StopRPC();
ShutdownRPCMining();
StopHTTPServer();
#ifdef ENABLE_WALLET
if (pwalletMain)
Expand Down Expand Up @@ -1574,7 +1573,6 @@ bool AppInit2(boost::thread_group& threadGroup, CScheduler& scheduler)
StartTorControl(threadGroup, scheduler);

StartNode(threadGroup, scheduler);
InitRPCMining();

// ********************************************************* Step 12: finished

Expand Down
61 changes: 1 addition & 60 deletions src/miner.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,7 @@ CBlockTemplate* BlockAssembler::CreateNewBlock(const CScript& scriptPubKeyIn, bo
coinbaseTx.vout[0].nValue = 0;
*pStakeReward = nFees + GetProofOfStakeReward(nHeight, chainparams.GetConsensus());
} else {

coinbaseTx.vout[0].scriptPubKey = scriptPubKeyIn;
coinbaseTx.vout[0].nValue = nFees + GetBlockSubsidy(nHeight, chainparams.GetConsensus());
}
Expand Down Expand Up @@ -621,66 +622,6 @@ void IncrementExtraNonce(CBlock* pblock, const CBlockIndex* pindexPrev, unsigned
pblock->hashMerkleRoot = BlockMerkleRoot(*pblock);
}

/* Prepares a block header for transmission using RPC getwork */
void FormatDataBuffer(CBlock *pblock, unsigned int *pdata) {
unsigned int i;

struct {
int nVersion;
uint256 hashPrevBlock;
uint256 hashMerkleRoot;
unsigned int nTime;
unsigned int nBits;
unsigned int nNonce;
} data;

data.nVersion = pblock->nVersion;
data.hashPrevBlock = pblock->hashPrevBlock;
data.hashMerkleRoot = pblock->hashMerkleRoot;
data.nTime = pblock->nTime;
data.nBits = pblock->nBits;
data.nNonce = pblock->nNonce;

for (i = 0; i < 20; i++)
pdata[i] = ((unsigned int *) &data)[i];
}

bool CheckWork(const CChainParams& chainparams, CBlock* pblock, CWallet& wallet, CReserveKey& reservekey) {
uint256 hashBlock = pblock->GetHash();
arith_uint256 hashTarget = arith_uint256().SetCompact(pblock->nBits);

if (!pblock->IsProofOfWork())
return error("%s: %s height %d is not a proof-of-work block", __func__, hashBlock.GetHex());

uint256 hashProof = pblock->GetPoWHash();

if (UintToArith256(hashProof) > hashTarget)
return error("%s: block %s height %d proof-of-work not meeting target", __func__, hashBlock.GetHex());

// Found a solution
{
LOCK(cs_main);
if (pblock->hashPrevBlock != hashBestChain)
return error("%s: generated block is stale", __func__);

// Remove key from key pool
reservekey.KeepKey();

// Track how many getdata requests this block gets
{
LOCK(wallet.cs_wallet);
wallet.mapRequestCount[hashBlock] = 0;
}

// Process this block the same as if we had received it from another node
CValidationState state;
if (!ProcessNewBlock(state, chainparams, NULL, pblock, true, NULL, false))
return error("%s: ProcessBlock, block not accepted", __func__);
}

return true;
}

/**
* Internal Staker
*/
Expand Down
7 changes: 0 additions & 7 deletions src/miner.h
Original file line number Diff line number Diff line change
Expand Up @@ -209,13 +209,6 @@ class BlockAssembler

/** Modify the extranonce in a block */
void IncrementExtraNonce(CBlock* pblock, const CBlockIndex* pindexPrev, unsigned int& nExtraNonce);

/** Prepares a block header for transmission using RPC getwork */
void FormatDataBuffer(CBlock *pblock, unsigned int *pdata);

/** Check mined block */
bool CheckWork(const CChainParams& chainparams, CBlock* pblock, CWallet& wallet, CReserveKey& reservekey);

int64_t UpdateTime(CBlockHeader* pblock, const Consensus::Params& consensusParams, const CBlockIndex* pindexPrev);

/** Sign proof-of-stake block */
Expand Down
1 change: 0 additions & 1 deletion src/rpc/client.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,6 @@ static const CRPCConvertParam vRPCConvertParams[] =
{ "listunspent", 0 },
{ "listunspent", 1 },
{ "listunspent", 2 },
{ "getwork", 0 },
{ "getblock", 1 },
{ "getblockheader", 1 },
{ "gettransaction", 1 },
Expand Down
146 changes: 0 additions & 146 deletions src/rpc/mining.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,6 @@
#include "util.h"
#include "utilstrencodings.h"
#include "validationinterface.h"
#ifdef ENABLE_WALLET
#include "wallet/wallet.h"
#endif

#include <stdint.h>

Expand All @@ -34,36 +31,6 @@

using namespace std;

#ifdef ENABLE_WALLET
// Key used by getwork miners.
// Allocated in InitRPCMining, free'd in ShutdownRPCMining
static CReserveKey* pMiningKey = NULL;

void InitRPCMining()
{
if (!pwalletMain)
return;

// getwork/getblocktemplate mining rewards paid here:
pMiningKey = new CReserveKey(pwalletMain);
}

void ShutdownRPCMining()
{
if (!pMiningKey)
return;

delete pMiningKey; pMiningKey = NULL;
}
#else
void InitRPCMining()
{
}
void ShutdownRPCMining()
{
}
#endif

/**
* Return average network hashes per second based on the last 'lookup' blocks,
* or from the last difficulty change if 'lookup' is nonpositive.
Expand Down Expand Up @@ -352,118 +319,6 @@ std::string gbt_vb_name(const Consensus::DeploymentPos pos) {
return s;
}

#ifdef ENABLE_WALLET
UniValue getwork(const UniValue& params, bool fHelp)
{
if (fHelp || params.size() > 1)
throw runtime_error(
"getwork ( \"data\" )\n"
"\nIf 'data' is not specified, it returns the formatted hash data to work on.\n"
"If 'data' is specified, tries to solve the block and returns true if it was successful.\n"
"\nArguments:\n"
"1. \"data\" (string, optional) The hex encoded data to solve\n"
"\nResult (when 'data' is not specified):\n"
"{\n"
" \"data\" : \"xxxxx\", (string) The block data\n"
" \"target\" : \"xxxx\" (string) The little endian hash target\n"
"}\n"
"If [data] is specified, verifies the PoW hash against target and returns true if successful."
"\nExamples:\n"
+ HelpExampleCli("getwork", "")
+ HelpExampleRpc("getwork", "")
);

if (vNodes.empty())
throw JSONRPCError(RPC_CLIENT_NOT_CONNECTED, "Trezarcoin is not connected!");

if (IsInitialBlockDownload())
throw JSONRPCError(RPC_CLIENT_IN_INITIAL_DOWNLOAD, "Trezarcoin is downloading blocks...");

typedef std::map<uint256, std::pair<CBlock*, CScript> > mapNewBlock_t;
static mapNewBlock_t mapNewBlock; // FIXME: thread safety

static boost::shared_ptr<CReserveScript> coinbaseScript;

if (params.size() == 0)
{
// Update block
static unsigned int nTransactionsUpdatedLast;
static CBlockIndex* pindexPrev;
static int64_t nStart;
static CBlockTemplate* pblocktemplate;

if (pindexPrev != chainActive.Tip() ||
(mempool.GetTransactionsUpdated() != nTransactionsUpdatedLast && GetTime() - nStart > 60))
{
if (pindexPrev != chainActive.Tip())
mapNewBlock.clear();

// Clear pindexPrev so future getworks make a new block, despite any failures from here on
pindexPrev = nullptr;

// Store the pindexBest used before CreateNewBlock, to avoid races
nTransactionsUpdatedLast = mempool.GetTransactionsUpdated();
CBlockIndex* pindexPrevNew = chainActive.Tip();
nStart = GetTime();

// Create new block
GetMainSignals().ScriptForMining(coinbaseScript);
pblocktemplate = BlockAssembler(Params()).CreateNewBlock(coinbaseScript->reserveScript);
if (!pblocktemplate)
throw JSONRPCError(RPC_OUT_OF_MEMORY, "Out of memory");

// Need to update only after we know CreateNewBlock succeeded
pindexPrev = pindexPrevNew;
}
CBlock* pblock = &pblocktemplate->block; // pointer for convenience

// Update nTime
UpdateTime(pblock, Params().GetConsensus(), pindexPrev);
pblock->nNonce = 0;

// Update nExtraNonce
static unsigned int nExtraNonce = 0;
IncrementExtraNonce(pblock, pindexPrev, nExtraNonce);

// Save
mapNewBlock[pblock->hashMerkleRoot] = make_pair(pblock, pblock->vtx[0].vin[0].scriptSig);

// Pre-build hash buffers
unsigned int pdata[32];
FormatDataBuffer(pblock, pdata);

arith_uint256 hashTarget = arith_uint256().SetCompact(pblock->nBits);

UniValue result(UniValue::VOBJ);
result.push_back(Pair("data", HexStr(BEGIN(pdata), (char *) &pdata[20])));
result.push_back(Pair("target", HexStr(BEGIN(hashTarget), END(hashTarget))));

return result;
}
else
{
// Parse parameters
vector<unsigned char> vchData = ParseHex(params[0].get_str());
if (vchData.size() < 80)
throw JSONRPCError(RPC_INVALID_PARAMETER, "Invalid parameter");
CBlock* pdata = (CBlock*)&vchData[0];

// Get saved block
if (!mapNewBlock.count(pdata->hashMerkleRoot))
return false;
CBlock* pblock = mapNewBlock[pdata->hashMerkleRoot].first;

pblock->nTime = pdata->nTime;
pblock->nNonce = pdata->nNonce;
pblock->vtx[0].vin[0].scriptSig = mapNewBlock[pdata->hashMerkleRoot].second;
pblock->hashMerkleRoot = pblock->BuildMerkleTree();

assert(pwalletMain != NULL);
return CheckWork(Params(), pblock, *pwalletMain, *pMiningKey);
}
}
#endif

UniValue getblocktemplate(const UniValue& params, bool fHelp)
{
if (fHelp || params.size() > 1)
Expand Down Expand Up @@ -1087,7 +942,6 @@ static const CRPCCommand commands[] =
{ "mining", "getmininginfo", &getmininginfo, true },
{ "mining", "prioritisetransaction", &prioritisetransaction, true },
{ "mining", "getblocktemplate", &getblocktemplate, true },
{ "mining", "getwork", &getwork, true },
{ "mining", "submitblock", &submitblock, true },

{ "generating", "staking", &staking, true },
Expand Down
2 changes: 1 addition & 1 deletion src/rpc/server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -372,7 +372,7 @@ void JSONRequest::parse(const UniValue& valRequest)
if (!valMethod.isStr())
throw JSONRPCError(RPC_INVALID_REQUEST, "Method must be a string");
strMethod = valMethod.get_str();
if (strMethod != "getwork" && strMethod != "getblocktemplate")
if (strMethod != "getblocktemplate")
LogPrint("rpc", "ThreadRPCServer method=%s\n", SanitizeString(strMethod));

// Parse params
Expand Down
3 changes: 0 additions & 3 deletions src/rpc/server.h
Original file line number Diff line number Diff line change
Expand Up @@ -182,9 +182,6 @@ extern uint256 ParseHashO(const UniValue& o, std::string strKey);
extern std::vector<unsigned char> ParseHexV(const UniValue& v, std::string strName);
extern std::vector<unsigned char> ParseHexO(const UniValue& o, std::string strKey);

extern void InitRPCMining();
extern void ShutdownRPCMining();

extern int64_t nWalletUnlockTime;
extern CAmount AmountFromValue(const UniValue& value);
extern UniValue ValueFromAmount(const CAmount& amount);
Expand Down

0 comments on commit 6e328f5

Please sign in to comment.