Skip to content

Commit

Permalink
Merge pull request #19 from subquery/remove-promise-all-ensure-poi
Browse files Browse the repository at this point in the history
remove promise all to ensure poi
  • Loading branch information
jiqiang90 authored Jun 20, 2024
2 parents cf18b45 + 183fced commit 15be922
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 57 deletions.
17 changes: 6 additions & 11 deletions src/mappings/NewEra.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,8 @@ async function processEraStakersClipped(
const exposures =
await api.query.staking.erasStakersClipped.entries(currentEra);

const eraValidatorInfos = exposures.map(([key, exposure]) => {
for (const [key, exposure] of exposures) {
const [, validatorId] = key.args;

let validatorIdString = validatorId.toString();
const exp = exposure as unknown as Exposure;
const eraValidatorInfo = new EraValidatorInfo(
Expand All @@ -53,10 +52,8 @@ async function processEraStakersClipped(
} as IndividualExposure;
}),
);
return eraValidatorInfo.save();
});

await Promise.allSettled(eraValidatorInfos);
await eraValidatorInfo.save();
}
}

async function processEraStakersPaged(
Expand Down Expand Up @@ -95,7 +92,7 @@ async function processEraStakersPaged(
{},
);

const eraValidatorInfos = overview.map(([key, exp]) => {
for (const [key, exp] of overview) {
const exposure = (
exp as unknown as Option<SpStakingPagedExposureMetadata>
).unwrap();
Expand All @@ -115,8 +112,6 @@ async function processEraStakersPaged(
exposure.own.toBigInt(),
others,
);
return eraValidatorInfo.save();
});

await Promise.allSettled(eraValidatorInfos);
await eraValidatorInfo.save();
}
}
84 changes: 38 additions & 46 deletions src/mappings/Rewards.ts
Original file line number Diff line number Diff line change
Expand Up @@ -350,61 +350,53 @@ async function buildRewardEvents<A>(
let blockNumber = block.block.header.number.toString();
let blockTimestamp = timestamp(block);

const [, savingPromises] = block.events.reduce<[A, Promise<void>[]]>(
(accumulator, eventRecord, eventIndex) => {
let [innerAccumulator, currentPromises] = accumulator;

if (
!(
eventRecord.event.method == eventMethod &&
eventRecord.event.section == eventSection
)
)
return accumulator;
let innerAccumulator = initialInnerAccumulator;
for (let eventIndex = 0; eventIndex < block.events.length; eventIndex++) {
const eventRecord = block.events[eventIndex];

let [account, amount] = decodeDataFromReward(
eventRecordToSubstrateEvent(eventRecord),
);
if (
!(
eventRecord.event.method === eventMethod &&
eventRecord.event.section === eventSection
)
)
continue;

const newAccumulator = produceNewAccumulator(
innerAccumulator,
account.toString(),
);
let [account, amount] = decodeDataFromReward(
eventRecordToSubstrateEvent(eventRecord),
);

const eventId = eventIdFromBlockAndIdx(
blockNumber,
eventIndex.toString(),
);
innerAccumulator = produceNewAccumulator(
innerAccumulator,
account.toString(),
);

const accountAddress = account.toString();
const destinationAddress = accountsMapping[accountAddress];
const eventId = eventIdFromBlockAndIdx(blockNumber, eventIndex.toString());

const element = new HistoryElement(
eventId,
block.block.header.number.toNumber(),
blockTimestamp,
destinationAddress != undefined ? destinationAddress : accountAddress,
);
const accountAddress = account.toString();
const destinationAddress = accountsMapping[accountAddress];

if (extrinsic !== undefined) {
element.extrinsicHash = extrinsic.extrinsic.hash.toString();
element.extrinsicIdx = extrinsic.idx;
}
element.reward = produceReward(
newAccumulator,
eventIndex,
accountAddress,
amount.toString(),
);
const element = new HistoryElement(
eventId,
block.block.header.number.toNumber(),
blockTimestamp,
destinationAddress !== undefined ? destinationAddress : accountAddress,
);

currentPromises.push(element.save());
if (extrinsic !== undefined) {
element.extrinsicHash = extrinsic.extrinsic.hash.toString();
element.extrinsicIdx = extrinsic.idx;
}

return [newAccumulator, currentPromises];
},
[initialInnerAccumulator, []],
);
element.reward = produceReward(
innerAccumulator,
eventIndex,
accountAddress,
amount.toString(),
);

await Promise.allSettled(savingPromises);
await element.save();
}
}

async function updateAccumulatedReward(
Expand Down

0 comments on commit 15be922

Please sign in to comment.