Merge pull request #235 from braydonf/bug/nullprevhash

GetBlockIndex segmentation fault with genesis block
This commit is contained in:
Chris Kleeschulte 2015-09-16 10:31:01 -04:00
commit a0d164661a
2 changed files with 14 additions and 3 deletions

View File

@ -231,6 +231,11 @@ describe('Daemon Binding Functionality', function() {
blockIndex.height.should.equal(i + 1); 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() { describe('get block index by height', function() {

View File

@ -1337,14 +1337,20 @@ NAN_METHOD(GetBlockIndex) {
} }
} }
Local<Object> obj = NanNew<Object>();
arith_uint256 cw = blockIndex->nChainWork; arith_uint256 cw = blockIndex->nChainWork;
CBlockIndex* prevBlockIndex = blockIndex->pprev; CBlockIndex* prevBlockIndex = blockIndex->pprev;
const uint256* prevHash = prevBlockIndex->phashBlock; if (&prevBlockIndex->phashBlock != 0) {
const uint256* prevHash = prevBlockIndex->phashBlock;
obj->Set(NanNew<String>("prevHash"), NanNew<String>(prevHash->GetHex()));
} else {
obj->Set(NanNew<String>("prevHash"), NanNull());
}
Local<Object> obj = NanNew<Object>();
obj->Set(NanNew<String>("hash"), NanNew<String>(blockIndex->phashBlock->GetHex())); obj->Set(NanNew<String>("hash"), NanNew<String>(blockIndex->phashBlock->GetHex()));
obj->Set(NanNew<String>("chainWork"), NanNew<String>(cw.GetHex())); obj->Set(NanNew<String>("chainWork"), NanNew<String>(cw.GetHex()));
obj->Set(NanNew<String>("prevHash"), NanNew<String>(prevHash->GetHex()));
obj->Set(NanNew<String>("height"), NanNew<Number>(blockIndex->nHeight)); obj->Set(NanNew<String>("height"), NanNew<Number>(blockIndex->nHeight));
NanReturnValue(obj); NanReturnValue(obj);