From 27e3121bb072580ec59be688661013c0dac358df Mon Sep 17 00:00:00 2001 From: poma Date: Mon, 4 Nov 2019 22:45:56 +0300 Subject: [PATCH] comments --- contracts/ERC20Mixer.sol | 4 ++-- contracts/ETHMixer.sol | 8 ++++---- contracts/MerkleTreeWithHistory.sol | 17 +++++++++++++---- contracts/Mixer.sol | 6 +++--- 4 files changed, 22 insertions(+), 13 deletions(-) diff --git a/contracts/ERC20Mixer.sol b/contracts/ERC20Mixer.sol index 041d814..9dfed90 100644 --- a/contracts/ERC20Mixer.sol +++ b/contracts/ERC20Mixer.sol @@ -47,7 +47,7 @@ contract ERC20Mixer is Mixer { (bool success, bytes memory data) = token.call(abi.encodeWithSelector(0x23b872dd /* transferFrom */, _from, _to, _amount)); require(success, "not enough allowed tokens"); - // if contract returns some data let's make sure that is `true` according to standard + // if contract returns some data lets make sure that is `true` according to standard if (data.length > 0) { require(data.length == 32, "data length should be either 0 or 32 bytes"); success = abi.decode(data, (bool)); @@ -59,7 +59,7 @@ contract ERC20Mixer is Mixer { (bool success, bytes memory data) = token.call(abi.encodeWithSelector(0xa9059cbb /* transfer */, _to, _amount)); require(success, "not enough tokens"); - // if contract returns some data let's make sure that is `true` according to standard + // if contract returns some data lets make sure that is `true` according to standard if (data.length > 0) { require(data.length == 32, "data length should be either 0 or 32 bytes"); success = abi.decode(data, (bool)); diff --git a/contracts/ETHMixer.sol b/contracts/ETHMixer.sol index 188dbb2..90984c7 100644 --- a/contracts/ETHMixer.sol +++ b/contracts/ETHMixer.sol @@ -22,6 +22,10 @@ contract ETHMixer is Mixer { ) Mixer(_verifier, _denomination, _merkleTreeHeight, _operator) public { } + function _processDeposit() internal { + require(msg.value == denomination, "Please send `mixDenomination` ETH along with transaction"); + } + function _processWithdraw(address payable _receiver, address payable _relayer, uint256 _fee, uint256 _refund) internal { // sanity checks require(msg.value == 0, "Message value is supposed to be zero for ETH mixer"); @@ -32,8 +36,4 @@ contract ETHMixer is Mixer { _relayer.transfer(_fee); } } - - function _processDeposit() internal { - require(msg.value == denomination, "Please send `mixDenomination` ETH along with transaction"); - } } diff --git a/contracts/MerkleTreeWithHistory.sol b/contracts/MerkleTreeWithHistory.sol index 75118e8..e1d1ef7 100644 --- a/contracts/MerkleTreeWithHistory.sol +++ b/contracts/MerkleTreeWithHistory.sol @@ -19,11 +19,13 @@ contract MerkleTreeWithHistory { uint256 public constant FIELD_SIZE = 21888242871839275222246405745257275088548364400416034343698204186575808495617; uint256 public constant ZERO_VALUE = 5702960885942360421128284892092891246826997279710054143430547229469817701242; // = MiMC("tornado") + uint256 public levels; + + // the following variables are made public for easier testing and debugging and + // are not supposed to be accessed in regular code uint256 public constant ROOT_HISTORY_SIZE = 100; uint256[ROOT_HISTORY_SIZE] public roots; uint256 public currentRootIndex = 0; - - uint256 public levels; uint32 public nextIndex = 0; uint256[] public filledSubtrees; uint256[] public zeros; @@ -45,9 +47,10 @@ contract MerkleTreeWithHistory { roots[0] = hashLeftRight(currentZero, currentZero); } + /** + @dev Hash 2 tree leaves, returns MiMC(_left, _right) + */ function hashLeftRight(uint256 _left, uint256 _right) public pure returns (uint256 hash) { - // those checks should never trigger in practice, because they're already performed by the snark verifier - // added for convenience if someone decides to call this function directly require(_left < FIELD_SIZE, "_left should be inside the field"); require(_right < FIELD_SIZE, "_right should be inside the field"); uint256 R = _left; @@ -90,6 +93,9 @@ contract MerkleTreeWithHistory { return nextIndex - 1; } + /** + @dev Whether the root is present in the root history + */ function isKnownRoot(uint256 _root) public view returns(bool) { if (_root == 0) { return false; @@ -123,6 +129,9 @@ contract MerkleTreeWithHistory { // } while (i != currentRootIndex); } + /** + @dev Returns the last root + */ function getLastRoot() public view returns(uint256) { return roots[currentRootIndex]; } diff --git a/contracts/Mixer.sol b/contracts/Mixer.sol index 0a6a8b2..7c703be 100644 --- a/contracts/Mixer.sol +++ b/contracts/Mixer.sol @@ -25,7 +25,6 @@ contract Mixer is MerkleTreeWithHistory { IVerifier public verifier; // operator can - // - receive a relayer fee // - disable new deposits in case of emergency // - update snark verification key until this ability is permanently disabled address public operator; @@ -42,8 +41,9 @@ contract Mixer is MerkleTreeWithHistory { /** @dev The constructor @param _verifier the address of SNARK verifier for this contract + @param _denomination transfer amount for each deposit @param _merkleTreeHeight the height of deposits' Merkle Tree - @param _operator operator address (see operator above) + @param _operator operator address (see operator comment above) */ constructor( IVerifier _verifier, @@ -75,7 +75,7 @@ contract Mixer is MerkleTreeWithHistory { function _processDeposit() internal; /** - @dev Withdraw deposit from the mixer. `proof` is a zkSNARK proof data, and input is an array of circuit public inputs + @dev Withdraw a deposit from the mixer. `proof` is a zkSNARK proof data, and input is an array of circuit public inputs `input` array consists of: - merkle root of all deposits in the mixer - hash of unique deposit nullifier to prevent double spends