Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

5836compare blocks #5842

Merged
merged 7 commits into from
Feb 16, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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 numbers.

```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
```
3 changes: 3 additions & 0 deletions packages/web3-utils/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 number) or both block number params (#5842)
58 changes: 21 additions & 37 deletions packages/web3-utils/src/validation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -109,56 +109,40 @@ 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
if (blockA === 'earliest' && blockB > 0) {
return -1;
}
if (blockB === 'earliest' || blockB === 0) {
// b !== a, thus a > b
if (blockB === 'earliest' && blockA > 0) {
return 1;
}
if (blockA === 'latest' || blockA === 'safe') {
if (blockB === 'pending' || blockB === 'latest') {

Muhammad-Altabba marked this conversation as resolved.
Show resolved Hide resolved
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);
Expand Down
32 changes: 17 additions & 15 deletions packages/web3-utils/test/fixtures/validation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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],
Muhammad-Altabba marked this conversation as resolved.
Show resolved Hide resolved
[['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],
Muhammad-Altabba marked this conversation as resolved.
Show resolved Hide resolved
[['safe', 0], 1],
[[0, 'safe'], -1],
[['safe', 'pending'], -1],
[['pending', 'safe'], 1],
[['finalized', 'finalized'], 0],
[['earliest', 'finalized'], -1],
[['finalized', 0], 1],
[['finalized', 'pending'], -1],
Muhammad-Altabba marked this conversation as resolved.
Show resolved Hide resolved
[[0, 'finalized'], -1],
[['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.');
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],
[['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],
Muhammad-Altabba marked this conversation as resolved.
Show resolved Hide resolved
];

export const isBloomValidData: [any, true][] = [
Expand Down
30 changes: 0 additions & 30 deletions packages/web3-validator/test/fixtures/validation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down