Skip to content

Commit

Permalink
Contract Compatibility (#459)
Browse files Browse the repository at this point in the history
* Reversing the order of siblings in merkle proofs such that the first in the list is the leaf sibling
* making token type a uint instead of a bool
* fixing unit tests that reversed siblings
* bumping contracts tests timeou
* Changing sleep test since it tests something we cannot control
  • Loading branch information
willmeister committed Sep 26, 2019
1 parent bf816d4 commit 42f632c
Show file tree
Hide file tree
Showing 8 changed files with 36 additions and 40 deletions.
2 changes: 1 addition & 1 deletion packages/contracts/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
"build/**/.js"
],
"scripts": {
"test": "waffle waffle-config.json && mocha --require ts-node/register 'test/**/*.spec.ts'",
"test": "waffle waffle-config.json && mocha --require ts-node/register 'test/**/*.spec.ts' --timeout 5000",
"lint": "tslint --format stylish --project .",
"fix": "prettier --config ./.prettierrc.js --write 'index.ts' '{src,test}/**/*.ts'",
"build": "waffle waffle-config.json && tsc -p .",
Expand Down
6 changes: 3 additions & 3 deletions packages/contracts/test/helpers/RollupBlock.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,9 +83,9 @@ export class RollupBlock {
hexStrToBuf(this.encodedTransitions[transitionIndex])
)
const path = bufToHexString(blockInclusion.key.toBuffer('B', 32))
const siblings = blockInclusion.siblings
.map((sibBuf) => bufToHexString(sibBuf))
.reverse()
const siblings = blockInclusion.siblings.map((sibBuf) =>
bufToHexString(sibBuf)
)
return {
blockNumber: this.blockNumber,
transitionIndex,
Expand Down
16 changes: 6 additions & 10 deletions packages/contracts/test/merklization/RollupMerkleUtils.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -155,11 +155,9 @@ describe('RollupMerkleUtils', () => {
)
// Extract the values we need for the proof in the form we need them
const path = bufToHexString(inclusionProof.key.toBuffer('B', 32))
// Extract the siblings but reverse the order (reversed order is what is expected by the contract
// which verifies bottom to top as opposed to top to bottom.
const siblings = inclusionProof.siblings
.map((sibBuf) => bufToHexString(sibBuf))
.reverse()
const siblings = inclusionProof.siblings.map((sibBuf) =>
bufToHexString(sibBuf)
)
const isValid = await rollupMerkleUtils.verify(
bufToHexString(inclusionProof.rootHash),
bufToHexString(inclusionProof.value),
Expand Down Expand Up @@ -200,11 +198,9 @@ describe('RollupMerkleUtils', () => {
)
// Extract the values we need for the proof in the form we need them
const path = bufToHexString(inclusionProof.key.toBuffer('B', 32))
// Extract the siblings but reverse the order (reversed order is what is expected by the contract
// which verifies bottom to top as opposed to top to bottom.
const siblings = inclusionProof.siblings
.map((sibBuf) => bufToHexString(sibBuf))
.reverse()
const siblings = inclusionProof.siblings.map((sibBuf) =>
bufToHexString(sibBuf)
)
await rollupMerkleUtils.store(
bufToHexString(inclusionProof.value),
path,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -407,9 +407,7 @@ describe('RollupChain', () => {
)
// Here we're storing the siblings in the format we need them!
siblings.push(
inclusionProof.siblings
.map((sibBuf) => bufToHexString(sibBuf))
.reverse()
inclusionProof.siblings.map((sibBuf) => bufToHexString(sibBuf))
)
}

Expand Down
8 changes: 5 additions & 3 deletions packages/core/src/app/block-production/merkle-tree.ts
Original file line number Diff line number Diff line change
Expand Up @@ -120,14 +120,16 @@ export class SparseMerkleTreeImpl implements SparseMerkleTree {
inclusionProof.value,
inclusionProof.key
)

let siblingIndex = 0
let parent: MerkleTreeNode = child
const nodesToStore: MerkleTreeNode[] = [child]
for (let parentDepth = this.height - 2; parentDepth >= 0; parentDepth--) {
child = parent

const childDepth: number = parentDepth + 1
// Since there's no root sibling, each sibling is one index lower
const childSiblingHash: Buffer = inclusionProof.siblings[childDepth - 1]
const childSiblingHash: Buffer = inclusionProof.siblings[siblingIndex++]
parent = this.calculateParentNode(
child,
childSiblingHash,
Expand Down Expand Up @@ -283,7 +285,7 @@ export class SparseMerkleTreeImpl implements SparseMerkleTree {
rootHash: this.root.hash,
key: leafKey,
value: leafValue,
siblings,
siblings: siblings.reverse(),
}
})
}
Expand Down Expand Up @@ -323,7 +325,7 @@ export class SparseMerkleTreeImpl implements SparseMerkleTree {
rootHash: this.root.hash,
key: leafKey,
value: SparseMerkleTreeImpl.emptyBuffer,
siblings,
siblings: siblings.reverse(),
})
}

Expand Down
27 changes: 13 additions & 14 deletions packages/core/test/app/block-production/merkle-tree.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ const verifyEmptyTreeWithDepth = async (
rootHash: await tree.getRootHash(),
key,
value: SparseMerkleTreeImpl.emptyBuffer,
siblings: siblings.reverse(),
siblings,
}

assert(
Expand Down Expand Up @@ -794,9 +794,9 @@ describe('SparseMerkleTreeImpl', () => {
assert(proof.siblings.length === 2)

let hash: Buffer = zeroHash
assert(proof.siblings[1].equals(hash))
hash = bufferHashFunction(hashBuffer.fill(hash, 0, 32).fill(hash, 32))
assert(proof.siblings[0].equals(hash))
hash = bufferHashFunction(hashBuffer.fill(hash, 0, 32).fill(hash, 32))
assert(proof.siblings[1].equals(hash))

assert(proof.rootHash.equals(await tree.getRootHash()))
})
Expand All @@ -821,9 +821,9 @@ describe('SparseMerkleTreeImpl', () => {
assert(proof.siblings.length === 2)

let hash: Buffer = zeroHash
assert(proof.siblings[1].equals(hash))
hash = bufferHashFunction(hashBuffer.fill(hash, 0, 32).fill(hash, 32))
assert(proof.siblings[0].equals(hash))
hash = bufferHashFunction(hashBuffer.fill(hash, 0, 32).fill(hash, 32))
assert(proof.siblings[1].equals(hash))

assert(proof.rootHash.equals(await tree.getRootHash()))
})
Expand Down Expand Up @@ -858,11 +858,11 @@ describe('SparseMerkleTreeImpl', () => {

assert(proof.siblings.length === 2)
let hash: Buffer = bufferHashFunction(oneData)
assert(proof.siblings[1].equals(hash))
assert(proof.siblings[0].equals(hash))
hash = bufferHashFunction(
hashBuffer.fill(zeroHash, 0, 32).fill(zeroHash, 32)
)
assert(proof.siblings[0].equals(hash))
assert(proof.siblings[1].equals(hash))

assert(proof.rootHash.equals(await tree.getRootHash()))

Expand All @@ -874,12 +874,11 @@ describe('SparseMerkleTreeImpl', () => {

assert(proof.siblings.length === 2)
hash = bufferHashFunction(zeroData)
assert(proof.siblings[1].equals(hash))
assert(proof.siblings[0].equals(hash))
hash = bufferHashFunction(
hashBuffer.fill(zeroHash, 0, 32).fill(zeroHash, 32)
)
assert(proof.siblings[0].equals(hash))

assert(proof.siblings[1].equals(hash))
assert(proof.rootHash.equals(await tree.getRootHash()))
})

Expand Down Expand Up @@ -918,11 +917,11 @@ describe('SparseMerkleTreeImpl', () => {
assert(proof.siblings.length === 2)

let hash: Buffer = zeroHash
assert(proof.siblings[1].equals(hash))
assert(proof.siblings[0].equals(hash))
hash = bufferHashFunction(
hashBuffer.fill(bufferHashFunction(twoData), 0, 32).fill(zeroHash, 32)
)
assert(proof.siblings[0].equals(hash))
assert(proof.siblings[1].equals(hash))
assert(proof.rootHash.equals(await tree.getRootHash()))

// Check Proof for TWO
Expand All @@ -934,11 +933,11 @@ describe('SparseMerkleTreeImpl', () => {
assert(proof.siblings.length === 2)

hash = zeroHash
assert(proof.siblings[1].equals(hash))
assert(proof.siblings[0].equals(hash))
hash = bufferHashFunction(
hashBuffer.fill(bufferHashFunction(zeroData), 0, 32).fill(zeroHash, 32)
)
assert(proof.siblings[0].equals(hash))
assert(proof.siblings[1].equals(hash))

assert(proof.rootHash.equals(await tree.getRootHash()))
})
Expand Down
3 changes: 2 additions & 1 deletion packages/core/test/app/utils/misc.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ describe('Miscellanous Utils', () => {
const end = Date.now()
const diff = end - start

diff.should.be.closeTo(100, 10)
diff.should.be.greaterThan(99)
diff.should.be.lessThan(150)
})
})

Expand Down
10 changes: 5 additions & 5 deletions packages/wallet/src/serialization/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@ export const abi = new ethers.utils.AbiCoder()
export const transferAbiTypes = [
'address', // sender address
'address', // recipient address
'bool', // token type
'uint', // token type
'uint32', // amount
]
export const swapAbiTypes = [
'address', // sender address
'bool', // token type
'uint', // token type
'uint32', // amount
'uint32', // min output amount
'uint', // timeout
Expand All @@ -23,7 +23,7 @@ export const swapTransitionAbiTypes = [
'bytes32', // state root
'uint32', // sender slot
'uint32', // uniswap slot
'bool', // token type
'uint', // token type
'uint32', // input amount
'uint32', // min output amount
'uint', // timeout
Expand All @@ -33,7 +33,7 @@ export const transferTransitionAbiTypes = [
'bytes32', // state root
'uint32', // sender slot
'uint32', // recipient slot
'bool', // token type
'uint', // token type
'uint32', // amount
'bytes', // transaction signature
]
Expand All @@ -42,7 +42,7 @@ export const createAndTransferTransitionAbiTypes = [
'uint32', // sender slot
'uint32', // recipient slot
'address', // created public key
'bool', // token type
'uint', // token type
'uint32', // amount
'bytes', // transaction signature
]
Expand Down

0 comments on commit 42f632c

Please sign in to comment.