Skip to content

Commit

Permalink
Audit/oz m05 merkletrie bug 193 (#102)
Browse files Browse the repository at this point in the history
* add revert for bad prefix

* sad path merkletrie tests

* bad node prefix test
  • Loading branch information
axchu authored Dec 8, 2020
1 parent 7c2a09a commit ebfafa3
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -328,6 +328,8 @@ library Lib_MerkleTrie {
currentKeyIncrement = sharedNibbleLength;
continue;
}
} else {
revert("Received a node with an unknown prefix");
}
} else {
revert("Received an unparseable node.");
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import { expect } from '../../../setup'

/* External Imports */
import * as rlp from 'rlp'
import { ethers } from '@nomiclabs/buidler'
import { Contract } from 'ethers'

/* Internal Imports */
import { TrieTestGenerator } from '../../../helpers'
import { toHexString, TrieTestGenerator } from '../../../helpers'

const NODE_COUNTS = [1, 2, 128]

Expand Down Expand Up @@ -107,13 +108,61 @@ describe('Lib_MerkleTrie', () => {
) {
it(`should correctly get the value of node #${i}`, async () => {
const test = await generator.makeInclusionProofTest(i)

expect(
await Lib_MerkleTrie.get(test.key, test.proof, test.root)
).to.deep.equal([true, test.val])
})
if (i > 3) {
it(`should revert when the proof node does not pass the root check`, async () => {
const test = await generator.makeInclusionProofTest(i-1)
const test2 = await generator.makeInclusionProofTest(i-2)
await expect (
Lib_MerkleTrie.get(test2.key, test.proof, test.root)
).to.be.revertedWith("Invalid large internal hash")
})
it(`should revert when the first proof element is not the root node`, async () => {
const test = await generator.makeInclusionProofTest(0)
let decodedProof = rlp.decode(test.proof)
decodedProof[0].write('abcd', 8) // change the 1st element (root) of the proof
const badProof = rlp.encode(decodedProof as rlp.Input)
await expect (
Lib_MerkleTrie.get(test.key, badProof, test.root)
).to.be.revertedWith("Invalid root hash")
})
it(`should be false when calling get on an incorrect key`, async () => {
const test = await generator.makeInclusionProofTest(i-1)
let newKey = test.key.slice(0, test.key.length - 8)
newKey = newKey.concat('88888888')
expect (
await Lib_MerkleTrie.get(newKey, test.proof, test.root)
).to.deep.equal([false, '0x'])
})
}
}
})
}
})
})

describe(`inside a trie with one node`, () => {
let generator: TrieTestGenerator
const nodeCount = 1
before(async () => {
generator = await TrieTestGenerator.fromRandom({
seed: `seed.get.${nodeCount}`,
nodeCount,
secure: false,
})
})

it(`should revert on an incorrect proof node prefix`, async () => {
const test = await generator.makeInclusionProofTest(0)
let decodedProof = rlp.decode(test.proof)
decodedProof[0].write('a', 3) // change the prefix
test.root = ethers.utils.keccak256(toHexString(decodedProof[0]));
const badProof = rlp.encode(decodedProof as rlp.Input)
await expect (
Lib_MerkleTrie.get(test.key, badProof, test.root)
).to.be.revertedWith("Received a node with an unknown prefix")
})
})
})

0 comments on commit ebfafa3

Please sign in to comment.