Skip to content

Commit

Permalink
Merge branch '246-init-scanner-switch' into 'dev'
Browse files Browse the repository at this point in the history
fix init scanner network watcher switch

Closes #246

See merge request ergo/rosen-bridge/watcher!289
  • Loading branch information
vorujack committed Aug 14, 2024
2 parents b805ecc + 75cad9b commit 3202557
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 43 deletions.
5 changes: 5 additions & 0 deletions .changeset/tricky-vans-cry.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@rosen-bridge/watcher': patch
---

Fix watcher network switch in scanner init
78 changes: 35 additions & 43 deletions src/jobs/initScanner.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,18 @@
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';
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<any>) => {
Expand All @@ -26,49 +27,40 @@ const scanningJob = async (interval: number, scanner: GeneralScanner<any>) => {
};

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) {
switch (config.networkWatcher) {
case Constants.CARDANO_CHAIN_NAME:
if (cardanoConfig.ogmios) {
(scanner.observationScanner as CardanoOgmiosScanner).start();
break;
}
scanningJob(
cardanoConfig.blockfrost.interval,
scanner.observationScanner as CardanoBlockFrostScanner
cardanoConfig.koios
? cardanoConfig.koios.interval
: cardanoConfig.blockfrost!.interval,
scanner.observationScanner as GeneralScanner<unknown>
).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) {
break;
case Constants.BITCOIN_CHAIN_NAME:
scanningJob(
bitcoinConfig.esplora.interval,
scanner.observationScanner as BitcoinEsploraScanner
bitcoinConfig.rpc
? bitcoinConfig.rpc.interval
: bitcoinConfig.esplora!.interval,
scanner.observationScanner as GeneralScanner<unknown>
).then(() => null);
} else if (bitcoinConfig.rpc) {
break;
case Constants.ETHEREUM_CHAIN_NAME:
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,
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`
);
break;
case Constants.ERGO_CHAIN_NAME:
break;
default:
throw new Error(
`The observing network [${config.networkWatcher}] is not supported`
);
}

// TODO: Add commitment cleanup job
Expand Down

0 comments on commit 3202557

Please sign in to comment.