Some pow.cc refactoring
This commit is contained in:
parent
225fba30e1
commit
719bed1f9b
17
src/pow.cc
17
src/pow.cc
|
@ -10,12 +10,12 @@
|
||||||
|
|
||||||
#define HASH_SIZE 64
|
#define HASH_SIZE 64
|
||||||
#define NTOHLL(x) ( ( (uint64_t)(ntohl( (unsigned int)((x << 32) >> 32) )) << 32) | ntohl( ((unsigned int)(x >> 32)) ) )
|
#define NTOHLL(x) ( ( (uint64_t)(ntohl( (unsigned int)((x << 32) >> 32) )) << 32) | ntohl( ((unsigned int)(x >> 32)) ) )
|
||||||
#define MAX_SAFE_JS_INTEGER 9007199254740991
|
|
||||||
|
|
||||||
int pow(uint32_t pool_size,
|
int pow(size_t pool_size,
|
||||||
int64_t target,
|
uint64_t target,
|
||||||
const uint8_t* initial_hash,
|
const uint8_t* initial_hash,
|
||||||
int64_t* nonce) {
|
uint64_t max_nonce,
|
||||||
|
uint64_t* nonce) {
|
||||||
uint8_t message[HASH_SIZE+sizeof(uint64_t)];
|
uint8_t message[HASH_SIZE+sizeof(uint64_t)];
|
||||||
uint8_t digest[HASH_SIZE];
|
uint8_t digest[HASH_SIZE];
|
||||||
uint64_t* be_nonce;
|
uint64_t* be_nonce;
|
||||||
|
@ -23,14 +23,18 @@ int pow(uint32_t pool_size,
|
||||||
uint64_t i;
|
uint64_t i;
|
||||||
SHA512_CTX sha;
|
SHA512_CTX sha;
|
||||||
|
|
||||||
|
if (!max_nonce) {
|
||||||
|
max_nonce = UINT64_MAX;
|
||||||
|
}
|
||||||
memcpy(message+sizeof(uint64_t), initial_hash, HASH_SIZE);
|
memcpy(message+sizeof(uint64_t), initial_hash, HASH_SIZE);
|
||||||
be_nonce = (uint64_t *)message;
|
be_nonce = (uint64_t *)message;
|
||||||
be_trial = (uint64_t *)digest;
|
be_trial = (uint64_t *)digest;
|
||||||
i = 0;
|
i = 0;
|
||||||
|
|
||||||
while (1) {
|
while (1) {
|
||||||
// This is very unlikely to be ever happen but it's better to be
|
// This is very unlikely to be ever happen but it's better to be
|
||||||
// sure anyway.
|
// sure anyway.
|
||||||
if (i > MAX_SAFE_JS_INTEGER) {
|
if (i > max_nonce) {
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
*be_nonce = NTOHLL(i);
|
*be_nonce = NTOHLL(i);
|
||||||
|
@ -40,11 +44,12 @@ int pow(uint32_t pool_size,
|
||||||
SHA512_Init(&sha);
|
SHA512_Init(&sha);
|
||||||
SHA512_Update(&sha, digest, HASH_SIZE);
|
SHA512_Update(&sha, digest, HASH_SIZE);
|
||||||
SHA512_Final(digest, &sha);
|
SHA512_Final(digest, &sha);
|
||||||
if (NTOHLL(*be_trial) <= (uint64_t)target) {
|
if (NTOHLL(*be_trial) <= target) {
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
i++;
|
i++;
|
||||||
}
|
}
|
||||||
|
|
||||||
*nonce = i;
|
*nonce = i;
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,9 +1,10 @@
|
||||||
#ifndef BITMESSAGE_POW_H
|
#ifndef BITMESSAGE_POW_H
|
||||||
#define BITMESSAGE_POW_H
|
#define BITMESSAGE_POW_H
|
||||||
|
|
||||||
int pow(uint32_t pool_size,
|
int pow(size_t pool_size,
|
||||||
int64_t target,
|
uint64_t target,
|
||||||
const uint8_t* initial_hash,
|
const uint8_t* initial_hash,
|
||||||
int64_t* nonce);
|
uint64_t max_nonce,
|
||||||
|
uint64_t* nonce);
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
|
@ -13,11 +13,13 @@ using v8::Object;
|
||||||
using v8::String;
|
using v8::String;
|
||||||
using v8::Integer;
|
using v8::Integer;
|
||||||
|
|
||||||
|
#define MAX_SAFE_JS_INTEGER 9007199254740991
|
||||||
|
|
||||||
class PowWorker : public NanAsyncWorker {
|
class PowWorker : public NanAsyncWorker {
|
||||||
public:
|
public:
|
||||||
PowWorker(NanCallback* callback,
|
PowWorker(NanCallback* callback,
|
||||||
uint32_t pool_size,
|
size_t pool_size,
|
||||||
int64_t target,
|
uint64_t target,
|
||||||
uint8_t* initial_hash)
|
uint8_t* initial_hash)
|
||||||
: NanAsyncWorker(callback),
|
: NanAsyncWorker(callback),
|
||||||
pool_size(pool_size),
|
pool_size(pool_size),
|
||||||
|
@ -32,7 +34,7 @@ class PowWorker : public NanAsyncWorker {
|
||||||
// here, so everything we need for input and output
|
// here, so everything we need for input and output
|
||||||
// should go on `this`.
|
// should go on `this`.
|
||||||
void Execute () {
|
void Execute () {
|
||||||
error = pow(pool_size, target, initial_hash, &nonce);
|
error = pow(pool_size, target, initial_hash, MAX_SAFE_JS_INTEGER, &nonce);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Executed when the async work is complete
|
// Executed when the async work is complete
|
||||||
|
@ -50,10 +52,10 @@ class PowWorker : public NanAsyncWorker {
|
||||||
}
|
}
|
||||||
|
|
||||||
private:
|
private:
|
||||||
uint32_t pool_size;
|
size_t pool_size;
|
||||||
int64_t target;
|
uint64_t target;
|
||||||
uint8_t* initial_hash;
|
uint8_t* initial_hash;
|
||||||
int64_t nonce;
|
uint64_t nonce;
|
||||||
int error;
|
int error;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -61,8 +63,8 @@ NAN_METHOD(PowAsync) {
|
||||||
NanScope();
|
NanScope();
|
||||||
|
|
||||||
NanCallback *callback = new NanCallback(args[3].As<Function>());
|
NanCallback *callback = new NanCallback(args[3].As<Function>());
|
||||||
uint32_t pool_size = args[0]->Uint32Value();
|
size_t pool_size = args[0]->Uint32Value();
|
||||||
int64_t target = args[1]->IntegerValue();
|
uint64_t target = args[1]->IntegerValue();
|
||||||
size_t length = Buffer::Length(args[2]->ToObject());
|
size_t length = Buffer::Length(args[2]->ToObject());
|
||||||
char* buf = Buffer::Data(args[2]->ToObject());
|
char* buf = Buffer::Data(args[2]->ToObject());
|
||||||
uint8_t* initial_hash = (uint8_t *)malloc(length);
|
uint8_t* initial_hash = (uint8_t *)malloc(length);
|
||||||
|
|
Loading…
Reference in New Issue
Block a user