-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Handle rejectAnnouncement call, #919
- Loading branch information
1 parent
af69caf
commit 61ac610
Showing
7 changed files
with
133 additions
and
43 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
41 changes: 41 additions & 0 deletions
41
backend/packages/pallet-proxy-scan/src/scan/extrinsics/calls/common/getRemovedIds.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
}; |
18 changes: 18 additions & 0 deletions
18
backend/packages/pallet-proxy-scan/src/scan/extrinsics/calls/common/query.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
}; |
62 changes: 62 additions & 0 deletions
62
backend/packages/pallet-proxy-scan/src/scan/extrinsics/calls/rejectAnnouncement.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters