tornado-core/test/MerkleTreeWithHistory.test.js

211 lines
7.0 KiB
JavaScript
Raw Normal View History

2019-07-16 22:49:45 +02:00
/* global artifacts, web3, contract, assert */
2021-02-11 07:23:18 +01:00
require('chai').use(require('bn-chai')(web3.utils.BN)).use(require('chai-as-promised')).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-11-04 22:04:22 +01:00
const snarkjs = require('snarkjs')
const bigInt = snarkjs.bigInt
2019-11-02 13:35:22 +01:00
const { ETH_AMOUNT, MERKLE_TREE_HEIGHT } = 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 = []
2021-02-11 07:23:18 +01:00
array.forEach((item) => {
2019-07-10 18:58:21 +02:00
arrayToPrint.push(item.toString())
})
return arrayToPrint
}
2019-11-04 22:04:22 +01: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
}
2021-02-11 07:23:18 +01: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
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 () => {
2021-02-11 07:23:18 +01:00
tree = new MerkleTree(levels, null, prefix)
hasherInstance = await hasherContract.deployed()
2021-02-11 08:00:53 +01:00
merkleTreeWithHistory = await MerkleTreeWithHistory.new(levels, hasherInstance.address)
2019-07-10 18:58:21 +02:00
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 () => {
2019-11-02 13:35:22 +01:00
const zeroValue = await merkleTreeWithHistory.ZERO_VALUE()
2019-11-02 14:04:17 +01:00
const firstSubtree = await merkleTreeWithHistory.filledSubtrees(0)
2019-11-04 22:04:22 +01:00
firstSubtree.should.be.equal(toFixedHex(zeroValue))
2019-11-02 14:04:17 +01:00
const firstZero = await merkleTreeWithHistory.zeros(0)
2019-11-04 22:04:22 +01:00
firstZero.should.be.equal(toFixedHex(zeroValue))
2019-07-10 18:58:21 +02:00
})
})
2019-07-16 17:58:42 +02:00
describe('merkleTreeLib', () => {
it('index_to_key', () => {
2021-02-11 07:23:18 +01:00
assert.equal(MerkleTree.index_to_key('test', 5, 20), 'test_tree_5_20')
2019-07-11 12:38:22 +02:00
})
it('tests insert', async () => {
hasher = new hasherImpl()
2021-02-11 07:23:18 +01:00
tree = new MerkleTree(2, null, prefix)
2019-11-04 22:04:22 +01:00
await tree.insert(toFixedHex('5'))
2019-07-16 17:58:42 +02:00
let { root, path_elements } = await tree.path(0)
2021-02-11 07:23:18 +01:00
const calculated_root = hasher.hash(null, hasher.hash(null, '5', path_elements[0]), path_elements[1])
2019-07-11 12:38:22 +02:00
// 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]
2021-02-11 07:23:18 +01: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
}
2021-02-11 07:23:18 +01:00
const batchTree = new MerkleTree(levels, elements, prefix)
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]
2021-02-11 07:23:18 +01:00
for (const [, el] of Object.entries(elements)) {
2019-07-17 23:25:16 +02:00
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]
2021-02-11 07:23:18 +01: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
}
2021-02-11 07:23:18 +01:00
const batchTree = new MerkleTree(levels, elements, prefix)
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 = []
2021-02-11 07:23:18 +01: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')
2021-02-11 07:23:18 +01:00
tree = new MerkleTree(levels, 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++) {
2019-11-04 22:04:22 +01:00
await merkleTreeWithHistory.insert(toFixedHex(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()
2019-11-04 22:04:22 +01:00
toFixedHex(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 () => {
2019-11-08 05:39:22 +01:00
const levels = 6
2021-02-11 08:00:53 +01:00
const merkleTreeWithHistory = await MerkleTreeWithHistory.new(levels, hasherInstance.address)
2019-07-16 13:04:14 +02:00
2021-02-11 07:23:18 +01:00
for (let i = 0; i < 2 ** levels; i++) {
await merkleTreeWithHistory.insert(toFixedHex(i + 42)).should.be.fulfilled
2019-07-16 13:04:14 +02:00
}
2019-11-04 22:04:22 +01:00
let error = await merkleTreeWithHistory.insert(toFixedHex(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
2019-11-04 22:04:22 +01:00
error = await merkleTreeWithHistory.insert(toFixedHex(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-11-08 05:39:22 +01:00
const levels = 6
2019-11-03 10:06:58 +01:00
const merkleTreeWithHistory = await MerkleTreeWithHistory.new(levels)
2019-11-02 13:35:22 +01:00
const zeroValue = await merkleTreeWithHistory.zeroValue()
2019-09-10 15:31:34 +02:00
const gas = await merkleTreeWithHistory.hashLeftRight.estimateGas(zeroValue, zeroValue)
console.log('gas', gas - 21000)
})
2019-07-10 18:58:21 +02:00
})
2019-11-03 10:06:58 +01:00
describe('#isKnownRoot', () => {
it('should work', async () => {
let path
for (let i = 1; i < 5; i++) {
2019-11-04 22:04:22 +01:00
await merkleTreeWithHistory.insert(toFixedHex(i), { from: sender }).should.be.fulfilled
2019-11-03 10:06:58 +01:00
await tree.insert(i)
path = await tree.path(i - 1)
2019-11-04 22:04:22 +01:00
let isKnown = await merkleTreeWithHistory.isKnownRoot(toFixedHex(path.root))
2019-11-03 10:06:58 +01:00
isKnown.should.be.equal(true)
}
2019-11-04 22:04:22 +01:00
await merkleTreeWithHistory.insert(toFixedHex(42), { from: sender }).should.be.fulfilled
2019-11-03 10:06:58 +01:00
// check outdated root
2019-11-04 22:04:22 +01:00
let isKnown = await merkleTreeWithHistory.isKnownRoot(toFixedHex(path.root))
2019-11-03 10:06:58 +01:00
isKnown.should.be.equal(true)
})
it('should not return uninitialized roots', async () => {
2019-11-04 22:04:22 +01:00
await merkleTreeWithHistory.insert(toFixedHex(42), { from: sender }).should.be.fulfilled
let isKnown = await merkleTreeWithHistory.isKnownRoot(toFixedHex(0))
2019-11-03 10:06:58 +01:00
isKnown.should.be.equal(false)
})
})
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()
2021-02-11 07:23:18 +01:00
tree = new MerkleTree(levels, null, prefix, null, hasher)
2019-07-10 18:58:21 +02:00
})
})