This commit is contained in:
poma 2021-02-11 09:23:18 +03:00
parent c6b442713a
commit 346ffcee3c
No known key found for this signature in database
GPG key ID: BA20CB01FE165657
35 changed files with 852 additions and 8943 deletions

View file

@ -1,8 +1,5 @@
/* global artifacts, web3, contract, assert */
require('chai')
.use(require('bn-chai')(web3.utils.BN))
.use(require('chai-as-promised'))
.should()
require('chai').use(require('bn-chai')(web3.utils.BN)).use(require('chai-as-promised')).should()
const { takeSnapshot, revertSnapshot } = require('../lib/ganacheHelper')
@ -20,7 +17,7 @@ const { ETH_AMOUNT, MERKLE_TREE_HEIGHT } = process.env
// eslint-disable-next-line no-unused-vars
function BNArrayToStringArray(array) {
const arrayToPrint = []
array.forEach(item => {
array.forEach((item) => {
arrayToPrint.push(item.toString())
})
return arrayToPrint
@ -33,7 +30,7 @@ function toFixedHex(number, length = 32) {
return str
}
contract('MerkleTreeWithHistory', accounts => {
contract('MerkleTreeWithHistory', (accounts) => {
let merkleTreeWithHistory
let hasherInstance
let levels = MERKLE_TREE_HEIGHT || 16
@ -46,11 +43,7 @@ contract('MerkleTreeWithHistory', accounts => {
let hasher
before(async () => {
tree = new MerkleTree(
levels,
null,
prefix,
)
tree = new MerkleTree(levels, null, prefix)
hasherInstance = await hasherContract.deployed()
await MerkleTreeWithHistory.link(hasherContract, hasherInstance.address)
merkleTreeWithHistory = await MerkleTreeWithHistory.new(levels)
@ -69,40 +62,26 @@ contract('MerkleTreeWithHistory', accounts => {
describe('merkleTreeLib', () => {
it('index_to_key', () => {
assert.equal(
MerkleTree.index_to_key('test', 5, 20),
'test_tree_5_20',
)
assert.equal(MerkleTree.index_to_key('test', 5, 20), 'test_tree_5_20')
})
it('tests insert', async () => {
hasher = new hasherImpl()
tree = new MerkleTree(
2,
null,
prefix,
)
tree = new MerkleTree(2, null, prefix)
await tree.insert(toFixedHex('5'))
let { root, path_elements } = await tree.path(0)
const calculated_root = hasher.hash(null,
hasher.hash(null, '5', path_elements[0]),
path_elements[1]
)
const calculated_root = hasher.hash(null, hasher.hash(null, '5', path_elements[0]), path_elements[1])
// console.log(root)
assert.equal(root, calculated_root)
})
it('creation odd elements count', async () => {
const elements = [12, 13, 14, 15, 16, 17, 18, 19, 20]
for(const [, el] of Object.entries(elements)) {
for (const [, el] of Object.entries(elements)) {
await tree.insert(el)
}
const batchTree = new MerkleTree(
levels,
elements,
prefix,
)
for(const [i] of Object.entries(elements)) {
const batchTree = new MerkleTree(levels, elements, prefix)
for (const [i] of Object.entries(elements)) {
const pathViaConstructor = await batchTree.path(i)
const pathViaUpdate = await tree.path(i)
pathViaConstructor.should.be.deep.equal(pathViaUpdate)
@ -111,7 +90,7 @@ contract('MerkleTreeWithHistory', accounts => {
it('should find an element', async () => {
const elements = [12, 13, 14, 15, 16, 17, 18, 19, 20]
for(const [, el] of Object.entries(elements)) {
for (const [, el] of Object.entries(elements)) {
await tree.insert(el)
}
let index = tree.getIndexByElement(13)
@ -132,16 +111,12 @@ contract('MerkleTreeWithHistory', accounts => {
it('creation even elements count', async () => {
const elements = [12, 13, 14, 15, 16, 17]
for(const [, el] of Object.entries(elements)) {
for (const [, el] of Object.entries(elements)) {
await tree.insert(el)
}
const batchTree = new MerkleTree(
levels,
elements,
prefix,
)
for(const [i] of Object.entries(elements)) {
const batchTree = new MerkleTree(levels, elements, prefix)
for (const [i] of Object.entries(elements)) {
const pathViaConstructor = await batchTree.path(i)
const pathViaUpdate = await tree.path(i)
pathViaConstructor.should.be.deep.equal(pathViaUpdate)
@ -150,15 +125,11 @@ contract('MerkleTreeWithHistory', accounts => {
it.skip('creation using 30000 elements', () => {
const elements = []
for(let i = 1000; i < 31001; i++) {
for (let i = 1000; i < 31001; i++) {
elements.push(i)
}
console.time('MerkleTree')
tree = new MerkleTree(
levels,
elements,
prefix,
)
tree = new MerkleTree(levels, elements, prefix)
console.timeEnd('MerkleTree')
// 2,7 GHz Intel Core i7
// 1000 : 1949.084ms
@ -184,8 +155,8 @@ contract('MerkleTreeWithHistory', accounts => {
const levels = 6
const merkleTreeWithHistory = await MerkleTreeWithHistory.new(levels)
for (let i = 0; i < 2**levels; i++) {
await merkleTreeWithHistory.insert(toFixedHex(i+42)).should.be.fulfilled
for (let i = 0; i < 2 ** levels; i++) {
await merkleTreeWithHistory.insert(toFixedHex(i + 42)).should.be.fulfilled
}
let error = await merkleTreeWithHistory.insert(toFixedHex(1337)).should.be.rejected
@ -230,18 +201,11 @@ contract('MerkleTreeWithHistory', accounts => {
})
})
afterEach(async () => {
await revertSnapshot(snapshotId.result)
// eslint-disable-next-line require-atomic-updates
snapshotId = await takeSnapshot()
hasher = new hasherImpl()
tree = new MerkleTree(
levels,
null,
prefix,
null,
hasher,
)
tree = new MerkleTree(levels, null, prefix, null, hasher)
})
})