Skip to content
This repository has been archived by the owner on Jun 11, 2024. It is now read-only.

Commit

Permalink
✅ Refactor eligibleDelegate store and add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
shuse2 committed Nov 3, 2022
1 parent 0dcc926 commit 9bf1b0b
Show file tree
Hide file tree
Showing 2 changed files with 96 additions and 5 deletions.
11 changes: 6 additions & 5 deletions framework/src/modules/dpos_v2/stores/eligible_delegates.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ export class EligibleDelegatesStore extends BaseStore<EligibleDelegate> {
public async getTop(context: ImmutableStoreGetter, count: number) {
return this.iterate(context, {
gte: Buffer.alloc(KEY_LENGTH, 0),
lte: Buffer.alloc(8 + 20, 255),
lte: Buffer.alloc(KEY_LENGTH, 255),
limit: count,
reverse: true,
});
Expand All @@ -63,7 +63,7 @@ export class EligibleDelegatesStore extends BaseStore<EligibleDelegate> {
public async getAll(context: ImmutableStoreGetter) {
return this.iterate(context, {
gte: Buffer.alloc(KEY_LENGTH, 0),
lte: Buffer.alloc(8 + 20, 255),
lte: Buffer.alloc(KEY_LENGTH, 255),
reverse: true,
});
}
Expand All @@ -83,6 +83,10 @@ export class EligibleDelegatesStore extends BaseStore<EligibleDelegate> {
const oldKey = this.getKey(address, oldWeight);
await this.del(context, oldKey);

if (delegate.isBanned) {
return;
}

const newWeight = getDelegateWeight(
BigInt(this._config.factorSelfVotes),
delegate.selfVotes,
Expand All @@ -92,9 +96,6 @@ export class EligibleDelegatesStore extends BaseStore<EligibleDelegate> {
return;
}

if (delegate.isBanned) {
return;
}
const lastPomHeight = delegate.pomHeights.length
? delegate.pomHeights[delegate.pomHeights.length - 1]
: 0;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@
* Removal or modification of this copyright notice is prohibited.
*/

import { utils } from '@liskhq/lisk-cryptography';
import { StoreGetter } from '../../../../../src/modules/base_store';
import { defaultConfig } from '../../../../../src/modules/dpos_v2/constants';
import { EligibleDelegatesStore } from '../../../../../src/modules/dpos_v2/stores/eligible_delegates';
import { PrefixedStateReadWriter } from '../../../../../src/state_machine/prefixed_state_read_writer';
import { InMemoryPrefixedStateDB } from '../../../../../src/testing/in_memory_prefixed_state';
Expand Down Expand Up @@ -40,11 +42,28 @@ describe('EligibleDelegatesStore', () => {
lastPomHeight: 978,
},
];
const defaultDelegateAccount = {
name: 'rand',
commission: 0,
consecutiveMissedBlocks: 0,
isBanned: false,
lastCommissionIncreaseHeight: 0,
lastGeneratedHeight: 0,
pomHeights: [],
selfVotes: BigInt(200000000000),
totalVotesReceived: BigInt(250000000000),
sharingCoefficients: [],
};

beforeEach(async () => {
stateStore = new PrefixedStateReadWriter(new InMemoryPrefixedStateDB());
context = createStoreGetter(stateStore);
eligibleDelegatesStore = new EligibleDelegatesStore('dpos');
eligibleDelegatesStore.init({
...defaultConfig,
minWeightStandby: BigInt(defaultConfig.minWeightStandby),
tokenIDDPoS: Buffer.from(defaultConfig.tokenIDDPoS, 'hex'),
});
for (const eligibleDelegate of eligibleDelegates) {
const key = eligibleDelegatesStore.getKey(
eligibleDelegate.address,
Expand Down Expand Up @@ -76,4 +95,75 @@ describe('EligibleDelegatesStore', () => {
expect(returnedValue[2].value.lastPomHeight).toEqual(278);
});
});

describe('splitKey', () => {
it('should return address and weight', () => {
const address = utils.getRandomBytes(20);
const key = eligibleDelegatesStore.getKey(address, BigInt(999));
expect(eligibleDelegatesStore.splitKey(key)).toEqual([address, BigInt(999)]);
});
});

describe('update', () => {
it('should delete original key and not insert if delegate is banned', async () => {
await eligibleDelegatesStore.update(context, eligibleDelegates[0].address, BigInt(10), {
...defaultDelegateAccount,
isBanned: true,
});
await expect(
eligibleDelegatesStore.has(
context,
eligibleDelegatesStore.getKey(
eligibleDelegates[0].address,
defaultDelegateAccount.totalVotesReceived,
),
),
).resolves.toBeFalse();
});

it('should delete original key and not insert if delegate does not have minWeight', async () => {
await eligibleDelegatesStore.update(context, eligibleDelegates[0].address, BigInt(10), {
...defaultDelegateAccount,
selfVotes: BigInt(0),
});

await expect(
eligibleDelegatesStore.has(
context,
eligibleDelegatesStore.getKey(eligibleDelegates[0].address, BigInt(0)),
),
).resolves.toBeFalse();
});

it('should insert new key with latest pomHeight', async () => {
await eligibleDelegatesStore.update(context, eligibleDelegates[0].address, BigInt(10), {
...defaultDelegateAccount,
pomHeights: [10, 20, 30],
});
await expect(
eligibleDelegatesStore.get(
context,
eligibleDelegatesStore.getKey(
eligibleDelegates[0].address,
defaultDelegateAccount.totalVotesReceived,
),
),
).resolves.toEqual({ lastPomHeight: 30 });
});

it('should insert new key with 0 if delegate does not have pomHeights', async () => {
await eligibleDelegatesStore.update(context, eligibleDelegates[0].address, BigInt(10), {
...defaultDelegateAccount,
});
await expect(
eligibleDelegatesStore.get(
context,
eligibleDelegatesStore.getKey(
eligibleDelegates[0].address,
defaultDelegateAccount.totalVotesReceived,
),
),
).resolves.toEqual({ lastPomHeight: 0 });
});
});
});

0 comments on commit 9bf1b0b

Please sign in to comment.