off-by-one fix and tests

This commit is contained in:
Alexey 2019-07-15 18:04:48 +03:00
parent 234fb8940e
commit 5c16e02c90
3 changed files with 107 additions and 12 deletions

View file

@ -84,7 +84,7 @@ contract MerkleTreeWithHistory {
}
// search most recent first
uint256 i;
for(i = current_root; i >= 0; i--) {
for(i = current_root; i < 2**256 - 1; i--) {
if (root == _roots[i]) {
return true;
}

View file

@ -9,6 +9,8 @@ contract IVerifier {
contract Mixer is MerkleTreeWithHistory {
uint256 public transferValue;
mapping(uint256 => bool) public nullifiers;
// we store all commitments just to prevent accidental deposits with the same commitment
mapping(uint256 => bool) public commitments;
IVerifier verifier;
event Deposit(address from, uint256 commitment);
@ -35,7 +37,9 @@ contract Mixer is MerkleTreeWithHistory {
*/
function deposit(uint256 commitment) public payable {
require(msg.value == transferValue, "Please send `transferValue` ETH along with transaction");
require(!commitments[commitment], "The commitment has been submitted");
_insert(commitment);
commitments[commitment] = true;
emit Deposit(msg.sender, commitment);
}