From 4e7eaee296e4a05fdbb4019080bde6d900459948 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=B8=83=E5=81=B6=E4=BB=80=E4=B9=88=E7=9A=84=E5=B0=B1?= =?UTF-8?q?=E6=98=AF=E5=B8=83=E5=81=B6?= Date: Wed, 4 Sep 2024 17:15:44 +0800 Subject: [PATCH] fix: ExplorerService missing error handling cause page crash (#425) --- src/pages/BlockDetail/index.tsx | 2 +- src/services/ExplorerService/index.ts | 10 ++++++++-- src/utils/address.ts | 8 ++++++++ src/utils/number.ts | 10 +++++++++- 4 files changed, 26 insertions(+), 4 deletions(-) diff --git a/src/pages/BlockDetail/index.tsx b/src/pages/BlockDetail/index.tsx index 6e48c1959..6f0a12d33 100644 --- a/src/pages/BlockDetail/index.tsx +++ b/src/pages/BlockDetail/index.tsx @@ -62,7 +62,7 @@ export default () => { ['node', 'block', 'info', blockHeightOrHash], () => isBlockNumber(blockHeightOrHash) - ? nodeService.rpc.getBlockByNumber(`0x${parseInt(blockHeightOrHash, 10).toString(16)}`) + ? nodeService.rpc.getBlockByNumber(`0x${Number(blockHeightOrHash).toString(16)}`) : nodeService.rpc.getBlock(blockHeightOrHash), { enabled: nodeModeActivated, diff --git a/src/services/ExplorerService/index.ts b/src/services/ExplorerService/index.ts index 754f9ff1c..024b86b0a 100644 --- a/src/services/ExplorerService/index.ts +++ b/src/services/ExplorerService/index.ts @@ -1,4 +1,4 @@ -import { BehaviorSubject, Subscription, map, switchMap, timer } from 'rxjs' +import { BehaviorSubject, Subscription, map, switchMap, timer, catchError, of } from 'rxjs' import { BLOCKCHAIN_ALERT_POLLING_TIME, BLOCK_POLLING_TIME, @@ -53,7 +53,12 @@ class ExplorerService { this.callbacksAtStop = new Subscription() this.callbacksAtStop.add( - timer(0, BLOCK_POLLING_TIME).pipe(switchMap(this.api.fetchStatistics)).subscribe(this.latestStatistics$), + timer(0, BLOCK_POLLING_TIME) + .pipe( + switchMap(this.api.fetchStatistics), + catchError(() => of(initStatistics)), + ) + .subscribe(this.latestStatistics$), ) this.callbacksAtStop.add( @@ -61,6 +66,7 @@ class ExplorerService { .pipe( switchMap(this.api.fetchTipBlockNumber), map(tipBlockNumber => Number(tipBlockNumber)), + catchError(() => of(0)), ) .subscribe(this.latestBlockNumber$), ) diff --git a/src/utils/address.ts b/src/utils/address.ts index 0d9eae59c..b94268d5f 100644 --- a/src/utils/address.ts +++ b/src/utils/address.ts @@ -11,6 +11,14 @@ export const encodeNewAddress = (script: Script) => { } export const encodeDeprecatedAddress = (script: Script) => { + if (script.hashType === 'data1') { + return encodeNewAddress(script) + } + + if (script.hashType === 'data2') { + return encodeNewAddress(script) + } + return generateAddress(script, { config: lumosConfig }) } diff --git a/src/utils/number.ts b/src/utils/number.ts index 1c897a28c..843ac108b 100644 --- a/src/utils/number.ts +++ b/src/utils/number.ts @@ -5,8 +5,16 @@ export function isNumeric(str: string) { return !Number.isNaN(str) && !Number.isNaN(parseFloat(str)) } +const BLOCK_HASH_LENGTH = 64 + export function isBlockNumber(str: string) { - return !Number.isNaN(str) && !Number.isNaN(parseFloat(str)) && parseFloat(str) !== 0 + if (str.length >= BLOCK_HASH_LENGTH) return false + + if (!Number.isNaN(str) && !Number.isNaN(parseFloat(str)) && parseFloat(str) !== 0) { + return true + } + + return !Number.isNaN(str) && !Number.isNaN(parseInt(str, 16)) && parseInt(str, 16) !== 0 } export const localeNumberString = (value: BigNumber | string | number): string => {