Skip to content

Commit

Permalink
fix: ethereumjs-util
Browse files Browse the repository at this point in the history
  • Loading branch information
nitinmittal23 committed Aug 6, 2024
1 parent 7077e83 commit 99691e7
Show file tree
Hide file tree
Showing 13 changed files with 971 additions and 1,610 deletions.
1,956 changes: 545 additions & 1,411 deletions package-lock.json

Large diffs are not rendered by default.

12 changes: 8 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,17 +40,21 @@
},
"homepage": "https://github.com/maticnetwork/matic.js#readme",
"dependencies": {
"@ethereumjs/block": "^3.6.2",
"@ethereumjs/block": "^5.2.0",
"@ethereumjs/trie": "^6.2.0",
"@ethereumjs/util": "^9.0.3",
"assert": "^2.1.0",
"ethereumjs-util": "^7.1.4",
"merkle-patricia-tree": "^4.2.4",
"bn.js": "^5.2.1",
"buffer": "^6.0.3",
"ethereum-cryptography": "^2.2.1",
"node-fetch": "^2.6.1",
"rlp": "^3.0.0",
"stream": "^0.0.3"
},
"devDependencies": {
"@types/node-fetch": "^2.6.11",
"copy-webpack-plugin": "^12.0.2",
"@typescript-eslint/parser": "^7.3.1",
"copy-webpack-plugin": "^12.0.2",
"cross-env": "^7.0.3",
"husky": "^9.0.11",
"lint-staged": "^15.2.2",
Expand Down
301 changes: 151 additions & 150 deletions src/pos/exit_util.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { RootChain } from "./root_chain";
import { Converter, ProofUtil, Web3SideChainClient } from "../utils";
import { bufferToHex, rlp } from "ethereumjs-util";
import { BufferUtil } from "../utils/buffer-utils";
import rlp from "rlp"
import { IBlockWithTransaction, ITransactionReceipt } from "../interfaces";
import { service } from "../services";
import { BaseBigNumber, BaseWeb3Client } from "../abstracts";
Expand Down Expand Up @@ -62,50 +63,50 @@ export class ExitUtil {
}

private getAllLogIndices_(logEventSig: string, receipt: ITransactionReceipt) {
let logIndices = [];

switch (logEventSig) {
case '0xddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef':
case '0xf94915c6d1fd521cee85359239227480c7e8776d7caf1fc3bacad5c269b66a14':
logIndices = receipt.logs.reduce(
(_, log, index) =>
((log.topics[0].toLowerCase() === logEventSig.toLowerCase() &&
log.topics[2].toLowerCase() === '0x0000000000000000000000000000000000000000000000000000000000000000') &&
logIndices.push(index), logIndices), []
);
break;

case '0xc3d58168c5ae7397731d063d5bbf3d657854427343f4c083240f7aacaa2d0f62':
case '0x4a39dc06d4c0dbc64b70af90fd698a233a518aa5d07e595d983b8c0526c8f7fb':
logIndices = receipt.logs.reduce(
(_, log, index) =>
let logIndices = [];

switch (logEventSig) {
case '0xddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef':
case '0xf94915c6d1fd521cee85359239227480c7e8776d7caf1fc3bacad5c269b66a14':
logIndices = receipt.logs.reduce(
(_, log, index) =>
((log.topics[0].toLowerCase() === logEventSig.toLowerCase() &&
log.topics[3].toLowerCase() === '0x0000000000000000000000000000000000000000000000000000000000000000') &&
logIndices.push(index), logIndices), []
);
break;

case '0xf871896b17e9cb7a64941c62c188a4f5c621b86800e3d15452ece01ce56073df':
logIndices = receipt.logs.reduce(
(_, log, index) =>
log.topics[2].toLowerCase() === '0x0000000000000000000000000000000000000000000000000000000000000000') &&
logIndices.push(index), logIndices), []
);
break;

case '0xc3d58168c5ae7397731d063d5bbf3d657854427343f4c083240f7aacaa2d0f62':
case '0x4a39dc06d4c0dbc64b70af90fd698a233a518aa5d07e595d983b8c0526c8f7fb':
logIndices = receipt.logs.reduce(
(_, log, index) =>
((log.topics[0].toLowerCase() === logEventSig.toLowerCase() &&
log.topics[3].toLowerCase() === '0x0000000000000000000000000000000000000000000000000000000000000000') &&
logIndices.push(index), logIndices), []
);
break;

case '0xf871896b17e9cb7a64941c62c188a4f5c621b86800e3d15452ece01ce56073df':
logIndices = receipt.logs.reduce(
(_, log, index) =>
((log.topics[0].toLowerCase() === '0xddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef' &&
log.topics[2].toLowerCase() === '0x0000000000000000000000000000000000000000000000000000000000000000') &&
logIndices.push(index), logIndices), []
);
break;

default:
logIndices = receipt.logs.reduce(
(_, log, index) =>
((log.topics[0].toLowerCase() === logEventSig.toLowerCase()) &&
logIndices.push(index), logIndices), []
);
}
if (logIndices.length === 0) {
throw new Error("Log not found in receipt");
}
return logIndices;
}
log.topics[2].toLowerCase() === '0x0000000000000000000000000000000000000000000000000000000000000000') &&
logIndices.push(index), logIndices), []
);
break;

