tornado-core/test/MerkleTreeWithHistory.test.js

219 lines
6.0 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 MiMC = artifacts.require('./MiMC.sol')
2019-07-12 06:24:59 -04:00
const MerkleTree = require('../lib/MerkleTree')
const MimcHasher = require('../lib/MiMC')
2019-07-11 06:38:22 -04:00
2019-08-27 16:42:24 -04:00
const { ETH_AMOUNT, MERKLE_TREE_HEIGHT, EMPTY_ELEMENT } = 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-07-16 11:58:42 -04:00
contract('MerkleTreeWithHistory', accounts => {
2019-07-10 12:58:21 -04:00
let merkleTreeWithHistory
let miMC
2019-07-16 07:37:48 -04:00
let levels = MERKLE_TREE_HEIGHT || 16
let zeroValue = EMPTY_ELEMENT || 1337
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,
zeroValue,
null,
prefix,
2019-07-11 06:38:22 -04:00
)
2019-07-12 17:50:26 -04:00
miMC = await MiMC.deployed()
2019-07-11 06:38:22 -04:00
await MerkleTreeWithHistory.link(MiMC, miMC.address)
2019-07-10 12:58:21 -04:00
merkleTreeWithHistory = await MerkleTreeWithHistory.new(levels, zeroValue)
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 () => {
const filled_subtrees = await merkleTreeWithHistory.filled_subtrees()
filled_subtrees[0].should.be.eq.BN(zeroValue)
const zeros = await merkleTreeWithHistory.zeros()
zeros[0].should.be.eq.BN(zeroValue)
})
})
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 MimcHasher()
2019-07-11 06:38:22 -04:00
tree = new MerkleTree(
2,
zeroValue,
null,
prefix,
2019-07-11 06:38:22 -04:00
)
2019-07-12 06:24:59 -04:00
await tree.insert('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,
zeroValue,
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,
zeroValue,
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,
zeroValue,
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++) {
await merkleTreeWithHistory.insert(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()
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 () => {
levels = 6
zeroValue = 1337
merkleTreeWithHistory = await MerkleTreeWithHistory.new(levels, zeroValue)
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-07-16 07:04:14 -04:00
await merkleTreeWithHistory.insert(i+42).should.be.fulfilled
}
let error = await merkleTreeWithHistory.insert(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
error = await merkleTreeWithHistory.insert(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('mimc gas', async () => {
levels = 6
zeroValue = 1337
merkleTreeWithHistory = await MerkleTreeWithHistory.new(levels, zeroValue)
const gas = await merkleTreeWithHistory.hashLeftRight.estimateGas(zeroValue, zeroValue)
console.log('gas', gas - 21000)
})
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 MimcHasher()
2019-07-11 06:38:22 -04:00
tree = new MerkleTree(
levels,
zeroValue,
null,
prefix,
null,
hasher,
2019-07-11 06:38:22 -04:00
)
2019-07-10 12:58:21 -04:00
})
})