Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat: MsgInstallBundle indexing #27

Merged
merged 3 commits into from
Jun 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions project.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,13 @@ const project: CosmosProject = {
file: "./proto/cosmos/gov/v1beta1/gov.proto",
messages: ["WeightedVoteOption"],
},
],
[
"/agoric.swingset.MsgInstallBundle",
{
file: "./proto/agoric/swingset/msgs.proto",
messages: ["MsgInstallBundle"],
},
],
]),
},
Expand Down Expand Up @@ -107,6 +114,13 @@ const project: CosmosProject = {
type: "state_change",
},
},
{
handler: 'handleBundleInstallMessage',
kind: CosmosHandlerKind.Message,
filter: {
type: '/agoric.swingset.MsgInstallBundle',
},
},
],
},
},
Expand Down
10 changes: 10 additions & 0 deletions schema.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -303,3 +303,13 @@ type VaultStatesDaily @entity {
liquidated: BigInt!
liquidatedClosed: BigInt!
}

type BundleInstall @entity {
id: ID!
blockHeight: BigInt!
blockTime: Date!
uncompressedSize: BigInt!
bundle: String!
compressedBundle: String!
submitter: String!
}
28 changes: 26 additions & 2 deletions src/mappings/mappingHandlers.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import { StateChangeEvent, IBCChannel, IBCTransfer, TransferType } from '../types';
import { CosmosEvent } from '@subql/types-cosmos';
import { StateChangeEvent, IBCChannel, IBCTransfer, TransferType, BundleInstall } from '../types';
import { CosmosEvent, CosmosMessage } from '@subql/types-cosmos';
import {
b64decode,
extractStoragePath,
getStateChangeModule,
resolveBrandNamesAndValues,
getEscrowAddress,
getAddressFromUint8Array,
} from './utils';

import {
Expand Down Expand Up @@ -128,6 +129,29 @@ export async function handleIbcReceivePacketEvent(cosmosEvent: CosmosEvent): Pro
await Promise.allSettled([transferRecord.save(), ibcChannel]);
}

export async function handleBundleInstallMessage(message: CosmosMessage): Promise<void> {
const { msg, block, tx } = message;

if (msg.typeUrl !== '/agoric.swingset.MsgInstallBundle') {
logger.warn('message type is not /agoric.swingset.MsgInstallBundle');
return;
}

// JSON.stringify converts the object from Uint8Array to readable string
const { uncompressedSize, compressedBundle, bundle } = JSON.parse(JSON.stringify(msg.decodedMsg));
const bundleRecord = new BundleInstall(
tx.hash,
BigInt(block.header.height),
block.header.time as any,
BigInt(uncompressedSize),
bundle || '',
compressedBundle || '',
getAddressFromUint8Array(msg.decodedMsg.submitter),
);

await bundleRecord.save();
}

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we should wrap this section of the code in a try-catch block to catch and log any errors that may occur.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

catching the error would result in skipping this message entirely. Throwing an error allows the indexing to stop at that point.
that is our current implementation for all of our functions

export async function handleStateChangeEvent(cosmosEvent: CosmosEvent): Promise<void> {
const { event, block } = cosmosEvent;

Expand Down
12 changes: 9 additions & 3 deletions src/mappings/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -101,10 +101,16 @@ export function dateToDayKey(timestamp: any): number {

export const getEscrowAddress = (port: string, channel: string) => {
const version = 'ics20-1';
const chainPrefix = 'agoric';
const stringtoSha = Buffer.from([...Buffer.from(version), 0, ...Buffer.from(`${port}/${channel}`)]);
const shaHash = sha256.sha256.array(stringtoSha.toString());
const bechWords = bech32.toWords(shaHash.slice(0, 20));
const address = bech32.encode(chainPrefix, bechWords);
const hashArray = new Uint8Array(shaHash.slice(0, 20));
const address = getAddressFromUint8Array(hashArray);
return address;
};

export const getAddressFromUint8Array = (uint8Array: Uint8Array, chainPrefix: string = 'agoric') => {
const words = bech32.toWords(uint8Array);
const bech32String = bech32.encode(chainPrefix, words);

return bech32String;
};