Skip to content

Commit

Permalink
fix: add more detailed try catch & add 0 til offset hash length = 64
Browse files Browse the repository at this point in the history
  • Loading branch information
ducphamle2 committed Sep 21, 2024
1 parent 5317171 commit 243d81f
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 23 deletions.
69 changes: 54 additions & 15 deletions packages/ton-to-cw/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ import {
TonbridgeValidatorClient,
} from "@oraichain/tonbridge-contracts-sdk";
import { Logger } from "winston";
import { liteServer_BlockData } from "ton-lite-client/dist/schema";
import { ParsedBlock } from "@oraichain/tonbridge-utils";

export default class TonToCwRelayer {
private blockProcessor: TonBlockProcessor;
Expand Down Expand Up @@ -43,27 +45,64 @@ export default class TonToCwRelayer {

while (true) {
try {
const latestMasterchainBlock =
await this.blockProcessor.getMasterchainInfo();
const { rawBlockData, parsedBlock } =
await this.blockProcessor.queryKeyBlock(
latestMasterchainBlock.last.seqno
);
this.logger.info(
"Prepare to verify masterchain keyblock: " + parsedBlock.info.seq_no
const latestMasterchainBlock = await this.getMasterchainInfo();
const { rawBlockData, parsedBlock } = await this.queryKeyBlock(
latestMasterchainBlock.last.seqno
);
await this.blockProcessor.verifyMasterchainKeyBlock(rawBlockData);
await this.blockProcessor.storeKeyBlockNextValSet(
rawBlockData,
parsedBlock
);
await this.txProcessor.processTransactions();
this.logger.info("Masterchain: " + latestMasterchainBlock.last.seqno);
this.logger.info("Keyblock: " + parsedBlock.info.seq_no);
await this.verifyMasterchainKeyBlock(rawBlockData);
await this.storeKeyBlockNextValSet(rawBlockData, parsedBlock);
await this.txProcessor.processTransactions(latestMasterchainBlock.last);
} catch (error) {
this.logger.error("error processing block and tx: ", error);
// do nothing since we already catch the error in individual methods
// this.logger.error("error processing block and tx: " + error);
}
await setTimeout(processInterval);
}
}

private async getMasterchainInfo() {
try {
// throw "getMasterchainInfo";
return this.blockProcessor.getMasterchainInfo();
} catch (error) {
this.logger.error("error getting masterchain info: " + error);
throw error;
}
}

private async queryKeyBlock(masterChainSeqNo: number) {
try {
return this.blockProcessor.queryKeyBlock(masterChainSeqNo);
} catch (error) {
this.logger.error("error queryKeyBlock: " + error);
throw error;
}
}

private async verifyMasterchainKeyBlock(rawBlockData: liteServer_BlockData) {
try {
return this.blockProcessor.verifyMasterchainKeyBlock(rawBlockData);
} catch (error) {
this.logger.error("Error verifyMasterchainKeyBlock: " + error);
throw error;
}
}

private async storeKeyBlockNextValSet(
rawBlockData: liteServer_BlockData,
parsedBlock: ParsedBlock
) {
try {
return this.blockProcessor.storeKeyBlockNextValSet(
rawBlockData,
parsedBlock
);
} catch (error) {
this.logger.error("Error storeKeyBlockNextValSet: ", error);
}
}
}

export async function createTonToCwRelayerWithConfig(
Expand Down
2 changes: 1 addition & 1 deletion packages/ton-to-cw/src/start.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ function validate() {
blockProcessor,
JETTON_BRIDGE,
logger,
"b346c48a06af4f743fa94975c37f3987e84c2aa4f0530f87378457d1d373d18c"
"f2c76b1d5a9bebda03e411e3cd87bfde08762fe263f3b6c3a9c850620dddcff7"
);

const relayer = new TonToCwRelayer()
Expand Down
25 changes: 18 additions & 7 deletions packages/ton-to-cw/src/tx-processor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@ import { setTimeout } from "timers/promises";

import { OPCODES } from "./constants";
import { Logger } from "winston";
import {
liteServer_masterchainInfo,
tonNode_blockIdExt,
} from "ton-lite-client/dist/schema";

export default class TonTxProcessor {
private limitPerTxQuery = 100; // limit per query
Expand All @@ -28,23 +32,29 @@ export default class TonTxProcessor {
this.initiallatestProcessedTxHash = latestProcessedTxHash;
}

private async queryUnprocessedTransactions() {
private async queryUnprocessedTransactions(
masterchainInfo: tonNode_blockIdExt
) {
const transactions: TransactionWithBlockId[] = [];
const jettonAddr = address(this.jettonBridgeAddress);
const masterchainInfo = await this.liteClient.getMasterchainInfo();
const accState = await this.liteClient.getAccountState(
jettonAddr,
masterchainInfo.last
masterchainInfo
);
let offset = {
hash: accState.lastTx.hash.toString(16),
lt: accState.lastTx.lt.toString(10),
};
while (true) {
// workaround. Bug of loadTransaction that causes the prev trans hash to be incomplete
if (offset.hash.length === 63) {
if (offset.hash.length < 64) {
this.logger.error(
"TonTxProcessor queryUnprocessedTransactions offset hash length < 64: " +
offset.hash
);
}
while (offset.hash.length < 64) {
offset.hash = "0" + offset.hash;
this.logger.info("TonTxProcessor:new offset hash: " + offset.hash);
}
const rawTxs = await this.liteClient.getAccountTransactions(
jettonAddr,
Expand Down Expand Up @@ -93,9 +103,10 @@ export default class TonTxProcessor {
return transactions;
}

async processTransactions() {
async processTransactions(masterchainInfo: tonNode_blockIdExt) {
try {
const transactions = await this.queryUnprocessedTransactions();
const transactions =
await this.queryUnprocessedTransactions(masterchainInfo);
this.logger.info(
"TonTxProcessor:unprocessed transactions: " + transactions.length
);
Expand Down

0 comments on commit 243d81f

Please sign in to comment.