Fix bug where if block wasn't found, getBlockAsync would throw. Now it returns undefined

This commit is contained in:
Fabio Berger
2018-09-24 15:02:06 +01:00
parent 8bce407aec
commit d0448c2bbd
7 changed files with 78 additions and 31 deletions

View File

@@ -329,23 +329,28 @@ export class Web3Wrapper {
/**
* Fetch a specific Ethereum block without transaction data
* @param blockParam The block you wish to fetch (blockHash, blockNumber or blockLiteral)
* @returns The requested block without transaction data
* @returns The requested block without transaction data, or undefined if block was not found
*/
public async getBlockAsync(blockParam: string | BlockParam): Promise<BlockWithoutTransactionData> {
public async getBlockIfExistsAsync(
blockParam: string | BlockParam,
): Promise<BlockWithoutTransactionData | undefined> {
Web3Wrapper._assertBlockParamOrString(blockParam);
const encodedBlockParam = marshaller.marshalBlockParam(blockParam);
const method = utils.isHexStrict(blockParam) ? 'eth_getBlockByHash' : 'eth_getBlockByNumber';
const shouldIncludeTransactionData = false;
const blockWithoutTransactionDataWithHexValues = await this._sendRawPayloadAsync<
const blockWithoutTransactionDataWithHexValuesOrNull = await this._sendRawPayloadAsync<
BlockWithoutTransactionDataRPC
>({
method,
params: [encodedBlockParam, shouldIncludeTransactionData],
});
const blockWithoutTransactionData = marshaller.unmarshalIntoBlockWithoutTransactionData(
blockWithoutTransactionDataWithHexValues,
);
return blockWithoutTransactionData;
let blockWithoutTransactionDataIfExists;
if (!_.isNull(blockWithoutTransactionDataWithHexValuesOrNull)) {
blockWithoutTransactionDataIfExists = marshaller.unmarshalIntoBlockWithoutTransactionData(
blockWithoutTransactionDataWithHexValuesOrNull,
);
}
return blockWithoutTransactionDataIfExists;
}
/**
* Fetch a specific Ethereum block with transaction data
@@ -376,8 +381,11 @@ export class Web3Wrapper {
*/
public async getBlockTimestampAsync(blockParam: string | BlockParam): Promise<number> {
Web3Wrapper._assertBlockParamOrString(blockParam);
const { timestamp } = await this.getBlockAsync(blockParam);
return timestamp;
const blockIfExists = await this.getBlockIfExistsAsync(blockParam);
if (_.isUndefined(blockIfExists)) {
throw new Error(`Failed to fetch block with blockParam: ${JSON.stringify(blockParam)}`);
}
return blockIfExists.timestamp;
}
/**
* Retrieve the user addresses available through the backing provider

View File

@@ -85,28 +85,40 @@ describe('Web3Wrapper tests', () => {
expect(typeof blockNumber).to.be.equal('number');
});
});
describe('#getBlockAsync', () => {
describe('#getBlockIfExistsAsync', () => {
it('gets block when supplied a valid BlockParamLiteral value', async () => {
const blockParamLiteral = BlockParamLiteral.Earliest;
const block = await web3Wrapper.getBlockAsync(blockParamLiteral);
expect(block.number).to.be.equal(0);
expect(utils.isBigNumber(block.difficulty)).to.equal(true);
expect(_.isNumber(block.gasLimit)).to.equal(true);
const blockIfExists = await web3Wrapper.getBlockIfExistsAsync(blockParamLiteral);
if (_.isUndefined(blockIfExists)) {
throw new Error('Expected block to exist');
}
expect(blockIfExists.number).to.be.equal(0);
expect(utils.isBigNumber(blockIfExists.difficulty)).to.equal(true);
expect(_.isNumber(blockIfExists.gasLimit)).to.equal(true);
});
it('gets block when supplied a block number', async () => {
const blockParamLiteral = 0;
const block = await web3Wrapper.getBlockAsync(blockParamLiteral);
expect(block.number).to.be.equal(0);
const blockIfExists = await web3Wrapper.getBlockIfExistsAsync(blockParamLiteral);
if (_.isUndefined(blockIfExists)) {
throw new Error('Expected block to exist');
}
expect(blockIfExists.number).to.be.equal(0);
});
it('gets block when supplied a block hash', async () => {
const blockParamLiteral = 0;
const block = await web3Wrapper.getBlockAsync(blockParamLiteral);
const sameBlock = await web3Wrapper.getBlockAsync(block.hash as string);
expect(sameBlock.number).to.be.equal(0);
const blockIfExists = await web3Wrapper.getBlockIfExistsAsync(blockParamLiteral);
if (_.isUndefined(blockIfExists)) {
throw new Error('Expected block to exist');
}
const sameBlockIfExists = await web3Wrapper.getBlockIfExistsAsync(blockIfExists.hash as string);
if (_.isUndefined(sameBlockIfExists)) {
throw new Error('Expected block to exist');
}
expect(sameBlockIfExists.number).to.be.equal(0);
});
it('should throw if supplied invalid blockParam value', async () => {
const invalidBlockParam = 'deadbeef';
expect(web3Wrapper.getBlockAsync(invalidBlockParam)).to.eventually.to.be.rejected();
expect(web3Wrapper.getBlockIfExistsAsync(invalidBlockParam)).to.eventually.to.be.rejected();
});
});
describe('#getBlockWithTransactionDataAsync', () => {