Skip to content

Commit

Permalink
Fix centrifuge block rewards check
Browse files Browse the repository at this point in the history
  • Loading branch information
wliyongfeng committed Nov 26, 2023
1 parent 9525f23 commit 2cff43e
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 9 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
const { getCfgBlockRewardCol } = require("../../../../mongo/data");
const { findRewardFromNextEvent } = require("./nextEvent");
const { findRewardFromNoExtrinsicEvents } = require("./noExtrinsicEvent");

function extractCollatorRewardInfo(blockEvents, treasuryDepositSort) {
const groupRewardedEvent = blockEvents[treasuryDepositSort - 2];
Expand All @@ -21,25 +23,25 @@ async function handleCentrifugeBlockRewards(event, indexer, blockEvents) {
return;
}

const nextEvent = blockEvents[sort + 1];
const { event: { section, method } } = nextEvent;
if (section !== "blockRewards" || method !== "NewSession") {
let rewardInfo = findRewardFromNextEvent(blockEvents, sort);
if (!rewardInfo) {
rewardInfo = findRewardFromNoExtrinsicEvents(blockEvents, sort);
}
if (!rewardInfo) {
return;
}

const balance = event.data[0].toString();
const eachCollatorReward = nextEvent.event.data[0].toString();
const totalReward = nextEvent.event.data[1].toString();

const { eachCollatorReward, totalReward } = rewardInfo;
const collatorRewardInfo = extractCollatorRewardInfo(blockEvents, sort);
if (!collatorRewardInfo) {
throw new Error(`Can not get centrifuge collator reward info at ${ indexer.blockHeight }`);
}

const obj = {
indexer,
section,
method,
section: "blockRewards",
method: "NewSession",
balance,
totalReward,
eachCollatorReward,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
function findRewardFromNextEvent(blockEvents, sort) {
const nextEvent = blockEvents[sort + 1];
const { event: { section, method } } = nextEvent;
if (section !== "blockRewards" || method !== "NewSession") {
return;
}

const eachCollatorReward = nextEvent.event.data[0].toString();
const totalReward = nextEvent.event.data[1].toString();
return {
eachCollatorReward,
totalReward,
}
}

module.exports = {
findRewardFromNextEvent,
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
function findRewardFromNoExtrinsicEvents(blockEvents, sort) {
const noExtrinsicEvents = blockEvents.filter((e, index) => e.phase.isNone && index > sort);
const targetEvent = noExtrinsicEvents.find(({ event }) => {
const { section, method } = event;
return section === "blockRewards" && method === "NewSession";
});
if (!targetEvent) {
return;
}

const eachCollatorReward = targetEvent.event.data[0].toString();
const totalReward = targetEvent.event.data[1].toString();
return {
eachCollatorReward,
totalReward,
}
}

module.exports = {
findRewardFromNoExtrinsicEvents,
}
2 changes: 1 addition & 1 deletion packages/input-scan/src/play.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ const {

async function test() {
const blockHeights = [
1499998,
4330800,
];

for (const height of blockHeights) {
Expand Down

0 comments on commit 2cff43e

Please sign in to comment.