Skip to content

Commit

Permalink
rpc getblock not checking proog of work and mutex lock/unlock on rx_s…
Browse files Browse the repository at this point in the history
…low_hash
  • Loading branch information
Ars0nic committed Sep 4, 2020
1 parent 777077d commit 83e6f4b
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 22 deletions.
20 changes: 14 additions & 6 deletions src/crypto/randomx.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,11 @@

//#define KEY_CHANGE 2048
//#define SWITCH_KEY 64
static CCriticalSection cs_randomx;

uint256 GetRandomXSeed(const uint32_t& nHeight)
{
// printf("the seed function \n");
printf("the seed function \n");
static uint256 current_key_block;
uint32_t SeedStartingHeight = Params().GetConsensus().RdxSeedHeight;
uint32_t SeedInterval = Params().GetConsensus().RdxSeedInterval;
Expand All @@ -42,13 +43,14 @@ uint256 GetRandomXSeed(const uint32_t& nHeight)
current_key_block = /*uint256("0xaf8367c7285833f5036ae884aadcc64a277b7e20bfb160cf57eaf7ece0e00c68size"); */ chainActive[second_check-SeedStartingHeight]->GetBlockHash();
}
}

printf("leaving the seed function \n");
return current_key_block;
}


void rx_slow_hash(const char* data, char* hash, int length, uint256 seedhash)
{
ENTER_CRITICAL_SECTION(cs_randomx)
// printf("%s\n", __func__);
static bool is_init; // = false;
// static char randomx_seed[64]; //={0}; // this should be 64 and not 32
Expand Down Expand Up @@ -87,12 +89,14 @@ void rx_slow_hash(const char* data, char* hash, int length, uint256 seedhash)
// printf("before calcultae hash %s\n", __func__);
randomx_calculate_hash(rx_vm, data, length, hash);
// printf("after calcultae hash %s\n", __func__);
LEAVE_CRITICAL_SECTION(cs_randomx)
}


void rx_slow_hash2(const char* data, char* hash, int length, uint256 seedhash)
{
// printf("%s\n", __func__);
ENTER_CRITICAL_SECTION(cs_randomx)
printf("%s ... ", __func__);
static bool is_init; // = false;
// static char randomx_seed[64]; //={0}; // this should be 64 and not 32
static uint256 randomx_seed2;
Expand All @@ -117,6 +121,7 @@ void rx_slow_hash2(const char* data, char* hash, int length, uint256 seedhash)
is_init = true;
}
if (randomx_seed2!=seedhash) {
printf(".... random seed changing .... ");
randomx_seed2 = seedhash;
// randomx_reinit();
randomx_destroy_vm(rx_vm);
Expand All @@ -125,17 +130,20 @@ void rx_slow_hash2(const char* data, char* hash, int length, uint256 seedhash)
cache = randomx_alloc_cache(flags);
randomx_init_cache(cache, randomx_seed2.GetHex().c_str(), 64);
rx_vm = randomx_create_vm(flags, cache, nullptr);


}
// printf("before calcultae hash %s\n", __func__);
printf(".... before hash ....%s\n", __func__);
randomx_calculate_hash(rx_vm, data, length, hash);
// printf("after calcultae hash %s\n", __func__);
printf("leaving %s\n", __func__);
LEAVE_CRITICAL_SECTION(cs_randomx)
}


void rx_slow_hash2_old(const char* data, char* hash, int length, uint256 seedhash)
{

printf("%s .....", __func__);
bool is_init = false;
// static char randomx_seed[64]; //={0}; // this should be 64 and not 32
uint256 randomx_seed2;
Expand All @@ -155,5 +163,5 @@ void rx_slow_hash2_old(const char* data, char* hash, int length, uint256 seedhas
randomx_destroy_vm(rx_vm);
randomx_release_cache(cache);


printf("leaving %s\n", __func__);
}
19 changes: 10 additions & 9 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1860,9 +1860,9 @@ bool WriteBlockToDisk(const CBlock& block, CDiskBlockPos& pos)
return true;
}

