Skip to content

Commit

Permalink
Merge pull request #814 from autonomys/feat/stakingSquidV14
Browse files Browse the repository at this point in the history
Staking squid v14
  • Loading branch information
marc-aurele-besner authored Aug 16, 2024
2 parents 70f309e + 40eb3da commit 907db36
Show file tree
Hide file tree
Showing 9 changed files with 149 additions and 34 deletions.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

18 changes: 17 additions & 1 deletion indexers/staking-squid/schema.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -99,9 +99,23 @@ type Operator @entity {
updatedAt: Int! @index
}

type DomainBlock @entity {
id: ID! @index
domainId: String! @index
blockNumber: Int! @index
blockHash: String! @index
extrinsicRoot: String!
consensusBlockNumber: Int! @index
consensusBlockHash: String! @index
timestamp: DateTime! @index
createdAt: Int! @index
updatedAt: Int! @index
}

type Bundle @entity {
id: ID! @index
domainId: String! @index
domainBlockId: String! @index
domainBlockNumber: Int! @index
domainBlockHash: String!
domainBlockExtrinsicRoot: String!
Expand All @@ -123,9 +137,11 @@ type Bundle @entity {

type BundleAuthor @entity {
id: ID! @index
bundleId: String! @index
domainId: String! @index
accountId: String! @index
operatorId: String! @index
bundleId: String! @index
domainBlockId: String! @index
}

enum NominatorStatus {
Expand Down
2 changes: 1 addition & 1 deletion indexers/staking-squid/squid.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
manifestVersion: subsquid.io/v0.1
name: staking-squid
version: 12
version: 14
description: Autonomys - Astral - Staking Indexer
build:
deploy:
Expand Down
76 changes: 53 additions & 23 deletions indexers/staking-squid/src/events/bundle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,10 @@ import {
getOrCreateDomain,
getOrCreateOperator,
} from "../storage";
import { createDomainBlock } from "../storage/domainBlock";
import { ExecutionReceipt, SealedBundleHeader } from "../types/v1";
import { bundleUID, getBlockNumber } from "../utils";
import { Cache } from "../utils/cache";
import { blockUID, bundleUID, getBlockNumber } from "../utils";
import { Cache, LastBlockBundleIndexKey } from "../utils/cache";

export function processBundleStoredEvent(
cache: Cache,
Expand Down Expand Up @@ -66,8 +67,7 @@ export function processBundleStoredEvent(
blockFees,
} = receipt;

const keyIdLastBlockBundleIndex =
"lastBlockBundleIndex:" + domainId + "-" + domainBlockHash;
const keyIdLastBlockBundleIndex: LastBlockBundleIndexKey = `lastBlockBundleIndex:${domainId}-${domainBlockHash}`;
const lastBlockBundleIndex = cache.internalKeyStore.get(
keyIdLastBlockBundleIndex
);
Expand All @@ -81,30 +81,60 @@ export function processBundleStoredEvent(
let bundle = cache.bundles.get(
bundleUID(domainId, domainBlockHash, blockBundleIndex)
);
let domainBlock = cache.domainBlocks.get(
blockUID(domainId, domainBlockNumber)
);
if (!domainBlock) {
domainBlock = createDomainBlock(
block,
domainId,
domainBlockNumber,
domainBlockHash,
{
extrinsicRoot: domainBlockExtrinsicRoot,
consensusBlockNumber: Number(consensusBlockNumber),
consensusBlockHash,
}
);
cache.domainBlocks.set(domainBlock.id, domainBlock);
}

if (!bundle) {
bundle = createBundle(domain.id, domainBlockHash, blockBundleIndex, {
domainBlockNumber: Number(domainBlockNumber),
bundle = createBundle(
domain.id,
domainBlock.id,
domainBlockHash,
domainBlockExtrinsicRoot,
consensusBlockNumber: Number(consensusBlockNumber),
consensusBlockHash,
totalTransfersIn,
transfersInCount,
totalTransfersOut,
transfersOutCount,
totalRejectedTransfersClaimed,
rejectedTransfersClaimedCount,
totalTransfersRejected,
transfersRejectedCount,
totalVolume,
consensusStorageFee: BigInt(blockFees.consensusStorageFee),
domainExecutionFee: BigInt(blockFees.domainExecutionFee),
burnedBalance: BigInt(blockFees.burnedBalance),
});
blockBundleIndex,
{
domainBlockId: domainBlock.id,
domainBlockNumber: Number(domainBlockNumber),
domainBlockHash,
domainBlockExtrinsicRoot,
consensusBlockNumber: Number(consensusBlockNumber),
consensusBlockHash,
totalTransfersIn,
transfersInCount,
totalTransfersOut,
transfersOutCount,
totalRejectedTransfersClaimed,
rejectedTransfersClaimedCount,
totalTransfersRejected,
transfersRejectedCount,
totalVolume,
consensusStorageFee: BigInt(blockFees.consensusStorageFee),
domainExecutionFee: BigInt(blockFees.domainExecutionFee),
burnedBalance: BigInt(blockFees.burnedBalance),
}
);
cache.bundles.set(bundle.id, bundle);

const bundleAuthor = createBundleAuthor(bundle.id, account.id, operator.id);
const bundleAuthor = createBundleAuthor(
domain.id,
account.id,
operator.id,
bundle.id,
domainBlock.id
);
cache.bundleAuthors.set(bundleAuthor.id, bundleAuthor);

domain.lastDomainBlockNumber = Number(domainBlockNumber);
Expand Down
1 change: 1 addition & 0 deletions indexers/staking-squid/src/model/generated/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ export * from "./_domainRuntime"
export * from "./account.model"
export * from "./operator.model"
export * from "./_operatorStatus"
export * from "./domainBlock.model"
export * from "./bundle.model"
export * from "./bundleAuthor.model"
export * from "./nominator.model"
Expand Down
10 changes: 8 additions & 2 deletions indexers/staking-squid/src/storage/bundle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,15 @@ import { bundleUID } from "../utils";

export const createBundle = (
domainId: string,
domainBlockId: string,
domainBlockHash: string,
domainBlockBundleIndex: number | string,
props?: Partial<Bundle>
): Bundle =>
new Bundle({
id: bundleUID(domainId, domainBlockHash, domainBlockBundleIndex),
domainId,
domainBlockId,
domainBlockNumber: 0,
domainBlockHash: "",
domainBlockExtrinsicRoot: "",
Expand All @@ -32,15 +34,19 @@ export const createBundle = (
});

export const createBundleAuthor = (
bundleId: string,
domainId: string,
accountId: string,
operatorId: string,
bundleId: string,
domainBlockId: string,
props?: Partial<BundleAuthor>
): BundleAuthor =>
new BundleAuthor({
id: randomUUID(),
bundleId,
domainId,
accountId,
operatorId,
bundleId,
domainBlockId,
...props,
});
24 changes: 24 additions & 0 deletions indexers/staking-squid/src/storage/domainBlock.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import { DomainBlock } from "../model";
import { CtxBlock } from "../processor";
import { blockUID, getBlockNumber, getTimestamp } from "../utils";

export const createDomainBlock = (
block: CtxBlock,
domainId: string,
blockNumber: number,
blockHash: string,
props?: Partial<DomainBlock>
): DomainBlock =>
new DomainBlock({
id: blockUID(domainId, blockNumber),
domainId,
blockNumber,
blockHash,
extrinsicRoot: "",
consensusBlockNumber: 0,
consensusBlockHash: "",
createdAt: getBlockNumber(block),
updatedAt: getBlockNumber(block),
timestamp: getTimestamp(block),
...props,
});
11 changes: 10 additions & 1 deletion indexers/staking-squid/src/utils/cache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {
BundleAuthor,
Deposit,
Domain,
DomainBlock,
Nominator,
Operator,
RewardEvent,
Expand All @@ -28,15 +29,20 @@ export type TemporaryCache = {
withdrawals: Map<string, Withdrawal>;
bundles: Map<string, Bundle>;
bundleAuthors: Map<string, BundleAuthor>;
domainBlocks: Map<string, DomainBlock>;
operatorRewardedEvents: Map<string, RewardEvent>;
stats: Map<string, Stats>;
statsPerDomain: Map<string, StatsPerDomain>;
statsPerOperator: Map<string, StatsPerOperator>;
};

export type LastBlockBundleIndexKey =
`lastBlockBundleIndex:${string}-${string}`;
type InternalKeyStore = LastBlockBundleIndexKey;

type CacheManager = {
isModified: boolean;
internalKeyStore: Map<string, string>;
internalKeyStore: Map<InternalKeyStore, string>;
};

export type Cache = PermanentCache & TemporaryCache & CacheManager;
Expand All @@ -53,6 +59,7 @@ export const initTemporaryCache: TemporaryCache = {
withdrawals: new Map(),
bundles: new Map(),
bundleAuthors: new Map(),
domainBlocks: new Map(),
operatorRewardedEvents: new Map(),
stats: new Map(),
statsPerDomain: new Map(),
Expand Down Expand Up @@ -126,6 +133,7 @@ export const save = async (ctx: Ctx<Store>, cache: Cache) => {
logTemp += logEntry("withdrawals", cache.withdrawals);
logTemp += logEntry("bundles", cache.bundles);
logTemp += logEntry("bundleAuthors", cache.bundleAuthors);
logTemp += logEntry("domainBlocks", cache.domainBlocks);
logTemp += logEntry("operatorRewardedEvents", cache.operatorRewardedEvents);
logTemp += logEntry("stats", cache.stats);
logTemp += logEntry("statsPerDomain", cache.statsPerDomain);
Expand All @@ -149,6 +157,7 @@ export const save = async (ctx: Ctx<Store>, cache: Cache) => {
cache.bundles.clear();
cache.bundleAuthors.clear();
cache.operatorRewardedEvents.clear();
cache.domainBlocks.clear();
cache.stats.clear();
cache.statsPerDomain.clear();
cache.statsPerOperator.clear();
Expand Down
5 changes: 5 additions & 0 deletions indexers/staking-squid/src/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,11 @@ export const bundleUID = (
domainBlockBundleIndex: number | string
): string => `${domainId}-${domainBlockHeight}-${domainBlockBundleIndex}`;

export const blockUID = (
domainId: number | string,
blockNumber: number | string
): string => `${domainId}-${blockNumber}`;

export const logBlock = (blocks: CtxBlock[]): void => {
const from = getBlockNumber(blocks[0]);
const to = getBlockNumber(blocks[blocks.length - 1]);
Expand Down

0 comments on commit 907db36

Please sign in to comment.