default:
logIndices = receipt.logs.reduce(
(_, log, index) =>
((log.topics[0].toLowerCase() === logEventSig.toLowerCase()) &&
logIndices.push(index), logIndices), []
);
}
if (logIndices.length === 0) {
throw new Error("Log not found in receipt");
}
return logIndices;
}

getChainBlockInfo(burnTxHash: string) {
return Promise.all([
Expand Down Expand Up @@ -240,7 +241,7 @@ export class ExitUtil {
}

if (index < 0) {
throw new Error('Index must not be a negative integer');
throw new Error('Index must not be a negative integer');
}

let txBlockNumber: number,
Expand Down Expand Up @@ -291,27 +292,27 @@ export class ExitUtil {
// step 6 - encode payload, convert into hex

// when token index is not 0
if(index > 0) {
const logIndices = this.getAllLogIndices_(
logEventSig, receipt
);

if(index >= logIndices.length) {
throw new Error('Index is greater than the number of tokens in this transaction');
}
if (index > 0) {
const logIndices = this.getAllLogIndices_(
logEventSig, receipt
);

return this.encodePayload_(
rootBlockInfo.headerBlockNumber.toNumber(),
blockProof,
txBlockNumber,
block.timestamp,
Buffer.from(block.transactionsRoot.slice(2), 'hex'),
Buffer.from(block.receiptsRoot.slice(2), 'hex'),
ProofUtil.getReceiptBytes(receipt), // rlp encoded
receiptProof.parentNodes,
receiptProof.path,
logIndices[index]
);
if (index >= logIndices.length) {
throw new Error('Index is greater than the number of tokens in this transaction');
}

return this.encodePayload_(
rootBlockInfo.headerBlockNumber.toNumber(),
blockProof,
txBlockNumber,
block.timestamp,
Buffer.from(block.transactionsRoot.slice(2), 'hex'),
Buffer.from(block.receiptsRoot.slice(2), 'hex'),
ProofUtil.getReceiptBytes(receipt), // rlp encoded
receiptProof.parentNodes,
receiptProof.path,
logIndices[index]
);
}

// when token index is 0
Expand All @@ -336,83 +337,83 @@ export class ExitUtil {

buildMultiplePayloadsForExit(burnTxHash: string, logEventSig: string, isFast: boolean) {

if (isFast && !service.network) {
new ErrorHelper(ERROR_TYPE.ProofAPINotSet).throw();
}

let txBlockNumber: number,
rootBlockInfo: IRootBlockInfo,
receipt: ITransactionReceipt,
block: IBlockWithTransaction,
blockProof;

return this.getChainBlockInfo(
burnTxHash
).then(blockInfo => {
if (!isFast && !this.isCheckPointed_(blockInfo)) {
throw new Error(
'Burn transaction has not been checkpointed as yet'
);
}

// step 1 - Get Block number from transaction hash
txBlockNumber = blockInfo.txBlockNumber;
// step 2- get transaction receipt from txhash and
// block information from block number
return Promise.all([
this.maticClient_.getTransactionReceipt(burnTxHash),
this.maticClient_.getBlockWithTransaction(txBlockNumber)
]);
}).then(result => {
[receipt, block] = result;
// step 3 - get information about block saved in parent chain
return (
isFast ? this.getRootBlockInfoFromAPI(txBlockNumber) :
this.getRootBlockInfo(txBlockNumber)
);
}).then(rootBlockInfoResult => {
rootBlockInfo = rootBlockInfoResult;
// step 4 - build block proof
return (
isFast ? this.getBlockProofFromAPI(txBlockNumber, rootBlockInfo) :
this.getBlockProof(txBlockNumber, rootBlockInfo)
);
}).then(blockProofResult => {
blockProof = blockProofResult;
// step 5- create receipt proof
return ProofUtil.getReceiptProof(
receipt,
block,
this.maticClient_,
this.requestConcurrency
);
}).then((receiptProof: any) => {
const logIndices = this.getAllLogIndices_(
logEventSig, receipt
);
const payloads:string[] = [];

// step 6 - encode payloads, convert into hex
for (const logIndex of logIndices){
payloads.push(
this.encodePayload_(
rootBlockInfo.headerBlockNumber.toNumber(),
blockProof,
txBlockNumber,
block.timestamp,
Buffer.from(block.transactionsRoot.slice(2), 'hex'),
Buffer.from(block.receiptsRoot.slice(2), 'hex'),
ProofUtil.getReceiptBytes(receipt), // rlp encoded
receiptProof.parentNodes,
receiptProof.path,
logIndex
)
if (isFast && !service.network) {
new ErrorHelper(ERROR_TYPE.ProofAPINotSet).throw();
}

let txBlockNumber: number,
rootBlockInfo: IRootBlockInfo,
receipt: ITransactionReceipt,
block: IBlockWithTransaction,
blockProof;

return this.getChainBlockInfo(
burnTxHash
).then(blockInfo => {
if (!isFast && !this.isCheckPointed_(blockInfo)) {
throw new Error(
'Burn transaction has not been checkpointed as yet'
);
}

// step 1 - Get Block number from transaction hash
txBlockNumber = blockInfo.txBlockNumber;
// step 2- get transaction receipt from txhash and
// block information from block number
return Promise.all([
this.maticClient_.getTransactionReceipt(burnTxHash),
this.maticClient_.getBlockWithTransaction(txBlockNumber)
]);
}).then(result => {
[receipt, block] = result;
// step 3 - get information about block saved in parent chain
return (
isFast ? this.getRootBlockInfoFromAPI(txBlockNumber) :
this.getRootBlockInfo(txBlockNumber)
);
}).then(rootBlockInfoResult => {
rootBlockInfo = rootBlockInfoResult;
// step 4 - build block proof
return (
isFast ? this.getBlockProofFromAPI(txBlockNumber, rootBlockInfo) :
this.getBlockProof(txBlockNumber, rootBlockInfo)
);
}).then(blockProofResult => {
blockProof = blockProofResult;
// step 5- create receipt proof
return ProofUtil.getReceiptProof(
receipt,
block,
this.maticClient_,
this.requestConcurrency
);
}).then((receiptProof: any) => {
const logIndices = this.getAllLogIndices_(
logEventSig, receipt
);
}
const payloads: string[] = [];

// step 6 - encode payloads, convert into hex
for (const logIndex of logIndices) {
payloads.push(
this.encodePayload_(
rootBlockInfo.headerBlockNumber.toNumber(),
blockProof,
txBlockNumber,
block.timestamp,
Buffer.from(block.transactionsRoot.slice(2), 'hex'),
Buffer.from(block.receiptsRoot.slice(2), 'hex'),
ProofUtil.getReceiptBytes(receipt), // rlp encoded
receiptProof.parentNodes,
receiptProof.path,
logIndex
)
);
}

return payloads;
});
}
return payloads;
});
}

private encodePayload_(
headerNumber,
Expand All @@ -426,19 +427,19 @@ export class ExitUtil {
path,
logIndex
) {
return bufferToHex(
return BufferUtil.bufferToHex(
rlp.encode([
headerNumber,
buildBlockProof,
blockNumber,
timestamp,
bufferToHex(transactionsRoot),
bufferToHex(receiptsRoot),
bufferToHex(receipt),
bufferToHex(rlp.encode(receiptParentNodes)),
bufferToHex(Buffer.concat([Buffer.from('00', 'hex'), path])),
BufferUtil.bufferToHex(transactionsRoot),
BufferUtil.bufferToHex(receiptsRoot),
BufferUtil.bufferToHex(receipt),
BufferUtil.bufferToHex(rlp.encode(receiptParentNodes) as Buffer),
BufferUtil.bufferToHex(Buffer.concat([Buffer.from('00', 'hex'), path])),
logIndex,
])
]) as Buffer
);
}

Expand Down Expand Up @@ -475,15 +476,15 @@ export class ExitUtil {
nibbleArr.push(Buffer.from('0' + (byte % 0x10).toString(16), 'hex'));
});

if(index > 0) {
const logIndices = this.getAllLogIndices_(logEventSig, receipt);
logIndex = logIndices[index];
if (index > 0) {
const logIndices = this.getAllLogIndices_(logEventSig, receipt);
logIndex = logIndices[index];
}

logIndex = this.getLogIndex_(logEventSig, receipt);

return this.maticClient_.etheriumSha3(
receipt.blockNumber, bufferToHex(Buffer.concat(nibbleArr)), logIndex
receipt.blockNumber, BufferUtil.bufferToHex(Buffer.concat(nibbleArr)), logIndex
);
});
}
Expand Down
Loading

0 comments on commit 99691e7

Please sign in to comment.