Skip to content

Commit

Permalink
A0-2470: Adjust block count by +1 in Staking/Performance tab (#54)
Browse files Browse the repository at this point in the history
  • Loading branch information
Marcin-Radecki authored May 8, 2023
1 parent da7b4d9 commit 6467eb1
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 6 deletions.
53 changes: 47 additions & 6 deletions packages/page-staking/src/Performance/useCommitteePerformance.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -33,19 +33,23 @@ function useSessionCommitteePerformanceImpl (sessions: number[]): SessionCommitt

const [firstBlockInSessionHashes, setFirstBlockInSessionHashes] = useState<Hash[]>([]);
const [lastBlockInSessionsHashes, setLastBlockInSessionsHashes] = useState<Hash[]>([]);
const [lastBlockInSessionPerAuraHashes, setLastBlockInSessionPerAuraHashes] = useState<Hash[]>([]);

const [committees, setCommittees] = useState<string[][]>([]);
const [sessionValidatorBlockCountLookups, setSessionValidatorBlockCountLookups] = useState<[string, number][][]>([]);
const [lastBlockPerAuraAuthors, setLastBlockPerAuraAuthors] = useState<(string | undefined)[]>([]);
const [committeeMemberPerformances, setCommitteeMemberPerformances] = useState<SessionCommitteePerformance[]>([]);

function getSessionFirstAndLastBlock (session: number, sessionPeriod: number) {
// due to how AURA works, first block of the session is actually N + 1, 0th (genesis) block
// is treated in a special way. N % sessions_period block is the last session block.
// however, due to how pallet elections writes down session validator block count we need to
// read that storage map from one block before last block, as in the last block counter is
// cleared; this means we miss statistics by one block here
// cleared; so we adjust +1 per block author info from what AURA thinks last block is
return {
first: session * sessionPeriod + 1,
last: (session + 1) * sessionPeriod - 1
last: (session + 1) * sessionPeriod - 1,
lastPerAura: (session + 1) * sessionPeriod
};
}

Expand All @@ -64,6 +68,21 @@ function useSessionCommitteePerformanceImpl (sessions: number[]): SessionCommitt
[api, JSON.stringify(sessions)]
);

useEffect(() => {
if (api && api.consts.elections) {
const sessionPeriod = Number(api.consts.elections.sessionPeriod.toString());

const promises = sessions.map((session) => api.rpc.chain.getBlockHash(getSessionFirstAndLastBlock(session, sessionPeriod).lastPerAura));

Promise.all(promises)
.then((blockHashes) => setLastBlockInSessionPerAuraHashes(blockHashes.filter((hash) => !hash.isEmpty)))
.catch(console.error);
}
},
// eslint-disable-next-line react-hooks/exhaustive-deps
[api, JSON.stringify(sessions)]
);

useEffect(() => {
if (api && api.consts.elections) {
const sessionPeriod = Number(api.consts.elections.sessionPeriod.toString());
Expand Down Expand Up @@ -94,6 +113,20 @@ function useSessionCommitteePerformanceImpl (sessions: number[]): SessionCommitt
[api, JSON.stringify(lastBlockInSessionsHashes)]
);

useEffect(() => {
const promisesLastBlockPerAuraHeaders = lastBlockInSessionPerAuraHashes.map((hash) =>
api.derive.chain.getHeader(hash)
);

Promise.all(promisesLastBlockPerAuraHeaders).then((headersExtended) => {
setLastBlockPerAuraAuthors(headersExtended.map((headerExtended) => headerExtended.author)
.map((author) => author?.toString()));
}).catch(console.error);
},
// eslint-disable-next-line react-hooks/exhaustive-deps
[api, JSON.stringify(lastBlockInSessionPerAuraHashes)]
);

useEffect(() => {
const apisAtFirstBlockPromises = firstBlockInSessionHashes.map((hash) => api.at(hash.toString()));

Expand All @@ -112,12 +145,18 @@ function useSessionCommitteePerformanceImpl (sessions: number[]): SessionCommitt
);

function getValidatorPerformance (validator: string,
sessionValidatorBlockCountLookup: [string, number][] | undefined): ValidatorPerformance {
sessionValidatorBlockCountLookup: [string, number][] | undefined,
lastBlockPerAuraAuthors: (string | undefined)[],
index: number): ValidatorPerformance {
const maybeCount = sessionValidatorBlockCountLookup?.find(([id]) => id === validator);
const count = maybeCount
let count = maybeCount
? maybeCount[1]
: (sessionValidatorBlockCountLookup ? 0 : undefined);

if (count && lastBlockPerAuraAuthors[index] === validator) {
count += 1;
}

return {
accountId: validator,
blockCount: count
Expand All @@ -132,7 +171,9 @@ function useSessionCommitteePerformanceImpl (sessions: number[]): SessionCommitt

if (committee) {
const validatorPerformances = committee.map((validator) => getValidatorPerformance(validator,
sessionValidatorBlockCountLookup));
sessionValidatorBlockCountLookup,
lastBlockPerAuraAuthors,
index));
const committeePerformance: SessionCommitteePerformance = {
expectedBlockCount: sessionPeriod / committee.length,
performance: validatorPerformances,
Expand All @@ -150,7 +191,7 @@ function useSessionCommitteePerformanceImpl (sessions: number[]): SessionCommitt
}));
},
// eslint-disable-next-line react-hooks/exhaustive-deps
[JSON.stringify(committees), JSON.stringify(sessionValidatorBlockCountLookups), JSON.stringify(sessions)]
[JSON.stringify(committees), JSON.stringify(sessionValidatorBlockCountLookups), JSON.stringify(sessions), JSON.stringify(lastBlockPerAuraAuthors)]
);

return committeeMemberPerformances;
Expand Down
1 change: 1 addition & 0 deletions packages/page-staking/src/Query/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ function Query ({ className }: Props): React.ReactElement<Props> {
const pastSessions = useMemo(() => {
if (currentSession && currentEra && historyDepth && minimumSessionNumber) {
const maxSessionQueryDepth = 4 * historyDepth;

const minSessionNumber = Math.max(minimumSessionNumber, currentSession - maxSessionQueryDepth);
const queryDepth = currentSession - minSessionNumber;

Expand Down

0 comments on commit 6467eb1

Please sign in to comment.