Skip to content

Commit

Permalink
fixup: almanac resolutions handler
Browse files Browse the repository at this point in the history
  • Loading branch information
bryanchriswhite committed Dec 20, 2022
1 parent 1f7a010 commit 81509e4
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 57 deletions.
65 changes: 10 additions & 55 deletions src/mappings/almanac/resolutions.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,4 @@
import {
AlmanacRecord,
AlmanacResolution,
Block,
Contract,
Event, EventAttribute,
InstantiateContractMessage,
Interface,
} from "../../types";
import {parseAttributes, WasmdEventAttributesI} from "../utils";
import {AlmanacRecord, AlmanacResolution, Contract, Interface,} from "../../types";

export async function cacheAlmanacResolution(contractId: string, agentId: string, record: AlmanacRecord): Promise<void> {
try {
Expand All @@ -25,51 +16,15 @@ export async function cacheAlmanacResolution(contractId: string, agentId: string
}

export async function expireAlmanacResolutionsRelativeToHeight(height: bigint): Promise<void> {
// TODO: optimize with custom `Store` methods exposing more sequelize functionality.
const almanacContracts = await store.getByField("Contract", "interface", Interface.MicroAgentAlmanac) as Contract[];

for (const contract of almanacContracts) {
const instantiateMsg = await InstantiateContractMessage.get(contract.instantiateMessageId);
try {
const {expiry_height} = JSON.parse(instantiateMsg.payload);
if (!expiry_height) {
logger.warn(`[expireAlmanacResolutionsRelativeToHeight] (height: ${height}): falsey expiry_height: ${expiry_height}`);
continue;
}

const blocks = await Block.getByHeight(BigInt(expiry_height));
if (!blocks || blocks.length !== 1) {
logger.error(`[expireAlmanacResolutionAtHeight] (height: ${height}): found ${blocks?.length ?? 0} blocks at that height`);
continue;
}

const events = await Event.getByBlockId(blocks[0].id);
if (!events || events.length !== 1) {
logger.error(`[expireAlmanacResolutionAtHeight] (height: ${height}): found ${events?.length ?? 0} events at that height`);
continue;
}

// TODO: consider relating EventAttributes to Blocks to reduce # of serial lookups.
for (const event of events) {
const eventAttributes = await EventAttribute.getByEventId(event.id);
if (!events || events.length !== 1) {
logger.error(`[expireAlmanacResolutionAtHeight] (height: ${height}): found ${eventAttributes?.length ?? 0} event attributes with event ID: ${event.id}`);
continue;
}

const attributes = parseAttributes<WasmdEventAttributesI>(eventAttributes);
if (!attributes.action || attributes.action !== "register") {
logger.warn(`[expireAlmanacResolutionsRelativeToHeight] (height: ${height}): expected "register" action, got: ${attributes.action}`);
continue;
}

// NB: resolution ID is equivalent to register event ID.
await expireAlmanacResolution(event.id);
}
} catch (error) {
logger.warn(`[expireAlmanacResolutionsRelativeToHeight] (height: ${height}): unable to parse instantiate message payload`);
}
}
// NB: resolution, record, and registration ID are the same across related entities.
const expiringResolutionIdsSql = `SELECT r.id
FROM app.almanac_registrations r
WHERE r.expiry_height <= ${height}
`;
const expiringResolutionIds = await store.selectRaw(expiringResolutionIdsSql);

// NB: will throw an error if any promise rejects.
await Promise.all(expiringResolutionIds.map(r => expireAlmanacResolution(String(r.id))));
}

export async function expireAlmanacResolution(id: string): Promise<void> {
Expand Down
4 changes: 2 additions & 2 deletions src/mappings/primitives.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import {
} from "./utils";
import {createHash} from "crypto";
import {toBech32} from "@cosmjs/encoding";
// import {expireAlmanacResolutionsRelativeToHeight} from "./almanac/resolutions";
import {expireAlmanacResolutionsRelativeToHeight} from "./almanac/resolutions";

export async function handleBlock(block: CosmosBlock): Promise<void> {
await attemptHandling(block, _handleBlock, _handleBlockError);
Expand Down Expand Up @@ -42,7 +42,7 @@ async function _handleBlock(block: CosmosBlock): Promise<void> {
});

await blockEntity.save();
// await expireAlmanacResolutionsAtHeight(height);
await expireAlmanacResolutionsRelativeToHeight(height);
}

async function _handleTransaction(tx: CosmosTransaction): Promise<void> {
Expand Down

0 comments on commit 81509e4

Please sign in to comment.