Skip to content

Commit

Permalink
Fix: not throw error if block for a block number is not found (#89)
Browse files Browse the repository at this point in the history
* Fix: not throw error if block for a block number is not found

* Fix: not throw error if block for a hash is not found
  • Loading branch information
jpuri authored Feb 16, 2023
1 parent 6324b20 commit 9f45aa7
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 6 deletions.
5 changes: 3 additions & 2 deletions getBlocksForRange.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,10 @@ async function getBlocksForRange({ provider, fromBlock, toBlock }) {
const missingBlockNumbers = Array(blockCountToQuery).fill()
.map((_,index) => fromBlockNumber + index)
.map(intToHex)
const blockBodies = await Promise.all(
let blockBodies = await Promise.all(
missingBlockNumbers.map(blockNum => query(provider, 'eth_getBlockByNumber', [blockNum, false]))
)
blockBodies = blockBodies.filter(block => block !== null);
return blockBodies
}

Expand Down Expand Up @@ -64,5 +65,5 @@ async function query(provider, method, params) {
);
}
}
throw new Error(`Block not found for params: ${JSON.stringify(params)}`);
return null;
}
8 changes: 4 additions & 4 deletions test/getBlocksForRange.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ test("return block number if there is no error", async (t) => {
t.end();
});

test("looks for a block number 3 times until throwing", async (t) => {
test("not throw error even if it is not found and filter out null", async (t) => {
const sendAsync = sinon
.stub()
.withArgs({
Expand All @@ -45,10 +45,10 @@ test("looks for a block number 3 times until throwing", async (t) => {
.callsArgWith(1, null, { result: null });
const provider = { sendAsync };
try {
await getBlocksForRange({ provider, fromBlock: "0x1", toBlock: "0x1" });
t.fail("Promise resolved when it was not supposed to");
const result = await getBlocksForRange({ provider, fromBlock: "0x1", toBlock: "0x1" });
t.deepEquals(result, []);
} catch (error) {
t.equal(error.message, 'Block not found for params: ["0x1",false]');
t.fail("Should not throw error if result is obtained");
}
t.end();
});

0 comments on commit 9f45aa7

Please sign in to comment.