diff --git a/integration/regtest.js b/integration/regtest.js index bf5c7ac5..0675bf52 100644 --- a/integration/regtest.js +++ b/integration/regtest.js @@ -231,6 +231,11 @@ describe('Daemon Binding Functionality', function() { blockIndex.height.should.equal(i + 1); }); }); + it('will get null prevHash for the genesis block', function() { + var blockIndex = bitcoind.getBlockIndex(0); + should.exist(blockIndex); + should.equal(blockIndex.prevHash, null); + }); }); describe('get block index by height', function() { diff --git a/src/libbitcoind.cc b/src/libbitcoind.cc index 0c3b8758..bb3ed015 100644 --- a/src/libbitcoind.cc +++ b/src/libbitcoind.cc @@ -1337,14 +1337,20 @@ NAN_METHOD(GetBlockIndex) { } } + Local obj = NanNew(); + arith_uint256 cw = blockIndex->nChainWork; CBlockIndex* prevBlockIndex = blockIndex->pprev; - const uint256* prevHash = prevBlockIndex->phashBlock; + if (&prevBlockIndex->phashBlock != 0) { + const uint256* prevHash = prevBlockIndex->phashBlock; + obj->Set(NanNew("prevHash"), NanNew(prevHash->GetHex())); + } else { + obj->Set(NanNew("prevHash"), NanNull()); + } - Local obj = NanNew(); obj->Set(NanNew("hash"), NanNew(blockIndex->phashBlock->GetHex())); obj->Set(NanNew("chainWork"), NanNew(cw.GetHex())); - obj->Set(NanNew("prevHash"), NanNew(prevHash->GetHex())); + obj->Set(NanNew("height"), NanNew(blockIndex->nHeight)); NanReturnValue(obj);