Skip to content

Commit

Permalink
Merge pull request #966 from autonomys/feat/remove-consensus-indexer-…
Browse files Browse the repository at this point in the history
…offset-logic

Remove consensus indexer offset logic
  • Loading branch information
marc-aurele-besner authored Nov 18, 2024
2 parents aeec606 + e90f614 commit 39d62d1
Show file tree
Hide file tree
Showing 6 changed files with 18 additions and 96 deletions.
28 changes: 0 additions & 28 deletions indexers/mainnet/consensus/src/mappings/helper.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,7 @@
import { account, blockNumber } from "@autonomys/auto-consensus";
import { ApiPromise, stringify } from "@autonomys/auto-utils";
import { SubstrateBlock } from "@subql/types";
import { decodeLog } from "./utils";

const DEFAULT_ACCOUNT_ID = "0x00";
const DEFAULT_CHAIN_HEAD_OFFSET = 10;

// Core Consensus Helper Functions

export const getBlockAuthor = (block: SubstrateBlock): string => {
const { digest } = block.block.header;
Expand All @@ -32,26 +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);

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...");
};
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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(
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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(
Expand Down
17 changes: 3 additions & 14 deletions indexers/mainnet/consensus/src/mappings/mappingHandlers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,7 @@ import {
createAndSaveExtrinsic,
createAndSaveLog,
} from "./db";
import {
getBlockAuthor,
preventIndexingTooCloseToTheHeadOfTheChain,
} from "./helper";
import { getBlockAuthor } from "./helper";
import {
handleExtrinsic,
handleFarmerBlockRewardEvent,
Expand All @@ -42,8 +39,6 @@ export async function handleBlock(_block: SubstrateBlock): Promise<void> {
events,
} = _block;
const height = BigInt(number.toString());
await preventIndexingTooCloseToTheHeadOfTheChain(height);

const blockHash = hash.toString();
const blockTimestamp = timestamp ? timestamp : new Date(0);
// Get block author
Expand Down Expand Up @@ -112,9 +107,6 @@ export async function handleCall(_call: SubstrateExtrinsic): Promise<void> {
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];
Expand Down Expand Up @@ -143,7 +135,7 @@ export async function handleCall(_call: SubstrateExtrinsic): Promise<void> {

await createAndSaveExtrinsic(
hash.toString(),
height,
BigInt(number.toString()),
hash.toString(),
idx,
methodToHuman.section,
Expand Down Expand Up @@ -176,9 +168,6 @@ export async function handleEvent(_event: SubstrateEvent): Promise<void> {
extrinsic,
event,
} = _event;
const height = BigInt(number.toString());
await preventIndexingTooCloseToTheHeadOfTheChain(height);

const primitive = event.toPrimitive() as EventPrimitive;
const human = event.toHuman() as EventHuman;

Expand All @@ -194,7 +183,7 @@ export async function handleEvent(_event: SubstrateEvent): Promise<void> {
const extrinsicHash = extrinsic ? extrinsic.extrinsic.hash.toString() : "";

await createAndSaveEvent(
height,
BigInt(number.toString()),
hash.toString(),
BigInt(idx),
extrinsicId,
Expand Down
28 changes: 0 additions & 28 deletions indexers/taurus/consensus/src/mappings/helper.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,7 @@
import { account, blockNumber } from "@autonomys/auto-consensus";
import { ApiPromise, stringify } from "@autonomys/auto-utils";
import { SubstrateBlock } from "@subql/types";
import { decodeLog } from "./utils";

const DEFAULT_ACCOUNT_ID = "0x00";
const DEFAULT_CHAIN_HEAD_OFFSET = 10;

// Core Consensus Helper Functions

export const getBlockAuthor = (block: SubstrateBlock): string => {
const { digest } = block.block.header;
Expand All @@ -32,26 +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);

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...");
};
12 changes: 6 additions & 6 deletions indexers/taurus/consensus/src/mappings/mappingAccountHandlers.ts
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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(
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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(
Expand Down
17 changes: 3 additions & 14 deletions indexers/taurus/consensus/src/mappings/mappingHandlers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,7 @@ import {
createAndSaveExtrinsic,
createAndSaveLog,
} from "./db";
import {
getBlockAuthor,
preventIndexingTooCloseToTheHeadOfTheChain,
} from "./helper";
import { getBlockAuthor } from "./helper";
import {
handleExtrinsic,
handleFarmerBlockRewardEvent,
Expand All @@ -42,8 +39,6 @@ export async function handleBlock(_block: SubstrateBlock): Promise<void> {
events,
} = _block;
const height = BigInt(number.toString());
await preventIndexingTooCloseToTheHeadOfTheChain(height);

const blockHash = hash.toString();
const blockTimestamp = timestamp ? timestamp : new Date(0);
// Get block author
Expand Down Expand Up @@ -112,9 +107,6 @@ export async function handleCall(_call: SubstrateExtrinsic): Promise<void> {
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];
Expand Down Expand Up @@ -143,7 +135,7 @@ export async function handleCall(_call: SubstrateExtrinsic): Promise<void> {

await createAndSaveExtrinsic(
hash.toString(),
height,
BigInt(number.toString()),
hash.toString(),
idx,
methodToHuman.section,
Expand Down Expand Up @@ -176,9 +168,6 @@ export async function handleEvent(_event: SubstrateEvent): Promise<void> {
extrinsic,
event,
} = _event;
const height = BigInt(number.toString());
await preventIndexingTooCloseToTheHeadOfTheChain(height);

const primitive = event.toPrimitive() as EventPrimitive;
const human = event.toHuman() as EventHuman;

Expand All @@ -194,7 +183,7 @@ export async function handleEvent(_event: SubstrateEvent): Promise<void> {
const extrinsicHash = extrinsic ? extrinsic.extrinsic.hash.toString() : "";

await createAndSaveEvent(
height,
BigInt(number.toString()),
hash.toString(),
BigInt(idx),
extrinsicId,
Expand Down

0 comments on commit 39d62d1

Please sign in to comment.