add pauseDeposits

This commit is contained in:
Roman Storm 2019-08-01 01:41:22 -07:00
parent 9b14a22b0d
commit d91435dd00
9 changed files with 1642 additions and 5 deletions

View File

@ -3,4 +3,5 @@ MERKLE_TREE_HEIGHT=16
AMOUNT=1000000000000000000
EMPTY_ELEMENT=1337
PRIVATE_KEY=
PAUSEACCOUNT=0x17017Ba4FA022299067B5F235e2F230973bc7Fed

1
.gitignore vendored
View File

@ -1,6 +1,7 @@
build
.vscode
/index.js
Mixer_flat.sol
# Created by .ignore support plugin (hsz.mobi)
### Node template

6
.solhint.json Normal file
View File

@ -0,0 +1,6 @@
{
"extends": "solhint:recommended",
"rules": {
"indent": ["error", 2]
}
}

View File

@ -8,6 +8,8 @@ contract IVerifier {
contract Mixer is MerkleTreeWithHistory {
uint256 public transferValue;
bool public isDepositsEnabled = true;
address public pauseAccount;
mapping(uint256 => bool) public nullifierHashes;
// we store all commitments just to prevent accidental deposits with the same commitment
mapping(uint256 => bool) public commitments;
@ -25,10 +27,12 @@ contract Mixer is MerkleTreeWithHistory {
address _verifier,
uint256 _transferValue,
uint8 _merkleTreeHeight,
uint256 _emptyElement
uint256 _emptyElement,
address _pauseAccount
) MerkleTreeWithHistory(_merkleTreeHeight, _emptyElement) public {
verifier = IVerifier(_verifier);
transferValue = _transferValue;
pauseAccount = _pauseAccount;
}
/**
@ -36,6 +40,7 @@ contract Mixer is MerkleTreeWithHistory {
@param commitment the note commitment, which is PedersenHash(nullifier + secret)
*/
function deposit(uint256 commitment) public payable {
require(isDepositsEnabled, "deposits disabled");
require(msg.value == transferValue, "Please send `transferValue` ETH along with transaction");
require(!commitments[commitment], "The commitment has been submitted");
_insert(commitment);
@ -70,6 +75,11 @@ contract Mixer is MerkleTreeWithHistory {
emit Withdraw(receiver, nullifierHash, fee);
}
function toggleDeposits() external {
require(msg.sender == pauseAccount, "unauthorized");
isDepositsEnabled = !isDepositsEnabled;
}
function isSpent(uint256 nullifier) public view returns(bool) {
return nullifierHashes[nullifier];
}

View File

@ -5,13 +5,13 @@ const Verifier = artifacts.require('Verifier')
const MiMC = artifacts.require('MiMC')
module.exports = function(deployer) {
module.exports = function(deployer, network, accounts) {
return deployer.then(async () => {
const { MERKLE_TREE_HEIGHT, AMOUNT, EMPTY_ELEMENT } = process.env
const verifier = await Verifier.deployed()
const miMC = await MiMC.deployed()
await Mixer.link(MiMC, miMC.address)
const mixer = await deployer.deploy(Mixer, verifier.address, AMOUNT, MERKLE_TREE_HEIGHT, EMPTY_ELEMENT)
const mixer = await deployer.deploy(Mixer, verifier.address, AMOUNT, MERKLE_TREE_HEIGHT, EMPTY_ELEMENT, accounts[0])
console.log('Mixer\'s address ', mixer.address)
})
}

1604
package-lock.json generated

File diff suppressed because it is too large Load Diff

View File

@ -14,7 +14,8 @@
"migrate": "npx truffle migrate --network kovan --reset",
"migrate:dev": "npx truffle migrate --network development --reset",
"browserify": "npx browserify cli.js -o index.js --exclude worker_threads",
"eslint": "npx eslint --ignore-path .gitignore ."
"eslint": "npx eslint --ignore-path .gitignore .",
"flat": "truffle-flattener contracts/Mixer.sol > Mixer_flat.sol"
},
"keywords": [],
"author": "",
@ -38,5 +39,8 @@
"web3": "^1.0.0-beta.55",
"web3-utils": "^1.0.0-beta.55",
"websnark": "git+https://github.com/poma/websnark.git#271c74a76f4d9277c049283c45929fb3a5bb46fc"
},
"devDependencies": {
"truffle-flattener": "^1.4.0"
}
}

View File

@ -111,6 +111,17 @@ contract('Mixer', accounts => {
logs[0].args.leafIndex.should.be.eq.BN(toBN(1))
})
it('should not deposit if disabled', async () => {
let commitment = 42;
(await mixer.isDepositsEnabled()).should.be.equal(true)
const err = await mixer.toggleDeposits({ from: accounts[1] }).should.be.rejected
err.reason.should.be.equal('unauthorized')
await mixer.toggleDeposits({ from: sender });
(await mixer.isDepositsEnabled()).should.be.equal(false)
let error = await mixer.deposit(commitment, { value, from: sender }).should.be.rejected
error.reason.should.be.equal('deposits disabled')
})
it('should throw if there is a such commitment', async () => {
const commitment = 42
await mixer.deposit(commitment, { value, from: sender }).should.be.fulfilled

View File

@ -72,7 +72,7 @@ module.exports = {
// docker: true, // Use "0.5.1" you've installed locally with docker (default: false)
settings: { // See the solidity docs for advice about optimization and evmVersion
optimizer: {
enabled: false,
enabled: true,
runs: 200
},
// evmVersion: "byzantium"