Skip to content

Commit

Permalink
Handle rejectAnnouncement call, #919
Browse files Browse the repository at this point in the history
  • Loading branch information
wliyongfeng committed Jun 20, 2024
1 parent af69caf commit 61ac610
Show file tree
Hide file tree
Showing 7 changed files with 133 additions and 43 deletions.
2 changes: 1 addition & 1 deletion backend/packages/pallet-proxy-scan/src/play.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ const {
(async () => {
await initPalletProxyScanDb();
await subscribeFinalizedHeight();
const blockHeights = [10966643, 10966707, 10966717, 10966740];
const blockHeights = [10966643, 10967652, 10967654, 10967656, 10967673];

const api = await getApi();
for (const height of blockHeights) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ async function handleAnnounced(event, indexer) {
delegate,
real,
callHash,
isRemoved: false,
isFinal: false,
state: "Announced",
indexer,
});

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
const {
chain: { getBlockHash },
} = require("@osn/scan-common");
const { generateAnnouncementId } = require("../../../common");
const { queryAnnouncements } = require("./query");

function calcRemovedIds(
delegate,
preAnnouncements = [],
nowAnnouncements = [],
) {
const preIds = preAnnouncements.map((a) =>
generateAnnouncementId(delegate, a.real, a.callHash, a.height),
);
const nowIds = nowAnnouncements.map((a) =>
generateAnnouncementId(delegate, a.real, a.callHash, a.height),
);
return preIds.filter((id) => !nowIds.includes(id));
}

async function getRemovedAnnouncementIds(delegate, real, indexer) {
const preBlockAnnouncements = await queryAnnouncements(
delegate,
real,
await getBlockHash(indexer.blockHeight - 1),
);
const currentBlockAnnouncements = await queryAnnouncements(
delegate,
real,
indexer.blockHash,
);
return calcRemovedIds(
delegate,
preBlockAnnouncements,
currentBlockAnnouncements,
);
}

module.exports = {
getRemovedAnnouncementIds,
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
const {
chain: { findBlockApi },
} = require("@osn/scan-common");
const { getProxySection } = require("../../../common");

async function queryAnnouncements(who, real, blockHash) {
const blockApi = await findBlockApi(blockHash);
const [announcements] = await blockApi.query[getProxySection()].announcements(
who,
);
return announcements
.filter((a) => a.real.toString() === real)
.map((a) => a.toJSON());
}

module.exports = {
queryAnnouncements,
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
const { getProxySection } = require("../../common");
const {
store: { setKnownHeightMark },
} = require("@statescan/common");
const { getRemovedAnnouncementIds } = require("./common/getRemovedIds");
const {
palletProxy: { getAnnouncementCol, getAnnouncementTimelineCol },
} = require("@statescan/mongo");

async function markAnnouncementRejectedByIds(removedIds, indexer) {
const col = await getAnnouncementCol();
const bulk = col.initializeUnorderedBulkOp();
for (const announcementId of removedIds) {
bulk
.find({ announcementId })
.updateOne({
$set: { isFinal: true, state: "Rejected", rejectedAt: indexer },
});
}

if (bulk.length > 0) {
await bulk.execute();
}
}

async function insertTimelineByIds(removedIds, indexer) {
const col = await getAnnouncementTimelineCol();
const bulk = col.initializeUnorderedBulkOp();
for (const announcementId of removedIds) {
bulk.insert({
announcementId,
name: "Rejected",
args: {},
indexer,
});
}

if (bulk.length > 0) {
await bulk.execute();
}
}

async function handleRejectAnnouncement(call, signer, extrinsicIndexer) {
const { section, method } = call;
if (getProxySection() !== section || "rejectAnnouncement" !== method) {
return;
}
setKnownHeightMark(extrinsicIndexer.blockHeight);

const delegate = call.args[0].toString();
const removedIds = await getRemovedAnnouncementIds(
delegate,
signer,
extrinsicIndexer,
);
await markAnnouncementRejectedByIds(removedIds, extrinsicIndexer);
await insertTimelineByIds(removedIds, extrinsicIndexer);
}

module.exports = {
handleRejectAnnouncement,
};
Original file line number Diff line number Diff line change
@@ -1,45 +1,21 @@
const {
chain: { findBlockApi, getBlockHash },
} = require("@osn/scan-common");
const { getProxySection, generateAnnouncementId } = require("../../common");
const { getProxySection } = require("../../common");
const {
palletProxy: { getAnnouncementCol, getAnnouncementTimelineCol },
} = require("@statescan/mongo");
const {
store: { setKnownHeightMark },
} = require("@statescan/common");

async function queryAnnouncements(who, real, blockHash) {
const blockApi = await findBlockApi(blockHash);
const [announcements] = await blockApi.query[getProxySection()].announcements(
who,
);
return announcements
.filter((a) => a.real.toString() === real)
.map((a) => a.toJSON());
}

function removedAnnouncementIds(
delegate,
preAnnouncements = [],
nowAnnouncements = [],
) {
const preIds = preAnnouncements.map((a) =>
generateAnnouncementId(delegate, a.real, a.callHash, a.height),
);
const nowIds = nowAnnouncements.map((a) =>
generateAnnouncementId(delegate, a.real, a.callHash, a.height),
);
return preIds.filter((id) => !nowIds.includes(id));
}
const { getRemovedAnnouncementIds } = require("./common/getRemovedIds");

async function markAnnouncementRemovedByIds(removedIds, indexer) {
const col = await getAnnouncementCol();
const bulk = col.initializeUnorderedBulkOp();
for (const announcementId of removedIds) {
bulk
.find({ announcementId })
.updateOne({ $set: { isRemoved: true, removedAt: indexer } });
.updateOne({
$set: { isFinal: true, state: "Removed", removedAt: indexer },
});
}

if (bulk.length > 0) {
Expand Down Expand Up @@ -72,20 +48,10 @@ async function handleRemoveAnnouncement(call, signer, extrinsicIndexer) {
setKnownHeightMark(extrinsicIndexer.blockHeight);

const real = call.args[0].toString();
const preBlockAnnouncements = await queryAnnouncements(
signer,
real,
await getBlockHash(extrinsicIndexer.blockHeight - 1),
);
const currentBlockAnnouncements = await queryAnnouncements(
const removedIds = await getRemovedAnnouncementIds(
signer,
real,
extrinsicIndexer.blockHash,
);
const removedIds = removedAnnouncementIds(
signer,
preBlockAnnouncements,
currentBlockAnnouncements,
extrinsicIndexer,
);

await markAnnouncementRemovedByIds(removedIds, extrinsicIndexer);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,13 @@ const {
const { handleRemoveProxies } = require("./calls/removeProxies");
const { handleKillPure } = require("./calls/killPure");
const { handleRemoveAnnouncement } = require("./calls/removeAnnouncement");
const { handleRejectAnnouncement } = require("./calls/rejectAnnouncement");

async function handleCalls(call, author, extrinsicIndexer) {
await handleRemoveProxies(call, author, extrinsicIndexer);
await handleKillPure(call, author, extrinsicIndexer);
await handleRemoveAnnouncement(call, author, extrinsicIndexer);
await handleRejectAnnouncement(call, author, extrinsicIndexer);
}

async function handleExtrinsics(extrinsics = [], allEvents = [], indexer) {
Expand Down

0 comments on commit 61ac610

Please sign in to comment.