Skip to content

Commit

Permalink
Optimize height queries
Browse files Browse the repository at this point in the history
  • Loading branch information
gorbak25 committed Oct 7, 2020
1 parent 83828dd commit f74ca4c
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 3 deletions.
9 changes: 8 additions & 1 deletion es/chain/node.js
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,14 @@ async function tx (hash, info = true) {
}

async function height () {
return (await this.api.getCurrentKeyBlockHeight()).height
try {
if (this.height_query_promise === undefined) {
this.height_query_promise = this.api.getCurrentKeyBlockHeight()
}
return (await this.height_query_promise).height
} finally {
this.height_query_promise = undefined
}
}

async function awaitHeight (height, { interval = 5000, attempts = 20 } = {}) {
Expand Down
26 changes: 24 additions & 2 deletions test/integration/chain.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,28 @@ describe('Node Chain', function () {
return client.height().should.eventually.be.a('number')
})

it('Compresses height queries', async () => {
const origFun = client.api.getCurrentKeyBlockHeight
try {
var calls = 0
client.api.getCurrentKeyBlockHeight = () => {
calls += 1
return origFun()
}
const H1P = client.height()
const H2P = client.height()
const H3P = client.height()
const H1 = await H1P
const H2 = await H2P
const H3 = await H3P
H1.should.be.equal(H2)
H1.should.be.equal(H3)
calls.should.be.equal(1)
} finally {
client.api.getCurrentKeyBlockHeight = origFun
}
})

it('waits for specified heights', async () => {
const target = await client.height() + 1
await client.awaitHeight(target, { interval: 200, attempts: 100 }).should.eventually.be.at.least(target)
Expand Down Expand Up @@ -92,12 +114,12 @@ describe('Node Chain', function () {
})

it('Wait for transaction confirmation', async () => {
const txData = await client.spend(1000, await client.address(), { confirm: true, interval: 200, attempts: 100 })
const txData = await client.spend(1000, await client.address(), { confirm: true, interval: 400, attempts: 50 })
const isConfirmed = (await client.height()) >= txData.blockHeight + 3

isConfirmed.should.be.equal(true)

const txData2 = await client.spend(1000, await client.address(), { confirm: 4, interval: 200, attempts: 100 })
const txData2 = await client.spend(1000, await client.address(), { confirm: 4, interval: 400, attempts: 50 })
const isConfirmed2 = (await client.height()) >= txData2.blockHeight + 4
isConfirmed2.should.be.equal(true)
})
Expand Down

0 comments on commit f74ca4c

Please sign in to comment.