Skip to content

Commit

Permalink
hexRLP > encodedRLP
Browse files Browse the repository at this point in the history
  • Loading branch information
wanwiset25 committed Jun 5, 2024
1 parent c499c34 commit 3c9d94d
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 82 deletions.
2 changes: 1 addition & 1 deletion src/processors/lite.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
10 changes: 2 additions & 8 deletions src/processors/reverseFull.ts
Original file line number Diff line number Diff line change
@@ -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";
Expand Down Expand Up @@ -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,
Expand Down
52 changes: 17 additions & 35 deletions src/service/mainnet/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ export interface MainnetBlockInfo {
mainnetBlockHash: string;
mainnetBlockNumber: number;
mainnetBlockRound: number;
hexRLP: string;
encodedRLP: string;
parentHash: string;
}
export interface SmartContractData {
Expand Down Expand Up @@ -98,7 +98,7 @@ export class MainnetService {
}

async submitTxs(
results: Array<{ hexRLP: string; blockNum: number }>
results: Array<{ encodedRLP: string; blockNum: number }>
): Promise<void> {
try {
if (!results.length) return;
Expand All @@ -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,
});
Expand Down Expand Up @@ -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");
Expand All @@ -171,7 +169,7 @@ export class MainnetService {
mainnetBlockHash: Hash,
mainnetBlockNumber: Number,
mainnetBlockRound: Round,
hexRLP: HexRLP,
encodedRLP: EncodedRLP,
parentHash: ParentHash,
};
} catch (error) {
Expand All @@ -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");
Expand All @@ -203,7 +200,7 @@ export class MainnetService {
mainnetBlockHash: Hash,
mainnetBlockNumber: Number,
mainnetBlockRound: Round,
hexRLP: HexRLP,
encodedRLP: EncodedRLP,
parentHash: ParentHash,
};
} catch (error) {
Expand All @@ -218,21 +215,21 @@ export class MainnetService {
async bulkGetRlpHeaders(
startingBlockNumber: number,
numberOfBlocksToFetch: number
): Promise<Array<{ hexRLP: string; blockNum: number }>> {
): Promise<Array<{ encodedRLP: string; blockNum: number }>> {
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;
Expand Down Expand Up @@ -314,7 +311,7 @@ export class LiteMainnetService {
}

async submitTxs(
results: Array<{ hexRLP: string; blockNum: number }>
results: Array<{ encodedRLP: string; blockNum: number }>
): Promise<void> {
try {
if (!results.length) return;
Expand All @@ -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,
Expand Down Expand Up @@ -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;
}
57 changes: 19 additions & 38 deletions src/service/subnet/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ export interface SubnetBlockInfo {
subnetBlockHash: string;
subnetBlockNumber: number;
subnetBlockRound: number;
hexRLP: string;
encodedRLP: string;
parentHash: string;
}

Expand Down Expand Up @@ -59,17 +59,15 @@ export class SubnetService {
async getLastCommittedBlockInfo(): Promise<SubnetBlockInfo> {
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");
Expand All @@ -78,7 +76,7 @@ export class SubnetService {
subnetBlockHash: Hash,
subnetBlockNumber: Number,
subnetBlockRound: Round,
hexRLP: HexRLP,
encodedRLP: EncodedRLP,
parentHash: ParentHash,
};
} catch (error) {
Expand All @@ -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");
Expand All @@ -109,7 +106,7 @@ export class SubnetService {
subnetBlockHash: Hash,
subnetBlockNumber: Number,
subnetBlockRound: Round,
hexRLP: HexRLP,
encodedRLP: EncodedRLP,
parentHash: ParentHash,
};
} catch (error) {
Expand All @@ -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");
Expand All @@ -143,7 +139,7 @@ export class SubnetService {
subnetBlockHash: Hash,
subnetBlockNumber: Number,
subnetBlockRound: Round,
hexRLP: HexRLP,
encodedRLP: EncodedRLP,
parentHash: ParentHash,
};
} catch (error) {
Expand All @@ -167,21 +163,21 @@ export class SubnetService {
async bulkGetRlpHeaders(
startingBlockNumber: number,
numberOfBlocksToFetch: number
): Promise<Array<{ hexRLP: string; blockNum: number }>> {
): Promise<Array<{ encodedRLP: string; blockNum: number }>> {
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;
Expand Down Expand Up @@ -241,19 +237,18 @@ export class SubnetService {
}

async submitTxs(
results: Array<{ hexRLP: string; blockNum: number }>
results: Array<{ encodedRLP: string; blockNum: number }>
): Promise<void> {
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,
});
Expand All @@ -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;
Expand All @@ -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;
}

0 comments on commit 3c9d94d

Please sign in to comment.