Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove empty rewards from cache #1935

Merged
merged 3 commits into from
Nov 24, 2019
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 35 additions & 21 deletions packages/app-staking/src/useSessionRewards.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -27,18 +27,35 @@ interface Serialized {
slashes: SerializedSlash[];
}

const MAX_BLOCKS = 2500;

function fromJSON (sessions: Serialized[]): SessionRewards[] {
return sessions.map(({ blockHash, blockNumber, isEventsEmpty, reward, sessionIndex, slashes }): SessionRewards => ({
blockHash: createType(registry, 'Hash', blockHash),
blockNumber: createType(registry, 'BlockNumber', blockNumber),
isEventsEmpty,
reward: createType(registry, 'Balance', reward),
sessionIndex: createType(registry, 'SessionIndex', sessionIndex),
slashes: slashes.map(({ accountId, amount }): Slash => ({
accountId: createType(registry, 'AccountId', accountId),
amount: createType(registry, 'Balance', amount)
let hasSome = false;
let keepAll = true;

return sessions
.map(({ blockHash, blockNumber, isEventsEmpty, reward, sessionIndex, slashes }): SessionRewards => ({
blockHash: createType(registry, 'Hash', blockHash),
blockNumber: createType(registry, 'BlockNumber', blockNumber),
isEventsEmpty,
reward: createType(registry, 'Balance', reward),
sessionIndex: createType(registry, 'SessionIndex', sessionIndex),
slashes: slashes.map(({ accountId, amount }): Slash => ({
accountId: createType(registry, 'AccountId', accountId),
amount: createType(registry, 'Balance', amount)
}))
}))
}));
.filter(({ isEventsEmpty }): boolean => {
if (!isEventsEmpty) {
// we first see if we have some data up to this point (we may not, i.e. non-archive)
hasSome = true;
} else if (hasSome) {
// if data is followed by empty, drop everything from here on
keepAll = false;
}

return keepAll;
});
}

function toJSON (sessions: SessionRewards[], maxSessions: number): Serialized[] {
Expand Down Expand Up @@ -120,38 +137,35 @@ export default function useSessionRewards (maxSessions: number): SessionRewards[

useEffect((): void => {
let workQueue = fromJSON(getCache() || []);
const savedNumber = workQueue[workQueue.length - 1]
? workQueue[workQueue.length - 1].blockNumber
: undefined;

setImmediate((): void => {
api.isReady.then(async (): Promise<void> => {
const maxSessionsStore = maxSessions + 1; // assuming first is a bust
const sessionLength = api.consts.babe?.epochDuration || new BN(500);
const eraLength = api.consts.staking.sessionsPerEra.toNumber();
const count = Math.min(sessionLength.muln(maxSessionsStore).divn(10).toNumber(), 7500);
const bestHeader = await api.rpc.chain.getHeader();
let retrieved = 0;
let toHash = bestHeader.hash;
let toNumber = bestHeader.number.unwrap().toBn();
let fromHash = api.genesisHash;
let fromNumber = bnMax(toNumber.subn(count), new BN(1));
let fromNumber = bnMax(toNumber.subn(MAX_BLOCKS), new BN(1));

while (true) {
fromHash = await api.rpc.chain.getBlockHash(fromNumber as any);
// console.log(`Updating rewards cache, #${fromNumber} -> #${toNumber}`);

const fromHash = await api.rpc.chain.getBlockHash(fromNumber as any);
const newQueue = await loadSome(api, fromHash, toHash);

retrieved += newQueue.length;
workQueue = mergeResults(workQueue, newQueue);
toHash = fromHash;
toNumber = fromNumber;
fromNumber = bnMax(toNumber.subn(count), new BN(1));
fromNumber = bnMax(toNumber.subn(MAX_BLOCKS), new BN(1));

setCache(toJSON(workQueue, maxSessionsStore));
setFiltered(workQueue.slice(-maxSessions));

const lastNumber = workQueue[workQueue.length - 1]?.blockNumber;

// we always want to retrieve at least 1 era worth of information, or we are at the start
if (!lastNumber || fromNumber.eqn(1) || (retrieved > eraLength && (workQueue.length >= maxSessionsStore) && fromNumber.lt(lastNumber))) {
if (!lastNumber || fromNumber.eqn(1) || ((workQueue.length >= maxSessionsStore) && fromNumber.lt(savedNumber || lastNumber))) {
break;
}
}
Expand Down