Skip to content

Commit

Permalink
Fabo/wait for data ready (#497)
Browse files Browse the repository at this point in the history
* allow to wait for data to be ready

(cherry picked from commit 744d9b3b445286a9637fdadbeb5c473165cec58e)

* wait for data to be ready

(cherry picked from commit b6d665fd88ae22e5e3753249f27730eb12faebcb)

* keep store in cosmos source

(cherry picked from commit a6bb9f023eb56fb57d9a0f3a3ae12c997f6d04e3)

* Update lib/resolvers.js
  • Loading branch information
faboweb authored Mar 23, 2020
1 parent cf30587 commit 3bfb9d6
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 13 deletions.
15 changes: 8 additions & 7 deletions lib/block-store.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,18 @@
class BlockStore {
constructor(network, readyCallback) {
constructor(network) {
this.network = network
this.latestHeight = 0
this.block = {}
this.stakingDenom = ''
this.annualProvision = 0
this.signedBlocksWindow = 0
this.validators = {}
this.ready = false
this.readyCallback = readyCallback

// system to stop queries to proceed if store data is not yet available
this.resolveReady
this.dataReady = new Promise(resolve => {
this.resolveReady = resolve
})
}

update({
Expand All @@ -26,10 +30,7 @@ class BlockStore {
this.signedBlocksWindow = Number(signedBlocksWindow)
this.validators = validators

if (!this.ready && this.readyCallback && this.latestHeight !== 0) {
this.readyCallback(this.latestHeight !== 0)
this.ready = true
}
this.resolveReady()
}
}

Expand Down
19 changes: 14 additions & 5 deletions lib/resolvers.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ async function validators(
{ networkId, searchTerm, activeOnly },
{ dataSources }
) {
await localStore(dataSources, networkId).dataReady
let validators = Object.values(localStore(dataSources, networkId).validators)
if (activeOnly) {
validators = validators.filter(({ status }) => status === 'ACTIVE')
Expand All @@ -85,6 +86,7 @@ async function validators(
}

async function validator(_, { networkId, operatorAddress }, { dataSources }) {
await localStore(dataSources, networkId).dataReady
const validatorInfo = await createDBInstance(
networkId
).getValidatorInfoByAddress(operatorAddress)
Expand All @@ -110,11 +112,12 @@ async function validator(_, { networkId, operatorAddress }, { dataSources }) {
}
}

function delegation(
async function delegation(
_,
{ networkId, delegatorAddress, operatorAddress },
{ dataSources }
) {
await localStore(dataSources, networkId).dataReady
const validator = localStore(dataSources, networkId).validators[
operatorAddress
]
Expand All @@ -129,6 +132,7 @@ async function delegations(
{ networkId, delegatorAddress },
{ dataSources }
) {
await localStore(dataSources, networkId).dataReady
const validatorsDictionary = localStore(dataSources, networkId).validators
const delegations = await remoteFetch(
dataSources,
Expand All @@ -152,6 +156,7 @@ async function undelegations(
{ networkId, delegatorAddress },
{ dataSources }
) {
await localStore(dataSources, networkId).dataReady
const validatorsDictionary = localStore(dataSources, networkId).validators
const undelegations = await remoteFetch(
dataSources,
Expand Down Expand Up @@ -179,7 +184,6 @@ const resolvers = {
},
Proposal: {
validator: (proposal, _, { dataSources }) => {
//
// Proposer value can be `unknown` (if proposal was issued in a previous chain),
// or standard address (i.e: cosmos19wlk8gkfjckqr8d73dyp4n0f0k89q4h7xr3uwj).
//
Expand Down Expand Up @@ -276,16 +280,19 @@ const resolvers = {
_,
{ networkId, address, fiatCurrency },
{ dataSources }
) =>
remoteFetch(dataSources, networkId).getBalancesFromAddress(
) => {
await localStore(dataSources, networkId).dataReady
return remoteFetch(dataSources, networkId).getBalancesFromAddress(
address,
fiatCurrency
),
)
},
balance: async (
_,
{ networkId, address, denom, fiatCurrency },
{ dataSources }
) => {
await localStore(dataSources, networkId).dataReady
const balances = await remoteFetch(
dataSources,
networkId
Expand All @@ -305,6 +312,7 @@ const resolvers = {
{ networkId, delegatorAddress, operatorAddress, fiatCurrency },
{ dataSources }
) => {
await localStore(dataSources, networkId).dataReady
const validatorsDictionary = localStore(dataSources, networkId).validators
let rewards = await remoteFetch(dataSources, networkId).getRewards(
delegatorAddress,
Expand Down Expand Up @@ -333,6 +341,7 @@ const resolvers = {
{ networkId, address, fiatCurrency },
{ dataSources, fingerprint }
) => {
await localStore(dataSources, networkId).dataReady
const validatorsDictionary = localStore(dataSources, networkId).validators
const overview = await remoteFetch(dataSources, networkId).getOverview(
address,
Expand Down
3 changes: 2 additions & 1 deletion lib/source/cosmosV0-source.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ const gasPrices = [
]

class CosmosV0API extends RESTDataSource {
constructor(network) {
constructor(network, store) {
super()
this.baseURL = network.api_url
this.initialize({})
Expand All @@ -26,6 +26,7 @@ class CosmosV0API extends RESTDataSource {
this.delegatorBech32Prefix = network.address_prefix
this.validatorConsensusBech32Prefix = `${network.address_prefix}valcons`
this.gasPrices = gasPrices
this.store = store

this.setReducers()
}
Expand Down

0 comments on commit 3bfb9d6

Please sign in to comment.