eslinter WIP

This commit is contained in:
Alexey 2019-07-16 18:58:42 +03:00
parent f3b1b7f96e
commit 2b2e174d06
6 changed files with 817 additions and 90 deletions

38
.eslintrc.json Normal file
View File

@ -0,0 +1,38 @@
{
"env": {
"node": true,
"es6": true,
"mocha": true
},
"extends": "eslint:recommended",
"globals": {
"Atomics": "readonly",
"SharedArrayBuffer": "readonly"
},
"parserOptions": {
"ecmaVersion": 2018
},
"rules": {
"indent": [
"error",
2
],
"linebreak-style": [
"error",
"unix"
],
"quotes": [
"error",
"single"
],
"semi": [
"error",
"never"
],
"object-curly-spacing": [
"error",
"always"
],
"require-await": "error"
}
}

743
package-lock.json generated

File diff suppressed because it is too large Load Diff

View File

@ -27,6 +27,7 @@
"circomlib": "^0.0.10", "circomlib": "^0.0.10",
"dotenv": "^8.0.0", "dotenv": "^8.0.0",
"express": "^4.17.1", "express": "^4.17.1",
"eslint": "^6.0.1",
"ganache-cli": "^6.4.5", "ganache-cli": "^6.4.5",
"snarkjs": "git+https://github.com/iden3/snarkjs.git#5fe2bd4642ec567c75ad5ac3f73687999c412e73", "snarkjs": "git+https://github.com/iden3/snarkjs.git#5fe2bd4642ec567c75ad5ac3f73687999c412e73",
"truffle": "^5.0.27", "truffle": "^5.0.27",

View File

