Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature reverse relayer #48

Closed
wants to merge 21 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 25 additions & 8 deletions src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,14 @@ dotenv.config({ path: ".env" });
export interface SubnetConfig {
url: string;
fetchWaitingTime: number;
smartContractAddress: string;
accountPK: string;
submitTransactionWaitingTime: number;
}

export interface MainnetConfig {
url: string;
fetchWaitingTime: number;
smartContractAddress: string;
accountPK: string;
submitTransactionWaitingTime: number;
Expand All @@ -23,7 +27,8 @@ export interface XdcZeroConfig {
isEnabled: boolean;
subnetZeroContractAddress?: string;
parentChainZeroContractAddress?: string;
walletPk?: string;
parentnetWalletPk?: string;
subnetWalletPk?: string;
}

export interface Config {
Expand All @@ -40,7 +45,10 @@ export interface Config {
notification: NotificationConfig;
chunkSize: number;
xdcZero: XdcZeroConfig;
relayerCsc: {
relayerCsc: { //Checkpoint Smart Contract is deployed on the Parentnet and stores Subnet data, check https://github.com/XinFinOrg/XDC-CSC
isEnabled: boolean;
}
reverseRelayerCsc:{ //Reverse Checkpoint Smart Contract is deployed in the Subnet and stores Parentnet data, check https://github.com/XinFinOrg/XDC-CSC
isEnabled: boolean;
}
}
Expand All @@ -49,12 +57,13 @@ const environment = process.env.NODE_ENV || "production";
export const devMode = environment != "production";

const getZeroConfig = (): XdcZeroConfig => {
const isEnabled: boolean = "PARENTNET_ZERO_WALLET_PK" in process.env && "SUBNET_ZERO_CONTRACT" in process.env && "PARENTNET_ZERO_CONTRACT" in process.env;
const isEnabled: boolean = "PARENTNET_ZERO_WALLET_PK" in process.env && "SUBNET_ZERO_WALLET_PK" in process.env && "SUBNET_ZERO_CONTRACT" in process.env && "PARENTNET_ZERO_CONTRACT" in process.env;
return isEnabled ? {
isEnabled,
subnetZeroContractAddress: process.env.SUBNET_ZERO_CONTRACT,
parentChainZeroContractAddress: process.env.PARENTNET_ZERO_CONTRACT,
walletPk: process.env.PARENTNET_ZERO_WALLET_PK.startsWith("0x") ? process.env.PARENTNET_ZERO_WALLET_PK : `0x${process.env.PARENTNET_ZERO_WALLET_PK}`
parentnetWalletPk: process.env.PARENTNET_ZERO_WALLET_PK.startsWith("0x") ? process.env.PARENTNET_ZERO_WALLET_PK : `0x${process.env.PARENTNET_ZERO_WALLET_PK}`,
subnetWalletPk: process.env.SUBNET_WALLET_PK.startsWith("0x") ? process.env.SUBNET_WALLET_PK : `0x${process.env.SUBNET_WALLET_PK}`
}: { isEnabled: false };
};

Expand All @@ -67,22 +76,30 @@ const config: Config = {
zeroJobExpression: "*/10 * * * * *", // every 10s
},
subnet: {
url: process.env.SUBNET_URL || "https://devnetstats.apothem.network/subnet",
url: process.env.SUBNET_URL || "",
smartContractAddress: process.env.REVERSE_CHECKPOINT_CONTRACT || "",
accountPK:
process.env.SUBNET_WALLET_PK ||
"0xa6538b992365dd26bbc2391ae6639bac0ed8599f8b45bca7c28c105959f02af0", // Default to a dummy key
fetchWaitingTime: +process.env.SN_FETCHING_WAITING_TIME || 0,
submitTransactionWaitingTime: +process.env.MN_TX_SUBMIT_WAITING_TIME || 100,
},
mainnet: {
url:
process.env.PARENTNET_URL ||
"https://devnetstats.apothem.network/mainnet",
process.env.PARENTNET_URL || "",
smartContractAddress: process.env.CHECKPOINT_CONTRACT || "",
accountPK:
process.env.PARENTNET_WALLET_PK ||
"0xa6538b992365dd26bbc2391ae6639bac0ed8599f8b45bca7c28c105959f02af4", // Default to a dummy key
fetchWaitingTime: +process.env.SN_FETCHING_WAITING_TIME || 0,
submitTransactionWaitingTime: +process.env.MN_TX_SUBMIT_WAITING_TIME || 100,
},
relayerCsc: {
isEnabled: "PARENTNET_WALLET_PK" in process.env && "CHECKPOINT_CONTRACT" in process.env && "PARENTNET_URL" in process.env
},
reverseRelayerCsc: {
isEnabled: "SUBNET_WALLET_PK" in process.env && "REVERSE_CHECKPOINT_CONTRACT" in process.env && "SUBNET_URL" in process.env
},
xdcZero: getZeroConfig(),
reBootstrapWaitingTime: +process.env.BOOTSTRAP_FAILURE_WAIT_TIME || 120000,
notification: {
Expand All @@ -92,7 +109,7 @@ const config: Config = {
}
: undefined,
},
chunkSize : parseInt(process.env.MAX_FETCH_BLOCK_SIZE) || 30,
chunkSize : parseInt(process.env.MAX_FETCH_BLOCK_SIZE) || 120,
};

export { config };
9 changes: 5 additions & 4 deletions src/processors/full.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import bunyan from "bunyan";
import { config } from "../config";
import { MainnetService, SmartContractData } from "../service/mainnet";
import { SubnetBlockInfo, SubnetService } from "../service/subnet";
import { SubnetService, SubnetBlockInfo } from "../service/subnet";
import { chunkBy, sleep } from "../utils";
import { ForkingError } from "../errors/forkingError";
import { BaseProcessor } from "./base";
Expand All @@ -27,8 +27,9 @@ export class Full extends BaseProcessor {

init() {
this.logger.info("Initialising XDC relayer");

this.queue.process(async (_, done) => {
this.logger.info("⏰ Executing normal flow periodically");
this.logger.info("⏰ Full Relayer: Executing normal flow periodically");
try {
done(null, await this.processEvent());
} catch (error) {
Expand Down Expand Up @@ -82,10 +83,10 @@ export class Full extends BaseProcessor {
startingBlockNumberToFetch,
numOfBlocks
);
await this.mainnetService.submitTxs(results);
await this.mainnetService.submitTxsDynamic(results);
startingBlockNumberToFetch += numOfBlocks;
}
this.logger.info("Sync completed!");
this.logger.info("Full CSC Sync completed!");
return;
}

Expand Down
36 changes: 32 additions & 4 deletions src/processors/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,36 +4,47 @@ import { createBullBoard } from '@bull-board/api';
import { BullAdapter } from '@bull-board/api/bullAdapter';
import { ExpressAdapter } from '@bull-board/express';

import { Zero, NAME as zeroName } from "./zero";
import { config } from "./../config";
import { Lite, NAME as liteName } from "./lite";
import { Full, NAME as fullName } from "./full";
import { ReverseFull, NAME as ReverseFullName } from "./reverseFull";
import { Zero, NAME as zeroName } from "./zero";
import { ReverseZero, NAME as ReverseZeroName } from "./reverseZero";
import { MainnetService } from "../service/mainnet";
import { SubnetService } from "../service/subnet";

enum Mode {
LITE = liteName,
FULL = fullName,
ZERO = zeroName
REVERSE_FULL = ReverseFullName,
ZERO = zeroName,
REVERSE_ZERO = ReverseZeroName
}

export class Processors {
logger: bunyan;
private processors: {
lite: Lite;
full: Full;
reverseFull: ReverseFull;
zero: Zero;
reverseZero: ReverseZero;
}
private mainnetService: MainnetService;
private subnetService: SubnetService;

constructor(logger: bunyan) {
this.logger = logger;
this.processors = {
lite: new Lite(logger),
full: new Full(logger),
zero: new Zero(logger)
reverseFull: new ReverseFull(logger),
zero: new Zero(logger),
reverseZero: new ReverseZero(logger),
// Register more processors here
};
this.mainnetService = new MainnetService(config.mainnet, logger);
this.subnetService = new SubnetService(config.subnet, logger);
}

// Register the event process. NOTE: this won't actually start the job processing until you call the reset
Expand Down Expand Up @@ -71,9 +82,15 @@ export class Processors {
case Mode.FULL:
await this.processors.full.reset();
break;
case Mode.REVERSE_FULL:
await this.processors.reverseFull.reset();
break;
case Mode.ZERO:
await this.processors.zero.reset();
break;
case Mode.REVERSE_ZERO:
await this.processors.reverseZero.reset();
break;
default:
throw new Error("No avaiable modes to choose from");
}
Expand All @@ -92,12 +109,23 @@ export class Processors {
modes.push(Mode.FULL);
break;
default:
throw new Error("No avaiable mode from mainnet smart contract API");
throw new Error("No avaiable mode from PARENTNET smart contract API");
}
}
if (config.reverseRelayerCsc.isEnabled){
const subnetSmartContractMode = await this.subnetService.Mode();
switch (subnetSmartContractMode) {
case "reverse_full":
modes.push(Mode.REVERSE_FULL);
break;
default:
throw new Error("No available mode from SUBNET smart contract API");
}
}

if (config.xdcZero.isEnabled) {
modes.push(Mode.ZERO);
modes.push(Mode.REVERSE_ZERO);
}

this.logger.info("Running modes: ", modes);
Expand Down
7 changes: 4 additions & 3 deletions src/processors/lite.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,12 @@ export class Lite extends BaseProcessor {
super(NAME);
this.logger = logger;
this.liteMainnetService = new LiteMainnetService(config.mainnet, logger);
this.subnetService = new SubnetService(config.subnet, logger);
// this.subnetService = new SubnetService(config.subnet, logger);
}

init() {
this.logger.info("Initialising XDC Lite relayer");

this.queue.process(async (_, done) => {
this.logger.info("⏰ Executing lite flow periodically");
try {
Expand Down Expand Up @@ -101,7 +102,7 @@ export class Lite extends BaseProcessor {
await this.liteMainnetService.commitHeader(
scHash,
results.map((item) => {
return "0x" + item.hexRLP;
return "0x" + item.encodedRLP;
})
);
} else {
Expand Down Expand Up @@ -129,7 +130,7 @@ export class Lite extends BaseProcessor {
scCommittedHeight = last.smartContractCommittedHeight;
scHash = last.smartContractHash;
}
this.logger.info("Sync completed!");
this.logger.info("Lite CSC Sync completed!");
return;
}
}
Loading
Loading