From 3c9d94d0d4af70cb20d7380beecb5ff07d0576e8 Mon Sep 17 00:00:00 2001 From: wanwiset25 Date: Wed, 5 Jun 2024 16:32:48 +0400 Subject: [PATCH] hexRLP > encodedRLP --- src/processors/lite.ts | 2 +- src/processors/reverseFull.ts | 10 ++---- src/service/mainnet/index.ts | 52 +++++++++++--------------------- src/service/subnet/index.ts | 57 ++++++++++++----------------------- 4 files changed, 39 insertions(+), 82 deletions(-) diff --git a/src/processors/lite.ts b/src/processors/lite.ts index cea03dd..1f42922 100644 --- a/src/processors/lite.ts +++ b/src/processors/lite.ts @@ -103,7 +103,7 @@ export class Lite extends BaseProcessor { await this.liteMainnetService.commitHeader( scHash, results.map((item) => { - return "0x" + item.hexRLP; + return "0x" + item.encodedRLP; }) ); } else { diff --git a/src/processors/reverseFull.ts b/src/processors/reverseFull.ts index cba79f3..99294e8 100644 --- a/src/processors/reverseFull.ts +++ b/src/processors/reverseFull.ts @@ -1,9 +1,7 @@ import bunyan from "bunyan"; import { config } from "../config"; -// import { MainnetService, SmartContractData } from "../service/mainnet"; -// import { SubnetBlockInfo, SubnetService } from "../service/subnet"; -import {SubnetService, SmartContractData } from "../service/subnet"; -import {MainnetService, MainnetBlockInfo} from "../service/mainnet"; +import { SubnetService, SmartContractData } from "../service/subnet"; +import { MainnetService, MainnetBlockInfo } from "../service/mainnet"; import { chunkBy, sleep } from "../utils"; import { ForkingError } from "../errors/forkingError"; import { BaseProcessor } from "./base"; @@ -55,13 +53,9 @@ export class ReverseFull extends BaseProcessor { async processEvent() { // Pull latest confirmed tx from subnet const smartContractData = await this.subnetService.getLastAuditedBlock(); - this.logger.error("gram sm data from subnet"); - this.logger.error(smartContractData); // Pull latest confirmed block from mainnet const latestMainnetCommittedBlock = await this.mainnetService.getLastCommittedBlockInfo(); - this.logger.error("grab mainnet committed block"); - this.logger.error(latestMainnetCommittedBlock.mainnetBlockNumber); const { shouldProcess, from, msg } = await this.shouldProcessSync( smartContractData, diff --git a/src/service/mainnet/index.ts b/src/service/mainnet/index.ts index a6c2838..29ce38a 100644 --- a/src/service/mainnet/index.ts +++ b/src/service/mainnet/index.ts @@ -17,7 +17,7 @@ export interface MainnetBlockInfo { mainnetBlockHash: string; mainnetBlockNumber: number; mainnetBlockRound: number; - hexRLP: string; + encodedRLP: string; parentHash: string; } export interface SmartContractData { @@ -98,7 +98,7 @@ export class MainnetService { } async submitTxs( - results: Array<{ hexRLP: string; blockNum: number }> + results: Array<{ encodedRLP: string; blockNum: number }> ): Promise { try { if (!results.length) return; @@ -107,10 +107,9 @@ export class MainnetService { results[results.length - 1].blockNum } as tx into PARENTNET` ); - //const encodedHexArray = results.map(r => "0x" + Buffer.from(r.encodedRLP, "base64").toString("hex")); //old method for reference - const hexArray = results.map((r) => "0x" + r.hexRLP); + const encodedHexArray = results.map(r => "0x" + Buffer.from(r.encodedRLP, "base64").toString("hex")); const transactionToBeSent = - await this.smartContractInstance.methods.receiveHeader(hexArray); + await this.smartContractInstance.methods.receiveHeader(encodedHexArray); const gas = await transactionToBeSent.estimateGas({ from: this.mainnetAccount.address, }); @@ -156,13 +155,12 @@ export class MainnetService { try { const { Hash, Number, Round, EncodedRLP, ParentHash } = await this.web3.xdcMainnet.getV2Block(`0x${blockNum.toString(16)}`); - const HexRLP = EncodedRLP; - if (!Hash || !Number || !HexRLP || !ParentHash) { + if (!Hash || !Number || !EncodedRLP || !ParentHash) { this.logger.error( "Invalid block hash or height or encodedRlp or ParentHash received", Hash, Number, - HexRLP, + EncodedRLP, ParentHash ); throw new Error("Unable to get committed block information by height from PARENTNET"); @@ -171,7 +169,7 @@ export class MainnetService { mainnetBlockHash: Hash, mainnetBlockNumber: Number, mainnetBlockRound: Round, - hexRLP: HexRLP, + encodedRLP: EncodedRLP, parentHash: ParentHash, }; } catch (error) { @@ -188,13 +186,12 @@ export class MainnetService { try { const { Hash, Number, Round, EncodedRLP, ParentHash } = await this.web3.xdcMainnet.getV2Block("committed"); - const HexRLP = EncodedRLP; - if (!Hash || !Number || !HexRLP || !ParentHash) { + if (!Hash || !Number || !EncodedRLP || !ParentHash) { this.logger.error( "Invalid block hash or height or encodedRlp or ParentHash received", Hash, Number, - HexRLP, + EncodedRLP, ParentHash ); throw new Error("Unable to get latest committed block information from PARENTNET"); @@ -203,7 +200,7 @@ export class MainnetService { mainnetBlockHash: Hash, mainnetBlockNumber: Number, mainnetBlockRound: Round, - hexRLP: HexRLP, + encodedRLP: EncodedRLP, parentHash: ParentHash, }; } catch (error) { @@ -218,21 +215,21 @@ export class MainnetService { async bulkGetRlpHeaders( startingBlockNumber: number, numberOfBlocksToFetch: number - ): Promise> { + ): Promise> { this.logger.info( "Fetch subnet node data from " + startingBlockNumber + " to " + (startingBlockNumber + numberOfBlocksToFetch - 1) ); - const rlpHeaders: Array<{ hexRLP: string; blockNum: number }> = []; + const rlpHeaders: Array<{ encodedRLP: string; blockNum: number }> = []; for ( let i = startingBlockNumber; i < startingBlockNumber + numberOfBlocksToFetch; i++ ) { - const { hexRLP } = await this.getCommittedBlockInfoByNum(i); - rlpHeaders.push({ hexRLP, blockNum: i }); + const { encodedRLP } = await this.getCommittedBlockInfoByNum(i); + rlpHeaders.push({ encodedRLP, blockNum: i }); await sleep(this.mainnetConfig.fetchWaitingTime); } return rlpHeaders; @@ -314,7 +311,7 @@ export class LiteMainnetService { } async submitTxs( - results: Array<{ hexRLP: string; blockNum: number }> + results: Array<{ encodedRLP: string; blockNum: number }> ): Promise { try { if (!results.length) return; @@ -326,10 +323,9 @@ export class LiteMainnetService { } as tx into PARENTNET` ); - //const encodedHexArray = results.map(r => "0x" + Buffer.from(r.encodedRLP, "base64").toString("hex")); //old method for reference - const hexArray = results.map((r) => "0x" + r.hexRLP); + const encodedHexArray = results.map(r => "0x" + Buffer.from(r.encodedRLP, "base64").toString("hex")); const transactionToBeSent = - await this.liteSmartContractInstance.methods.receiveHeader(hexArray); + await this.liteSmartContractInstance.methods.receiveHeader(encodedHexArray); const gas = await transactionToBeSent.estimateGas({ from: this.mainnetAccount.address, @@ -451,17 +447,3 @@ export class LiteMainnetService { } } - -function base64ToHex(base64String: string) { - // Step 1: Decode base64 string to binary data - const binaryString = atob(base64String); - - // Step 2: Convert binary data to hex - let hexString = ""; - for (let i = 0; i < binaryString.length; i++) { - const hex = binaryString.charCodeAt(i).toString(16); - hexString += hex.length === 2 ? hex : "0" + hex; - } - - return hexString; -} \ No newline at end of file diff --git a/src/service/subnet/index.ts b/src/service/subnet/index.ts index 0c94d3d..a53a887 100644 --- a/src/service/subnet/index.ts +++ b/src/service/subnet/index.ts @@ -16,7 +16,7 @@ export interface SubnetBlockInfo { subnetBlockHash: string; subnetBlockNumber: number; subnetBlockRound: number; - hexRLP: string; + encodedRLP: string; parentHash: string; } @@ -59,17 +59,15 @@ export class SubnetService { async getLastCommittedBlockInfo(): Promise { try { const x = await this.web3.xdcSubnet.getV2Block("committed"); - console.log(x); const { Hash, Number, Round, EncodedRLP, ParentHash } = // await this.web3.xdcSubnet.getV2Block("committed"); await this.web3.xdcSubnet.getV2Block("latest"); - const HexRLP = EncodedRLP; - if (!Hash || !Number || !HexRLP || !ParentHash) { + if (!Hash || !Number || !EncodedRLP || !ParentHash) { this.logger.error( "Invalid block hash or height or encodedRlp or ParentHash received", Hash, Number, - HexRLP, + EncodedRLP, ParentHash ); throw new Error("Unable to get latest committed block information on SUBNET"); @@ -78,7 +76,7 @@ export class SubnetService { subnetBlockHash: Hash, subnetBlockNumber: Number, subnetBlockRound: Round, - hexRLP: HexRLP, + encodedRLP: EncodedRLP, parentHash: ParentHash, }; } catch (error) { @@ -94,13 +92,12 @@ export class SubnetService { try { const { Hash, Number, Round, EncodedRLP, ParentHash } = await this.web3.xdcSubnet.getV2Block(`0x${blockNum.toString(16)}`); - const HexRLP = EncodedRLP; - if (!Hash || !Number || !HexRLP || !ParentHash) { + if (!Hash || !Number || !EncodedRLP || !ParentHash) { this.logger.error( "Invalid block hash or height or encodedRlp or ParentHash received", Hash, Number, - HexRLP, + EncodedRLP, ParentHash ); throw new Error("Unable to get committed block information by height on SUBNET"); @@ -109,7 +106,7 @@ export class SubnetService { subnetBlockHash: Hash, subnetBlockNumber: Number, subnetBlockRound: Round, - hexRLP: HexRLP, + encodedRLP: EncodedRLP, parentHash: ParentHash, }; } catch (error) { @@ -128,13 +125,12 @@ export class SubnetService { try { const { Hash, Number, Round, EncodedRLP, ParentHash } = await this.web3.xdcSubnet.getV2BlockByHash(blockHash); - const HexRLP = EncodedRLP; - if (!Hash || !Number || !HexRLP || !ParentHash) { + if (!Hash || !Number || !EncodedRLP || !ParentHash) { this.logger.error( "Invalid block hash or height or encodedRlp or ParentHash received", Hash, Number, - HexRLP, + EncodedRLP, ParentHash ); throw new Error("Unable to get committed block information by hash on SUBNET"); @@ -143,7 +139,7 @@ export class SubnetService { subnetBlockHash: Hash, subnetBlockNumber: Number, subnetBlockRound: Round, - hexRLP: HexRLP, + encodedRLP: EncodedRLP, parentHash: ParentHash, }; } catch (error) { @@ -167,21 +163,21 @@ export class SubnetService { async bulkGetRlpHeaders( startingBlockNumber: number, numberOfBlocksToFetch: number - ): Promise> { + ): Promise> { this.logger.info( "Fetch subnet node data from " + startingBlockNumber + " to " + (startingBlockNumber + numberOfBlocksToFetch - 1) ); - const rlpHeaders: Array<{ hexRLP: string; blockNum: number }> = []; + const rlpHeaders: Array<{ encodedRLP: string; blockNum: number }> = []; for ( let i = startingBlockNumber; i < startingBlockNumber + numberOfBlocksToFetch; i++ ) { - const { hexRLP } = await this.getCommittedBlockInfoByNum(i); - rlpHeaders.push({ hexRLP, blockNum: i }); + const { encodedRLP } = await this.getCommittedBlockInfoByNum(i); + rlpHeaders.push({ encodedRLP, blockNum: i }); await sleep(this.subnetConfig.fetchWaitingTime); } return rlpHeaders; @@ -241,19 +237,18 @@ export class SubnetService { } async submitTxs( - results: Array<{ hexRLP: string; blockNum: number }> + results: Array<{ encodedRLP: string; blockNum: number }> ): Promise { try { if (!results.length) return; this.logger.info( `Submit the subnet block up to ${ results[results.length - 1].blockNum - } as tx into PARENTNET` + } as tx into SUBNET` ); - //const encodedHexArray = results.map(r => "0x" + Buffer.from(r.encodedRLP, "base64").toString("hex")); //old method for reference - const hexArray = results.map((r) => "0x" + r.hexRLP); + const encodedHexArray = results.map(r => "0x" + Buffer.from(r.encodedRLP, "base64").toString("hex")); //old method for reference const transactionToBeSent = - await this.smartContractInstance.methods.receiveHeader(hexArray); + await this.smartContractInstance.methods.receiveHeader(encodedHexArray); const gas = await transactionToBeSent.estimateGas({ from: this.subnetAccount.address, }); @@ -272,7 +267,7 @@ export class SubnetService { await sleep(this.subnetConfig.submitTransactionWaitingTime); } catch (error) { - this.logger.error("Fail to submit transactions into PARENTNET", { + this.logger.error("Fail to submit transactions into SUBNET", { message: error.message, }); throw error; @@ -289,17 +284,3 @@ export class SubnetService { } } } - -function base64ToHex(base64String: string) { - // Step 1: Decode base64 string to binary data - const binaryString = atob(base64String); - - // Step 2: Convert binary data to hex - let hexString = ""; - for (let i = 0; i < binaryString.length; i++) { - const hex = binaryString.charCodeAt(i).toString(16); - hexString += hex.length === 2 ? hex : "0" + hex; - } - - return hexString; -} \ No newline at end of file