@ -1,38 +1,39 @@
function send(method, params = []) { function send(method, params = []) {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
// eslint-disable-next-line no-undef
web3.currentProvider.send({ web3.currentProvider.send({
jsonrpc: '2.0', jsonrpc: '2.0',
id: Date.now(), id: Date.now(),
method, method,
params params
}, (err, res) => { }, (err, res) => {
return err ? reject(err) : resolve(res); return err ? reject(err) : resolve(res)
}); })
}); })
} }
const takeSnapshot = async seconds => { const takeSnapshot = async () => {
return await send('evm_snapshot'); return await send('evm_snapshot')
} }
const revertSnapshot = async (id) => { const revertSnapshot = async (id) => {
await send('evm_revert', [id]); await send('evm_revert', [id])
} }
const mineBlock = async (timestamp) => { const mineBlock = async (timestamp) => {
await send('evm_mine', [timestamp]); await send('evm_mine', [timestamp])
} }
const increaseTime = async (seconds) => { const increaseTime = async (seconds) => {
await send('evm_increaseTime', [seconds]); await send('evm_increaseTime', [seconds])
} }
const minerStop = async () => { const minerStop = async () => {
await send('miner_stop', []); await send('miner_stop', [])
} }
const minerStart = async () => { const minerStart = async () => {
await send('miner_start', []); await send('miner_start', [])
} }
module.exports = { module.exports = {
@ -42,4 +43,4 @@ module.exports = {
minerStop, minerStop,
minerStart, minerStart,
increaseTime, increaseTime,
}; }

View File

@ -1,19 +1,19 @@
const snarkjs = require("snarkjs"); const snarkjs = require('snarkjs')
const groth = snarkjs["groth"]; const groth = snarkjs['groth']
const crypto = require("crypto"); const crypto = require('crypto')
const circomlib = require('circomlib'); const circomlib = require('circomlib')
const pedersen = circomlib.pedersenHash; const pedersen = circomlib.pedersenHash
const babyjub = circomlib.babyJub; const babyjub = circomlib.babyJub
const websnarkUtils = require('websnark/src/utils'); const websnarkUtils = require('websnark/src/utils')
const unstringifyBigInts2 = require("snarkjs/src/stringifybigint").unstringifyBigInts; const unstringifyBigInts2 = require('snarkjs/src/stringifybigint').unstringifyBigInts
const rbigint = (nbytes) => snarkjs.bigInt.leBuff2int(crypto.randomBytes(nbytes)); const rbigint = (nbytes) => snarkjs.bigInt.leBuff2int(crypto.randomBytes(nbytes))
const pedersenHash = (data) => babyjub.unpackPoint(pedersen.hash(data))[0]; const pedersenHash = (data) => babyjub.unpackPoint(pedersen.hash(data))[0]
function snarkVerify(proof) { function snarkVerify(proof) {
proof = unstringifyBigInts2(websnarkUtils.fromSolidityInput(proof)); proof = unstringifyBigInts2(websnarkUtils.fromSolidityInput(proof))
const verification_key = unstringifyBigInts2(require('../build/circuits/withdraw_verification_key.json')); const verification_key = unstringifyBigInts2(require('../build/circuits/withdraw_verification_key.json'))
return groth.isValid(verification_key, proof, proof.publicSignals); return groth.isValid(verification_key, proof, proof.publicSignals)
} }
module.exports = {rbigint, pedersenHash, snarkVerify}; module.exports = { rbigint, pedersenHash, snarkVerify }

View File

@ -1,10 +1,10 @@
const should = require('chai') /*global artifacts, web3, contract, assert*/
require('chai')
.use(require('bn-chai')(web3.utils.BN)) .use(require('bn-chai')(web3.utils.BN))
.use(require('chai-as-promised')) .use(require('chai-as-promised'))
.should() .should()
const { toWei, toBN } = require('web3-utils') const { takeSnapshot, revertSnapshot } = require('../scripts/ganacheHelper')
const { takeSnapshot, revertSnapshot, increaseTime } = require('../scripts/ganacheHelper');
const MerkleTreeWithHistory = artifacts.require('./MerkleTreeWithHistoryMock.sol') const MerkleTreeWithHistory = artifacts.require('./MerkleTreeWithHistoryMock.sol')
const MiMC = artifacts.require('./MiMC.sol') const MiMC = artifacts.require('./MiMC.sol')
@ -14,6 +14,7 @@ const MimcHasher = require('../lib/MiMC')
const { AMOUNT, MERKLE_TREE_HEIGHT, EMPTY_ELEMENT } = process.env const { AMOUNT, MERKLE_TREE_HEIGHT, EMPTY_ELEMENT } = process.env
// eslint-disable-next-line no-unused-vars
function BNArrayToStringArray(array) { function BNArrayToStringArray(array) {
const arrayToPrint = [] const arrayToPrint = []
array.forEach(item => { array.forEach(item => {
@ -22,13 +23,13 @@ function BNArrayToStringArray(array) {
return arrayToPrint return arrayToPrint
} }
contract('MerkleTreeWithHistory', async accounts => { contract('MerkleTreeWithHistory', accounts => {
let merkleTreeWithHistory let merkleTreeWithHistory
let miMC let miMC
const sender = accounts[0]
const emptyAddress = '0x0000000000000000000000000000000000000000'
let levels = MERKLE_TREE_HEIGHT || 16 let levels = MERKLE_TREE_HEIGHT || 16
let zeroValue = EMPTY_ELEMENT || 1337 let zeroValue = EMPTY_ELEMENT || 1337
const sender = accounts[0]
// eslint-disable-next-line no-unused-vars
const value = AMOUNT || '1000000000000000000' const value = AMOUNT || '1000000000000000000'
let snapshotId let snapshotId
let prefix = 'test' let prefix = 'test'
@ -48,26 +49,20 @@ contract('MerkleTreeWithHistory', async accounts => {
snapshotId = await takeSnapshot() snapshotId = await takeSnapshot()
}) })
describe('#constructor', async () => { describe('#constructor', () => {
it('should initialize', async () => { it('should initialize', async () => {
const filled_subtrees = await merkleTreeWithHistory.filled_subtrees() const filled_subtrees = await merkleTreeWithHistory.filled_subtrees()
// console.log('filled_subtrees', BNArrayToStringArray(filled_subtrees))
const root = await merkleTreeWithHistory.getLastRoot()
// console.log('root', root.toString())
filled_subtrees[0].should.be.eq.BN(zeroValue) filled_subtrees[0].should.be.eq.BN(zeroValue)
const zeros = await merkleTreeWithHistory.zeros() const zeros = await merkleTreeWithHistory.zeros()
// console.log('zeros', BNArrayToStringArray(zeros))
zeros[0].should.be.eq.BN(zeroValue) zeros[0].should.be.eq.BN(zeroValue)
const roots = await merkleTreeWithHistory.roots()
// console.log('roots', BNArrayToStringArray(roots))
}) })
}) })
describe('merkleTreeLib', async () => { describe('merkleTreeLib', () => {
it('index_to_key', async () => { it('index_to_key', () => {
assert.equal( assert.equal(
MerkleTree.index_to_key('test', 5, 20), MerkleTree.index_to_key('test', 5, 20),
"test_tree_5_20", 'test_tree_5_20',
) )
}) })
@ -80,7 +75,7 @@ contract('MerkleTreeWithHistory', async accounts => {
prefix, prefix,
) )
await tree.insert('5') await tree.insert('5')
let {root, path_elements, path_index} = await tree.path(0) let { root, path_elements } = await tree.path(0)
const calculated_root = hasher.hash(null, const calculated_root = hasher.hash(null,
hasher.hash(null, '5', path_elements[0]), hasher.hash(null, '5', path_elements[0]),
path_elements[1] path_elements[1]
@ -90,7 +85,7 @@ contract('MerkleTreeWithHistory', async accounts => {
}) })
it('creation odd elements count', async () => { it('creation odd elements count', async () => {
const elements = [12, 13, 14, 15, 16, 17, 18, 19, 20] const elements = [12, 13, 14, 15, 16, 17, 18, 19, 20]
for(const [i, el] of Object.entries(elements)) { for(const [, el] of Object.entries(elements)) {
await tree.insert(el) await tree.insert(el)
} }
@ -99,8 +94,8 @@ contract('MerkleTreeWithHistory', async accounts => {
zeroValue, zeroValue,
elements, elements,
prefix, prefix,
); )
for(const [i, el] of Object.entries(elements)) { for(const [i] of Object.entries(elements)) {
const pathViaConstructor = await batchTree.path(i) const pathViaConstructor = await batchTree.path(i)
const pathViaUpdate = await tree.path(i) const pathViaUpdate = await tree.path(i)
pathViaConstructor.should.be.deep.equal(pathViaUpdate) pathViaConstructor.should.be.deep.equal(pathViaUpdate)
@ -109,7 +104,7 @@ contract('MerkleTreeWithHistory', async accounts => {
it('creation even elements count', async () => { it('creation even elements count', async () => {
const elements = [12, 13, 14, 15, 16, 17] const elements = [12, 13, 14, 15, 16, 17]
for(const [i, el] of Object.entries(elements)) { for(const [, el] of Object.entries(elements)) {
await tree.insert(el) await tree.insert(el)
} }
@ -118,27 +113,27 @@ contract('MerkleTreeWithHistory', async accounts => {
zeroValue, zeroValue,
elements, elements,
prefix, prefix,
); )
for(const [i, el] of Object.entries(elements)) { for(const [i] of Object.entries(elements)) {
const pathViaConstructor = await batchTree.path(i) const pathViaConstructor = await batchTree.path(i)
const pathViaUpdate = await tree.path(i) const pathViaUpdate = await tree.path(i)
pathViaConstructor.should.be.deep.equal(pathViaUpdate) pathViaConstructor.should.be.deep.equal(pathViaUpdate)
} }
}) })
it.skip('creation using 30000 elements', async () => { it.skip('creation using 30000 elements', () => {
const elements = [] const elements = []
for(let i = 1000; i < 31001; i++) { for(let i = 1000; i < 31001; i++) {
elements.push(i) elements.push(i)
} }
console.time('MerkleTree'); console.time('MerkleTree')
tree = new MerkleTree( tree = new MerkleTree(
levels, levels,
zeroValue, zeroValue,
elements, elements,
prefix, prefix,
); )
console.timeEnd('MerkleTree'); console.timeEnd('MerkleTree')
// 2,7 GHz Intel Core i7 // 2,7 GHz Intel Core i7
// 1000 : 1949.084ms // 1000 : 1949.084ms
// 10000: 19456.220ms // 10000: 19456.220ms
@ -146,21 +141,16 @@ contract('MerkleTreeWithHistory', async accounts => {
}) })
}) })
describe('#insert', async () => { describe('#insert', () => {
it('should insert', async () => { it('should insert', async () => {
let rootFromContract let rootFromContract
for (i = 1; i < 11; i++) { for (let i = 1; i < 11; i++) {
await merkleTreeWithHistory.insert(i) await merkleTreeWithHistory.insert(i, { from: sender })
await tree.insert(i) await tree.insert(i)
filled_subtrees = await merkleTreeWithHistory.filled_subtrees() let { root } = await tree.path(i - 1)
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() rootFromContract = await merkleTreeWithHistory.getLastRoot()
root.should.be.equal(rootFromContract.toString()) root.should.be.equal(rootFromContract.toString())
// console.log('rootFromCon', root.toString())
} }
}) })
@ -169,7 +159,7 @@ contract('MerkleTreeWithHistory', async accounts => {
zeroValue = 1337 zeroValue = 1337
merkleTreeWithHistory = await MerkleTreeWithHistory.new(levels, zeroValue) merkleTreeWithHistory = await MerkleTreeWithHistory.new(levels, zeroValue)
for (i = 0; i < 2**(levels - 1); i++) { for (let i = 0; i < 2**(levels - 1); i++) {
await merkleTreeWithHistory.insert(i+42).should.be.fulfilled await merkleTreeWithHistory.insert(i+42).should.be.fulfilled
} }
@ -181,15 +171,9 @@ contract('MerkleTreeWithHistory', async accounts => {
}) })
}) })
describe('#MIMC', async () => {
it.skip('gas price', async () => {
const gas = await merkleTreeWithHistory.hashLeftRight.estimateGas(1,2)
// console.log('gas', gas)
})
})
afterEach(async () => { afterEach(async () => {
await revertSnapshot(snapshotId.result) await revertSnapshot(snapshotId.result)
// eslint-disable-next-line require-atomic-updates
snapshotId = await takeSnapshot() snapshotId = await takeSnapshot()
hasher = new MimcHasher() hasher = new MimcHasher()
tree = new MerkleTree( tree = new MerkleTree(