Skip to content

Commit

Permalink
feat: add block tag enum for validating reorgPeriod (hyperlane-xyz#4739)
Browse files Browse the repository at this point in the history
### Description

- feat: add `EthJsonRpcBlockParameterTag` enum for validating
reorgPeriod
- cli will check that reorgPeriod is either a number or one of the
appropriate tags, but the underlying schema is still left loose to be
forwards-compatible
- exporting the enum will also let us use this in unit tests on the
registry side

### Drive-by changes

na

### Related issues

na

### Backward compatibility

ye

### Testing

ci

---------

Signed-off-by: pbio <[email protected]>
  • Loading branch information
paulbalaji authored and tiendn committed Oct 25, 2024
1 parent c156f5a commit 9d7d809
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 8 deletions.
6 changes: 6 additions & 0 deletions .changeset/quiet-cooks-join.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
'@hyperlane-xyz/cli': minor
'@hyperlane-xyz/sdk': minor
---

Add `EthJsonRpcBlockParameterTag` enum for validating reorgPeriod
21 changes: 15 additions & 6 deletions typescript/cli/src/config/chain.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { stringify as yamlStringify } from 'yaml';
import {
ChainMetadata,
ChainMetadataSchema,
EthJsonRpcBlockParameterTag,
ExplorerFamily,
ZChainName,
} from '@hyperlane-xyz/sdk';
Expand Down Expand Up @@ -168,9 +169,11 @@ async function addBlockOrGasConfig(metadata: ChainMetadata): Promise<void> {
}

async function addBlockConfig(metadata: ChainMetadata): Promise<void> {
const parseReorgPeriod = (value: string) => {
const parseReorgPeriod = (
value: string,
): number | EthJsonRpcBlockParameterTag => {
const parsed = parseInt(value, 10);
return isNaN(parsed) ? value : parsed;
return isNaN(parsed) ? (value as EthJsonRpcBlockParameterTag) : parsed;
};

const wantBlockConfig = await confirm({
Expand All @@ -184,10 +187,16 @@ async function addBlockConfig(metadata: ChainMetadata): Promise<void> {
});
const blockReorgPeriod = await input({
message:
'Enter no. of blocks before a transaction has a near-zero chance of reverting (0-500) or block tag:',
validate: (value) =>
isNaN(parseInt(value)) ||
(parseInt(value) >= 0 && parseInt(value) <= 500),
'Enter no. of blocks before a transaction has a near-zero chance of reverting (0-500) or block tag (earliest, latest, safe, finalized, pending):',
validate: (value) => {
const parsedInt = parseInt(value, 10);
return (
Object.values(EthJsonRpcBlockParameterTag).includes(
value as EthJsonRpcBlockParameterTag,
) ||
(!isNaN(parsedInt) && parsedInt >= 0 && parsedInt <= 500)
);
},
});
const blockTimeEstimate = await input({
message: 'Enter the rough estimate of time per block in seconds (0-20):',
Expand Down
1 change: 1 addition & 0 deletions typescript/sdk/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,7 @@ export {
export {
BlockExplorer,
BlockExplorerSchema,
EthJsonRpcBlockParameterTag,
ChainMetadata,
ChainMetadataSchema,
ChainMetadataSchemaObject,
Expand Down
8 changes: 6 additions & 2 deletions typescript/sdk/src/metadata/chainMetadata.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,11 @@ import { expect } from 'chai';

import { ProtocolType } from '@hyperlane-xyz/utils';

import { ChainMetadata, isValidChainMetadata } from './chainMetadataTypes.js';
import {
ChainMetadata,
EthJsonRpcBlockParameterTag,
isValidChainMetadata,
} from './chainMetadataTypes.js';

const minimalSchema: ChainMetadata = {
chainId: 5,
Expand Down Expand Up @@ -68,7 +72,7 @@ describe('ChainMetadataSchema', () => {
...minimalSchema,
blocks: {
confirmations: 1,
reorgPeriod: 'finalized',
reorgPeriod: EthJsonRpcBlockParameterTag.Finalized,
},
}),
).to.eq(true);
Expand Down
8 changes: 8 additions & 0 deletions typescript/sdk/src/metadata/chainMetadataTypes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,14 @@ import { ChainMap } from '../types.js';

import { ZChainName, ZNzUint, ZUint } from './customZodTypes.js';

export enum EthJsonRpcBlockParameterTag {
Earliest = 'earliest',
Latest = 'latest',
Safe = 'safe',
Finalized = 'finalized',
Pending = 'pending',
}

export enum ExplorerFamily {
Etherscan = 'etherscan',
Blockscout = 'blockscout',
Expand Down

0 comments on commit 9d7d809

Please sign in to comment.