From a2283ae0cc983793917dd0cfd74aa5d4b4007bc8 Mon Sep 17 00:00:00 2001 From: Christopher Jeffrey Date: Tue, 28 Oct 2014 13:27:04 -0700 Subject: [PATCH] massively improve GetProgress() method. --- src/bitcoindjs.cc | 70 +++++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 65 insertions(+), 5 deletions(-) diff --git a/src/bitcoindjs.cc b/src/bitcoindjs.cc index 64469e9f..7fc0c386 100644 --- a/src/bitcoindjs.cc +++ b/src/bitcoindjs.cc @@ -227,6 +227,9 @@ async_get_block(uv_work_t *req); static void async_get_block_after(uv_work_t *req); +static void +get_genesis_block(CBlock *genesis); + static void async_get_progress_after(uv_work_t *req); @@ -1435,6 +1438,40 @@ NAN_METHOD(GetProgress) { NanReturnValue(Undefined()); } +// Luckily, we never have to change this. +static void +get_genesis_block(CBlock *genesis) { + // Satoshi's first coinbase: + const char* pszTimestamp = + "The Times 03/Jan/2009 Chancellor on brink of second bailout for banks"; + CMutableTransaction txNew; + txNew.vin.resize(1); + txNew.vout.resize(1); + txNew.vin[0].scriptSig = CScript() + << 486604799 + << CScriptNum(4) + << vector((const unsigned char*)pszTimestamp, + (const unsigned char*)pszTimestamp + strlen(pszTimestamp)); + txNew.vout[0].nValue = 50 * COIN; + txNew.vout[0].scriptPubKey = CScript() << ParseHex( + "04678afdb0fe5548271967f1a67130b7105cd6a828e03909a67962e0ea1f61deb649f6" + "bc3f4cef38c4f35504e51ec112de5c384df7ba0b8d578a4c702b6bf11d5f" + ) << OP_CHECKSIG; + genesis->vtx.push_back(txNew); + genesis->hashPrevBlock = 0; + genesis->hashMerkleRoot = genesis->BuildMerkleTree(); + genesis->nVersion = 1; + genesis->nTime = 1231006505; + genesis->nBits = 0x1d00ffff; + genesis->nNonce = 2083236893; + const uint256& hashGenesisBlock = genesis->GetHash(); + hashGenesisBlock = genesis->GetHash(); + assert(hashGenesisBlock == uint256( + "0x000000000019d6689c085ae165831e934ff763ae46a2a6c172b3f1b60a8ce26f")); + assert(genesis->hashMerkleRoot == uint256( + "0x4a5e1e4baab89f3a32518a88c31bc87f618f76673e2cc77ab2127b7afdeda33b")); +} + static void async_get_progress_after(uv_work_t *req) { NanScope(); @@ -1451,21 +1488,44 @@ async_get_progress_after(uv_work_t *req) { } } else { const CBlock& cblock = data->result_block; + CBlockIndex* cblock_index = data->result_blockindex; + + Local jsblock = NanNew(); + cblock_to_jsblock(cblock, cblock_index, jsblock, false); + + CBlock cgenesis; + get_genesis_block(&cgenesis); + + Local genesis = NanNew(); + cblock_to_jsblock(cgenesis, NULL, genesis, false); uint32_t ts_ = cblock.GetBlockTime(); time_t now_ = time(NULL); uint64_t ts = (uint64_t)ts_; - // Assume last block was ten minutes ago: - uint64_t now = ((uint64_t)now_ * 1000) - (10 * (60 * 1000)); - uint64_t diff = now - ts; - unsigned int perc = 100 - (diff / now * 100); + // Assume last block was ten minutes ago: + uint64_t now = (((uint64_t)now_ * 1000) - (10 * (60 * 1000))) / 1000; + uint64_t left = (now - ts) / 1000; + + unsigned int hours_behind = left / 60 / 60; + unsigned int days_behind = left / 60 / 60 / 24; + unsigned int percent = 100 - (left / now * 100); + + Local result = NanNew(); + + result->Set(NanNew("blocks"), NanNew(cblock_index->nHeight)); + result->Set(NanNew("connections"), NanNew((int)vNodes.size())->ToInt32()); + result->Set(NanNew("genesisBlock"), genesis); + result->Set(NanNew("currentBlock"), jsblock); + result->Set(NanNew("hoursBehind"), NanNew(hours_behind)); + result->Set(NanNew("daysBehind"), NanNew(days_behind)); + result->Set(NanNew("percent"), NanNew(percent)); const unsigned argc = 2; Local argv[argc] = { Local::New(Null()), - Local::New(NanNew(perc)) + Local::New(result) }; TryCatch try_catch; data->callback->Call(Context::GetCurrent()->Global(), argc, argv);