bool ReadBlockFromDisk(CBlock& block, const CDiskBlockPos& pos, int nHeight, const Consensus::Params& consensusParams)
bool ReadBlockFromDisk(CBlock& block, const CDiskBlockPos& pos, int nHeight, const Consensus::Params& consensusParams, bool required)
{

printf("** required =%d **",required);
block.SetNull();

// Open history file to read
Expand All @@ -1878,7 +1878,8 @@ bool ReadBlockFromDisk(CBlock& block, const CDiskBlockPos& pos, int nHeight, con
}

// Check the header
if (block.IsProofOfWork()) {
if (block.IsProofOfWork() && required==true) {
printf(" required = %d ",required);
if (!CheckProofOfWork(block.GetHash(nHeight,2), block.nBits, consensusParams))
return error("ReadBlockFromDisk : Errors in block header");
}
Expand All @@ -1889,12 +1890,12 @@ bool ReadBlockFromDisk(CBlock& block, const CDiskBlockPos& pos, int nHeight, con
return true;
}

bool ReadBlockFromDisk(CBlock& block, const CBlockIndex* pindex, const Consensus::Params& consensusParams) {

if (!ReadBlockFromDisk(block, pindex->GetBlockPos(), pindex->nHeight, consensusParams))
bool ReadBlockFromDisk(CBlock& block, const CBlockIndex* pindex, const Consensus::Params& consensusParams, bool required) {
printf("** required =%d **",required);
if (!ReadBlockFromDisk(block, pindex->GetBlockPos(), pindex->nHeight, consensusParams, required))
return false;
//both phi1612 and phi2 hashes do not match indexed hash
if (block.GetHash() != pindex->GetBlockHash() && block.GetHash(pindex->nHeight,2) != pindex->GetBlockHash() && block.GetHash(pindex->nHeight) != pindex->GetBlockHash()) {
//both phi1612 and phi2 hashes do not match indexed hash // rdx pow never matches GetBlockHash per design
if (block.GetHash() != pindex->GetBlockHash() && block.GetHash(pindex->nHeight) != pindex->GetBlockHash()) {
LogPrintf("%s : block=%s index=%s\n", __func__, block.GetHash().GetHex(), pindex->GetBlockHash().GetHex());
return error("ReadBlockFromDisk(CBlock&, CBlockIndex*) : GetHash() doesn't match index");
}
Expand Down Expand Up @@ -4934,7 +4935,7 @@ bool ProcessNewBlock(CValidationState& state, const CChainParams& chainparams, C
else if (pblock->GetHash() != chainparams.GetConsensus().hashGenesisBlock && pfrom != NULL) {
CBlockIndex* pindexPrev = LookupBlockIndex(pblock->hashPrevBlock);
if (!pindexPrev) {
pfrom->PushMessage("getblocks", chainActive.GetLocator(), uint256(0));
pfrom->PushMessage("wtf getblocks", chainActive.GetLocator(), uint256(0));
return false;
} else {
nHeight = pindexPrev->nHeight + 1;
Expand Down
4 changes: 2 additions & 2 deletions src/main.h
Original file line number Diff line number Diff line change
Expand Up @@ -657,8 +657,8 @@ bool GetSpentIndex(CSpentIndexKey &key, CSpentIndexValue &value);

/** Functions for disk access for blocks */
bool WriteBlockToDisk(const CBlock& block, CDiskBlockPos& pos);
bool ReadBlockFromDisk(CBlock& block, const CDiskBlockPos& pos, int nHeight, const Consensus::Params& consensusParams);
bool ReadBlockFromDisk(CBlock& block, const CBlockIndex* pindex, const Consensus::Params& consensusParams);
bool ReadBlockFromDisk(CBlock& block, const CDiskBlockPos& pos, int nHeight, const Consensus::Params& consensusParams, bool required = true);
bool ReadBlockFromDisk(CBlock& block, const CBlockIndex* pindex, const Consensus::Params& consensusParams, bool required = true);


/** Functions for validating blocks and updating the block tree */
Expand Down
12 changes: 8 additions & 4 deletions src/rpcblockchain.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ CBlockIndex* GetLastBlockOfType(const int nPoS) // 0: PoW; 1: PoS

UniValue blockToJSON(const CBlock& block, const CBlockIndex* blockindex, bool txDetails = false)
{
printf("%s \n",__func__);
UniValue result(UniValue::VOBJ);
result.push_back(Pair("hash", block.GetHash(blockindex->nHeight).GetHex()));
int confirmations = -1;
Expand Down Expand Up @@ -109,6 +110,8 @@ UniValue blockToJSON(const CBlock& block, const CBlockIndex* blockindex, bool tx
CBlockIndex* pnext = chainActive.Next(blockindex);
if (pnext)
result.push_back(Pair("nextblockhash", pnext->GetBlockHash().GetHex()));

printf("leaving %s \n",__func__);
return result;
}

Expand Down Expand Up @@ -346,6 +349,7 @@ UniValue getblockhash(const UniValue& params, bool fHelp)

UniValue getblock(const UniValue& params, bool fHelp)
{
printf("entering %s ",__func__);
if (fHelp || params.size() < 1 || params.size() > 2)
throw runtime_error(
"getblock \"hash\" ( verbose )\n"
Expand Down Expand Up @@ -392,17 +396,17 @@ UniValue getblock(const UniValue& params, bool fHelp)

CBlock block;
CBlockIndex* pblockindex = mapBlockIndex[hash];

if (!ReadBlockFromDisk(block, pblockindex, Params().GetConsensus()))
printf("...before ReadBlockFromDisk...");
if (!ReadBlockFromDisk(block, pblockindex, Params().GetConsensus(),false)) // check proof of work not required here
throw JSONRPCError(RPC_INTERNAL_ERROR, "Can't read block from disk");

printf("...after ReadBlockFromDisk...");
if (!fVerbose) {
CDataStream ssBlock(SER_NETWORK, PROTOCOL_VERSION);
ssBlock << block;
std::string strHex = HexStr(ssBlock.begin(), ssBlock.end());
return strHex;
}

printf("%s \n",__func__);
return blockToJSON(block, pblockindex);
}

Expand Down
7 changes: 6 additions & 1 deletion src/rpcmining.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -967,8 +967,11 @@ class submitblock_StateCatcher : public CValidationInterface
protected:
virtual void BlockChecked(const CBlock& block, const CValidationState& stateIn)
{
if (block.GetHash(nHeight) != hash)
printf("check submitted block \n");
if (block.GetHash(nHeight) != hash) {
printf("block.GetHash(nHeight) != hash");
return;
}
found = true;
state = stateIn;
};
Expand Down Expand Up @@ -1031,7 +1034,9 @@ UniValue submitblock(const UniValue& params, bool fHelp)

submitblock_StateCatcher sc(block.GetHash(TheHeight), TheHeight);
RegisterValidationInterface(&sc);
printf("entering ProcessNewBlock\n");
bool fAccepted = ProcessNewBlock(state, Params(), NULL, &block);
printf("leaving ProcessNewBlock\n");
UnregisterValidationInterface(&sc);
if (fBlockPresent) {
if (fAccepted && !sc.found) {
Expand Down

0 comments on commit 83e6f4b

Please sign in to comment.