Skip to content

Commit

Permalink
checkNetwork fix (#5988)
Browse files Browse the repository at this point in the history
* bug fix ens check nw

* changelog

* changelog updates

* tests updated
  • Loading branch information
jdevcs authored Apr 5, 2023
1 parent 8463f6b commit 8da5d83
Show file tree
Hide file tree
Showing 10 changed files with 60 additions and 42 deletions.
26 changes: 25 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -1321,75 +1321,93 @@ should use 4.0.1-alpha.0 for testing.

#### web3

- Added source files (#5956)
- Added hybrid build (ESM and CJS) of library (#5904)

#### web3-core

- Added hybrid build (ESM and CJS) of library (#5904)
- Added source files (#5956)

#### web3-errors

- Added hybrid build (ESM and CJS) of library (#5904)
- Added source files (#5956)

#### web3-eth

- Added hybrid build (ESM and CJS) of library (#5904)
- Added source files (#5956)

#### web3-eth-abi

- Added hybrid build (ESM and CJS) of library (#5904)
- Added source files (#5956)

#### web3-eth-accounts

- Added source files (#5956)
- Added hybrid build (ESM and CJS) of library (#5904)

#### web3-eth-contract

- `input` is now an acceptable property for `ContractInitOptions` in place of `data` (either can be used, but `input` is used withing the `Contract` class) (#5915)
- Added source files (#5956)
- Added hybrid build (ESM and CJS) of library (#5904)
- `input` is now an acceptable property for `ContractInitOptions` in place of `data` (either can be used, but `input` is used withing the

#### web3-eth-ens

- Added source files (#5956)
- Added hybrid build (ESM and CJS) of library (#5904)

#### web3-eth-iban

- Added source files (#5956)
- Added hybrid build (ESM and CJS) of library (#5904)

#### web3-eth-personal

- Added source files (#5956)
- Added hybrid build (ESM and CJS) of library (#5904)

#### web3-net

- Added source files (#5956)
- Added hybrid build (ESM and CJS) of library (#5904)

#### web3-providers-http

- Added source files (#5956)
- Added hybrid build (ESM and CJS) of library (#5904)

#### web3-providers-ipc

- Added source files (#5956)
- Added hybrid build (ESM and CJS) of library (#5904)

#### web3-providers-ws

- Added source files (#5956)
- Added hybrid build (ESM and CJS) of library (#5904)

#### web3-rpc-methods

- Added source files (#5956)
- Added hybrid build (ESM and CJS) of library (#5904)

#### web3-types

- Added hybrid build (ESM and CJS) of library (#5904)
- Added source files (#5956)

#### web3-utils

- Added source files (#5956)
- Added hybrid build (ESM and CJS) of library (#5904)

#### web3-validator

- Added source files (#5956)
- Added hybrid build (ESM and CJS) of library (#5904)

### Removed
Expand All @@ -1405,3 +1423,9 @@ should use 4.0.1-alpha.0 for testing.
#### web3-eth-contract

- `data` was removed as a property of `ContractOptions` type (#5915)

### Fixed

#### web3-eth-ens

- Bug fix of `checkNetwork` in ENS (#5988)
5 changes: 1 addition & 4 deletions packages/web3-core/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
### Added

- Added hybrid build (ESM and CJS) of library (#5904)
- Added source files (#5956)

### Changed

Expand All @@ -96,7 +97,3 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
### Removed

- `getConfig` method from `Web3Config` class, `config` is now public and accessible using `Web3Config.config` (#5950)

### Added

- Added source files (#5956)
5 changes: 1 addition & 4 deletions packages/web3-errors/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -91,11 +91,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
### Added

- Added hybrid build (ESM and CJS) of library (#5904)
- Added source files (#5956)

### Changed

- `gasLimit` is no longer accepted as a parameter for `MissingGasError` and `TransactionGasMismatchError, and is also no longer included in error message (#5915)

### Added

- Added source files (#5956)
5 changes: 1 addition & 4 deletions packages/web3-eth-abi/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -93,11 +93,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
### Added

- Added hybrid build (ESM and CJS) of library (#5904)
- Added source files (#5956)

### Removed

- Removed `formatDecodedObject` function (#5934)

### Added

- Added source files (#5956)
4 changes: 4 additions & 0 deletions packages/web3-eth-ens/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -77,3 +77,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

- Added source files (#5956)
- Added hybrid build (ESM and CJS) of library (#5904)

### Fixed

- Bug fix of `checkNetwork` in ENS (#5988)
5 changes: 5 additions & 0 deletions packages/web3-eth-ens/src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,3 +47,8 @@ export const registryAddresses: { [T: string]: string } = {
main: '0x00000000000C2E074eC69A0dFb2997BA6C7d2e1e',
goerli: '0x00000000000C2E074eC69A0dFb2997BA6C7d2e1e',
};

export const networkIds: { [T: string]: string } = {
'0x1': 'main',
'0x5': 'goerli',
};
11 changes: 5 additions & 6 deletions packages/web3-eth-ens/src/ens.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ You should have received a copy of the GNU Lesser General Public License
along with web3.js. If not, see <http://www.gnu.org/licenses/>.
*/

import { getBlock } from 'web3-eth';
import { isSyncing } from 'web3-eth';
import {
RevertInstructionError,
ENSNetworkNotSyncedError,
Expand All @@ -35,7 +35,7 @@ import { NonPayableCallOptions, Contract } from 'web3-eth-contract';

import { PublicResolverAbi } from './abi/ens/PublicResolver';
import { Registry } from './registry';
import { registryAddresses } from './config';
import { networkIds, registryAddresses } from './config';
import { Resolver } from './resolver';

/**
Expand Down Expand Up @@ -449,10 +449,9 @@ export class ENS extends Web3Context<EthExecutionAPI & Web3NetAPI> {
public async checkNetwork() {
const now = Date.now() / 1000;
if (!this._lastSyncCheck || now - this._lastSyncCheck > 3600) {
const block = await getBlock(this, 'latest', false, DEFAULT_RETURN_FORMAT);
const headAge = BigInt(now) - BigInt(block.timestamp);
const syncInfo = await isSyncing(this);

if (headAge > 3600) {
if (!(typeof syncInfo === 'boolean' && !syncInfo)) {
throw new ENSNetworkNotSyncedError();
}

Expand All @@ -466,7 +465,7 @@ export class ENS extends Web3Context<EthExecutionAPI & Web3NetAPI> {
...DEFAULT_RETURN_FORMAT,
number: FMT_NUMBER.HEX,
}); // get the network from provider
const addr = registryAddresses[networkType];
const addr = registryAddresses[networkIds[networkType]];

if (typeof addr === 'undefined') {
throw new ENSUnsupportedNetworkError(networkType);
Expand Down
31 changes: 16 additions & 15 deletions packages/web3-eth-ens/test/unit/ens.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ import { Web3Context, Web3ContextObject, Web3PromiEvent } from 'web3-core';
import { Contract } from 'web3-eth-contract';
import { ENSNetworkNotSyncedError, ENSUnsupportedNetworkError } from 'web3-errors';
import { sha3Raw, DEFAULT_RETURN_FORMAT } from 'web3-utils';
import { Block } from 'web3-types';
import { registryAddresses } from '../../src/config';
import { PublicResolverAbi } from '../../src/abi/ens/PublicResolver';

Expand All @@ -29,13 +28,13 @@ jest.useFakeTimers().setSystemTime(new Date('2020-01-01'));

jest.mock('web3-eth', () => ({
__esModule: true,
getBlock: jest.fn(),
isSyncing: jest.fn(),
}));

// eslint-disable-next-line @typescript-eslint/no-require-imports, @typescript-eslint/no-var-requires
const { getBlock } = require('web3-eth');
const { isSyncing } = require('web3-eth');

const expectedNetworkId = 'main';
const expectedNetworkId = '0x1';
jest.mock('web3-net', () => ({
getId: jest.fn(),
}));
Expand Down Expand Up @@ -381,17 +380,19 @@ describe('ens', () => {
// eslint-disable-next-line @typescript-eslint/no-unsafe-call
getId.mockReset();
// eslint-disable-next-line @typescript-eslint/no-unsafe-call
getBlock.mockReset();
isSyncing.mockReset();
});
it('Not last sync/ENSNetworkNotSyncedError', async () => {
// eslint-disable-next-line @typescript-eslint/no-unsafe-call
getId.mockImplementation(() => expectedNetworkId);

// eslint-disable-next-line @typescript-eslint/no-unsafe-call
getBlock.mockImplementation(() => {
isSyncing.mockImplementation(() => {
return {
timestamp: BigInt(Date.now() / 1000 - 3601),
} as unknown as Block;
startingBlock: 100,
currentBlock: 312,
highestBlock: 51266,
} as unknown;
});
await expect(ens.checkNetwork()).rejects.toThrow(new ENSNetworkNotSyncedError());
});
Expand All @@ -402,10 +403,8 @@ describe('ens', () => {

// An initial check, in order to update `_lastSyncCheck`
// eslint-disable-next-line @typescript-eslint/no-unsafe-call
getBlock.mockImplementation(() => {
return {
timestamp: BigInt(Date.now() / 1000),
} as unknown as Block;
isSyncing.mockImplementation(() => {
return false;
});
// update `_lastSyncCheck`
await ens.checkNetwork();
Expand All @@ -426,10 +425,12 @@ describe('ens', () => {
getId.mockImplementation(() => network);

// eslint-disable-next-line @typescript-eslint/no-unsafe-call
getBlock.mockImplementation(() => {
isSyncing.mockImplementation(() => {
return {
timestamp: BigInt(111111111),
} as unknown as Block;
startingBlock: 100,
currentBlock: 312,
highestBlock: 51266,
} as unknown;
});

await expect(ens.checkNetwork()).rejects.toThrow(
Expand Down
5 changes: 1 addition & 4 deletions packages/web3-eth/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -111,13 +111,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
### Added

- Added hybrid build (ESM and CJS) of library (#5904)
- Added source files (#5956)

### Changed

- `signTransaction` will now return `gas` instead of `gasLimit` for returned transaction object regardless of what property name the provider uses (#5915)
- `formatTransaction` will now replace `data` transaction property with `input` (#5915)
- `isTransactionCall` will now check if `value.input` `isHexStrict` if provided (#5915)

### Added

- Added source files (#5956)
5 changes: 1 addition & 4 deletions packages/web3-types/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -83,11 +83,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
### Added

- Added hybrid build (ESM and CJS) of library (#5904)
- Added source files (#5956)

### Changed

- `data` property in `TransactionOutput` was renamed to `input` (#5915)

### Added

- Added source files (#5956)

0 comments on commit 8da5d83

Please sign in to comment.