From 1e62c5f62fedda0f9acb8e969132b76114499ece Mon Sep 17 00:00:00 2001 From: Marc-Aurele Besner Date: Mon, 18 Nov 2024 13:28:44 -0500 Subject: [PATCH 1/2] remove consensus chain offset on indexer --- .../mainnet/consensus/src/mappings/helper.ts | 22 +------------------ .../consensus/src/mappings/mappingHandlers.ts | 17 +++----------- .../taurus/consensus/src/mappings/helper.ts | 22 +------------------ .../consensus/src/mappings/mappingHandlers.ts | 17 +++----------- 4 files changed, 8 insertions(+), 70 deletions(-) diff --git a/indexers/mainnet/consensus/src/mappings/helper.ts b/indexers/mainnet/consensus/src/mappings/helper.ts index 53890382..f3dcc2af 100644 --- a/indexers/mainnet/consensus/src/mappings/helper.ts +++ b/indexers/mainnet/consensus/src/mappings/helper.ts @@ -1,10 +1,8 @@ -import { account, blockNumber } from "@autonomys/auto-consensus"; -import { ApiPromise, stringify } from "@autonomys/auto-utils"; +import { account } from "@autonomys/auto-consensus"; import { SubstrateBlock } from "@subql/types"; import { decodeLog } from "./utils"; const DEFAULT_ACCOUNT_ID = "0x00"; -const DEFAULT_CHAIN_HEAD_OFFSET = 10; // Core Consensus Helper Functions @@ -37,21 +35,3 @@ export const getBlockAuthor = (block: SubstrateBlock): string => { export const getAccountBalance = async (accountId: string) => await account(api as any, accountId); - -export const preventIndexingTooCloseToTheHeadOfTheChain = async ( - indexingBlockHeight: number | bigint -) => { - if (!unsafeApi) throw new Error("Unsafe API not found"); - if ( - typeof indexingBlockHeight !== "number" && - typeof indexingBlockHeight !== "bigint" - ) - throw new Error("Indexing block height must be a number or bigint"); - if (typeof indexingBlockHeight === "number") - indexingBlockHeight = BigInt(indexingBlockHeight); - - const targetHeight = await blockNumber(unsafeApi as unknown as ApiPromise); - - if (indexingBlockHeight > BigInt(targetHeight - DEFAULT_CHAIN_HEAD_OFFSET)) - throw new Error("Indexing too close to the head of the chain, skipping..."); -}; diff --git a/indexers/mainnet/consensus/src/mappings/mappingHandlers.ts b/indexers/mainnet/consensus/src/mappings/mappingHandlers.ts index 52b47747..d9d68e10 100644 --- a/indexers/mainnet/consensus/src/mappings/mappingHandlers.ts +++ b/indexers/mainnet/consensus/src/mappings/mappingHandlers.ts @@ -12,10 +12,7 @@ import { createAndSaveExtrinsic, createAndSaveLog, } from "./db"; -import { - getBlockAuthor, - preventIndexingTooCloseToTheHeadOfTheChain, -} from "./helper"; +import { getBlockAuthor } from "./helper"; import { handleExtrinsic, handleFarmerBlockRewardEvent, @@ -42,8 +39,6 @@ export async function handleBlock(_block: SubstrateBlock): Promise { events, } = _block; const height = BigInt(number.toString()); - await preventIndexingTooCloseToTheHeadOfTheChain(height); - const blockHash = hash.toString(); const blockTimestamp = timestamp ? timestamp : new Date(0); // Get block author @@ -112,9 +107,6 @@ export async function handleCall(_call: SubstrateExtrinsic): Promise { success, events, } = _call; - const height = BigInt(number.toString()); - await preventIndexingTooCloseToTheHeadOfTheChain(height); - const methodToHuman = method.toHuman() as ExtrinsicHuman; const methodToPrimitive = method.toPrimitive() as ExtrinsicPrimitive; const eventRecord = events[idx]; @@ -143,7 +135,7 @@ export async function handleCall(_call: SubstrateExtrinsic): Promise { await createAndSaveExtrinsic( hash.toString(), - height, + BigInt(number.toString()), hash.toString(), idx, methodToHuman.section, @@ -176,9 +168,6 @@ export async function handleEvent(_event: SubstrateEvent): Promise { extrinsic, event, } = _event; - const height = BigInt(number.toString()); - await preventIndexingTooCloseToTheHeadOfTheChain(height); - const primitive = event.toPrimitive() as EventPrimitive; const human = event.toHuman() as EventHuman; @@ -194,7 +183,7 @@ export async function handleEvent(_event: SubstrateEvent): Promise { const extrinsicHash = extrinsic ? extrinsic.extrinsic.hash.toString() : ""; await createAndSaveEvent( - height, + BigInt(number.toString()), hash.toString(), BigInt(idx), extrinsicId, diff --git a/indexers/taurus/consensus/src/mappings/helper.ts b/indexers/taurus/consensus/src/mappings/helper.ts index 53890382..f3dcc2af 100644 --- a/indexers/taurus/consensus/src/mappings/helper.ts +++ b/indexers/taurus/consensus/src/mappings/helper.ts @@ -1,10 +1,8 @@ -import { account, blockNumber } from "@autonomys/auto-consensus"; -import { ApiPromise, stringify } from "@autonomys/auto-utils"; +import { account } from "@autonomys/auto-consensus"; import { SubstrateBlock } from "@subql/types"; import { decodeLog } from "./utils"; const DEFAULT_ACCOUNT_ID = "0x00"; -const DEFAULT_CHAIN_HEAD_OFFSET = 10; // Core Consensus Helper Functions @@ -37,21 +35,3 @@ export const getBlockAuthor = (block: SubstrateBlock): string => { export const getAccountBalance = async (accountId: string) => await account(api as any, accountId); - -export const preventIndexingTooCloseToTheHeadOfTheChain = async ( - indexingBlockHeight: number | bigint -) => { - if (!unsafeApi) throw new Error("Unsafe API not found"); - if ( - typeof indexingBlockHeight !== "number" && - typeof indexingBlockHeight !== "bigint" - ) - throw new Error("Indexing block height must be a number or bigint"); - if (typeof indexingBlockHeight === "number") - indexingBlockHeight = BigInt(indexingBlockHeight); - - const targetHeight = await blockNumber(unsafeApi as unknown as ApiPromise); - - if (indexingBlockHeight > BigInt(targetHeight - DEFAULT_CHAIN_HEAD_OFFSET)) - throw new Error("Indexing too close to the head of the chain, skipping..."); -}; diff --git a/indexers/taurus/consensus/src/mappings/mappingHandlers.ts b/indexers/taurus/consensus/src/mappings/mappingHandlers.ts index 52b47747..d9d68e10 100644 --- a/indexers/taurus/consensus/src/mappings/mappingHandlers.ts +++ b/indexers/taurus/consensus/src/mappings/mappingHandlers.ts @@ -12,10 +12,7 @@ import { createAndSaveExtrinsic, createAndSaveLog, } from "./db"; -import { - getBlockAuthor, - preventIndexingTooCloseToTheHeadOfTheChain, -} from "./helper"; +import { getBlockAuthor } from "./helper"; import { handleExtrinsic, handleFarmerBlockRewardEvent, @@ -42,8 +39,6 @@ export async function handleBlock(_block: SubstrateBlock): Promise { events, } = _block; const height = BigInt(number.toString()); - await preventIndexingTooCloseToTheHeadOfTheChain(height); - const blockHash = hash.toString(); const blockTimestamp = timestamp ? timestamp : new Date(0); // Get block author @@ -112,9 +107,6 @@ export async function handleCall(_call: SubstrateExtrinsic): Promise { success, events, } = _call; - const height = BigInt(number.toString()); - await preventIndexingTooCloseToTheHeadOfTheChain(height); - const methodToHuman = method.toHuman() as ExtrinsicHuman; const methodToPrimitive = method.toPrimitive() as ExtrinsicPrimitive; const eventRecord = events[idx]; @@ -143,7 +135,7 @@ export async function handleCall(_call: SubstrateExtrinsic): Promise { await createAndSaveExtrinsic( hash.toString(), - height, + BigInt(number.toString()), hash.toString(), idx, methodToHuman.section, @@ -176,9 +168,6 @@ export async function handleEvent(_event: SubstrateEvent): Promise { extrinsic, event, } = _event; - const height = BigInt(number.toString()); - await preventIndexingTooCloseToTheHeadOfTheChain(height); - const primitive = event.toPrimitive() as EventPrimitive; const human = event.toHuman() as EventHuman; @@ -194,7 +183,7 @@ export async function handleEvent(_event: SubstrateEvent): Promise { const extrinsicHash = extrinsic ? extrinsic.extrinsic.hash.toString() : ""; await createAndSaveEvent( - height, + BigInt(number.toString()), hash.toString(), BigInt(idx), extrinsicId, From e90f6143f0d23cebf9308c5cbe1e5bd1d43c4e19 Mon Sep 17 00:00:00 2001 From: Marc-Aurele Besner Date: Mon, 18 Nov 2024 13:31:09 -0500 Subject: [PATCH 2/2] simplify logic --- indexers/mainnet/consensus/src/mappings/helper.ts | 8 -------- .../consensus/src/mappings/mappingAccountHandlers.ts | 12 ++++++------ indexers/taurus/consensus/src/mappings/helper.ts | 8 -------- .../consensus/src/mappings/mappingAccountHandlers.ts | 12 ++++++------ 4 files changed, 12 insertions(+), 28 deletions(-) diff --git a/indexers/mainnet/consensus/src/mappings/helper.ts b/indexers/mainnet/consensus/src/mappings/helper.ts index f3dcc2af..d47f3838 100644 --- a/indexers/mainnet/consensus/src/mappings/helper.ts +++ b/indexers/mainnet/consensus/src/mappings/helper.ts @@ -1,11 +1,8 @@ -import { account } from "@autonomys/auto-consensus"; import { SubstrateBlock } from "@subql/types"; import { decodeLog } from "./utils"; const DEFAULT_ACCOUNT_ID = "0x00"; -// Core Consensus Helper Functions - export const getBlockAuthor = (block: SubstrateBlock): string => { const { digest } = block.block.header; const preRuntimeRaw = digest.logs.find((digestI) => digestI.isPreRuntime); @@ -30,8 +27,3 @@ export const getBlockAuthor = (block: SubstrateBlock): string => { } return DEFAULT_ACCOUNT_ID; }; - -// Accounts Helper Functions - -export const getAccountBalance = async (accountId: string) => - await account(api as any, accountId); diff --git a/indexers/mainnet/consensus/src/mappings/mappingAccountHandlers.ts b/indexers/mainnet/consensus/src/mappings/mappingAccountHandlers.ts index b0224ec6..7045ec68 100644 --- a/indexers/mainnet/consensus/src/mappings/mappingAccountHandlers.ts +++ b/indexers/mainnet/consensus/src/mappings/mappingAccountHandlers.ts @@ -1,10 +1,10 @@ +import { account } from "@autonomys/auto-consensus"; import { SubstrateEvent, SubstrateExtrinsic } from "@subql/types"; import { createAndSaveAccountHistory, createAndSaveReward, createAndSaveTransfer, } from "./db"; -import { getAccountBalance } from "./helper"; export async function handleTransferEvent( event: SubstrateEvent @@ -20,8 +20,8 @@ export async function handleTransferEvent( const to = _to.toString(); const amount = BigInt(_amount.toString()); - const fromBalance = await getAccountBalance(from); - const toBalance = await getAccountBalance(to); + const fromBalance = await account(api as any, from); + const toBalance = await account(api as any, to); // create or update and save accounts await createAndSaveAccountHistory( @@ -69,7 +69,7 @@ export async function handleExtrinsic( const blockNumber = BigInt(number.toString()); const address = signer.toString(); - const balance = await getAccountBalance(address); + const balance = await account(api as any, address); // create or update and save accounts await createAndSaveAccountHistory( @@ -103,7 +103,7 @@ export async function handleFarmerVoteRewardEvent( const voter = _voter.toString(); const blockNumber = BigInt(number.toString()); - const balance = await getAccountBalance(voter); + const balance = await account(api as any, voter); // create or update and save accounts await createAndSaveAccountHistory( @@ -147,7 +147,7 @@ export async function handleFarmerBlockRewardEvent( const blockAuthor = _blockAuthor.toString(); const blockNumber = BigInt(number.toString()); - const balance = await getAccountBalance(blockAuthor); + const balance = await account(api as any, blockAuthor); // create or update and save accounts await createAndSaveAccountHistory( diff --git a/indexers/taurus/consensus/src/mappings/helper.ts b/indexers/taurus/consensus/src/mappings/helper.ts index f3dcc2af..d47f3838 100644 --- a/indexers/taurus/consensus/src/mappings/helper.ts +++ b/indexers/taurus/consensus/src/mappings/helper.ts @@ -1,11 +1,8 @@ -import { account } from "@autonomys/auto-consensus"; import { SubstrateBlock } from "@subql/types"; import { decodeLog } from "./utils"; const DEFAULT_ACCOUNT_ID = "0x00"; -// Core Consensus Helper Functions - export const getBlockAuthor = (block: SubstrateBlock): string => { const { digest } = block.block.header; const preRuntimeRaw = digest.logs.find((digestI) => digestI.isPreRuntime); @@ -30,8 +27,3 @@ export const getBlockAuthor = (block: SubstrateBlock): string => { } return DEFAULT_ACCOUNT_ID; }; - -// Accounts Helper Functions - -export const getAccountBalance = async (accountId: string) => - await account(api as any, accountId); diff --git a/indexers/taurus/consensus/src/mappings/mappingAccountHandlers.ts b/indexers/taurus/consensus/src/mappings/mappingAccountHandlers.ts index b0224ec6..7045ec68 100644 --- a/indexers/taurus/consensus/src/mappings/mappingAccountHandlers.ts +++ b/indexers/taurus/consensus/src/mappings/mappingAccountHandlers.ts @@ -1,10 +1,10 @@ +import { account } from "@autonomys/auto-consensus"; import { SubstrateEvent, SubstrateExtrinsic } from "@subql/types"; import { createAndSaveAccountHistory, createAndSaveReward, createAndSaveTransfer, } from "./db"; -import { getAccountBalance } from "./helper"; export async function handleTransferEvent( event: SubstrateEvent @@ -20,8 +20,8 @@ export async function handleTransferEvent( const to = _to.toString(); const amount = BigInt(_amount.toString()); - const fromBalance = await getAccountBalance(from); - const toBalance = await getAccountBalance(to); + const fromBalance = await account(api as any, from); + const toBalance = await account(api as any, to); // create or update and save accounts await createAndSaveAccountHistory( @@ -69,7 +69,7 @@ export async function handleExtrinsic( const blockNumber = BigInt(number.toString()); const address = signer.toString(); - const balance = await getAccountBalance(address); + const balance = await account(api as any, address); // create or update and save accounts await createAndSaveAccountHistory( @@ -103,7 +103,7 @@ export async function handleFarmerVoteRewardEvent( const voter = _voter.toString(); const blockNumber = BigInt(number.toString()); - const balance = await getAccountBalance(voter); + const balance = await account(api as any, voter); // create or update and save accounts await createAndSaveAccountHistory( @@ -147,7 +147,7 @@ export async function handleFarmerBlockRewardEvent( const blockAuthor = _blockAuthor.toString(); const blockNumber = BigInt(number.toString()); - const balance = await getAccountBalance(blockAuthor); + const balance = await account(api as any, blockAuthor); // create or update and save accounts await createAndSaveAccountHistory(