fix block header receipt trie not working
MODIFY
package-lock.json
package-lock.json
"ethereumjs-util": "^5.1.1",
"tweetnacl": "^1.0.3",
"tweetnacl-util": "^0.15.0"
+ },
+ "dependencies": {
+ "bn.js": {
+ "version": "4.12.0",
+ "resolved": "https://registry.npmjs.org/bn.js/-/bn.js-4.12.0.tgz",
+ "integrity": "sha512-c98Bf3tPniI+scsdk237ku1Dc3ujXQTSgyiPUDEOe7tRkhrqridvh8klBv0HCEso1OLOYcHuCv/cS6DNxKH+ZA=="
+ },
+ "ethereumjs-util": {
+ "version": "5.2.1",
+ "resolved": "https://registry.npmjs.org/ethereumjs-util/-/ethereumjs-util-5.2.1.tgz",
+ "integrity": "sha512-v3kT+7zdyCm1HIqWlLNrHGqHGLpGYIhjeHxQjnDXjLT2FyGJDsd3LWMYUo7pAFRrk86CR3nUJfhC81CCoJNNGQ==",
+ "requires": {
+ "bn.js": "^4.11.0",
+ "create-hash": "^1.1.2",
+ "elliptic": "^6.5.2",
+ "ethereum-cryptography": "^0.1.3",
+ "ethjs-util": "^0.1.3",
+ "rlp": "^2.0.0",
+ "safe-buffer": "^5.1.1"
+ }
+ }
}
},
"ethereum-common": {
"requires": {
"ethereum-common": "^0.0.18",
"ethereumjs-util": "^5.0.0"
- }
+ },
+ "dependencies": {
+ "bn.js": {
+ "version": "4.12.0",
+ "resolved": "https://registry.npmjs.org/bn.js/-/bn.js-4.12.0.tgz",
+ "integrity": "sha512-c98Bf3tPniI+scsdk237ku1Dc3ujXQTSgyiPUDEOe7tRkhrqridvh8klBv0HCEso1OLOYcHuCv/cS6DNxKH+ZA=="
},
"ethereumjs-util": {
"version": "5.2.1",
"ethjs-util": "^0.1.3",
"rlp": "^2.0.0",
"safe-buffer": "^5.1.1"
+ }
+ }
+ }
+ },
+ "ethereumjs-util": {
+ "version": "7.1.4",
+ "resolved": "https://registry.npmjs.org/ethereumjs-util/-/ethereumjs-util-7.1.4.tgz",
+ "integrity": "sha512-p6KmuPCX4mZIqsQzXfmSx9Y0l2hqf+VkAiwSisW3UKUFdk8ZkAt+AYaor83z2nSi6CU2zSsXMlD80hAbNEGM0A==",
+ "requires": {
+ "@types/bn.js": "^5.1.0",
+ "bn.js": "^5.1.2",
+ "create-hash": "^1.1.2",
+ "ethereum-cryptography": "^0.1.3",
+ "rlp": "^2.2.4"
},
"dependencies": {
"bn.js": {
- "version": "4.12.0",
- "resolved": "https://registry.npmjs.org/bn.js/-/bn.js-4.12.0.tgz",
- "integrity": "sha512-c98Bf3tPniI+scsdk237ku1Dc3ujXQTSgyiPUDEOe7tRkhrqridvh8klBv0HCEso1OLOYcHuCv/cS6DNxKH+ZA=="
+ "version": "5.2.0",
+ "resolved": "https://registry.npmjs.org/bn.js/-/bn.js-5.2.0.tgz",
+ "integrity": "sha512-D7iWRBvnZE8ecXiLj/9wbxH7Tk79fAh8IHaTNq1RWRixsS02W+5qS+iE9yq6RYl0asXx5tw0bLhmT5pIfbSquw=="
}
}
},

MODIFY
package.json
package.json
"@ethereumjs/block": "^3.6.2",
"eth-sig-util": "^2.5.3",
"ethereumjs-tx": "1.3.7",
- "ethereumjs-util": "^5.2.0",
+ "ethereumjs-util": "^7.1.4",
"merkle-patricia-tree": "^4.2.4",
"node-fetch": "^2.6.1"
},

