fix: Math.floor :bug: in MerkleTree / parallelize fetch block proofs
MODIFY
src/common/ExitManager.ts
src/common/ExitManager.ts
@@ -44,9 +44,9 @@ export default class ExitManager extends ContractsBase {
// build block proof
const blockProof = await Proofs.buildBlockProof(
this.web3Client.getMaticWeb3(),
- headerBlock.start,
- headerBlock.end,
- burnTx.blockNumber
+ parseInt(headerBlock.start, 10),
+ parseInt(headerBlock.end, 10),
+ parseInt(burnTx.blockNumber + '', 10)
)
const receiptProof: any = await Proofs.getReceiptProof(receipt, block, this.web3Client.getMaticWeb3())

MODIFY
src/libs/MerkleTree.ts
src/libs/MerkleTree.ts
@@ -17,11 +17,7 @@ export default class MerkleTree {
throw new Error('Depth must be 20 or less')
}
- const l = leaves.concat(
- Array.from(Array(Math.pow(2, depth) - leaves.length), () =>
- utils.zeros(32)
- )
- )
+ const l = leaves.concat(Array.from(Array(Math.pow(2, depth) - leaves.length), () => utils.zeros(32)))
this.leaves = l
this.layers = [l]
@@ -79,7 +75,7 @@ export default class MerkleTree {
} else {
siblingIndex = index - 1
}
- index = index / 2
+ index = Math.floor(index / 2)
proof.push(this.layers[i][siblingIndex])
}
}
@@ -100,7 +96,7 @@ export default class MerkleTree {
hash = sha3(SafeBuffer.concat([node, hash]))
}
- index = index / 2
+ index = Math.floor(index / 2)
}
return SafeBuffer.compare(hash, root) === 0

MODIFY
src/libs/ProofsUtil.ts
src/libs/ProofsUtil.ts
const BN = require('bn.js')
+const bluebird = require('bluebird')
const Trie = require('merkle-patricia-tree')
const EthereumTx = require('ethereumjs-tx')
@@ -30,17 +31,20 @@ export default class ProofsUtil {
static async buildBlockProof(web3, start, end, blockNumber) {
logger.debug('buildBlockProof...', start, end, blockNumber)
const tree = await ProofsUtil.buildBlockHeaderMerkle(web3, start, end)
- const proof = tree.getProof(ProofsUtil.getBlockHeader(await web3.eth.getBlock(blockNumber, true)))
+ const proof = tree.getProof(ProofsUtil.getBlockHeader(await web3.eth.getBlock(blockNumber)))
return ethUtils.bufferToHex(Buffer.concat(proof))
}
static async buildBlockHeaderMerkle(web3, start, end) {
- const headers = []
- for (let i = start; i <= end; i++) {
+ const headers = new Array(end - start + 1)
+ await bluebird.map(
+ headers,
+ async (_, i) => {
logger.debug('fetching block', i)
- const _blockHeader = ProofsUtil.getBlockHeader(await web3.eth.getBlock(i, true))
- headers.push(_blockHeader)
- }
+ headers[i] = ProofsUtil.getBlockHeader(await web3.eth.getBlock(i + start))
+ },
+ { concurrency: 20 }
+ )
return new MerkleTree(headers)
}

fix: Math.floor :bug: in MerkleTree / parallelize fetch block proofs