This repository has been archived by the owner on Jun 11, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 455
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
POM Plugin contradicting header validation to the plugin (#8861)
* ♻️ Adds contradicting header validation to the plugin instead of invoking chain_areHeadersContradicting endpoint * ♻️ ✅ * 🚚 ♻️ ✅ get_contradicting_block_header.spec.ts to db.spec.ts --------- Co-authored-by: !shan <ishantiw.quasar@gmail.com>
- Loading branch information
Showing
8 changed files
with
143 additions
and
29 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,7 @@ | ||
/* eslint-disable @typescript-eslint/no-empty-function */ | ||
import { ReportMisbehaviorPlugin } from '@liskhq/lisk-framework-report-misbehavior-plugin'; | ||
import { Application } from 'lisk-sdk'; | ||
|
||
export const registerPlugins = (_app: Application): void => {}; | ||
export const registerPlugins = (_app: Application): void => { | ||
_app.registerPlugin(new ReportMisbehaviorPlugin()); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
114 changes: 114 additions & 0 deletions
114
framework-plugins/lisk-framework-report-misbehavior-plugin/test/unit/db.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,114 @@ | ||
/* | ||
* Copyright © 2023 Lisk Foundation | ||
* | ||
* See the LICENSE file at the top-level directory of this distribution | ||
* for licensing information. | ||
* | ||
* Unless otherwise agreed in a custom licensing agreement with the Lisk Foundation, | ||
* no part of this software, including this file, may be copied, modified, | ||
* propagated, or distributed except according to the terms contained in the | ||
* LICENSE file. | ||
* | ||
* Removal or modification of this copyright notice is prohibited. | ||
*/ | ||
|
||
import { rmSync } from 'fs-extra'; | ||
import * as sdk from 'lisk-sdk'; | ||
import { chain, db as liskDB, blockHeaderSchema, codec, BlockHeader } from 'lisk-sdk'; | ||
import { getContradictingBlockHeader, saveBlockHeaders } from '../../src/db'; | ||
|
||
describe('db', () => { | ||
const dbPath = 'test-lisk-framework-report-misbehavior-plugin.db'; | ||
const db = new liskDB.Database(dbPath); | ||
|
||
const random20Bytes = Buffer.from('40ff452fae2affe6eeef3c30e53e9eac35a1bc43', 'hex'); | ||
const random32Bytes = Buffer.from( | ||
'3d1b5dd1ef4ff7b22359598ebdf58966a51adcc03e02ad356632743e65898990', | ||
'hex', | ||
); | ||
const header1 = new chain.BlockHeader({ | ||
height: 100, | ||
aggregateCommit: { | ||
aggregationBits: Buffer.alloc(0), | ||
certificateSignature: Buffer.alloc(0), | ||
height: 0, | ||
}, | ||
impliesMaxPrevotes: false, | ||
generatorAddress: random20Bytes, | ||
maxHeightGenerated: 0, | ||
maxHeightPrevoted: 50, | ||
previousBlockID: random32Bytes, | ||
timestamp: 100, | ||
version: 2, | ||
assetRoot: random32Bytes, | ||
eventRoot: random32Bytes, | ||
stateRoot: random32Bytes, | ||
transactionRoot: random32Bytes, | ||
validatorsHash: random32Bytes, | ||
signature: random32Bytes, | ||
}); | ||
|
||
const headerBytes1 = codec.encode(blockHeaderSchema, header1); | ||
|
||
afterAll(async () => { | ||
await db.clear(); | ||
|
||
db.close(); | ||
|
||
rmSync(dbPath, { | ||
recursive: true, | ||
force: true, | ||
}); | ||
}); | ||
|
||
describe('getContradictingBlockHeader', () => { | ||
const headerClone = BlockHeader.fromBytes(headerBytes1); | ||
|
||
const header2 = new chain.BlockHeader({ | ||
id: Buffer.from('ff', 'hex'), | ||
height: 100, | ||
aggregateCommit: { | ||
aggregationBits: Buffer.alloc(0), | ||
certificateSignature: Buffer.alloc(0), | ||
height: 0, | ||
}, | ||
impliesMaxPrevotes: false, | ||
generatorAddress: random20Bytes, | ||
maxHeightGenerated: 0, | ||
maxHeightPrevoted: 32, | ||
previousBlockID: random32Bytes, | ||
timestamp: 100, | ||
version: 2, | ||
assetRoot: random32Bytes, | ||
eventRoot: random32Bytes, | ||
stateRoot: random32Bytes, | ||
transactionRoot: random32Bytes, | ||
validatorsHash: random32Bytes, | ||
signature: random32Bytes, | ||
}); | ||
|
||
beforeEach(async () => { | ||
await saveBlockHeaders(db, headerBytes1); | ||
}); | ||
|
||
it('should return undefined if plugin database is empty', async () => { | ||
await expect(getContradictingBlockHeader(db, headerClone)).resolves.toBeUndefined(); | ||
}); | ||
|
||
it('should return undefined if block headers have same id', async () => { | ||
await expect(getContradictingBlockHeader(db, headerClone)).resolves.toBeUndefined(); | ||
}); | ||
|
||
it('should return undefined if block headers are not contradicting', async () => { | ||
jest.spyOn(sdk, 'areDistinctHeadersContradicting').mockImplementation(() => false); | ||
|
||
await expect(getContradictingBlockHeader(db, header2)).resolves.toBeUndefined(); | ||
}); | ||
|
||
it('should return the block in plugin db if blocks are contradicting', async () => { | ||
jest.spyOn(sdk, 'areDistinctHeadersContradicting').mockImplementation(() => true); | ||
|
||
await expect(getContradictingBlockHeader(db, header2)).resolves.toEqual(headerClone); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters