tornado-core/test/MerkleTreeWithHistory.test.js

219 lines
6.1 KiB
JavaScript
Raw Normal View History

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