Skip to content

Commit 81509e4

Browse files
fixup: almanac resolutions handler
1 parent 1f7a010 commit 81509e4

File tree

2 files changed

+12
-57
lines changed

2 files changed

+12
-57
lines changed

src/mappings/almanac/resolutions.ts

Lines changed: 10 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,4 @@
1-
import {
2-
AlmanacRecord,
3-
AlmanacResolution,
4-
Block,
5-
Contract,
6-
Event, EventAttribute,
7-
InstantiateContractMessage,
8-
Interface,
9-
} from "../../types";
10-
import {parseAttributes, WasmdEventAttributesI} from "../utils";
1+
import {AlmanacRecord, AlmanacResolution, Contract, Interface,} from "../../types";
112

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

2718
export async function expireAlmanacResolutionsRelativeToHeight(height: bigint): Promise<void> {
28-
// TODO: optimize with custom `Store` methods exposing more sequelize functionality.
29-
const almanacContracts = await store.getByField("Contract", "interface", Interface.MicroAgentAlmanac) as Contract[];
30-
31-
for (const contract of almanacContracts) {
32-
const instantiateMsg = await InstantiateContractMessage.get(contract.instantiateMessageId);
33-
try {
34-
const {expiry_height} = JSON.parse(instantiateMsg.payload);
35-
if (!expiry_height) {
36-
logger.warn(`[expireAlmanacResolutionsRelativeToHeight] (height: ${height}): falsey expiry_height: ${expiry_height}`);
37-
continue;
38-
}
39-
40-
const blocks = await Block.getByHeight(BigInt(expiry_height));
41-
if (!blocks || blocks.length !== 1) {
42-
logger.error(`[expireAlmanacResolutionAtHeight] (height: ${height}): found ${blocks?.length ?? 0} blocks at that height`);
43-
continue;
44-
}
45-
46-
const events = await Event.getByBlockId(blocks[0].id);
47-
if (!events || events.length !== 1) {
48-
logger.error(`[expireAlmanacResolutionAtHeight] (height: ${height}): found ${events?.length ?? 0} events at that height`);
49-
continue;
50-
}
51-
52-
// TODO: consider relating EventAttributes to Blocks to reduce # of serial lookups.
53-
for (const event of events) {
54-
const eventAttributes = await EventAttribute.getByEventId(event.id);
55-
if (!events || events.length !== 1) {
56-
logger.error(`[expireAlmanacResolutionAtHeight] (height: ${height}): found ${eventAttributes?.length ?? 0} event attributes with event ID: ${event.id}`);
57-
continue;
58-
}
59-
60-
const attributes = parseAttributes<WasmdEventAttributesI>(eventAttributes);
61-
if (!attributes.action || attributes.action !== "register") {
62-
logger.warn(`[expireAlmanacResolutionsRelativeToHeight] (height: ${height}): expected "register" action, got: ${attributes.action}`);
63-
continue;
64-
}
65-
66-
// NB: resolution ID is equivalent to register event ID.
67-
await expireAlmanacResolution(event.id);
68-
}
69-
} catch (error) {
70-
logger.warn(`[expireAlmanacResolutionsRelativeToHeight] (height: ${height}): unable to parse instantiate message payload`);
71-
}
72-
}
19+
// NB: resolution, record, and registration ID are the same across related entities.
20+
const expiringResolutionIdsSql = `SELECT r.id
21+
FROM app.almanac_registrations r
22+
WHERE r.expiry_height <= ${height}
23+
`;
24+
const expiringResolutionIds = await store.selectRaw(expiringResolutionIdsSql);
25+
26+
// NB: will throw an error if any promise rejects.
27+
await Promise.all(expiringResolutionIds.map(r => expireAlmanacResolution(String(r.id))));
7328
}
7429

7530
export async function expireAlmanacResolution(id: string): Promise<void> {

src/mappings/primitives.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import {
1010
} from "./utils";
1111
import {createHash} from "crypto";
1212
import {toBech32} from "@cosmjs/encoding";
13-
// import {expireAlmanacResolutionsRelativeToHeight} from "./almanac/resolutions";
13+
import {expireAlmanacResolutionsRelativeToHeight} from "./almanac/resolutions";
1414

1515
export async function handleBlock(block: CosmosBlock): Promise<void> {
1616
await attemptHandling(block, _handleBlock, _handleBlockError);
@@ -42,7 +42,7 @@ async function _handleBlock(block: CosmosBlock): Promise<void> {
4242
});
4343

4444
await blockEntity.save();
45-
// await expireAlmanacResolutionsAtHeight(height);
45+
await expireAlmanacResolutionsRelativeToHeight(height);
4646
}
4747

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

0 commit comments

Comments
 (0)