MODIFY
src/pos/exit_util.ts
src/pos/exit_util.ts
import { RootChain } from "./root_chain";
import { Converter, ProofUtil, Web3SideChainClient } from "../utils";
-import ethUtils from "ethereumjs-util";
+import { bufferToHex, rlp } from "ethereumjs-util";
import { IBlockWithTransaction, ITransactionReceipt } from "../interfaces";
import { service } from "../services";
import { BaseBigNumber, BaseWeb3Client } from "../abstracts";
@@ -256,17 +256,17 @@ export class ExitUtil {
path,
logIndex
) {
- return ethUtils.bufferToHex(
- ethUtils.rlp.encode([
+ return bufferToHex(
+ rlp.encode([
headerNumber,
buildBlockProof,
blockNumber,
timestamp,
- ethUtils.bufferToHex(transactionsRoot),
- ethUtils.bufferToHex(receiptsRoot),
- ethUtils.bufferToHex(receipt),
- ethUtils.bufferToHex(ethUtils.rlp.encode(receiptParentNodes)),
- ethUtils.bufferToHex(Buffer.concat([Buffer.from('00', 'hex'), path])),
+ bufferToHex(transactionsRoot),
+ bufferToHex(receiptsRoot),
+ bufferToHex(receipt),
+ bufferToHex(rlp.encode(receiptParentNodes)),
+ bufferToHex(Buffer.concat([Buffer.from('00', 'hex'), path])),
logIndex,
])
);
@@ -306,7 +306,7 @@ export class ExitUtil {
});
return this.maticClient_.etheriumSha3(
- receipt.blockNumber, ethUtils.bufferToHex(Buffer.concat(nibbleArr)), logIndex
+ receipt.blockNumber, bufferToHex(Buffer.concat(nibbleArr)), logIndex
);
});
}

