diff --git a/packages/ton-to-cw/src/block-processor.ts b/packages/ton-to-cw/src/block-processor.ts index f9afaa6..577fc4f 100644 --- a/packages/ton-to-cw/src/block-processor.ts +++ b/packages/ton-to-cw/src/block-processor.ts @@ -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) { @@ -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); @@ -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({ @@ -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", @@ -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( diff --git a/packages/ton-to-cw/src/index.ts b/packages/ton-to-cw/src/index.ts index 224bede..f554318 100644 --- a/packages/ton-to-cw/src/index.ts +++ b/packages/ton-to-cw/src/index.ts @@ -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 @@ -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( diff --git a/packages/ton-to-cw/src/start.ts b/packages/ton-to-cw/src/start.ts index a4a869d..9cf6690 100644 --- a/packages/ton-to-cw/src/start.ts +++ b/packages/ton-to-cw/src/start.ts @@ -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" @@ -82,8 +88,7 @@ function validate() { liteClient, blockProcessor, JETTON_BRIDGE, - logger, - "bece09e6b5b32d1f6a209e42c608fca09ae464232cbf62ebf9f6561948c01754" + logger ); const relayer = new TonToCwRelayer()