From ad68a560c48adad1cdb70931b276fd9517ec2c12 Mon Sep 17 00:00:00 2001 From: "fateme.r" Date: Tue, 13 Aug 2024 10:50:08 +0330 Subject: [PATCH 1/4] fix init scanner network watcher switch --- src/jobs/initScanner.ts | 93 ++++++++++++++++++++++------------------- 1 file changed, 51 insertions(+), 42 deletions(-) diff --git a/src/jobs/initScanner.ts b/src/jobs/initScanner.ts index 1bc2980..d5227d7 100644 --- a/src/jobs/initScanner.ts +++ b/src/jobs/initScanner.ts @@ -12,6 +12,14 @@ import { scanner } from '../utils/scanner'; import WinstonLogger from '@rosen-bridge/winston-logger'; import { EvmRpcScanner } from '@rosen-bridge/evm-rpc-scanner'; +const allConfig = getConfig(); +const { + general: config, + cardano: cardanoConfig, + bitcoin: bitcoinConfig, + ethereum: ethereumConfig, +} = allConfig; + const logger = WinstonLogger.getInstance().getLogger(import.meta.url); const scanningJob = async (interval: number, scanner: GeneralScanner) => { @@ -26,49 +34,50 @@ const scanningJob = async (interval: number, scanner: GeneralScanner) => { }; export const scannerInit = () => { - const allConfig = getConfig(); - const config = allConfig.general; scanningJob(config.ergoInterval, scanner.ergoScanner).then(() => null); - if (config.networkWatcher === Constants.CARDANO_CHAIN_NAME) { - const cardanoConfig = allConfig.cardano; - if (cardanoConfig.ogmios) - (scanner.observationScanner as CardanoOgmiosScanner).start(); - else if (cardanoConfig.koios) { - scanningJob( - cardanoConfig.koios.interval, - scanner.observationScanner as CardanoKoiosScanner - ).then(() => null); - } else if (cardanoConfig.blockfrost) { - scanningJob( - cardanoConfig.blockfrost.interval, - scanner.observationScanner as CardanoBlockFrostScanner - ).then(() => null); - } else throw new Error(`Cardano scanner is not configured properly`); - } else if (config.networkWatcher === Constants.BITCOIN_CHAIN_NAME) { - const bitcoinConfig = allConfig.bitcoin; - if (bitcoinConfig.esplora) { - scanningJob( - bitcoinConfig.esplora.interval, - scanner.observationScanner as BitcoinEsploraScanner - ).then(() => null); - } else if (bitcoinConfig.rpc) { - scanningJob( - bitcoinConfig.rpc.interval, - scanner.observationScanner as BitcoinRpcScanner - ).then(() => null); - } else throw new Error(`Bitcoin scanner is not configured properly`); - } else if (config.networkWatcher === Constants.ETHEREUM_CHAIN_NAME) { - const ethereumConfig = allConfig.ethereum; - if (ethereumConfig.rpc) { - scanningJob( - ethereumConfig.rpc.interval, - scanner.observationScanner as EvmRpcScanner - ).then(() => null); - } else throw new Error(`Ethereum scanner is not configured properly`); - } else { - throw new Error( - `The observing network [${config.networkWatcher}] is not supported` - ); + switch (config.networkWatcher) { + case Constants.CARDANO_CHAIN_NAME: + if (cardanoConfig.ogmios) + (scanner.observationScanner as CardanoOgmiosScanner).start(); + else if (cardanoConfig.koios) { + scanningJob( + cardanoConfig.koios.interval, + scanner.observationScanner as CardanoKoiosScanner + ).then(() => null); + } else if (cardanoConfig.blockfrost) { + scanningJob( + cardanoConfig.blockfrost.interval, + scanner.observationScanner as CardanoBlockFrostScanner + ).then(() => null); + } else throw new Error(`Cardano scanner is not configured properly`); + break; + case Constants.BITCOIN_CHAIN_NAME: + if (bitcoinConfig.esplora) { + scanningJob( + bitcoinConfig.esplora.interval, + scanner.observationScanner as BitcoinEsploraScanner + ).then(() => null); + } else if (bitcoinConfig.rpc) { + scanningJob( + bitcoinConfig.rpc.interval, + scanner.observationScanner as BitcoinRpcScanner + ).then(() => null); + } else throw new Error(`Bitcoin scanner is not configured properly`); + break; + case Constants.ETHEREUM_CHAIN_NAME: + if (ethereumConfig.rpc) { + scanningJob( + ethereumConfig.rpc.interval, + scanner.observationScanner as EvmRpcScanner + ).then(() => null); + } else throw new Error(`Ethereum scanner is not configured properly`); + break; + case Constants.ERGO_CHAIN_NAME: + break; + default: + throw new Error( + `The observing network [${config.networkWatcher}] is not supported` + ); } // TODO: Add commitment cleanup job From 0c08d246d75238540fb5ef0ca6dc652609f04dc5 Mon Sep 17 00:00:00 2001 From: "fateme.r" Date: Tue, 13 Aug 2024 12:36:35 +0330 Subject: [PATCH 2/4] Fix scanning job code format --- src/jobs/initScanner.ts | 50 ++++++++++++++++++----------------------- 1 file changed, 22 insertions(+), 28 deletions(-) diff --git a/src/jobs/initScanner.ts b/src/jobs/initScanner.ts index d5227d7..8e24d7b 100644 --- a/src/jobs/initScanner.ts +++ b/src/jobs/initScanner.ts @@ -39,38 +39,32 @@ export const scannerInit = () => { case Constants.CARDANO_CHAIN_NAME: if (cardanoConfig.ogmios) (scanner.observationScanner as CardanoOgmiosScanner).start(); - else if (cardanoConfig.koios) { - scanningJob( - cardanoConfig.koios.interval, - scanner.observationScanner as CardanoKoiosScanner - ).then(() => null); - } else if (cardanoConfig.blockfrost) { - scanningJob( - cardanoConfig.blockfrost.interval, - scanner.observationScanner as CardanoBlockFrostScanner - ).then(() => null); - } else throw new Error(`Cardano scanner is not configured properly`); + else if (!cardanoConfig.blockfrost && !cardanoConfig.koios) + throw new Error(`Cardano scanner is not configured properly`); + scanningJob( + cardanoConfig.koios + ? cardanoConfig.koios.interval + : cardanoConfig.blockfrost!.interval, + scanner.observationScanner as GeneralScanner + ).then(() => null); break; case Constants.BITCOIN_CHAIN_NAME: - if (bitcoinConfig.esplora) { - scanningJob( - bitcoinConfig.esplora.interval, - scanner.observationScanner as BitcoinEsploraScanner - ).then(() => null); - } else if (bitcoinConfig.rpc) { - scanningJob( - bitcoinConfig.rpc.interval, - scanner.observationScanner as BitcoinRpcScanner - ).then(() => null); - } else throw new Error(`Bitcoin scanner is not configured properly`); + if (!bitcoinConfig.esplora && bitcoinConfig.rpc) + throw new Error(`Bitcoin scanner is not configured properly`); + scanningJob( + bitcoinConfig.rpc + ? bitcoinConfig.rpc.interval + : bitcoinConfig.esplora!.interval, + scanner.observationScanner as GeneralScanner + ).then(() => null); break; case Constants.ETHEREUM_CHAIN_NAME: - if (ethereumConfig.rpc) { - scanningJob( - ethereumConfig.rpc.interval, - scanner.observationScanner as EvmRpcScanner - ).then(() => null); - } else throw new Error(`Ethereum scanner is not configured properly`); + if (!ethereumConfig.rpc) + throw new Error(`Ethereum scanner is not configured properly`); + scanningJob( + ethereumConfig.rpc.interval, + scanner.observationScanner as EvmRpcScanner + ).then(() => null); break; case Constants.ERGO_CHAIN_NAME: break; From d40bdd0b550d6e30f365e8699e84eb80ee6ebd0d Mon Sep 17 00:00:00 2001 From: "fateme.r" Date: Tue, 13 Aug 2024 12:37:32 +0330 Subject: [PATCH 3/4] add changeset --- .changeset/tricky-vans-cry.md | 5 +++++ 1 file changed, 5 insertions(+) create mode 100644 .changeset/tricky-vans-cry.md diff --git a/.changeset/tricky-vans-cry.md b/.changeset/tricky-vans-cry.md new file mode 100644 index 0000000..c586cd4 --- /dev/null +++ b/.changeset/tricky-vans-cry.md @@ -0,0 +1,5 @@ +--- +'@rosen-bridge/watcher': patch +--- + +Fix watcher network switch in scanner init From 75cad9bbc2a5a72d4a8ff30d7de181894c0279d6 Mon Sep 17 00:00:00 2001 From: "fateme.r" Date: Tue, 13 Aug 2024 14:56:12 +0330 Subject: [PATCH 4/4] fix switch case bugs --- src/jobs/initScanner.ts | 21 +++++---------------- 1 file changed, 5 insertions(+), 16 deletions(-) diff --git a/src/jobs/initScanner.ts b/src/jobs/initScanner.ts index 8e24d7b..558de00 100644 --- a/src/jobs/initScanner.ts +++ b/src/jobs/initScanner.ts @@ -1,11 +1,4 @@ -import { - CardanoBlockFrostScanner, - CardanoKoiosScanner, - CardanoOgmiosScanner, - GeneralScanner, -} from '@rosen-bridge/scanner'; -import { BitcoinEsploraScanner } from '@rosen-bridge/bitcoin-esplora-scanner'; -import { BitcoinRpcScanner } from '@rosen-bridge/bitcoin-rpc-scanner'; +import { CardanoOgmiosScanner, GeneralScanner } from '@rosen-bridge/scanner'; import * as Constants from '../config/constants'; import { getConfig } from '../config/config'; import { scanner } from '../utils/scanner'; @@ -37,10 +30,10 @@ export const scannerInit = () => { scanningJob(config.ergoInterval, scanner.ergoScanner).then(() => null); switch (config.networkWatcher) { case Constants.CARDANO_CHAIN_NAME: - if (cardanoConfig.ogmios) + if (cardanoConfig.ogmios) { (scanner.observationScanner as CardanoOgmiosScanner).start(); - else if (!cardanoConfig.blockfrost && !cardanoConfig.koios) - throw new Error(`Cardano scanner is not configured properly`); + break; + } scanningJob( cardanoConfig.koios ? cardanoConfig.koios.interval @@ -49,8 +42,6 @@ export const scannerInit = () => { ).then(() => null); break; case Constants.BITCOIN_CHAIN_NAME: - if (!bitcoinConfig.esplora && bitcoinConfig.rpc) - throw new Error(`Bitcoin scanner is not configured properly`); scanningJob( bitcoinConfig.rpc ? bitcoinConfig.rpc.interval @@ -59,10 +50,8 @@ export const scannerInit = () => { ).then(() => null); break; case Constants.ETHEREUM_CHAIN_NAME: - if (!ethereumConfig.rpc) - throw new Error(`Ethereum scanner is not configured properly`); scanningJob( - ethereumConfig.rpc.interval, + ethereumConfig.rpc!.interval, scanner.observationScanner as EvmRpcScanner ).then(() => null); break;