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

Commit

Permalink
Feedback
Browse files Browse the repository at this point in the history
  • Loading branch information
Incede committed May 10, 2023
1 parent 1a42e7c commit 9778678
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 63 deletions.
6 changes: 3 additions & 3 deletions framework/src/modules/random/stores/used_hash_onions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -99,17 +99,17 @@ export class UsedHashOnionsStore extends BaseOffchainStore<UsedHashOnionStoreObj
);

if (index === -1) {
originalUsedHashOnions.push(usedHashOnion);
// if newly inserted entry has count 0, then cleanup previous entries
if (usedHashOnion.count === 0) {
const newUsedHashonions = [usedHashOnion];
const newUsedHashOnions = [usedHashOnion];
await this.set(
ctx,
address,
this._filterUsedHashOnions(newUsedHashonions, finalizedHeight),
this._filterUsedHashOnions(newUsedHashOnions, finalizedHeight),
);
return;
}
originalUsedHashOnions.push(usedHashOnion);
} else {
// eslint-disable-next-line no-param-reassign
originalUsedHashOnions[index] = usedHashOnion;
Expand Down
61 changes: 1 addition & 60 deletions framework/test/unit/modules/random/module.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -261,7 +261,7 @@ describe('RandomModule', () => {

const targetValidatorAddress = address.getAddressFromLisk32Address(targetValidator.address);

it('should assign seed reveal to block header asset and update the used hash onion', async () => {
it('should assign seed reveal to block header asset, update and overwrite the used hash onion when forging the same height', async () => {
// Arrange
const blockGenerateContext: InsertAssetContext = testing.createBlockGenerateContext({
assets: assetStub,
Expand Down Expand Up @@ -304,65 +304,6 @@ describe('RandomModule', () => {
).resolves.toEqual(defaultUsedHashOnionUpdated);
});

it('should overwrite the used hash onion when forging the same height', async () => {
// Arrange
const usedHashOnionInput: UsedHashOnionStoreObject = {
usedHashOnions: [
{
count: 5,
height: 9,
},
{
count: 6,
height: 12,
},
{
count: 7,
height: 15,
},
],
};

const blockGenerateContext: InsertAssetContext = testing.createBlockGenerateContext({
assets: assetStub,
logger: testing.mocks.loggerMock,
chainID: defaultChainID,
getOffchainStore: (p1, p2) => offchainStore.getStore(p1, p2),
getMethodContext: jest.fn() as any,
getStore: jest.fn() as any,
header: { height: 15, generatorAddress: targetValidatorAddress } as any,
});
await randomModule.offchainStores
.get(UsedHashOnionsStore)
.set(blockGenerateContext, targetValidatorAddress, usedHashOnionInput);

const seed = targetValidator.hashOnion.hashes[1];
const hashes = utils.hashOnion(
Buffer.from(seed, 'hex'),
targetValidator.hashOnion.distance,
1,
);

// Act
await randomModule.init({
genesisConfig: {} as GenesisConfig,
moduleConfig: {},
});
await randomModule.insertAssets(blockGenerateContext);

// Assert
expect(assetStub.setAsset).toHaveBeenCalledTimes(1);
expect(assetStub.setAsset).toHaveBeenCalledWith(
randomModule.name,
codec.encode(blockHeaderAssetRandomModule, { seedReveal: hashes[7] }),
);
await expect(
randomModule.offchainStores
.get(UsedHashOnionsStore)
.get(blockGenerateContext, targetValidatorAddress),
).resolves.toEqual(defaultUsedHashOnionUpdated);
});

it('should remove other used hash onions except count 0 at current height if all entries in the used hash onion array have height greater than the current block height', async () => {
// Arrange
// when all entries have height more than the current block height, an entry with count 0 will be initialzed in the used hash onions array
Expand Down

0 comments on commit 9778678

Please sign in to comment.