Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cache all api.query queries #22

Merged
merged 5 commits into from
Aug 13, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion project.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ network:
dataSources:
- name: main
kind: substrate/Runtime
startBlock: 8647000
startBlock: 0

# polkadot test slash bloks: 3570179

Expand Down
11 changes: 5 additions & 6 deletions src/mappings/NewEra.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,18 @@
import {SubstrateEvent} from "@subql/types";
import {eventId} from "./common";
import {cachedCurrentEra, cachedEraStakers, eventId} from "./common";
import { EraValidatorInfo } from "../types/models/EraValidatorInfo";

export async function handleNewEra(event: SubstrateEvent): Promise<void> {
let eraOption = await api.query.staking.currentEra()
let eraIndex = eraOption.unwrap()
let currentEra = await cachedCurrentEra(event.block)

const exposures = await api.query.staking.erasStakers.entries(eraIndex);
const exposures = await cachedEraStakers(currentEra.toNumber());

let eraValidatorInfos = exposures.map(([key, exposure]) => {
const [, validatorId] = key.args

let validatorIdString = validatorId.toString()
const eraValidatorInfo = new EraValidatorInfo(eventId(event)+validatorIdString)
eraValidatorInfo.era = eraIndex.toNumber()
eraValidatorInfo.era = currentEra.toNumber()
eraValidatorInfo.address = validatorIdString
eraValidatorInfo.total = exposure.total.toString()
eraValidatorInfo.own = exposure.own.toString()
Expand All @@ -27,4 +26,4 @@ export async function handleNewEra(event: SubstrateEvent): Promise<void> {
})

await Promise.allSettled(eraValidatorInfos)
}
}
8 changes: 5 additions & 3 deletions src/mappings/Rewards.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@ import {
timestamp,
eventId,
isProxy,
callFromProxy
callFromProxy,
cachedCurrentEra,
cachedEraStakers
} from "./common";
import {CallBase} from "@polkadot/types/types/calls";
import {AnyTuple} from "@polkadot/types/types/codec";
Expand Down Expand Up @@ -147,12 +149,12 @@ async function handleSlashForTxHistory(slashEvent: SubstrateEvent): Promise<void
return;
}

const currentEra = (await api.query.staking.currentEra()).unwrap();
const currentEra = await cachedCurrentEra(slashEvent.block);
const slashDefferDuration = api.consts.staking.slashDeferDuration

const slashEra = currentEra.toNumber() - slashDefferDuration.toNumber()

const eraStakersInSlashEra = await api.query.staking.erasStakers.entries(slashEra);
const eraStakersInSlashEra = await cachedEraStakers(slashEra);
const validatorsInSlashEra = eraStakersInSlashEra.map(([key, exposure]) => {
let [, validatorId] = key.args

Expand Down
44 changes: 42 additions & 2 deletions src/mappings/StakeChanged.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import {SubstrateEvent} from "@subql/types";
import {AccumulatedStake, StakeChange} from "../types";
import {eventId, timestamp} from "./common";
import {blockNumber, eventId, timestamp} from "./common";
import {Balance} from "@polkadot/types/interfaces";
import {RewardDestination} from "@polkadot/types/interfaces/staking";

Expand Down Expand Up @@ -55,11 +55,51 @@ export async function handleSlashForAnalytics(event: SubstrateEvent): Promise<vo
await element.save()
}

let rewardDestinationByAddress: {[blockId: string]: {[address: string]: RewardDestination}} = {}

async function cachedRewardDestination(accountAddress: string, event: SubstrateEvent): Promise<RewardDestination> {
const blockId = blockNumber(event)
let cachedBlock = rewardDestinationByAddress[blockId]

if (cachedBlock !== undefined) {
return cachedBlock[accountAddress]
} else {
const allAccountsInBlock = event.block.events
.filter(event => {
return event.event.method == "Reward" && event.event.section == "staking"
})
.map(event => {
let {event: {data: [accountId, ]}} = event
return accountId
});

const payees = await api.query.staking.payee.multi(allAccountsInBlock);
const rewardDestinations = payees.map(payee => { return payee as RewardDestination });

let destinationByAddress: {[address: string]: RewardDestination} = {}

// something went wrong, so just query for single accountAddress
if (rewardDestinations.length !== allAccountsInBlock.length) {
const payee = await api.query.staking.payee(accountAddress);
destinationByAddress[accountAddress] = payee;
rewardDestinationByAddress[blockId] = destinationByAddress
return payee
}
allAccountsInBlock.forEach((account, index) => {
let accountAddress = account.toString()
let rewardDestination = rewardDestinations[index]
destinationByAddress[accountAddress] = rewardDestination
})
rewardDestinationByAddress[blockId] = destinationByAddress
return destinationByAddress[accountAddress]
}
}

export async function handleRewardRestakeForAnalytics(event: SubstrateEvent): Promise<void> {
let {event: {data: [accountId, amount]}} = event
let accountAddress = accountId.toString()

const payee: RewardDestination = await api.query.staking.payee(accountAddress);
const payee = await cachedRewardDestination(accountAddress, event)
if (payee.isStaked) {
let amountBalance = (amount as Balance).toBigInt()
let accumulatedAmount = await handleAccumulatedStake(accountAddress, amountBalance)
Expand Down
32 changes: 32 additions & 0 deletions src/mappings/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ import {Balance} from "@polkadot/types/interfaces";
import {CallBase} from "@polkadot/types/types/calls";
import {AnyTuple} from "@polkadot/types/types/codec";
import { Vec } from '@polkadot/types';
import {EraIndex} from "@polkadot/types/interfaces/staking"
import { StorageKey } from "@polkadot/types";
import { AccountId } from "@polkadot/types/interfaces";
import { Exposure } from "@polkadot/types/interfaces";

const batchCalls = ["batch", "batchAll"]
const transferCalls = ["transfer", "transferKeepAlive"]
Expand Down Expand Up @@ -79,3 +83,31 @@ export function exportFeeFromDepositEventAsString(extrinsic?: SubstrateExtrinsic
return "0"
}
}

let currentEraByBlockId: {[blockId: string]: EraIndex} = {}

export async function cachedCurrentEra(block: SubstrateBlock): Promise<EraIndex> {
let key = block.block.header.number.toString()
let cachedValue = currentEraByBlockId[key]
if (cachedValue !== undefined) {
return cachedValue
} else {
let eraOption = await api.query.staking.currentEra()
let eraIndex = eraOption.unwrap()
currentEraByBlockId[key] = eraIndex
return eraIndex
}
}

let eraStakersByEra: {[era: number]: [StorageKey<[EraIndex, AccountId]>, Exposure][]} = {}

export async function cachedEraStakers(era: number): Promise<[StorageKey<[EraIndex, AccountId]>, Exposure][]> {
let cachedValue = eraStakersByEra[era]
if (cachedValue !== undefined) {
return cachedValue
} else {
let eraStakers = await api.query.staking.erasStakers.entries(era);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we query and save this guys also when election comlpetes. So one improvement in future is to reuse that data.

eraStakersByEra[era] = eraStakers
return eraStakers
}
}