forked from sickpig/bch-rpc-explorer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
bch-diff.cpp
34 lines (28 loc) · 786 Bytes
/
bch-diff.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
#include <cinttypes>
#include <cstdint>
#include <napi.h>
/**
* Calculate the difficulty for a given block index.
*/
static Napi::Number GetDifficulty(const Napi::CallbackInfo& info)
{
auto env = info.Env();
const auto nBits = info[0].As<Napi::Number>().Uint32Value();
int nShift = (nBits >> 24) & 0xff;
double dDiff = double(0x0000ffff) / double(nBits & 0x00ffffff);
while (nShift < 29) {
dDiff *= 256.0;
nShift++;
}
while (nShift > 29) {
dDiff /= 256.0;
nShift--;
}
return Napi::Number::New(env, dDiff);
}
static Napi::Object Init(Napi::Env env, Napi::Object exports)
{
exports["GetDifficulty"] = Napi::Function::New(env, GetDifficulty);
return exports;
}
NODE_API_MODULE(NODE_GYP_MODULE_NAME, Init)