Skip to content

Commit f1dc1e9

Browse files
gabrocheleaujochem-brouwerholgerd77acolytec3
authored
lint: add error rules for assert.ok and assert.notOk (#3927)
* monorepo: add warn rules for assert.ok and assert.notOk * monorepo: fix some assert.ok and notok * chore: move from warn to error * RLP: Move EthereumJSError form util to RLP and start using EthereumJSError in RLP (#3924) * util/rlp: move ethereumjserror to rlp * rlp: use ethereumjserror * rlp: add linter rules to not allow `new Error` (use EthereumJSError in RLP also) --------- Co-authored-by: Holger Drewes <[email protected]> * Configure lint to require .ts * Warn on js extension * disable ban-comment rule in client cli utils * block: fix remaining assert.ok * restrict js in relative imports * monorepo: more assert.ok cases * monorepo: fix remaining assertions * monorepo: fix remaining errors * mpt: fix tests * statemanager: fix failing tests * client: fix failing tests * monorepo: fix more type issues * lint: more fixes * statemanager: fix test * Update config/eslint.config.mjs Co-authored-by: Jochem Brouwer <[email protected]> * Update packages/block/test/block.spec.ts Co-authored-by: Jochem Brouwer <[email protected]> * block: fix type issue * chore: fix mpt isTrue * Test cleanup * fix another test * chore: correct wording of errors * client: adjust fullethereumservice tests * block: remove unused async * blockchain: improve async testing * vm: apply suggestions * vm: remove .only --------- Co-authored-by: Jochem Brouwer <[email protected]> Co-authored-by: Holger Drewes <[email protected]> Co-authored-by: acolytec3 <[email protected]>
1 parent 97498e4 commit f1dc1e9

File tree

132 files changed

+1434
-1509
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

132 files changed

+1434
-1509
lines changed

config/eslint.config.mjs

Lines changed: 30 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ export default [
4040
{
4141
selector: "ThrowStatement > NewExpression[callee.name='Error']",
4242
message: "Throwing default JS Errors is not allowed. Only throw `EthereumJSError` (see the util package)",
43-
}
43+
},
4444
],
4545
"no-restricted-globals": [
4646
"error",
@@ -70,7 +70,7 @@ export default [
7070
'@typescript-eslint/no-explicit-any': 'off',
7171
'@typescript-eslint/no-unsafe-function-type': 'off', // TODO: Decide if this is needed
7272
'@typescript-eslint/no-unused-expressions': 'off', // TODO: Decide if this is needed
73-
'@typescript-eslint/no-unused-vars': ['error', { argsIgnorePattern: '^_', varsIgnorePattern: '^_'}],
73+
'@typescript-eslint/no-unused-vars': ['error', { argsIgnorePattern: '^_', varsIgnorePattern: '^_' }],
7474
'@typescript-eslint/ban-ts-comment': 'warn', // TODO: We should clean up ts comments and replace with ts-expect-error
7575
'@typescript-eslint/no-empty-object-type': ['error', {
7676
allowInterfaces: 'with-single-extends',
@@ -88,7 +88,6 @@ export default [
8888
'import/default': 'off',
8989
'import/export': 'error',
9090
'import/exports-last': 'off',
91-
'import/extensions': ['error','ignorePackages'],
9291
'import/first': 'error',
9392
'import/group-exports': 'off',
9493
'import/named': 'off',
@@ -110,13 +109,25 @@ export default [
110109

111110
},
112111
},
112+
{
113+
files: ["**/*.ts"],
114+
ignores: ["**/examples/*.ts"],
115+
rules: {
116+
"no-restricted-imports": ["error", {
117+
"patterns": [{
118+
"group": ["../**/*.js", "./**/*.js"],
119+
"message": "use .ts extensions in relative imports"
120+
}]
121+
}],
122+
}
123+
},
113124
{
114125
plugins: {
115126
i: importPlugin,
116127
},
117128
files: ['**/src/**/*.ts', '**/bin/**/*.ts'],
118129
rules: {
119-
'i/no-extraneous-dependencies': 'error'
130+
'i/no-extraneous-dependencies': 'error',
120131
},
121132
},
122133
{
@@ -128,7 +139,7 @@ export default [
128139
}
129140
},
130141
{
131-
files: ['**/*.js', '**/*.cjs','**/*.cts'],
142+
files: ['**/*.js', '**/*.cjs', '**/*.cts'],
132143
rules: {
133144
'@typescript-eslint/no-require-imports': 'off',
134145
'no-undef': 'off',
@@ -158,7 +169,20 @@ export default [
158169
{
159170
files: ['packages/devp2p/src/ext/**', 'packages/client/src/ext/**', '**/test/**/*.ts',],
160171
rules: {
161-
'no-restricted-syntax': 'off'
172+
'no-restricted-syntax': 'off',
173+
"no-restricted-properties": [
174+
"error",
175+
{
176+
"object": "assert",
177+
"property": "ok",
178+
"message": "Usage of assert.ok is forbidden because it relies on truthiness."
179+
},
180+
{
181+
"object": "assert",
182+
"property": "notOk",
183+
"message": "Usage of assert.notOk is forbidden because it relies on falseness."
184+
}
185+
],
162186
},
163187
},
164188
{

packages/binarytree/test/binarytree.spec.ts

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ describe('insert', () => {
7676

7777
await tree.put(stem, [index], [value])
7878
const [retrievedValue] = await tree.get(stem, [index])
79-
assert.exists(retrievedValue, 'Retrieved value should exist')
79+
assert.isDefined(retrievedValue, 'Retrieved value should exist')
8080
assert.isTrue(
8181
equalsBytes(retrievedValue!, value),
8282
'Retrieved value should match inserted value',
@@ -113,8 +113,8 @@ describe('insert', () => {
113113
const [retrievedValue2] = await tree.get(stem2, [index2])
114114

115115
// Retrieved values should exist
116-
assert.exists(retrievedValue1, 'Value for key1 should exist')
117-
assert.exists(retrievedValue2, 'Value for key2 should exist')
116+
assert.isDefined(retrievedValue1, 'Value for key1 should exist')
117+
assert.isDefined(retrievedValue2, 'Value for key2 should exist')
118118

119119
// Check that the computed state root matches the expected hash.
120120
assert.equal(
@@ -139,7 +139,7 @@ describe('insert', () => {
139139

140140
for (let i = 0; i < suffixes.length; i++) {
141141
const [retrievedValue] = await tree.get(stem, [suffixes[i]])
142-
assert.exists(retrievedValue, `Value at suffix ${suffixes[i]} should exist`)
142+
assert.isDefined(retrievedValue, `Value at suffix ${suffixes[i]} should exist`)
143143
assert.isTrue(
144144
equalsBytes(retrievedValue!, values[i]),
145145
`Value at suffix ${suffixes[i]} should match inserted value`,
@@ -166,8 +166,8 @@ describe('insert', () => {
166166
for (let i = 0; i < suffixes1.length; i++) {
167167
const [retrievedValue1] = await tree.get(stem1, [suffixes1[i]])
168168
const [retrievedValue2] = await tree.get(stem2, [suffixes2[i]])
169-
assert.exists(retrievedValue1, `Value at suffix ${suffixes1[i]} should exist`)
170-
assert.exists(retrievedValue2, `Value at suffix ${suffixes2[i]} should exist`)
169+
assert.isDefined(retrievedValue1, `Value at suffix ${suffixes1[i]} should exist`)
170+
assert.isDefined(retrievedValue2, `Value at suffix ${suffixes2[i]} should exist`)
171171
assert.isTrue(
172172
equalsBytes(retrievedValue1!, values1[i]),
173173
`Value at suffix ${suffixes1[i]} should match inserted value`,
@@ -199,8 +199,8 @@ describe('insert', () => {
199199
const [retrievedValue1] = await tree.get(stem1, [index1])
200200
const [retrievedValue2] = await tree.get(stem2, [index2])
201201

202-
assert.exists(retrievedValue1, 'Value for key1 should exist')
203-
assert.exists(retrievedValue2, 'Value for key2 should exist')
202+
assert.isDefined(retrievedValue1, 'Value for key1 should exist')
203+
assert.isDefined(retrievedValue2, 'Value for key2 should exist')
204204
assert.isTrue(
205205
equalsBytes(retrievedValue1!, value1),
206206
'Value for key1 should match inserted value',
@@ -237,9 +237,9 @@ describe('insert', () => {
237237
const [retrievedValue2] = await tree1.get(stem2, [index2])
238238
const [retrievedValue3] = await tree1.get(stem3, [index3])
239239

240-
assert.exists(retrievedValue1, 'Value for key1 should exist')
241-
assert.exists(retrievedValue2, 'Value for key2 should exist')
242-
assert.exists(retrievedValue3, 'Value for key3 should exist')
240+
assert.isDefined(retrievedValue1, 'Value for key1 should exist')
241+
assert.isDefined(retrievedValue2, 'Value for key2 should exist')
242+
assert.isDefined(retrievedValue3, 'Value for key3 should exist')
243243
assert.isTrue(
244244
equalsBytes(retrievedValue1!, value1),
245245
'Value for key1 should match inserted value',
@@ -287,9 +287,9 @@ describe('insert', () => {
287287
const [retrievedValue2] = await tree1.get(stem2, [index2])
288288
const [retrievedValue3] = await tree1.get(stem3, [index3])
289289

290-
assert.exists(retrievedValue1, 'Value for key1 should exist')
291-
assert.exists(retrievedValue2, 'Value for key2 should exist')
292-
assert.exists(retrievedValue3, 'Value for key3 should exist')
290+
assert.isDefined(retrievedValue1, 'Value for key1 should exist')
291+
assert.isDefined(retrievedValue2, 'Value for key2 should exist')
292+
assert.isDefined(retrievedValue3, 'Value for key3 should exist')
293293
assert.isTrue(
294294
equalsBytes(retrievedValue1!, value1),
295295
'Value for key1 should match inserted value',
@@ -405,7 +405,7 @@ describe('insert', () => {
405405
const stem = hashedKey.slice(0, 31)
406406
const index = hashedKey[31]
407407
const [retrievedValue] = await tree1.get(stem, [index])
408-
assert.exists(
408+
assert.isDefined(
409409
retrievedValue,
410410
`Value for key ${bytesToHex(hashedKey)} | unhashed: ${bytesToHex(originalKey)} should exist`,
411411
)
@@ -448,7 +448,7 @@ describe('insert', () => {
448448

449449
// Ensure that we can retrieve the updated value
450450
const [retrievedValue] = await tree1.get(stemToUpdate, [indexToUpdate])
451-
assert.exists(retrievedValue, 'Updated value should exist')
451+
assert.isDefined(retrievedValue, 'Updated value should exist')
452452
assert.isTrue(
453453
equalsBytes(retrievedValue!, updatedValue),
454454
'Retrieved value should match the updated value',
@@ -481,7 +481,7 @@ describe('insert', () => {
481481
}
482482

483483
const leafValues = await dumpLeafValues(tree, tree.root())
484-
assert.exists(leafValues)
484+
assert.isDefined(leafValues)
485485
assert.equal(leafValues!.length, 100)
486486

487487
const expectedValues = keyValuePairs.map(({ value }) => bytesToHex(value)).sort()
@@ -493,7 +493,7 @@ describe('insert', () => {
493493
assert.deepEqual(actualKeys, expectedKeys)
494494

495495
const nodeHashes = await dumpNodeHashes(tree, tree.root())
496-
assert.exists(nodeHashes)
496+
assert.isDefined(nodeHashes)
497497
expect(nodeHashes!.length).toBeGreaterThan(100)
498498
assert.equal(nodeHashes![0][1], bytesToHex(tree.root()))
499499
})
@@ -513,7 +513,7 @@ describe('insert', () => {
513513

514514
const [retrievedValue] = await tree.get(stem, [index])
515515

516-
assert.exists(retrievedValue, 'Retrieved value should exist')
516+
assert.isDefined(retrievedValue, 'Retrieved value should exist')
517517
assert.isTrue(
518518
equalsBytes(retrievedValue!, value2),
519519
'Retrieved value should match the updated value',
@@ -553,7 +553,7 @@ describe('insert', () => {
553553
const [retrievedValue1] = await tree.get(stem1, [index1])
554554
const [retrievedValue2] = await tree.get(stem2, [index2])
555555

556-
assert.exists(retrievedValue1, 'Retrieved value should exist')
556+
assert.isDefined(retrievedValue1, 'Retrieved value should exist')
557557
assert.notExists(retrievedValue2, 'Deleted value should not exist')
558558
assert.isTrue(
559559
equalsBytes(initialRoot, recoveredRoot),

packages/binarytree/test/node/internalNode.spec.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,8 @@ describe('InternalBinaryNode', () => {
2222
// Verify the type
2323
assert.equal(decoded.type, BinaryNodeType.Internal)
2424
const [leftRecoveredChild, rightRecoveredChild] = (decoded as InternalBinaryNode).children
25-
assert.exists(leftRecoveredChild, 'Left child should exist')
26-
assert.exists(rightRecoveredChild, 'Right child should exist')
25+
assert.isDefined(leftRecoveredChild, 'Left child should exist')
26+
assert.isDefined(rightRecoveredChild, 'Right child should exist')
2727
assert.isTrue(
2828
equalsBytes(leftRecoveredChild!.hash, leftCanonicalChild.hash),
2929
'Left child hash should round-trip',

packages/binarytree/test/node/stemNode.spec.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,19 +40,19 @@ describe('StemBinaryNode', () => {
4040
assert.equal(recovered.values.length, 256, 'Values array should have 256 elements')
4141

4242
// Check that the non-null values round-trip.
43-
assert.exists(recovered.values[3], 'Value at index 3 should exist')
43+
assert.isDefined(recovered.values[3], 'Value at index 3 should exist')
4444
assert.isTrue(
4545
equalsBytes(recovered.values[3]!, value3),
4646
'Value at index 3 should round-trip correctly',
4747
)
4848

49-
assert.exists(recovered.values[100], 'Value at index 100 should exist')
49+
assert.isDefined(recovered.values[100], 'Value at index 100 should exist')
5050
assert.isTrue(
5151
equalsBytes(recovered.values[100]!, value100),
5252
'Value at index 100 should round-trip correctly',
5353
)
5454

55-
assert.exists(recovered.values[255], 'Value at index 255 should exist')
55+
assert.isDefined(recovered.values[255], 'Value at index 255 should exist')
5656
assert.isTrue(
5757
equalsBytes(recovered.values[255]!, value255),
5858
'Value at index 255 should round-trip correctly',

packages/block/test/block.spec.ts

Lines changed: 17 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ describe('[Block]: block functions', () => {
2929
it('should test block initialization', () => {
3030
const common = new Common({ chain: Mainnet, hardfork: Hardfork.Chainstart })
3131
const genesis = createBlock({}, { common })
32-
assert.exists(bytesToHex(genesis.hash()), 'block should initialize')
32+
assert.isDefined(bytesToHex(genesis.hash()), 'block should initialize')
3333

3434
const params = JSON.parse(JSON.stringify(paramsBlock))
3535
params['1']['minGasLimit'] = 3000 // 5000
@@ -41,28 +41,22 @@ describe('[Block]: block functions', () => {
4141
)
4242

4343
const emptyBlock = createEmptyBlock({}, { common })
44-
assert.exists(bytesToHex(emptyBlock.hash()), 'block should initialize')
44+
assert.isDefined(bytesToHex(emptyBlock.hash()), 'block should initialize')
4545

4646
// test default freeze values
4747
// also test if the options are carried over to the constructor
4848
block = createBlock({})
49-
assert.isTrue(Object.isFrozen(block), 'block should be frozen by default')
49+
assert.isFrozen(block, 'block should be frozen by default')
5050

5151
block = createBlock({}, { freeze: false })
52-
assert.ok(
53-
!Object.isFrozen(block),
54-
'block should not be frozen when freeze deactivated in options',
55-
)
52+
assert.isNotFrozen(block, 'block should not be frozen when freeze deactivated in options')
5653

5754
const rlpBlock = block.serialize()
5855
block = createBlockFromRLP(rlpBlock)
59-
assert.isTrue(Object.isFrozen(block), 'block should be frozen by default')
56+
assert.isFrozen(block, 'block should be frozen by default')
6057

6158
block = createBlockFromRLP(rlpBlock, { freeze: false })
62-
assert.ok(
63-
!Object.isFrozen(block),
64-
'block should not be frozen when freeze deactivated in options',
65-
)
59+
assert.isNotFrozen(block, 'block should not be frozen when freeze deactivated in options')
6660

6761
const zero = new Uint8Array(0)
6862
const headerArray: Uint8Array[] = []
@@ -82,13 +76,10 @@ describe('[Block]: block functions', () => {
8276
const valuesArray = <BlockBytes>[headerArray, [], []]
8377

8478
block = createBlockFromBytesArray(valuesArray, { common })
85-
assert.isTrue(Object.isFrozen(block), 'block should be frozen by default')
79+
assert.isFrozen(block, 'block should be frozen by default')
8680

8781
block = createBlockFromBytesArray(valuesArray, { common, freeze: false })
88-
assert.ok(
89-
!Object.isFrozen(block),
90-
'block should not be frozen when freeze deactivated in options',
91-
)
82+
assert.isNotFrozen(block, 'block should not be frozen when freeze deactivated in options')
9283
})
9384

9485
it('initialization -> setHardfork option', () => {
@@ -165,8 +156,8 @@ describe('[Block]: block functions', () => {
165156
})
166157

167158
async function testTransactionValidation(block: Block) {
168-
assert.ok(block.transactionsAreValid())
169-
assert.ok(block.getTransactionsValidationErrors().length === 0)
159+
assert.isTrue(block.transactionsAreValid())
160+
assert.isEmpty(block.getTransactionsValidationErrors())
170161
}
171162

172163
it('should test transaction validation - invalid tx trie', async () => {
@@ -441,13 +432,14 @@ describe('[Block]: block functions', () => {
441432
},
442433
)
443434

444-
assert.ok(
445-
blockWithDifficultyCalculation.header.difficulty > BigInt(0),
435+
assert.notEqual(
436+
blockWithDifficultyCalculation.header.difficulty,
437+
BigInt(0),
446438
'header difficulty should be set if difficulty header is given',
447439
)
448-
assert.ok(
449-
blockWithDifficultyCalculation.header.ethashCanonicalDifficulty(genesis.header) ===
450-
blockWithDifficultyCalculation.header.difficulty,
440+
assert.equal(
441+
blockWithDifficultyCalculation.header.ethashCanonicalDifficulty(genesis.header),
442+
blockWithDifficultyCalculation.header.difficulty,
451443
'header difficulty is canonical difficulty if difficulty header is given',
452444
)
453445

@@ -467,7 +459,7 @@ describe('[Block]: block functions', () => {
467459
},
468460
)
469461

470-
assert.ok(
462+
assert.isTrue(
471463
block_farAhead.header.difficulty > BigInt(0),
472464
'should allow me to provide a bogus next block to calculate difficulty on when providing a difficulty header',
473465
)

packages/block/test/clique.spec.ts

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -30,13 +30,13 @@ describe('[Header]: Clique PoA Functionality', () => {
3030
)
3131

3232
header = createBlockHeader({ extraData: new Uint8Array(97) }, { common })
33-
assert.ok(
33+
assert.isTrue(
3434
cliqueIsEpochTransition(header),
3535
'cliqueIsEpochTransition() -> should indicate an epoch transition for the genesis block',
3636
)
3737

3838
header = createBlockHeader({ number: 1, extraData: new Uint8Array(97) }, { common })
39-
assert.notOk(
39+
assert.isFalse(
4040
cliqueIsEpochTransition(header),
4141
'cliqueIsEpochTransition() -> should correctly identify a non-epoch block',
4242
)
@@ -60,7 +60,7 @@ describe('[Header]: Clique PoA Functionality', () => {
6060
)
6161

6262
header = createBlockHeader({ number: 60000, extraData: new Uint8Array(137) }, { common })
63-
assert.ok(
63+
assert.isTrue(
6464
cliqueIsEpochTransition(header),
6565
'cliqueIsEpochTransition() -> should correctly identify an epoch block',
6666
)
@@ -107,11 +107,14 @@ describe('[Header]: Clique PoA Functionality', () => {
107107
)
108108

109109
assert.equal(header.extraData.length, 97)
110-
assert.ok(cliqueVerifySignature(header, [A.address]), 'should verify signature')
111-
assert.ok(cliqueSigner(header).equals(A.address), 'should recover the correct signer address')
110+
assert.isTrue(cliqueVerifySignature(header, [A.address]), 'should verify signature')
111+
assert.isTrue(
112+
cliqueSigner(header).equals(A.address),
113+
'should recover the correct signer address',
114+
)
112115

113116
header = createBlockHeader({ extraData: new Uint8Array(97) }, { common })
114-
assert.ok(
117+
assert.isTrue(
115118
cliqueSigner(header).equals(createZeroAddress()),
116119
'should return zero address on default block',
117120
)

0 commit comments

Comments
 (0)