MODIFY
src/utils/proof_util.ts
src/utils/proof_util.ts
import { BaseWeb3Client } from "../abstracts";
import { MerkleTree } from "./merkle_tree";
-import ethUtils from "ethereumjs-util";
+import { bufferToHex, keccak256, rlp, setLengthLeft, toBuffer } from "ethereumjs-util";
import { ITransactionReceipt, IBlock, IBlockWithTransaction } from "../interfaces";
import { mapPromise } from "./map_promise";
// const TRIE = require('merkle-patricia-tree');
import { BaseTrie as TRIE } from 'merkle-patricia-tree';
-const rlp = ethUtils.rlp;
// import EthereumBlock from 'ethereumjs-block/from-rpc';
-import { Block as EthereumBlock } from '@ethereumjs/block';
+import { Block as EthereumBlock, BlockHeader } from '@ethereumjs/block';
import { Converter, promiseResolve, utils } from "..";
+// const rlp = ethUtils.rlp;
// Implementation adapted from Tom French's `matic-proofs` library used under MIT License
// https://github.com/TomAFrench/matic-proofs
@@ -75,7 +75,7 @@ export class ProofUtil {
const leafRoots = this.recursiveZeroHash(subTreeHeight, web3);
// Build a merkle tree of correct size for the subtree using these merkle roots
- const leaves = Array.from({ length: 2 ** heightDifference }, () => ethUtils.toBuffer(leafRoots));
+ const leaves = Array.from({ length: 2 ** heightDifference }, () => toBuffer(leafRoots));
leaves[0] = remainingNodesHash;
const subTreeMerkleRoot = new MerkleTree(leaves).getRoot();
reversedProof.push(subTreeMerkleRoot);
@@ -91,10 +91,10 @@ export class ProofUtil {
return ProofUtil.getFastMerkleProof(
maticWeb3, blockNumber, startBlock, endBlock
).then(proof => {
- return ethUtils.bufferToHex(
+ return bufferToHex(
Buffer.concat(
proof.map(p => {
- return ethUtils.toBuffer(p);
+ return toBuffer(p);
})
)
);
@@ -103,7 +103,7 @@ export class ProofUtil {
static queryRootHash(client: BaseWeb3Client, startBlock: number, endBlock: number) {
return client.getRootHash(startBlock, endBlock).then(rootHash => {
- return ethUtils.toBuffer(`0x${rootHash}`);
+ return toBuffer(`0x${rootHash}`);
}).catch(_ => {
return null;
});
@@ -112,13 +112,13 @@ export class ProofUtil {
static recursiveZeroHash(n: number, client: BaseWeb3Client) {
if (n === 0) return '0x0000000000000000000000000000000000000000000000000000000000000000';
const subHash = this.recursiveZeroHash(n - 1, client);
- return ethUtils.keccak256(
- ethUtils.toBuffer(client.encodeParameters([subHash, subHash], ['bytes32', 'bytes32'],))
+ return keccak256(
+ toBuffer(client.encodeParameters([subHash, subHash], ['bytes32', 'bytes32'],))
);
}
static getReceiptProof(receipt: ITransactionReceipt, block: IBlockWithTransaction, web3: BaseWeb3Client, requestConcurrency = Infinity, receiptsVal?: ITransactionReceipt[]) {
- const stateSyncTxHash = ethUtils.bufferToHex(ProofUtil.getStateSyncTxHash(block));
+ const stateSyncTxHash = bufferToHex(ProofUtil.getStateSyncTxHash(block));
const receiptsTrie = new TRIE();
let receiptPromise: Promise<ITransactionReceipt[]>;
if (!receiptsVal) {
@@ -180,7 +180,7 @@ export class ProofUtil {
// }
// const prf = {
- // blockHash: ethUtils.toBuffer(receipt.blockHash),
+ // blockHash: toBuffer(receipt.blockHash),
// parentNodes: stack.map(s => s.raw),
// root: ProofUtil.getRawHeader(block).receiptTrie,
// path: rlp.encode(receipt.transactionIndex),
@@ -195,8 +195,8 @@ export class ProofUtil {
}
// result.node.value
const prf = {
- blockHash: ethUtils.toBuffer(receipt.blockHash),
- parentNodes: result.stack.map(s => s.raw),
+ blockHash: toBuffer(receipt.blockHash),
+ parentNodes: result.stack.map(s => s.raw()),
root: ProofUtil.getRawHeader(block).receiptTrie,
path: rlp.encode(receipt.transactionIndex),
value: ProofUtil.isTypedReceipt(receipt) ? result.node.value : rlp.decode(result.node.value)
@@ -218,43 +218,53 @@ export class ProofUtil {
// This is derived from block's hash and number
// state-sync tx hash = keccak256("matic-bor-receipt-" + block.number + block.hash)
static getStateSyncTxHash(block): Buffer {
- return ethUtils.keccak256(
+ return keccak256(
Buffer.concat([
- ethUtils.toBuffer('matic-bor-receipt-'), // prefix for bor receipt
- ethUtils.setLengthLeft(ethUtils.toBuffer(block.number), 8), // 8 bytes of block number (BigEndian)
- ethUtils.toBuffer(block.hash), // block hash
+ toBuffer(
+ Converter.toHex('matic-bor-receipt-')
+ ), // prefix for bor receipt
+ setLengthLeft(toBuffer(block.number), 8), // 8 bytes of block number (BigEndian)
+ toBuffer(block.hash), // block hash
])
);
}
static getReceiptBytes(receipt: ITransactionReceipt) {
let encodedData = rlp.encode([
- ethUtils.toBuffer(
+ toBuffer(
receipt.status !== undefined && receipt.status != null ? (receipt.status ? '0x1' : '0x') : receipt.root
),
- ethUtils.toBuffer(receipt.cumulativeGasUsed),
- ethUtils.toBuffer(receipt.logsBloom),
+ toBuffer(receipt.cumulativeGasUsed),
+ toBuffer(receipt.logsBloom),
// encoded log array
receipt.logs.map(l => {
// [address, [topics array], data]
return [
- ethUtils.toBuffer(l.address), // convert address to buffer
- l.topics.map(ethUtils.toBuffer), // convert topics to buffer
- ethUtils.toBuffer(l.data), // convert data to buffer
+ toBuffer(l.address), // convert address to buffer
+ l.topics.map(toBuffer), // convert topics to buffer
+ toBuffer(l.data), // convert data to buffer
];
}),
]);
if (ProofUtil.isTypedReceipt(receipt)) {
- encodedData = Buffer.concat([ethUtils.toBuffer(receipt.type), encodedData]);
+ encodedData = Buffer.concat([toBuffer(receipt.type), encodedData]);
}
return encodedData;
}
static getRawHeader(_block) {
- if (typeof _block.difficulty !== 'string') {
- _block.difficulty = '0x' + _block.difficulty.toString(16);
- }
- const block = new EthereumBlock(_block);
- return block.header;
+ // if (typeof _block.difficulty !== 'string') {
+ // _block.difficulty = '0x' + _block.difficulty.toString(16);
+ // }
+ _block.difficulty = Converter.toHex(_block.difficulty);
+ // console.log('_block', _block);
+ // const block = EthereumBlock.fromBlockData({
+ // transactions: _block.transactions,
+ // // uncleHeaders:
+ // });
+ // block.header.receiptTrie
+ const rawHeader = BlockHeader.fromHeaderData(_block);
+ console.log(`block header`, rawHeader.receiptTrie);
+ return rawHeader;
}
}

MODIFY
test/debug.js
test/debug.js
@@ -43,7 +43,7 @@ const execute = async () => {
// setProofApi("https://apis.matic.network");
- var result = await goerliERC20Token.withdrawExit('0x1c20c41b9d97d1026aa456a21f13725df63edec1b1f43aacb180ebcc6340a2d3', {
+ var result = await goerliERC20Token.isWithdrawExited('0x1c20c41b9d97d1026aa456a21f13725df63edec1b1f43aacb180ebcc6340a2d3', {
returnTransaction: true
});

fix block header receipt trie not working