Skip to content

Commit

Permalink
Merge pull request #20 from oraidex/fix/optimize-gas-fees
Browse files Browse the repository at this point in the history
fix: remove redundant method & refactor code
  • Loading branch information
trung2891 authored Feb 18, 2025
2 parents 9580a70 + 8ba81fa commit b223b35
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 72 deletions.
74 changes: 20 additions & 54 deletions packages/ton-to-cw/src/block-processor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,21 @@ export default class TonBlockProcessor {
protected logger: Logger
) {}

async isBlockVerified(rootHash: Buffer) {
try {
const isBlockVerified = await this.validator.isVerifiedBlock({
rootHash: rootHash.toString("hex"),
});

return isBlockVerified;
} catch (error) {
this.logger.error(
`Error querying block verification status: ${JSON.stringify(error)}`
);
return false;
}
}

async queryKeyBlock(masterChainSeqNo: number) {
let initBlockSeqno = masterChainSeqNo;
while (true) {
Expand Down Expand Up @@ -158,9 +173,7 @@ export default class TonBlockProcessor {
}

async verifyMasterchainBlockByBlockId(blockId: BlockID) {
const isBlockVerified = await this.validator.isVerifiedBlock({
rootHash: blockId.rootHash.toString("hex"),
});
const isBlockVerified = await this.isBlockVerified(blockId.rootHash);
if (isBlockVerified) return;

const vdata = await this.getMasterchainBlockValSignatures(blockId.seqno);
Expand All @@ -178,10 +191,9 @@ export default class TonBlockProcessor {
}

async verifyMasterchainKeyBlock(rawBlockData: liteServer_BlockData) {
const isBlockVerified = await this.validator.isVerifiedBlock({
rootHash: rawBlockData.id.rootHash.toString("hex"),
});

const isBlockVerified = await this.isBlockVerified(
rawBlockData.id.rootHash
);
if (isBlockVerified) return;
const keyblockBoc = rawBlockData.data.toString("hex");
await this.validator.prepareNewKeyBlock({
Expand All @@ -201,50 +213,6 @@ export default class TonBlockProcessor {
);
}

async storeKeyBlockNextValSet(
rawBlockData: liteServer_BlockData,
parsedBlock: ParsedBlock
) {
const nextValidators = parsedBlock.extra.custom.config.config.map.get("24");
// if empty then we do nothing and wait til next end of consensus
if (!nextValidators) {
this.allValidators = [];
return;
}

const nextValSetFirstPubkey = Buffer.from(
nextValidators.next_validators.list.map.get("0").public_key.pubkey
).toString("hex");
const allValidators =
this.allValidators.length > 0
? this.allValidators
: await this.queryAllValidators();

// if we already updated the keyblock with next valset -> cache the valset and ignore
if (allValidators.some((val) => val.pubkey === nextValSetFirstPubkey)) {
this.allValidators = allValidators;
return;
}

const keyblockBoc = rawBlockData.data.toString("hex");
await this.validator.prepareNewKeyBlock({
keyblockBoc,
});
const vdata = await this.getMasterchainBlockValSignatures(
rawBlockData.id.seqno
);

await this.validator.verifyKeyBlock({
rootHash: rawBlockData.id.rootHash.toString("hex"),
fileHash: rawBlockData.id.fileHash.toString("hex"),
vdata,
});

this.logger.info(
`Updated keyblock ${rawBlockData.id.seqno} with new validator set successfully`
);
}

private async getMasterchainBlockValSignatures(seqno: number) {
const valSignatures = (await this.tonweb.provider.send(
"getMasterchainBlockSignatures",
Expand All @@ -267,9 +235,7 @@ export default class TonBlockProcessor {
}

async verifyShardBlocks(shardId: BlockID) {
const isBlockVerified = await this.validator.isVerifiedBlock({
rootHash: shardId.rootHash.toString("hex"),
});
const isBlockVerified = await this.isBlockVerified(shardId.rootHash);
if (isBlockVerified) return;

const shardProof = await this.liteClient.engine.query(
Expand Down
15 changes: 0 additions & 15 deletions packages/ton-to-cw/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,6 @@ export default class TonToCwRelayer {
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) {
// do nothing since we already catch the error in individual methods
Expand Down Expand Up @@ -88,20 +87,6 @@ export default class TonToCwRelayer {
throw error;
}
}

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

export async function createTonToCwRelayerWithConfig(
Expand Down
11 changes: 8 additions & 3 deletions packages/ton-to-cw/src/start.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,13 @@ function validate() {
format: format.combine(format.timestamp(), format.json()),
transports: [new transports.Console()],
});
const client = await initSignClient(process.env.MNEMONIC, process.env.COSMOS_RPC_URL, undefined, undefined, process.env.GAS_FEES);
const client = await initSignClient(
process.env.MNEMONIC,
process.env.COSMOS_RPC_URL,
undefined,
undefined,
process.env.GAS_FEES
);
// setup lite engine server
const { liteservers } = await fetch(
"https://ton.org/global.config.json"
Expand Down Expand Up @@ -82,8 +88,7 @@ function validate() {
liteClient,
blockProcessor,
JETTON_BRIDGE,
logger,
"bece09e6b5b32d1f6a209e42c608fca09ae464232cbf62ebf9f6561948c01754"
logger
);

const relayer = new TonToCwRelayer()
Expand Down

0 comments on commit b223b35

Please sign in to comment.