From 5c232838eda4364a584cd505cb8e36311776b5aa Mon Sep 17 00:00:00 2001 From: Qiwei Yang Date: Thu, 15 Aug 2024 20:46:31 +0800 Subject: [PATCH] fix local fork cannot recover from failure (#10835) * fix local fork cannot recover from failure * lint * update chopsticks version --- packages/apps/package.json | 2 +- packages/react-api/src/Api.tsx | 33 +++++++++++++++++++++++++-------- yarn.lock | 34 +++++++++++++++++----------------- 3 files changed, 43 insertions(+), 26 deletions(-) diff --git a/packages/apps/package.json b/packages/apps/package.json index f804bf6fc3..dd6c982e07 100644 --- a/packages/apps/package.json +++ b/packages/apps/package.json @@ -16,7 +16,7 @@ "type": "module", "version": "0.142.2-24-x", "dependencies": { - "@acala-network/chopsticks-core": "^0.10.0", + "@acala-network/chopsticks-core": "^0.13.3", "@polkadot/apps-config": "^0.142.2-24-x", "@polkadot/apps-routing": "^0.142.2-24-x", "@polkadot/dev": "^0.78.4", diff --git a/packages/react-api/src/Api.tsx b/packages/react-api/src/Api.tsx index f33a851849..034cbeeee4 100644 --- a/packages/react-api/src/Api.tsx +++ b/packages/react-api/src/Api.tsx @@ -232,14 +232,15 @@ async function createApi (apiUrl: string, signer: ApiSigner, isLocalFork: boolea const types = getDevTypes(); const isLight = apiUrl.startsWith('light://'); let provider; - let chopsticksFork: Blockchain | null = null; - try { - if (isLight) { - provider = await getLightProvider(apiUrl.replace('light://', '')); - } else if (isLocalFork) { - provider = await ChopsticksProvider.fromEndpoint(apiUrl); - chopsticksFork = provider.chain; + let chopsticksFork: Blockchain | null = null; + let chopsticksProvider; + let setupChopsticksSuccess = false; + + if (isLocalFork) { + try { + chopsticksProvider = await ChopsticksProvider.fromEndpoint(apiUrl); + chopsticksFork = chopsticksProvider.chain; await setStorage(chopsticksFork, { System: { Account: [ @@ -247,6 +248,22 @@ async function createApi (apiUrl: string, signer: ApiSigner, isLocalFork: boolea ] } }); + setupChopsticksSuccess = true; + } catch (error) { + store.set('localFork', ''); + const msg = `Local fork failed, please refresh to switch back to default API provider. This is likely due to chain not supported by chopsticks. + Please consider to send an issue to https://github.com/AcalaNetwork/chopsticks.`; + + onError(new Error(msg)); + throw error; + } + } + + try { + if (isLight) { + provider = await getLightProvider(apiUrl.replace('light://', '')); + } else if (isLocalFork && setupChopsticksSuccess) { + provider = chopsticksProvider; } else { provider = new WsProvider(apiUrl); } @@ -261,7 +278,7 @@ async function createApi (apiUrl: string, signer: ApiSigner, isLocalFork: boolea // See https://github.com/polkadot-js/api/pull/4672#issuecomment-1078843960 if (isLight) { - await provider.connect(); + await provider?.connect(); } } catch (error) { onError(error); diff --git a/yarn.lock b/yarn.lock index 90ff293405..31a48a4858 100644 --- a/yarn.lock +++ b/yarn.lock @@ -19,17 +19,17 @@ __metadata: languageName: node linkType: hard -"@acala-network/chopsticks-core@npm:^0.10.0": - version: 0.10.0 - resolution: "@acala-network/chopsticks-core@npm:0.10.0" +"@acala-network/chopsticks-core@npm:^0.13.3": + version: 0.13.3 + resolution: "@acala-network/chopsticks-core@npm:0.13.3" dependencies: - "@acala-network/chopsticks-executor": "npm:0.10.0" - "@polkadot/rpc-provider": "npm:^10.11.2" - "@polkadot/types": "npm:^10.11.2" - "@polkadot/types-codec": "npm:^10.11.2" - "@polkadot/types-known": "npm:^10.11.2" - "@polkadot/util": "npm:^12.6.2" - "@polkadot/util-crypto": "npm:^12.6.2" + "@acala-network/chopsticks-executor": "npm:0.13.3" + "@polkadot/rpc-provider": "npm:^12.3.1" + "@polkadot/types": "npm:^12.3.1" + "@polkadot/types-codec": "npm:^12.3.1" + "@polkadot/types-known": "npm:^12.3.1" + "@polkadot/util": "npm:^13.0.2" + "@polkadot/util-crypto": "npm:^13.0.2" comlink: "npm:^4.4.1" eventemitter3: "npm:^5.0.1" lodash: "npm:^4.17.21" @@ -38,17 +38,17 @@ __metadata: pino-pretty: "npm:^11.0.0" rxjs: "npm:^7.8.1" zod: "npm:^3.22.4" - checksum: 10/2fcf74c0208b0431124c1a5633e3e14ba6eb341135a91aca4ebd82b18ef1415b80c4335cb46e9c298fbf3e7113c1cf568568b6a056f460880370428cd53cecde + checksum: 10/87bebd6c0a1326b4f248d8b1f3fb2ab2cc6762c181c1820e378cd50701610bdeada583e11018cac10d48ee76c26ccc2c612324e88e55f09f3e839c42daea16a6 languageName: node linkType: hard -"@acala-network/chopsticks-executor@npm:0.10.0": - version: 0.10.0 - resolution: "@acala-network/chopsticks-executor@npm:0.10.0" +"@acala-network/chopsticks-executor@npm:0.13.3": + version: 0.13.3 + resolution: "@acala-network/chopsticks-executor@npm:0.13.3" dependencies: - "@polkadot/util": "npm:^12.6.2" + "@polkadot/util": "npm:^13.0.2" "@polkadot/wasm-util": "npm:^7.3.2" - checksum: 10/0c209dc01eb10f20dbb2e6492a0c78c8ecc2820839ded693f65402de7a7ab3cd8f84d0de42dac3e733fcca5c468ad6cf8c8453848e27468906313e6bda626734 + checksum: 10/f48a9928c8f98579dece7b26a924a00854bc724c6d524e4cb8a9e00016f8eb57466859a28531eb2a7180bce452723c07c4628414f73d5424e90815937a0523ed languageName: node linkType: hard @@ -1840,7 +1840,7 @@ __metadata: version: 0.0.0-use.local resolution: "@polkadot/apps@workspace:packages/apps" dependencies: - "@acala-network/chopsticks-core": "npm:^0.10.0" + "@acala-network/chopsticks-core": "npm:^0.13.3" "@polkadot/apps-config": "npm:^0.142.2-24-x" "@polkadot/apps-routing": "npm:^0.142.2-24-x" "@polkadot/dev": "npm:^0.78.4"