Skip to content

Commit

Permalink
Merge pull request #1375 from aeternity/feature/check-tx-pool
Browse files Browse the repository at this point in the history
feat(poll): use getCheckTxInPool if available
  • Loading branch information
davidyuk authored Jan 24, 2022
2 parents 6ce7542 + 690db5b commit 17e6f36
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 10 deletions.
7 changes: 4 additions & 3 deletions src/chain/node.js
Original file line number Diff line number Diff line change
Expand Up @@ -161,13 +161,14 @@ async function poll (
) {
const max = await this.height() + blocks
do {
const tx = await this.tx(th).catch(_ => null)
if (tx && (tx.blockHeight !== -1 || (allowUnsynced && tx.height))) {
const tx = await this.tx(th)
if (tx.blockHeight !== -1 || (allowUnsynced && tx.height)) {
return tx
}
await pause(interval)
} while (await this.height() < max)
throw new TxTimedOutError(blocks, th)
const status = this.api.getCheckTxInPool && (await this.api.getCheckTxInPool(th)).status
throw new TxTimedOutError(blocks, th, status)
}

async function getTxInfo (hash) {
Expand Down
2 changes: 1 addition & 1 deletion src/node.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ const Node = AsyncInit.compose({
async init ({ url, internalUrl, ignoreVersion }) {
if (!url) throw new MissingParamError('"url" required')
this.url = url.replace(/\/$/, '')
this.internalUrl = internalUrl ? internalUrl.replace(/\/$/, '') : this.url
this.internalUrl = internalUrl?.replace(/\/$/, '')
const client = await genSwaggerClient(`${this.url}/api?oas3`, {
internalUrl: this.internalUrl,
responseInterceptor: response => {
Expand Down
8 changes: 6 additions & 2 deletions src/utils/errors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -161,8 +161,12 @@ export class RequestTimedOutError extends AeError {
}

export class TxTimedOutError extends AeError {
constructor (blocks: number, th: string) {
super(`Giving up after ${blocks} blocks mined, transaction hash: ${th}`)
constructor (blocks: number, th: string, status?: string) {
super([
`Giving up after ${blocks} blocks mined`,
`transaction hash: ${th}`,
...status ? [`status: ${status}`] : []
].join(', '))
this.name = 'TxTimedOutError'
}
}
Expand Down
7 changes: 4 additions & 3 deletions src/utils/swagger.js
Original file line number Diff line number Diff line change
Expand Up @@ -78,8 +78,9 @@ export default async (
})
}))

const combinedApi = [
...Object.values(external.apis),
const combinedApi = Object.assign(
{},
...external.apis.external ? [external.apis.external] : Object.values(external.apis),
mapObject(internal?.apis.internal || {}, ([key, handler]) => [key, (...args) => {
if (!warnedAboutInternalApiUsage) {
console.warn(
Expand All @@ -91,7 +92,7 @@ export default async (
}
return handler(...args)
}])
].reduce((acc, n) => ({ ...acc, ...n }))
)

const opSpecs = Object.values(spec.paths)
.map(paths => Object.values(paths))
Expand Down
7 changes: 6 additions & 1 deletion test/integration/node.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,16 @@ describe('Node client', function () {
expect(node.revision).to.be.a('string')
})

it('loads operations', () => {
it('wraps endpoints', () => {
['postTransaction', 'getCurrentKeyBlock']
.map(method => expect(node.api[method]).to.be.a('function'))
})

it('don\'t wraps internal endpoints if internalUrl is not provided', async () => {
const node = await Node({ url, ignoreVersion })
expect(node.api.getPendingTransactions).to.be.an('undefined')
})

it('gets key blocks by height for the first 3 blocks', async () => {
expect(node.api.getKeyBlockByHeight).to.be.a('function')
const blocks = await Promise.all([1, 2, 3].map(i => node.api.getKeyBlockByHeight(i)))
Expand Down

0 comments on commit 17e6f36

Please sign in to comment.