Skip to content
Permalink
Browse files

Devethgani3 (#106)

* Fix crash bug in bh_shared.go

* fix test performance for nonceutil_test.go

* Fix submit response when on handling bytom share submit
  • Loading branch information...
ganibc committed Jul 16, 2018
1 parent f71d115 commit 9ec357d006ebf280c73dfcdf8646faf7811989af
BIN +22.6 KB (100%) prebuild/bytom/bh_shared.a
Binary file not shown.
@@ -209,6 +209,18 @@ int checkProofOfWork(EncodeBlockHeader_return encoded, StratumJobBytom *sJob, St

}

void StratumSessionBytom::Bytom_rpc2ResponseBoolean(const string &idStr, bool result, const string& failMessage) {
if(result)
{
const string s = Strings::Format("{\"id\":%s,\"jsonrpc\":\"2.0\",\"result\":{\"status\":\"OK\"},\"error\":null}\n", idStr.c_str());
sendData(s);
}
else
{
const string s = Strings::Format("{\"id\":%s,\"jsonrpc\":\"2.0\",\"result\":null,\"error\":{\"code\":-1, \"message\":\"%s\"}}\n", idStr.c_str(), failMessage.c_str());
sendData(s);
}
}

void StratumSessionBytom::handleRequest_Submit(const string &idStr, const JsonNode &jparams)
{
@@ -226,7 +238,7 @@ void StratumSessionBytom::handleRequest_Submit(const string &idStr, const JsonNo

ServerBytom *s = dynamic_cast<ServerBytom*> (server_);
if (s == nullptr) {
responseError(idStr, StratumStatus::REJECT_NO_REASON);
Bytom_rpc2ResponseBoolean(idStr, false, "Server error. Contact support.");
LOG(FATAL) << "Code error, casting Server to ServerBytom failed";
// should we assert here?
return;
@@ -237,7 +249,7 @@ void StratumSessionBytom::handleRequest_Submit(const string &idStr, const JsonNo
LocalJob *localJob= findLocalJob(shortJobId);
if (nullptr == localJob)
{
responseError(idStr, StratumStatus::JOB_NOT_FOUND);
Bytom_rpc2ResponseBoolean(idStr, false, "Block expired");
LOG(ERROR) << "can not find local bytom job id=" << (int)shortJobId;
return;
}
@@ -246,15 +258,15 @@ void StratumSessionBytom::handleRequest_Submit(const string &idStr, const JsonNo
exjob = server_->jobRepository_->getStratumJobEx(localJob->jobId_);
if (nullptr == exjob || nullptr == exjob->sjob_)
{
responseError(idStr, StratumStatus::JOB_NOT_FOUND);
Bytom_rpc2ResponseBoolean(idStr, false, "Block expired");
LOG(ERROR) << "bytom local job not found " << std::hex << localJob->jobId_;
return;
}

StratumJobBytom *sJob = dynamic_cast<StratumJobBytom *>(exjob->sjob_);
if (nullptr == sJob)
{
responseError(idStr, StratumStatus::REJECT_NO_REASON);
Bytom_rpc2ResponseBoolean(idStr, false, "Unknown reason");
LOG(FATAL) << "Code error, casting stratum job bytom failed for job id=" << std::hex << localJob->jobId_;
return;
}
@@ -315,29 +327,41 @@ void StratumSessionBytom::handleRequest_Submit(const string &idStr, const JsonNo
if(exjob->isStale())
{
share.status_ = StratumStatus::JOB_NOT_FOUND;
responseError(idStr, StratumStatus::JOB_NOT_FOUND);
Bytom_rpc2ResponseBoolean(idStr, false, "Block expired");
}
else
{
EncodeBlockHeader_return encoded = EncodeBlockHeader(sJob->blockHeader_.version, sJob->blockHeader_.height, (char *)sJob->blockHeader_.previousBlockHash.c_str(), sJob->blockHeader_.timestamp,
nonce, sJob->blockHeader_.bits, (char *)sJob->blockHeader_.transactionStatusHash.c_str(), (char *)sJob->blockHeader_.transactionsMerkleRoot.c_str());
int powResult = BytomUtils::checkProofOfWork(encoded, sJob, localJob);
switch(powResult)
share.status_ = powResult;
if(powResult == StratumStatus::SOLVED)
{
case StratumStatus::SOLVED:
LOG(INFO) << "share solved";
s->sendSolvedShare2Kafka(nonce, encoded.r0, share.height_, Bytom_TargetCompactToDifficulty(sJob->blockHeader_.bits), worker_);
server_->jobRepository_->markAllJobsAsStale();
// do not put break here! It needs to run "ACCEPT" code.
case StratumStatus::ACCEPT:
diffController_->addAcceptedShare(share.shareDiff_);
rpc2ResponseBoolean(idStr, true);
break;
std::cout << "share solved\n";
LOG(INFO) << "share solved";
s->sendSolvedShare2Kafka(nonce, encoded.r0, share.height_, Bytom_TargetCompactToDifficulty(sJob->blockHeader_.bits), worker_);
server_->jobRepository_->markAllJobsAsStale();
diffController_->addAcceptedShare(share.shareDiff_);
Bytom_rpc2ResponseBoolean(idStr, true);
}
else if(powResult == StratumStatus::ACCEPT)
{
diffController_->addAcceptedShare(share.shareDiff_);
Bytom_rpc2ResponseBoolean(idStr, true);
}
else
{
std::string failMessage = "Unknown reason";
switch(share.status_)
{
case StratumStatus::LOW_DIFFICULTY:
failMessage = "Low difficulty share";
break;
}
Bytom_rpc2ResponseBoolean(idStr, false, failMessage);
}
free(encoded.r0);
free(encoded.r1);

share.status_ = powResult;
}


@@ -40,6 +40,8 @@ class StratumSessionBytom : public StratumSession
bool needToSendLoginResponse() const override {return false;}

private:
void Bytom_rpc2ResponseBoolean(const string &idStr, bool result, const string& failMessage = "");

uint8 shortJobId_; //jobId starts from 1
};

@@ -2,7 +2,6 @@ package main

import "C"
import (
"log"
"math/big"

"github.com/bytom/consensus/difficulty"
@@ -84,7 +83,7 @@ func CheckProofOfWorkCPU(hash, seed []byte, bits uint64) bool {
hseed := bc.NewHash(xSeed)

compareHash := tensority.AIHash.Hash(&hhash, &hseed)
log.Printf("Proof hash: 0x%s", compareHash.String())
// log.Printf("Proof hash: 0x%s", compareHash.String())
return difficulty.HashToBig(compareHash).Cmp(difficulty.CompactToBig(bits)) <= 0
}

@@ -107,7 +106,13 @@ func CalculateTargetBinaryByDifficulty(diff uint64, out []byte) {
targetCompact := CalculateTargetCompactByDifficulty(diff)
targetBig := difficulty.CompactToBig(targetCompact)
targetBytes := targetBig.Bytes()
copy(out[:], targetBytes[:32])
bytesLen := len(targetBytes)
startIdx := 32 - bytesLen
for i := 0; i < startIdx; i++ {
out[i] = 0
}
copy(out[startIdx:32], targetBytes[:bytesLen])
// log.Printf("diff %d - compact %d - big %v - bytes %v len %d - out %v len %d", diff, targetCompact, *targetBig, targetBytes, len(targetBytes), out, len(out))
}

//export CalculateTargetCompactByDifficulty
@@ -141,14 +141,16 @@ func TestHash(t *testing.T) {
startT := time.Now()
count := 0
for i, tt := range tests {
bhhash := bc.NewHash(tt.blockHeader)
sdhash := bc.NewHash(tt.seed)

resultCPU := HashCPU(&bhhash, &sdhash)

fmt.Printf("Test case %d:\n", i+1)
for a := 0; a < 100; a++ {
sT := time.Now()
bhhash := bc.NewHash(tt.blockHeader)
sdhash := bc.NewHash(tt.seed)
result := Hash(&bhhash, &sdhash)
resultCPU := HashCPU(&bhhash, &sdhash)
fmt.Printf("%s -- %s", result.String(), resultCPU.String())
// fmt.Printf("%s -- %s", result.String(), resultCPU.String())

if *result != *resultCPU {
fmt.Printf("\tFAIL CPU AND GPU test\n")

0 comments on commit 9ec357d

Please sign in to comment.
You can’t perform that action at this time.