Skip to content

Commit

Permalink
limit txs pages load by two pages per request (#314)
Browse files Browse the repository at this point in the history
* limit txs pages load by two pages per request

* delete block from loadpaginatedtxs

* small fixes

* Update lib/source/cosmosV2-source.js

* linted

Co-authored-by: Ana G. <[email protected]>
Co-authored-by: Fabian <[email protected]>
  • Loading branch information
3 people authored Feb 26, 2020
1 parent 20f7d60 commit dc25a73
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 5 deletions.
9 changes: 9 additions & 0 deletions lib/source/cosmosV0-source.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,15 @@ class CosmosV0API extends RESTDataSource {
this.reducers = require('../reducers/cosmosV0-reducers')
}

// hacky way to get error text
async getError(url) {
try {
return await this.get(url)
} catch (error) {
return error.extensions.response.body.error
}
}

async getRetry(url, intent = 0) {
// check cache size, and flush it if it's bigger than something
if ((await this.cache.getTotalSize()) > 100000) {
Expand Down
38 changes: 33 additions & 5 deletions lib/source/cosmosV2-source.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
const CosmosV0API = require('./cosmosV0-source')
const PAGE_RECORDS_COUNT = 100

class CosmosV2API extends CosmosV0API {
setReducers() {
Expand All @@ -16,28 +17,55 @@ class CosmosV2API extends CosmosV0API {
}

async loadPaginatedTxs(url, page = 1, totalAmount = 0) {
const pagination = `&limit=1000000000&page=${page}`
if (page < 1) {
return []
}
const pagination = `&limit=${PAGE_RECORDS_COUNT}&page=${page}`
let allTxs = []

const { txs, total_count } = await this.getRetry(`${url}${pagination}`)
allTxs = allTxs.concat(txs)

// limitation by totalAmount or by page limit
if (totalAmount !== 0 || page === 1) {
return allTxs
}
// there is a bug in page_number in gaia-13007 so we can't use is
if (allTxs.length + totalAmount < Number(total_count)) {
return allTxs.concat(
await this.loadPaginatedTxs(url, page + 1, totalAmount + allTxs.length)
await this.loadPaginatedTxs(url, page - 1, totalAmount + allTxs.length)
)
}

return allTxs
}

async getPageCount(url) {
// hacky way to get page count — requesting too many pages and fetching the error
let error = await this.getError(
url + `&limit=${PAGE_RECORDS_COUNT}&page=10000000000000`
)
/* The error message received looks like this:
* {
* error: "TxSearch: response error: RPC error -32603 - Internal error: page should be within [0, 7] range, given 100"
* }
* We are therefor looking for [0, 7]. The latter of the 2 array entries gives us the last page.
*/
const pages = error.match(/\[[0-9]*, ([0-9]*)\]/)
return pages[1]
}

async getTransactions(address) {
this.checkAddress(address)

// getting page count
const senderPage = await this.getPageCount(`/txs?message.sender=${address}`)
const recipientPage = await this.getPageCount(
`/txs?transfer.recipient=${address}`
)

const txs = await Promise.all([
this.loadPaginatedTxs(`/txs?message.sender=${address}`),
this.loadPaginatedTxs(`/txs?transfer.recipient=${address}`)
this.loadPaginatedTxs(`/txs?message.sender=${address}`, senderPage),
this.loadPaginatedTxs(`/txs?transfer.recipient=${address}`, recipientPage)
]).then(([senderTxs, recipientTxs]) => [].concat(senderTxs, recipientTxs))

return this.reducers.formatTransactionsReducer(txs, this.reducers)
Expand Down

0 comments on commit dc25a73

Please sign in to comment.