tornado-core/test/MerkleTreeWithHistory.test.js

205 lines
6.0 KiB
JavaScript
Raw Normal View History

2019-07-10 18:58:21 +02:00
const should = require('chai')
.use(require('bn-chai')(web3.utils.BN))
.use(require('chai-as-promised'))
.should()
const { toWei, toBN } = require('web3-utils')
const { takeSnapshot, revertSnapshot, increaseTime } = require('../scripts/ganacheHelper');
const MerkleTreeWithHistory = artifacts.require('./MerkleTreeWithHistoryMock.sol')
const MiMC = artifacts.require('./MiMC.sol')
2019-07-12 12:24:59 +02:00
const MerkleTree = require('../lib/MerkleTree')
const MimcHasher = require('../lib/MiMC')
2019-07-11 12:38:22 +02:00
2019-07-16 13:04:14 +02:00
const { AMOUNT, MERKLE_TREE_HEIGHT, EMPTY_ELEMENT } = process.env
2019-07-10 18:58:21 +02:00
function BNArrayToStringArray(array) {
const arrayToPrint = []
array.forEach(item => {
arrayToPrint.push(item.toString())
})
return arrayToPrint
}
contract('MerkleTreeWithHistory', async accounts => {
let merkleTreeWithHistory
let miMC
const sender = accounts[0]
const emptyAddress = '0x0000000000000000000000000000000000000000'
2019-07-16 13:37:48 +02:00
let levels = MERKLE_TREE_HEIGHT || 16
let zeroValue = EMPTY_ELEMENT || 1337
2019-07-16 13:04:14 +02:00
const value = 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
)
2019-07-12 23:50:26 +02:00
miMC = await MiMC.deployed()
2019-07-11 12:38:22 +02:00
await MerkleTreeWithHistory.link(MiMC, miMC.address)
2019-07-10 18:58:21 +02:00
merkleTreeWithHistory = await MerkleTreeWithHistory.new(levels, zeroValue)
snapshotId = await takeSnapshot()
})
2019-07-12 18:58:02 +02:00
describe('#constructor', async () => {
2019-07-10 18:58:21 +02:00
it('should initialize', async () => {
const filled_subtrees = await merkleTreeWithHistory.filled_subtrees()
2019-07-11 12:38:22 +02:00
// console.log('filled_subtrees', BNArrayToStringArray(filled_subtrees))
2019-07-10 18:58:21 +02:00
const root = await merkleTreeWithHistory.getLastRoot()
2019-07-11 12:38:22 +02:00
// console.log('root', root.toString())
2019-07-10 18:58:21 +02:00
filled_subtrees[0].should.be.eq.BN(zeroValue)
const zeros = await merkleTreeWithHistory.zeros()
// console.log('zeros', BNArrayToStringArray(zeros))
zeros[0].should.be.eq.BN(zeroValue)
const roots = await merkleTreeWithHistory.roots()
// console.log('roots', BNArrayToStringArray(roots))
})
})
2019-07-11 12:38:22 +02:00
describe('merkleTreeLib', async () => {
it('index_to_key', async () => {
assert.equal(
MerkleTree.index_to_key('test', 5, 20),
"test_tree_5_20",
)
})
it('tests insert', async () => {
hasher = new MimcHasher()
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-11 12:38:22 +02:00
let {root, path_elements, path_index} = await tree.path(0)
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]
for(const [i, 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-11 16:04:36 +02:00
);
for(const [i, el] of Object.entries(elements)) {
const pathViaConstructor = await batchTree.path(i)
const pathViaUpdate = await tree.path(i)
pathViaConstructor.should.be.deep.equal(pathViaUpdate)
}
})
it('creation even elements count', async () => {
const elements = [12, 13, 14, 15, 16, 17]
for(const [i, 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-11 16:04:36 +02:00
);
for(const [i, el] of Object.entries(elements)) {
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-12 12:49:01 +02:00
it.skip('creation using 30000 elements', async () => {
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)
}
console.time('MerkleTree');
tree = new MerkleTree(
levels,
zeroValue,
elements,
prefix,
2019-07-12 12:24:59 +02:00
);
console.timeEnd('MerkleTree');
// 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-10 18:58:21 +02:00
describe('#insert', async () => {
it('should insert', async () => {
2019-07-11 12:38:22 +02:00
let rootFromContract
2019-07-10 18:58:21 +02:00
for (i = 1; i < 11; i++) {
await merkleTreeWithHistory.insert(i)
2019-07-12 12:24:59 +02:00
await tree.insert(i)
2019-07-10 18:58:21 +02:00
filled_subtrees = await merkleTreeWithHistory.filled_subtrees()
2019-07-11 12:38:22 +02:00
let {root, path_elements, path_index} = await tree.path(i - 1)
// console.log('path_elements ', path_elements)
// console.log('filled_subtrees', BNArrayToStringArray(filled_subtrees))
// console.log('rootFromLib', root)
rootFromContract = await merkleTreeWithHistory.getLastRoot()
root.should.be.equal(rootFromContract.toString())
// console.log('rootFromCon', root.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-07-16 13:37:48 +02:00
for (i = 0; i < 2**(levels - 1); 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
error.reason.should.be.equal('Merkle tree is full')
error = await merkleTreeWithHistory.insert(1).should.be.rejected
error.reason.should.be.equal('Merkle tree is full')
})
2019-07-10 18:58:21 +02:00
})
2019-07-12 23:50:26 +02:00
describe('#MIMC', async () => {
it.skip('gas price', async () => {
const gas = await merkleTreeWithHistory.hashLeftRight.estimateGas(1,2)
// console.log('gas', gas)
2019-07-12 23:50:26 +02:00
})
})
2019-07-10 18:58:21 +02:00
afterEach(async () => {
await revertSnapshot(snapshotId.result)
snapshotId = await takeSnapshot()
hasher = new MimcHasher()
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
})
})