diff --git a/contracts/FullCheckpoint.sol b/contracts/FullCheckpoint.sol index eb0b4d5..2571b1a 100644 --- a/contracts/FullCheckpoint.sol +++ b/contracts/FullCheckpoint.sol @@ -360,6 +360,17 @@ contract FullCheckpoint { function getHeader( bytes32 blockHash ) public view returns (HeaderInfo memory) { + if (headerTree[blockHash].mix == 0) { + return ( + HeaderInfo({ + parentHash: 0, + number: 0, + roundNum: 0, + mainnetNum: -1, + finalized: false + }) + ); + } bool finalized = false; if (int64(uint64(headerTree[blockHash].mix)) != -1) { finalized = true; diff --git a/contracts/LiteCheckpoint.sol b/contracts/LiteCheckpoint.sol index 031a163..0bdecbe 100644 --- a/contracts/LiteCheckpoint.sol +++ b/contracts/LiteCheckpoint.sol @@ -350,6 +350,18 @@ contract LiteCheckpoint { function getHeader( bytes32 blockHash ) public view returns (HeaderInfo memory) { + if (headerTree[blockHash] == 0) { + return ( + HeaderInfo({ + parentHash: 0, + number: 0, + roundNum: 0, + mainnetNum: -1, + finalized: false + }) + ); + } + bool finalized = false; if (int64(uint64(headerTree[blockHash])) != -1) { finalized = true; diff --git a/test/FullCheckpoint.js b/test/FullCheckpoint.js index 85ed47b..d991070 100644 --- a/test/FullCheckpoint.js +++ b/test/FullCheckpoint.js @@ -92,6 +92,14 @@ describe("checkpoint", () => { }); describe("test checkpoint real block data", () => { + it("should check defalut value", async () => { + const nonExistentBlock = await checkpoint.getHeader( + "0x3a9114857792f2a10b4d04ded4e29cb2371535ed749a7686aa2e9885c6007e25" + ); + + expect(nonExistentBlock.mainnetNum).to.eq(-1); + expect(nonExistentBlock.finalized).to.eq(false); + }); it("should verify roots", async () => { const genesisHash = blockToHash(genesis); const roots = await checkpoint.getRoots(genesisHash); diff --git a/test/LiteCheckpoint.js b/test/LiteCheckpoint.js index d01f25a..ece066e 100644 --- a/test/LiteCheckpoint.js +++ b/test/LiteCheckpoint.js @@ -89,6 +89,14 @@ describe("lite checkpoint", () => { await loadFixture(fixture)); }); describe("test lite checkpoint real block data", () => { + it("should check defalut value", async () => { + const nonExistentBlock = await liteCheckpoint.getHeader( + "0x3a9114857792f2a10b4d04ded4e29cb2371535ed749a7686aa2e9885c6007e25" + ); + + expect(nonExistentBlock.mainnetNum).to.eq(-1); + expect(nonExistentBlock.finalized).to.eq(false); + }); it("should receive a new header which has only the next and uncommitted", async () => { await liteCheckpoint.receiveHeader([block451Encoded]); const block2Hash = blockToHash(block451Encoded);