Skip to content

Commit

Permalink
refactor: primitives handlers error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
bryanchriswhite committed Nov 16, 2022
1 parent ae84da1 commit 72ab1a1
Showing 1 changed file with 40 additions and 4 deletions.
44 changes: 40 additions & 4 deletions src/mappings/primitives.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,26 @@
import {CosmosBlock, CosmosEvent, CosmosMessage, CosmosTransaction} from "@subql/types-cosmos";
import {Block, Event, Message, Transaction, TxStatus} from "../types";
import {messageId} from "./utils";
import {attemptHandling, messageId, trackUnprocessable} from "./utils";
import {createHash} from "crypto";
import {toBech32} from "@cosmjs/encoding";

export async function handleBlock(block: CosmosBlock): Promise<void> {
await attemptHandling(block, _handleBlock, _handleBlockError);
}

export async function handleTransaction(tx: CosmosTransaction): Promise<void> {
await attemptHandling(tx, _handleTransaction, _handleTransactionError);
}

export async function handleMessage(msg: CosmosMessage): Promise<void> {
await attemptHandling(msg, _handleMessage, _handleMessageError);
}

export async function handleEvent(event: CosmosEvent): Promise<void> {
await attemptHandling(event, _handleEvent, _handleEventError);
}

async function _handleBlock(block: CosmosBlock): Promise<void> {
logger.info(`[handleBlock] (block.header.height): indexing block ${block.block.header.height}`);

const {id, header: {chainId, height, time}} = block.block;
Expand All @@ -19,7 +35,7 @@ export async function handleBlock(block: CosmosBlock): Promise<void> {
await blockEntity.save();
}

export async function handleTransaction(tx: CosmosTransaction): Promise<void> {
async function _handleTransaction(tx: CosmosTransaction): Promise<void> {
logger.info(`[handleTransaction] (block ${tx.block.block.header.height}): indexing transaction ${tx.idx + 1} / ${tx.block.txs.length}`);
logger.debug(`[handleTransaction] (tx.decodedTx): ${JSON.stringify(tx.decodedTx, null, 2)}`);
logger.debug(`[handleTransaction] (tx.tx.log): ${tx.tx.log}`);
Expand Down Expand Up @@ -67,7 +83,7 @@ export async function handleTransaction(tx: CosmosTransaction): Promise<void> {
await txEntity.save();
}

export async function handleMessage(msg: CosmosMessage): Promise<void> {
async function _handleMessage(msg: CosmosMessage): Promise<void> {
logger.info(`[handleMessage] (tx ${msg.tx.hash}): indexing message ${msg.idx + 1} / ${msg.tx.decodedTx.body.messages.length}`);
logger.debug(`[handleMessage] (msg.msg): ${JSON.stringify(msg.msg, null, 2)}`);
delete msg.msg?.decodedMsg?.wasmByteCode;
Expand All @@ -83,7 +99,7 @@ export async function handleMessage(msg: CosmosMessage): Promise<void> {
await msgEntity.save();
}

export async function handleEvent(event: CosmosEvent): Promise<void> {
async function _handleEvent(event: CosmosEvent): Promise<void> {
logger.info(`[handleEvent] (tx ${event.tx.hash}): indexing event ${event.idx + 1} / ${event.tx.tx.events.length}`);
logger.debug(`[handleEvent] (event.event): ${JSON.stringify(event.event, null, 2)}`);
logger.debug(`[handleEvent] (event.log): ${JSON.stringify(event.log, null, 2)}`);
Expand All @@ -105,3 +121,23 @@ export async function handleEvent(event: CosmosEvent): Promise<void> {

await eventEntity.save();
}

async function _handleBlockError(err: Error, _: CosmosBlock): Promise<void> {
// NB: we won't have persisted any related entities yet.
await trackUnprocessable(err, {});
}

async function _handleTransactionError(err: Error, tx: CosmosTransaction): Promise<void> {
const primitives = {block: tx.block};
await trackUnprocessable(err, primitives);
}

async function _handleMessageError(err: Error, tx: CosmosTransaction): Promise<void> {
const primitives = {block: tx.block, tx: tx};
await trackUnprocessable(err, primitives);
}

async function _handleEventError(err: Error, event: CosmosEvent): Promise<void> {
const primitives = {block: event.block, tx: event.tx};
await trackUnprocessable(err, primitives);
}

0 comments on commit 72ab1a1

Please sign in to comment.