From 94795736c2de6ee596958757520d2aa0de56bb82 Mon Sep 17 00:00:00 2001 From: jdevcs Date: Wed, 15 Feb 2023 13:08:43 +0100 Subject: [PATCH 1/7] updated compare blocks --- packages/web3-utils/src/validation.ts | 60 +++++++++------------------ 1 file changed, 19 insertions(+), 41 deletions(-) diff --git a/packages/web3-utils/src/validation.ts b/packages/web3-utils/src/validation.ts index 9aebbcc3814..3da368c186f 100644 --- a/packages/web3-utils/src/validation.ts +++ b/packages/web3-utils/src/validation.ts @@ -30,7 +30,7 @@ import { isNullish as isNullishValidator, isBlockTag, } from 'web3-validator'; -import { BlockNumberOrTag } from 'web3-types'; +import { BlockNumberOrTag, BlockTags } from 'web3-types'; /** * @deprecated Will be removed in next release. Please use `web3-validator` package instead. @@ -109,56 +109,34 @@ export const isTopicInBloom = isTopicInBloomValidator; * Returns -1 if a \< b, returns 1 if a \> b and returns 0 if a == b */ export const compareBlockNumbers = (blockA: BlockNumberOrTag, blockB: BlockNumberOrTag) => { - // string validation - if (blockA === 'genesis' || blockB === 'genesis') - throw new InvalidBlockError('Genesis tag not supported'); // for more specific error message - if (typeof blockA === 'string' && !isBlockTag(blockA)) throw new InvalidBlockError(blockA); - if (typeof blockB === 'string' && !isBlockTag(blockB)) throw new InvalidBlockError(blockB); - - // Increasing order: earliest, finalized , safe, latest, pending - // safe vs block-num cant be compared as block number provided can be on left or right side of safe tag, until safe tag block number is extracted and compared + const isABlockTag = typeof blockA === 'string' && isBlockTag(blockA); + const isBBlockTag = typeof blockB === 'string' && isBlockTag(blockB); if ( blockA === blockB || - ((blockA === 'earliest' || blockA === 0) && (blockB === 'earliest' || blockB === 0)) + ((blockA === 'earliest' || blockA === 0) && (blockB === 'earliest' || blockB === 0)) // only exception compare blocktag with number ) { return 0; } - if (blockA === 'earliest' || blockA === 0) { - // b !== a, thus a < b - return -1; - } - if (blockB === 'earliest' || blockB === 0) { - // b !== a, thus a > b - return 1; - } - if (blockA === 'latest' || blockA === 'safe') { - if (blockB === 'pending' || blockB === 'latest') { + + if (isABlockTag && isBBlockTag) { + // Increasing order: earliest, finalized , safe, latest, pending + const tagsOrder = { + [BlockTags.EARLIEST as string]: 1, + [BlockTags.FINALIZED as string]: 2, + [BlockTags.SAFE as string]: 3, + [BlockTags.LATEST as string]: 4, + [BlockTags.PENDING as string]: 5, + }; + + if (tagsOrder[blockA] < tagsOrder[blockB]) { return -1; } - // b !== ("pending" OR "latest"), thus a > b - return 1; - } - if (blockB === 'latest' || blockB === 'safe') { - if (blockA === 'pending' || blockA === 'latest') { - return 1; - } - // b !== ("pending" OR "latest"), thus a > b - return -1; - } - if (blockA === 'pending') { - // b (== OR <) "latest", thus a > b + return 1; } - if (blockB === 'pending') { - return -1; - } - - if (blockA === 'finalized' || blockB === 'finalized') { - // either a or b is "finalized" and the other one did not fall into any of the conditions above, so the other one is a number - throw new InvalidBlockError( - `Cannot compare finalized tag with ${blockA === 'finalized' ? blockB : blockA}`, - ); + if ((isABlockTag && !isBBlockTag) || (!isABlockTag && isBBlockTag)) { + throw new InvalidBlockError('Cannot compare blocktag with provided non-blocktag input.'); } const bigIntA = BigInt(blockA); From 4127bf61203ed159dfbc0e3e0aa13f34acdc4561 Mon Sep 17 00:00:00 2001 From: jdevcs Date: Wed, 15 Feb 2023 13:09:16 +0100 Subject: [PATCH 2/7] updated tests --- .../web3-utils/test/fixtures/validation.ts | 32 ++++++++++--------- .../test/fixtures/validation.ts | 30 ----------------- 2 files changed, 17 insertions(+), 45 deletions(-) diff --git a/packages/web3-utils/test/fixtures/validation.ts b/packages/web3-utils/test/fixtures/validation.ts index b7f194fcfad..ce7a4d2ae37 100644 --- a/packages/web3-utils/test/fixtures/validation.ts +++ b/packages/web3-utils/test/fixtures/validation.ts @@ -28,41 +28,43 @@ export const compareBlockNumbersValidData: [[Numbers, Numbers], number][] = [ [[1, BigInt(1)], 0], [[1, BigInt(2)], -1], [[2, BigInt(1)], 1], - [['earliest', 0], 0], + [[0, 'earliest'], 0], + [['earliest', 'earliest'], 0], [['pending', 'pending'], 0], [['latest', 'latest'], 0], - [['earliest', 2], -1], [['earliest', 'pending'], -1], [[BigInt('9007199254740992'), BigInt('9007199254740991')], 1], [[13532346, 13532300], 1], [['pending', 'latest'], 1], - [['latest', 0], 1], - [['latest', BigInt(1)], 1], - [['pending', 0], 1], - [['pending', BigInt(1)], 1], [['safe', 'safe'], 0], [['earliest', 'safe'], -1], - [['safe', 0], 1], - [[0, 'safe'], -1], [['safe', 'pending'], -1], [['pending', 'safe'], 1], [['finalized', 'finalized'], 0], [['earliest', 'finalized'], -1], - [['finalized', 0], 1], [['finalized', 'pending'], -1], - [[0, 'finalized'], -1], [['pending', 'finalized'], 1], [['safe', 'latest'], -1], [['latest', 'safe'], 1], ]; +const errorObj = new InvalidBlockError('Cannot compare blocktag with provided non-blocktag input.'); export const compareBlockNumbersInvalidData: [[Numbers, Numbers], InvalidBlockError][] = [ - [['pending', 'unknown'], new InvalidBlockError('unknown')], - [['', 'pending'], new InvalidBlockError('')], - [[22, 'finalized'], new InvalidBlockError('Cannot compare finalized tag with 22')], - [['finalized', 22], new InvalidBlockError('Cannot compare finalized tag with 22')], - [['genesis', 'finalized'], new InvalidBlockError('Genesis tag not supported')], + [['pending', 'unknown'], errorObj], + [['', 'pending'], errorObj], + [[22, 'finalized'], errorObj], + [['finalized', 22], errorObj], + [['earliest', 2], errorObj], + [[2, 'earliest'], errorObj], + [['latest', 110], errorObj], + [[222, 'latest'], errorObj], + [['pending', 230], errorObj], + [[10000, 'pending'], errorObj], + [['latest', BigInt(1)], errorObj], + [['pending', BigInt(1)], errorObj], + [['safe', 0], errorObj], + [[0, 'safe'], errorObj], ]; export const isBloomValidData: [any, true][] = [ diff --git a/packages/web3-validator/test/fixtures/validation.ts b/packages/web3-validator/test/fixtures/validation.ts index 39177e59e71..f93d47c5fec 100644 --- a/packages/web3-validator/test/fixtures/validation.ts +++ b/packages/web3-validator/test/fixtures/validation.ts @@ -238,36 +238,6 @@ export const invalidAddressData: any[] = [ '-0x407d73d8a49eeb85d32cf465507dd71d507100c1', ]; -export const compareBlockNumbersValidData: [[any, any], number][] = [ - [[1, 1], 0], - [[1, 2], -1], - [[2, 1], 1], - [[BigInt(1), BigInt(1)], 0], - [[BigInt(1), BigInt(2)], -1], - [[BigInt(2), BigInt(1)], 1], - [[1, BigInt(1)], 0], - [[1, BigInt(2)], -1], - [[2, BigInt(1)], 1], - [['genesis', 'earliest'], 0], - [['genesis', 0], 0], - [['earliest', 0], 0], - [['pending', 'pending'], 0], - [['latest', 'latest'], 0], - [['earliest', 2], -1], - [['earliest', BigInt(2)], -1], - [['earliest', 'pending'], -1], - [['genesis', 2], -1], - [['genesis', 'latest'], -1], - [['genesis', 'pending'], -1], - [[BigInt('9007199254740992'), BigInt('9007199254740991')], 1], - [[13532346, 13532300], 1], - [['pending', 'latest'], 1], - [['latest', 0], 1], - [['latest', BigInt(1)], 1], - [['pending', 0], 1], - [['pending', BigInt(1)], 1], -]; - export const validBloomData: any[] = [ '0x00000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000000000000200000000000000000000000000008000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000000000000000000000004000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000010000000000000000000000000000000000010000000000402000000000000000000000020000010000000000000000000000000000000000000000000000000000000000000', '0x00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000', From 36f601993a792f98250ed436ff3372a3bc94b1d3 Mon Sep 17 00:00:00 2001 From: jdevcs Date: Wed, 15 Feb 2023 13:10:19 +0100 Subject: [PATCH 3/7] change log update --- packages/web3-utils/CHANGELOG.md | 3 +++ 1 file changed, 3 insertions(+) diff --git a/packages/web3-utils/CHANGELOG.md b/packages/web3-utils/CHANGELOG.md index f42c77cdd61..8f2c37cd421 100644 --- a/packages/web3-utils/CHANGELOG.md +++ b/packages/web3-utils/CHANGELOG.md @@ -83,3 +83,6 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - Added support of `safe` and `finalized` block tags (#5823) +### Changed + +- `compareBlockNumbers` function now only supports comparison of both blocktags params ( except `earliest` vs 0) or both block number params From 52009f50ffe6a958c79b1db9b1fb235f19887264 Mon Sep 17 00:00:00 2001 From: jdevcs Date: Wed, 15 Feb 2023 13:31:40 +0100 Subject: [PATCH 4/7] migration guide update --- .../web3_utils_migration_guide.md | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/docs/docs/guides/web3_migration_guide/web3_utils_migration_guide.md b/docs/docs/guides/web3_migration_guide/web3_utils_migration_guide.md index 093eed34f4c..488cdce9b24 100644 --- a/docs/docs/guides/web3_migration_guide/web3_utils_migration_guide.md +++ b/docs/docs/guides/web3_migration_guide/web3_utils_migration_guide.md @@ -62,3 +62,15 @@ isHex('-0x'); // in 1.x used to return `true`. But changed in 4.x to return `fal isHexStrict('-0x'); // in 1.x used to return `true`. But changed in 4.x to return `false` // `false` ``` + +## Other functions + +`compareBlockNumbers` now accepts either both block tags or both block numbers for comparison as parameters. The only exception is comparison of block tag `earliest` with 0. + +```ts +compareBlockNumbers('earliest', 'safe'); // its valid comparison, and it will return `-1` + +compareBlockNumbers(8692, 2); // its valid comparison, and it will return `1` + +compareBlockNumbers('latest', 500); // in 1.x it used to return `1`, but now it will throw error InvalidBlockError +``` From 74fef119dd71a5c411c1341eb677761cc39a4d1d Mon Sep 17 00:00:00 2001 From: jdevcs Date: Thu, 16 Feb 2023 12:37:54 +0100 Subject: [PATCH 5/7] earliest comp with num --- .../web3_migration_guide/web3_utils_migration_guide.md | 2 +- packages/web3-utils/src/validation.ts | 6 ++++++ packages/web3-utils/test/fixtures/validation.ts | 4 ++-- 3 files changed, 9 insertions(+), 3 deletions(-) diff --git a/docs/docs/guides/web3_migration_guide/web3_utils_migration_guide.md b/docs/docs/guides/web3_migration_guide/web3_utils_migration_guide.md index 488cdce9b24..ab12bb31214 100644 --- a/docs/docs/guides/web3_migration_guide/web3_utils_migration_guide.md +++ b/docs/docs/guides/web3_migration_guide/web3_utils_migration_guide.md @@ -65,7 +65,7 @@ isHexStrict('-0x'); // in 1.x used to return `true`. But changed in 4.x to retur ## Other functions -`compareBlockNumbers` now accepts either both block tags or both block numbers for comparison as parameters. The only exception is comparison of block tag `earliest` with 0. +`compareBlockNumbers` now accepts either both block tags or both block numbers for comparison as parameters. The only exception is comparison of block tag `earliest` with numbers. ```ts compareBlockNumbers('earliest', 'safe'); // its valid comparison, and it will return `-1` diff --git a/packages/web3-utils/src/validation.ts b/packages/web3-utils/src/validation.ts index 3da368c186f..725e2723494 100644 --- a/packages/web3-utils/src/validation.ts +++ b/packages/web3-utils/src/validation.ts @@ -118,6 +118,12 @@ export const compareBlockNumbers = (blockA: BlockNumberOrTag, blockB: BlockNumbe ) { return 0; } + if (blockA === 'earliest' && blockB > 0) { + return -1; + } + if (blockB === 'earliest' && blockA > 0) { + return 1; + } if (isABlockTag && isBBlockTag) { // Increasing order: earliest, finalized , safe, latest, pending diff --git a/packages/web3-utils/test/fixtures/validation.ts b/packages/web3-utils/test/fixtures/validation.ts index ce7a4d2ae37..ec88e4fcb12 100644 --- a/packages/web3-utils/test/fixtures/validation.ts +++ b/packages/web3-utils/test/fixtures/validation.ts @@ -47,6 +47,8 @@ export const compareBlockNumbersValidData: [[Numbers, Numbers], number][] = [ [['pending', 'finalized'], 1], [['safe', 'latest'], -1], [['latest', 'safe'], 1], + [['earliest', 2], -1], + [[2, 'earliest'], 1], ]; const errorObj = new InvalidBlockError('Cannot compare blocktag with provided non-blocktag input.'); @@ -55,8 +57,6 @@ export const compareBlockNumbersInvalidData: [[Numbers, Numbers], InvalidBlockEr [['', 'pending'], errorObj], [[22, 'finalized'], errorObj], [['finalized', 22], errorObj], - [['earliest', 2], errorObj], - [[2, 'earliest'], errorObj], [['latest', 110], errorObj], [[222, 'latest'], errorObj], [['pending', 230], errorObj], From 5479d31ab85855238982f13b336ea35d7aa7947a Mon Sep 17 00:00:00 2001 From: jdevcs Date: Thu, 16 Feb 2023 14:06:15 +0100 Subject: [PATCH 6/7] change log update --- packages/web3-utils/CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/web3-utils/CHANGELOG.md b/packages/web3-utils/CHANGELOG.md index 8f2c37cd421..12e983a1cd3 100644 --- a/packages/web3-utils/CHANGELOG.md +++ b/packages/web3-utils/CHANGELOG.md @@ -85,4 +85,4 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ### Changed -- `compareBlockNumbers` function now only supports comparison of both blocktags params ( except `earliest` vs 0) or both block number params +- `compareBlockNumbers` function now only supports comparison of both blocktags params ( except `earliest` vs number) or both block number params From 8dfd07840605ca56119b38696a06bf1a1c360ef2 Mon Sep 17 00:00:00 2001 From: jdevcs Date: Thu, 16 Feb 2023 14:10:39 +0100 Subject: [PATCH 7/7] changelog update --- packages/web3-utils/CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/web3-utils/CHANGELOG.md b/packages/web3-utils/CHANGELOG.md index 12e983a1cd3..80e34a11587 100644 --- a/packages/web3-utils/CHANGELOG.md +++ b/packages/web3-utils/CHANGELOG.md @@ -85,4 +85,4 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ### Changed -- `compareBlockNumbers` function now only supports comparison of both blocktags params ( except `earliest` vs number) or both block number params +- `compareBlockNumbers` function now only supports comparison of both blocktags params ( except `earliest` vs number) or both block number params (#5842)