use waffle

This commit is contained in:
poma 2021-08-21 15:46:17 +03:00
parent eb7b83ca19
commit 576ba47938
No known key found for this signature in database
GPG Key ID: BA20CB01FE165657
2 changed files with 27 additions and 35 deletions

View File

@ -75,13 +75,6 @@ function shuffle(array) {
return array return array
} }
async function takeSnapshot() {
return await ethers.provider.send('evm_snapshot', [])
}
async function revertSnapshot(id) {
await ethers.provider.send('evm_revert', [id])
}
module.exports = { module.exports = {
FIELD_SIZE, FIELD_SIZE,
@ -91,7 +84,5 @@ module.exports = {
poseidonHash, poseidonHash,
poseidonHash2, poseidonHash2,
getExtDataHash, getExtDataHash,
takeSnapshot,
revertSnapshot,
shuffle, shuffle,
} }

View File

@ -1,9 +1,9 @@
const hre = require('hardhat') const hre = require('hardhat')
const ethers = hre.ethers const { ethers, waffle } = hre
const { expect, should } = require('chai') const { loadFixture } = waffle
should() const { expect } = require('chai')
const { poseidonHash2, toFixedHex, takeSnapshot, revertSnapshot } = require('../src/utils') const { poseidonHash2, toFixedHex } = require('../src/utils')
const Utxo = require('../src/utxo') const Utxo = require('../src/utxo')
const MERKLE_TREE_HEIGHT = 5 const MERKLE_TREE_HEIGHT = 5
@ -14,30 +14,29 @@ const { Keypair } = require('../src/keypair')
describe('TornadoPool', function () { describe('TornadoPool', function () {
this.timeout(20000) this.timeout(20000)
let snapshotId, sender
/** @type {TornadoPool} */
let tornadoPool
/* prettier-ignore */ async function deploy(contractName, ...args) {
before(async function () { const Factory = await ethers.getContractFactory(contractName)
;[sender] = await ethers.getSigners() const instance = await Factory.deploy(...args)
await instance.deployed()
const Verifier2 = await ethers.getContractFactory('Verifier2') return instance
const verifier2 = await Verifier2.deploy() }
await verifier2.deployed()
const Verifier16 = await ethers.getContractFactory('Verifier16') async function fixture() {
const verifier16 = await Verifier16.deploy() const [deployer] = await ethers.getSigners()
await verifier16.deployed()
const verifier2 = await deploy('Verifier2')
const verifier16 = await deploy('Verifier16')
const tree = new MerkleTree(MERKLE_TREE_HEIGHT, [], { hashFunction: poseidonHash2 }) const tree = new MerkleTree(MERKLE_TREE_HEIGHT, [], { hashFunction: poseidonHash2 })
const root = await tree.root() const root = await tree.root()
/** @type {TornadoPool} */
const tornadoPool = await deploy('TornadoPool', verifier2.address, verifier16.address, toFixedHex(root))
const Pool = await ethers.getContractFactory('TornadoPool') return { tornadoPool, deployer }
tornadoPool = await Pool.deploy(verifier2.address, verifier16.address, toFixedHex(root)) }
snapshotId = await takeSnapshot()
})
it('encrypt -> decrypt should work', () => { it('encrypt -> decrypt should work', () => {
const data = Buffer.from([0xff, 0xaa, 0x00, 0x01]) const data = Buffer.from([0xff, 0xaa, 0x00, 0x01])
@ -49,6 +48,7 @@ describe('TornadoPool', function () {
}) })
it('constants check', async () => { it('constants check', async () => {
const { tornadoPool } = await loadFixture(fixture)
const maxFee = await tornadoPool.MAX_FEE() const maxFee = await tornadoPool.MAX_FEE()
const maxExtAmount = await tornadoPool.MAX_EXT_AMOUNT() const maxExtAmount = await tornadoPool.MAX_EXT_AMOUNT()
const fieldSize = await tornadoPool.FIELD_SIZE() const fieldSize = await tornadoPool.FIELD_SIZE()
@ -57,6 +57,9 @@ describe('TornadoPool', function () {
}) })
it('should register and deposit', async function () { it('should register and deposit', async function () {
let { tornadoPool } = await loadFixture(fixture)
const sender = (await ethers.getSigners())[1]
// Alice deposits into tornado pool // Alice deposits into tornado pool
const aliceDepositAmount = 1e7 const aliceDepositAmount = 1e7
const aliceDepositUtxo = new Utxo({ amount: aliceDepositAmount }) const aliceDepositUtxo = new Utxo({ amount: aliceDepositAmount })
@ -115,6 +118,8 @@ describe('TornadoPool', function () {
}) })
it('should deposit, transact and withdraw', async function () { it('should deposit, transact and withdraw', async function () {
const { tornadoPool } = await loadFixture(fixture)
// Alice deposits into tornado pool // Alice deposits into tornado pool
const aliceDepositAmount = 1e7 const aliceDepositAmount = 1e7
const aliceDepositUtxo = new Utxo({ amount: aliceDepositAmount }) const aliceDepositUtxo = new Utxo({ amount: aliceDepositAmount })
@ -162,11 +167,7 @@ describe('TornadoPool', function () {
}) })
it('should work with 16 inputs', async function () { it('should work with 16 inputs', async function () {
const { tornadoPool } = await loadFixture(fixture)
await transaction({ tornadoPool, inputs: [new Utxo(), new Utxo(), new Utxo()] }) await transaction({ tornadoPool, inputs: [new Utxo(), new Utxo(), new Utxo()] })
}) })
afterEach(async () => {
await revertSnapshot(snapshotId)
snapshotId = await takeSnapshot()
})
}) })