From 4df1fb06b11a1f3c1ac6577285f20cec75e02415 Mon Sep 17 00:00:00 2001 From: Amaury Martiny Date: Thu, 25 Oct 2018 15:44:59 +0200 Subject: [PATCH 1/2] Fix eth_getTransactionReceipt throwing error --- packages/light.js/package.json | 1 + packages/light.js/src/rpc/other/post.ts | 25 +++++++++++++++++++------ yarn.lock | 10 ++++++++++ 3 files changed, 30 insertions(+), 6 deletions(-) diff --git a/packages/light.js/package.json b/packages/light.js/package.json index 85603fc3..97e0a708 100644 --- a/packages/light.js/package.json +++ b/packages/light.js/package.json @@ -28,6 +28,7 @@ "dependencies": { "@parity/abi": "^2.1.4", "@parity/api": "^2.1.23", + "async-retry": "^1.2.3", "bignumber.js": "^7.2.1", "debug": "^3.1.0", "memoizee": "^0.4.12" diff --git a/packages/light.js/src/rpc/other/post.ts b/packages/light.js/src/rpc/other/post.ts index 6766037b..85c25f5e 100644 --- a/packages/light.js/src/rpc/other/post.ts +++ b/packages/light.js/src/rpc/other/post.ts @@ -3,6 +3,8 @@ // // SPDX-License-Identifier: MIT +import * as asyncRetry from 'async-retry'; +import * as debug from 'debug'; import { Observable, Observer } from 'rxjs'; import { createApiFromProvider, getApi } from '../../api'; @@ -44,13 +46,24 @@ export function post$(tx: Tx, options: PostOptions = {}) { observer.next({ signed: transactionHash, schedule: tx.condition }); } else { observer.next({ signed: transactionHash }); - const receipt = await api.pollMethod( - 'eth_getTransactionReceipt', - transactionHash, - ( - receipt: any // TODO Receipt use @parity/api type - ) => receipt && receipt.blockNumber && !receipt.blockNumber.eq(0) + + // We poll `eth_getTransactionReceipt` for 20s, until we get a valid receipt + const receipt = await asyncRetry( + async (_, attempt) => { + debug('@parity/light.js:api')( + `Attempt #${attempt} to eth_getTransactionReceipt.` + ); + const rcpt = await api.eth.getTransactionReceipt(transactionHash); + if (!rcpt || !rcpt.blockNumber || rcpt.blockNumber.eq(0)) { + throw new Error('Receipt is invalid.'); + } + return rcpt; + }, + { + retries: 20 + } ); + observer.next({ confirmed: receipt }); } diff --git a/yarn.lock b/yarn.lock index eca5c01b..f55227bf 100644 --- a/yarn.lock +++ b/yarn.lock @@ -379,6 +379,12 @@ async-retry@^1.2.1: dependencies: retry "0.10.1" +async-retry@^1.2.3: + version "1.2.3" + resolved "https://registry.yarnpkg.com/async-retry/-/async-retry-1.2.3.tgz#a6521f338358d322b1a0012b79030c6f411d1ce0" + dependencies: + retry "0.12.0" + async@^1.4.0, async@^1.5.0: version "1.5.2" resolved "https://registry.yarnpkg.com/async/-/async-1.5.2.tgz#ec6a61ae56480c0c3cb241c95618e20892f9672a" @@ -4379,6 +4385,10 @@ retry@0.10.1: version "0.10.1" resolved "https://registry.yarnpkg.com/retry/-/retry-0.10.1.tgz#e76388d217992c252750241d3d3956fed98d8ff4" +retry@0.12.0: + version "0.12.0" + resolved "https://registry.yarnpkg.com/retry/-/retry-0.12.0.tgz#1b42a6266a21f07421d1b0b54b7dc167b01c013b" + right-align@^0.1.1: version "0.1.3" resolved "https://registry.yarnpkg.com/right-align/-/right-align-0.1.3.tgz#61339b722fe6a3515689210d24e14c96148613ef" From 701075b0564d5a66109e85f40f308a1d70c6e2d8 Mon Sep 17 00:00:00 2001 From: Amaury Martiny Date: Thu, 25 Oct 2018 16:07:51 +0200 Subject: [PATCH 2/2] Fix debug message --- packages/light.js/src/rpc/other/post.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/light.js/src/rpc/other/post.ts b/packages/light.js/src/rpc/other/post.ts index 85c25f5e..777eecca 100644 --- a/packages/light.js/src/rpc/other/post.ts +++ b/packages/light.js/src/rpc/other/post.ts @@ -50,7 +50,7 @@ export function post$(tx: Tx, options: PostOptions = {}) { // We poll `eth_getTransactionReceipt` for 20s, until we get a valid receipt const receipt = await asyncRetry( async (_, attempt) => { - debug('@parity/light.js:api')( + debug('@parity/light.js:post$')( `Attempt #${attempt} to eth_getTransactionReceipt.` ); const rcpt = await api.eth.getTransactionReceipt(transactionHash);