tornado-core/test/MerkleTreeWithHistory.test.js

248 lines
7.2 KiB
JavaScript
Raw Normal View History

2019-07-16 16:49:45 -04:00
/* global artifacts, web3, contract, assert */
2019-07-16 11:58:42 -04:00
require('chai')
2019-07-10 12:58:21 -04:00
.use(require('bn-chai')(web3.utils.BN))
.use(require('chai-as-promised'))
2019-07-16 11:58:42 -04:00
.should()
2019-07-10 12:58:21 -04:00
2019-07-18 14:38:21 -04:00
const { takeSnapshot, revertSnapshot } = require('../lib/ganacheHelper')
2019-07-10 12:58:21 -04:00
const MerkleTreeWithHistory = artifacts.require('./MerkleTreeWithHistoryMock.sol')
const hasherContract = artifacts.require('./Hasher.sol')
2019-07-10 12:58:21 -04:00
2019-07-12 06:24:59 -04:00
const MerkleTree = require('../lib/MerkleTree')
const hasherImpl = require('../lib/MiMC')
2019-07-11 06:38:22 -04:00
2019-11-04 16:04:22 -05:00
const snarkjs = require('snarkjs')
const bigInt = snarkjs.bigInt
2019-11-02 08:35:22 -04:00
const { ETH_AMOUNT, MERKLE_TREE_HEIGHT } = process.env
2019-07-16 07:04:14 -04:00
2019-07-16 11:58:42 -04:00
// eslint-disable-next-line no-unused-vars
2019-07-10 12:58:21 -04:00
function BNArrayToStringArray(array) {
const arrayToPrint = []
array.forEach(item => {
arrayToPrint.push(item.toString())
})
return arrayToPrint
}
2019-11-04 16:04:22 -05:00
function toFixedHex(number, length = 32) {
let str = bigInt(number).toString(16)
while (str.length < length * 2) str = '0' + str
str = '0x' + str
return str
}
2019-07-16 11:58:42 -04:00
contract('MerkleTreeWithHistory', accounts => {
2019-07-10 12:58:21 -04:00
let merkleTreeWithHistory
let hasherInstance
2019-07-16 07:37:48 -04:00
let levels = MERKLE_TREE_HEIGHT || 16
2019-07-16 11:58:42 -04:00
const sender = accounts[0]
// eslint-disable-next-line no-unused-vars
2019-08-27 16:42:24 -04:00
const value = ETH_AMOUNT || '1000000000000000000'
2019-07-10 12:58:21 -04:00
let snapshotId
2019-07-11 06:38:22 -04:00
let prefix = 'test'
let tree
let hasher
2019-07-10 12:58:21 -04:00
before(async () => {
2019-07-11 06:38:22 -04:00
tree = new MerkleTree(
levels,
null,
prefix,
2019-07-11 06:38:22 -04:00
)
hasherInstance = await hasherContract.deployed()
await MerkleTreeWithHistory.link(hasherContract, hasherInstance.address)
2019-11-02 08:35:22 -04:00
merkleTreeWithHistory = await MerkleTreeWithHistory.new(levels)
2019-07-10 12:58:21 -04:00
snapshotId = await takeSnapshot()
})
2019-07-16 11:58:42 -04:00
describe('#constructor', () => {
2019-07-10 12:58:21 -04:00
it('should initialize', async () => {
2019-11-02 08:35:22 -04:00
const zeroValue = await merkleTreeWithHistory.ZERO_VALUE()
2019-11-02 09:04:17 -04:00
const firstSubtree = await merkleTreeWithHistory.filledSubtrees(0)
2019-11-04 16:04:22 -05:00
firstSubtree.should.be.equal(toFixedHex(zeroValue))
2019-11-02 09:04:17 -04:00
const firstZero = await merkleTreeWithHistory.zeros(0)
2019-11-04 16:04:22 -05:00
firstZero.should.be.equal(toFixedHex(zeroValue))
2019-07-10 12:58:21 -04:00
})
})
2019-07-16 11:58:42 -04:00
describe('merkleTreeLib', () => {
it('index_to_key', () => {
2019-07-11 06:38:22 -04:00
assert.equal(
MerkleTree.index_to_key('test', 5, 20),
2019-07-16 11:58:42 -04:00
'test_tree_5_20',
2019-07-11 06:38:22 -04:00
)
})
it('tests insert', async () => {
hasher = new hasherImpl()
2019-07-11 06:38:22 -04:00
tree = new MerkleTree(
2,
null,
prefix,
2019-07-11 06:38:22 -04:00
)
2019-11-04 16:04:22 -05:00
await tree.insert(toFixedHex('5'))
2019-07-16 11:58:42 -04:00
let { root, path_elements } = await tree.path(0)
2019-07-11 06:38:22 -04:00
const calculated_root = hasher.hash(null,
hasher.hash(null, '5', path_elements[0]),
path_elements[1]
)
// console.log(root)
assert.equal(root, calculated_root)
})
2019-07-11 10:04:36 -04:00
it('creation odd elements count', async () => {
const elements = [12, 13, 14, 15, 16, 17, 18, 19, 20]
2019-07-16 11:58:42 -04:00
for(const [, el] of Object.entries(elements)) {
2019-07-12 06:24:59 -04:00
await tree.insert(el)
2019-07-11 10:04:36 -04:00
}
const batchTree = new MerkleTree(
levels,
elements,
prefix,
2019-07-16 11:58:42 -04:00
)
for(const [i] of Object.entries(elements)) {
2019-07-11 10:04:36 -04:00
const pathViaConstructor = await batchTree.path(i)
const pathViaUpdate = await tree.path(i)
pathViaConstructor.should.be.deep.equal(pathViaUpdate)
}
})
2019-07-17 17:25:16 -04:00
it('should find an element', async () => {
const elements = [12, 13, 14, 15, 16, 17, 18, 19, 20]
for(const [, el] of Object.entries(elements)) {
await tree.insert(el)
}
let index = tree.getIndexByElement(13)
index.should.be.equal(1)
index = tree.getIndexByElement(19)
index.should.be.equal(7)
index = tree.getIndexByElement(12)
index.should.be.equal(0)
index = tree.getIndexByElement(20)
index.should.be.equal(8)
index = tree.getIndexByElement(42)
index.should.be.equal(false)
})
2019-07-11 10:04:36 -04:00
it('creation even elements count', async () => {
const elements = [12, 13, 14, 15, 16, 17]
2019-07-16 11:58:42 -04:00
for(const [, el] of Object.entries(elements)) {
2019-07-12 06:24:59 -04:00
await tree.insert(el)
2019-07-11 10:04:36 -04:00
}
const batchTree = new MerkleTree(
levels,
elements,
prefix,
2019-07-16 11:58:42 -04:00
)
for(const [i] of Object.entries(elements)) {
2019-07-11 10:04:36 -04:00
const pathViaConstructor = await batchTree.path(i)
const pathViaUpdate = await tree.path(i)
pathViaConstructor.should.be.deep.equal(pathViaUpdate)
}
})
2019-07-12 06:24:59 -04:00
2019-07-16 11:58:42 -04:00
it.skip('creation using 30000 elements', () => {
2019-07-12 06:24:59 -04:00
const elements = []
2019-07-12 06:49:01 -04:00
for(let i = 1000; i < 31001; i++) {
2019-07-12 06:24:59 -04:00
elements.push(i)
}
2019-07-16 11:58:42 -04:00
console.time('MerkleTree')
2019-07-12 06:24:59 -04:00
tree = new MerkleTree(
levels,
elements,
prefix,
2019-07-16 11:58:42 -04:00
)
console.timeEnd('MerkleTree')
2019-07-12 06:24:59 -04:00
// 2,7 GHz Intel Core i7
// 1000 : 1949.084ms
// 10000: 19456.220ms
2019-07-12 06:49:01 -04:00
// 30000: 63406.679ms
2019-07-12 06:24:59 -04:00
})
2019-07-11 06:38:22 -04:00
})
2019-07-16 11:58:42 -04:00
describe('#insert', () => {
2019-07-10 12:58:21 -04:00
it('should insert', async () => {
2019-07-11 06:38:22 -04:00
let rootFromContract
2019-07-10 12:58:21 -04:00
2019-07-16 11:58:42 -04:00
for (let i = 1; i < 11; i++) {
2019-11-04 16:04:22 -05:00
await merkleTreeWithHistory.insert(toFixedHex(i), { from: sender })
2019-07-12 06:24:59 -04:00
await tree.insert(i)
2019-07-16 11:58:42 -04:00
let { root } = await tree.path(i - 1)
2019-07-11 06:38:22 -04:00
rootFromContract = await merkleTreeWithHistory.getLastRoot()
2019-11-04 16:04:22 -05:00
toFixedHex(root).should.be.equal(rootFromContract.toString())
2019-07-10 12:58:21 -04:00
}
})
2019-07-16 07:04:14 -04:00
2019-07-16 07:37:48 -04:00
it('should reject if tree is full', async () => {
2019-11-07 23:39:22 -05:00
const levels = 6
2019-11-03 04:06:58 -05:00
const merkleTreeWithHistory = await MerkleTreeWithHistory.new(levels)
2019-07-16 07:04:14 -04:00
2019-09-16 06:07:14 -04:00
for (let i = 0; i < 2**levels; i++) {
2019-11-04 16:04:22 -05:00
await merkleTreeWithHistory.insert(toFixedHex(i+42)).should.be.fulfilled
2019-07-16 07:04:14 -04:00
}
2019-11-04 16:04:22 -05:00
let error = await merkleTreeWithHistory.insert(toFixedHex(1337)).should.be.rejected
2019-09-11 04:25:32 -04:00
error.reason.should.be.equal('Merkle tree is full. No more leafs can be added')
2019-07-16 07:04:14 -04:00
2019-11-04 16:04:22 -05:00
error = await merkleTreeWithHistory.insert(toFixedHex(1)).should.be.rejected
2019-09-11 04:25:32 -04:00
error.reason.should.be.equal('Merkle tree is full. No more leafs can be added')
2019-07-16 07:04:14 -04:00
})
2019-09-10 09:31:34 -04:00
it.skip('hasher gas', async () => {
2019-11-07 23:39:22 -05:00
const levels = 6
2019-11-03 04:06:58 -05:00
const merkleTreeWithHistory = await MerkleTreeWithHistory.new(levels)
2019-11-02 08:35:22 -04:00
const zeroValue = await merkleTreeWithHistory.zeroValue()
2019-09-10 09:31:34 -04:00
const gas = await merkleTreeWithHistory.hashLeftRight.estimateGas(zeroValue, zeroValue)
console.log('gas', gas - 21000)
})
2019-07-10 12:58:21 -04:00
})
2019-11-03 04:06:58 -05:00
describe('#isKnownRoot', () => {
it('should work', async () => {
let path
for (let i = 1; i < 5; i++) {
2019-11-04 16:04:22 -05:00
await merkleTreeWithHistory.insert(toFixedHex(i), { from: sender }).should.be.fulfilled
2019-11-03 04:06:58 -05:00
await tree.insert(i)
path = await tree.path(i - 1)
2019-11-04 16:04:22 -05:00
let isKnown = await merkleTreeWithHistory.isKnownRoot(toFixedHex(path.root))
2019-11-03 04:06:58 -05:00
isKnown.should.be.equal(true)
}
2019-11-04 16:04:22 -05:00
await merkleTreeWithHistory.insert(toFixedHex(42), { from: sender }).should.be.fulfilled
2019-11-03 04:06:58 -05:00
// check outdated root
2019-11-04 16:04:22 -05:00
let isKnown = await merkleTreeWithHistory.isKnownRoot(toFixedHex(path.root))
2019-11-03 04:06:58 -05:00
isKnown.should.be.equal(true)
})
it('should not return uninitialized roots', async () => {
2019-11-04 16:04:22 -05:00
await merkleTreeWithHistory.insert(toFixedHex(42), { from: sender }).should.be.fulfilled
let isKnown = await merkleTreeWithHistory.isKnownRoot(toFixedHex(0))
2019-11-03 04:06:58 -05:00
isKnown.should.be.equal(false)
})
})
2019-07-10 12:58:21 -04:00
afterEach(async () => {
await revertSnapshot(snapshotId.result)
2019-07-16 11:58:42 -04:00
// eslint-disable-next-line require-atomic-updates
2019-07-10 12:58:21 -04:00
snapshotId = await takeSnapshot()
hasher = new hasherImpl()
2019-07-11 06:38:22 -04:00
tree = new MerkleTree(
levels,
null,
prefix,
null,
hasher,
2019-07-11 06:38:22 -04:00
)
2019-07-10 12:58:21 -04:00
})
})