tornado-core/test/MerkleTreeWithHistory.test.js

125 lines
4.3 KiB
JavaScript
Raw Normal View History

2020-07-31 19:38:50 +02:00
/* global artifacts, web3, contract */
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
2020-08-01 04:28:14 +02:00
const { takeSnapshot, revertSnapshot } = require('../scripts/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
2020-07-31 19:38:50 +02:00
const MerkleTree = require('fixed-merkle-tree')
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 tree
2019-07-10 18:58:21 +02:00
before(async () => {
2020-07-31 19:38:50 +02:00
tree = new MerkleTree(levels)
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('#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 })
2020-07-31 19:38:50 +02:00
tree.insert(i)
2019-07-11 12:38:22 +02:00
rootFromContract = await merkleTreeWithHistory.getLastRoot()
2020-07-31 19:38:50 +02:00
toFixedHex(tree.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
2021-03-11 20:51:09 +01:00
error.reason.should.be.equal('Merkle tree is full. No more leaves 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
2021-03-11 20:51:09 +01:00
error.reason.should.be.equal('Merkle tree is full. No more leaves 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 () => {
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)
2020-07-31 19:38:50 +02:00
let isKnown = await merkleTreeWithHistory.isKnownRoot(toFixedHex(tree.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
2020-07-31 19:38:50 +02:00
let isKnown = await merkleTreeWithHistory.isKnownRoot(toFixedHex(tree.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()
2020-07-31 19:38:50 +02:00
tree = new MerkleTree(levels)
2019-07-10 18:58:21 +02:00
})
})