Skip to content

Commit

Permalink
feat: add custom comet.blockResults for finalizeBlockEvents
Browse files Browse the repository at this point in the history
  • Loading branch information
MichaelPretorius committed Dec 10, 2024
1 parent 13aa615 commit c5b7224
Show file tree
Hide file tree
Showing 5 changed files with 998 additions and 1,490 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
"author": "Ixo Foundation",
"license": "Apache 2",
"dependencies": {
"@ixo/impactxclient-sdk": "2.2.0",
"@ixo/impactxclient-sdk": "2.3.1",
"@sentry/node": "7.36.0",
"@sentry/tracing": "7.36.0",
"body-parser": "1.20.1",
Expand Down
32 changes: 30 additions & 2 deletions src/sync/sync_blocks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,34 @@ export const startSync = async () => {

const blockHeight = Number(block.block!.header!.height.low);

// if blockTM has finalizeBlockEvents, we need to split them into beginBlockEvents and endBlockEvents
// loop through finalizeBlockEvents and find the mode (BeginBlock or EndBlock) and add it to the corresponding array
// pushing original objects for optimization
let beginBlockEvents: Event[] = [];
let endBlockEvents: Event[] = [];
if (blockTM.finalizeBlockEvents.length > 0) {
for (let i = 0; i < blockTM.finalizeBlockEvents.length; i++) {
const event = blockTM.finalizeBlockEvents[i];
const attributes = event.attributes;
if (!attributes) continue;

for (let j = 0; j < attributes.length; j++) {
const attr = attributes[j];
if (attr.key === "mode") {
if (attr.value === "BeginBlock") {
beginBlockEvents.push(event as any);
} else if (attr.value === "EndBlock") {
endBlockEvents.push(event as any);
}
break; // Stop searching attributes once mode is found
}
}
}
} else {
beginBlockEvents = blockTM.beginBlockEvents as any;
endBlockEvents = blockTM.endBlockEvents as any;
}

await withTransaction(async (client) => {
currentPool = client;
await Promise.all([
Expand All @@ -69,8 +97,8 @@ export const startSync = async () => {
block.blockId!.hash!,
utils.proto.fromTimestamp(block.block!.header!.time!),
txsEvent.txResponses,
blockTM.beginBlockEvents as any,
blockTM.endBlockEvents as any
beginBlockEvents,
endBlockEvents
),
updateChain({
chainId: currentChain.chainId,
Expand Down
6 changes: 3 additions & 3 deletions src/sync/sync_chain.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { connectComet, CometClient } from "@cosmjs/tendermint-rpc";
// import { connectComet, CometClient } from "@cosmjs/tendermint-rpc";
import * as Proto from "../util/proto";
import { createQueryClient, createRegistry } from "@ixo/impactxclient-sdk";
import { RPC } from "../util/secrets";
Expand All @@ -7,13 +7,13 @@ import { Chain, createChain, getChain } from "../postgres/chain";
export let currentChain: Chain;
export let queryClient: Awaited<ReturnType<typeof createQueryClient>>;
export let registry: ReturnType<typeof createRegistry>;
export let cometClient: CometClient;
// export let cometClient: CometClient;

export const syncChain = async () => {
try {
queryClient = await createQueryClient(RPC);
registry = createRegistry();
cometClient = await connectComet(RPC);
// cometClient = await connectComet(RPC);

const res = await Proto.getLatestBlock();
const chainId = res?.block?.header?.chainId || "";
Expand Down
31 changes: 18 additions & 13 deletions src/util/proto.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
import { Event as ABCIEvent } from "@ixo/impactxclient-sdk/types/codegen/tendermint/abci/types";
import { Event as TendermintEvent } from "@cosmjs/tendermint-rpc/build/tendermint37/responses";
import { TxResponse } from "@ixo/impactxclient-sdk/types/codegen/cosmos/base/abci/v1beta1/abci";
import { cosmos, utils } from "@ixo/impactxclient-sdk";
import { cosmos, customQueries, utils } from "@ixo/impactxclient-sdk";
import Long from "long";
import { queryClient, registry, cometClient } from "../sync/sync_chain";
import { queryClient, registry } from "../sync/sync_chain";
import { RPC } from "./secrets";

export const getLatestBlock = async () => {
try {
Expand Down Expand Up @@ -51,7 +50,8 @@ export const getTxsEvent = async (height: number) => {

export const getTMBlockbyHeight = async (height: number) => {
try {
const res = await cometClient.blockResults(height);
const res = await customQueries.comet.blockResults(height, RPC, false);
// const res = await cometClient.blockResults(height);
// console.dir(res, { depth: null });
return res;
} catch (error) {
Expand Down Expand Up @@ -91,16 +91,21 @@ export const decodeMessage = (tx: any) => {
};

export const decodeEvent = (event: any) => {
// If event attributes is stringified, then it is a cosmos-sdk event, no need
// to map over attributes, slight optimization
if (typeof event.attributes?.[0]?.value === "string") {
return {
type: event.type,
attributes: event.attributes,
};
}

// otherwise we can assume all attributes are Uint8Array
const attributes = event.attributes.map((attr) => ({
key:
typeof attr.key === "string"
? attr.key
: utils.conversions.Uint8ArrayToJS(attr.key),
value:
typeof attr.value === "string"
? attr.value
: utils.conversions.Uint8ArrayToJS(attr.value),
key: utils.conversions.Uint8ArrayToJS(attr.key),
value: utils.conversions.Uint8ArrayToJS(attr.value),
}));

return {
type: event.type,
attributes: attributes,
Expand Down
Loading

0 comments on commit c5b7224

Please sign in to comment.