diff --git a/cli.js b/cli.js index 88f6978..61aae9a 100755 --- a/cli.js +++ b/cli.js @@ -150,13 +150,13 @@ async function withdraw(note, receiver) { console.log('Getting current state from mixer contract') const events = await mixer.getPastEvents('Deposit', { fromBlock: mixer.deployedBlock, toBlock: 'latest' }) const leaves = events - .sort((a, b) => a.returnValues.leafIndex.sub(b.returnValues.leafIndex)) // Sort events in chronological order + .sort((a, b) => a.returnValues.leafIndex - b.returnValues.leafIndex) // Sort events in chronological order .map(e => e.returnValues.commitment) const tree = new merkleTree(MERKLE_TREE_HEIGHT, leaves) // Find current commitment in the tree let depositEvent = events.find(e => e.returnValues.commitment.eq(paddedCommitment)) - let leafIndex = depositEvent ? depositEvent.returnValues.leafIndex.toNumber() : -1 + let leafIndex = depositEvent ? depositEvent.returnValues.leafIndex : -1 // Validate that our data is correct const isValidRoot = await mixer.methods.isKnownRoot(await tree.root()).call() diff --git a/contracts/ERC20Mixer.sol b/contracts/ERC20Mixer.sol index 8c44ac7..4d07fd8 100644 --- a/contracts/ERC20Mixer.sol +++ b/contracts/ERC20Mixer.sol @@ -19,7 +19,7 @@ contract ERC20Mixer is Mixer { constructor( IVerifier _verifier, uint256 _denomination, - uint8 _merkleTreeHeight, + uint32 _merkleTreeHeight, address _operator, address _token ) Mixer(_verifier, _denomination, _merkleTreeHeight, _operator) public { diff --git a/contracts/ETHMixer.sol b/contracts/ETHMixer.sol index 90984c7..b88a734 100644 --- a/contracts/ETHMixer.sol +++ b/contracts/ETHMixer.sol @@ -17,7 +17,7 @@ contract ETHMixer is Mixer { constructor( IVerifier _verifier, uint256 _denomination, - uint8 _merkleTreeHeight, + uint32 _merkleTreeHeight, address _operator ) Mixer(_verifier, _denomination, _merkleTreeHeight, _operator) public { } diff --git a/contracts/MerkleTreeWithHistory.sol b/contracts/MerkleTreeWithHistory.sol index 6c3b1d7..36161a4 100644 --- a/contracts/MerkleTreeWithHistory.sol +++ b/contracts/MerkleTreeWithHistory.sol @@ -19,26 +19,27 @@ contract MerkleTreeWithHistory { uint256 public constant FIELD_SIZE = 21888242871839275222246405745257275088548364400416034343698204186575808495617; uint256 public constant ZERO_VALUE = 5702960885942360421128284892092891246826997279710054143430547229469817701242; // = MiMC("tornado") - uint256 public levels; + uint32 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; + uint32 public constant ROOT_HISTORY_SIZE = 100; uint256[ROOT_HISTORY_SIZE] public roots; - uint256 public currentRootIndex = 0; + uint32 public currentRootIndex = 0; uint32 public nextIndex = 0; uint256[] public filledSubtrees; uint256[] public zeros; - constructor(uint256 _treeLevels) public { + constructor(uint32 _treeLevels) public { require(_treeLevels > 0, "_treeLevels should be greater than zero"); + require(_treeLevels < 32, "_treeLevels should be less than 32"); levels = _treeLevels; uint256 currentZero = ZERO_VALUE; zeros.push(currentZero); filledSubtrees.push(currentZero); - for (uint8 i = 1; i < levels; i++) { + for (uint32 i = 1; i < levels; i++) { currentZero = hashLeftRight(currentZero, currentZero); zeros.push(currentZero); filledSubtrees.push(currentZero); @@ -61,15 +62,15 @@ contract MerkleTreeWithHistory { return R; } - function _insert(uint256 _leaf) internal returns(uint256 index) { + function _insert(uint256 _leaf) internal returns(uint32 index) { uint32 currentIndex = nextIndex; - require(currentIndex != 2**levels, "Merkle tree is full. No more leafs can be added"); + require(currentIndex != uint32(2)**levels, "Merkle tree is full. No more leafs can be added"); nextIndex += 1; uint256 currentLevelHash = _leaf; uint256 left; uint256 right; - for (uint256 i = 0; i < levels; i++) { + for (uint32 i = 0; i < levels; i++) { if (currentIndex % 2 == 0) { left = currentLevelHash; right = zeros[i]; @@ -97,7 +98,7 @@ contract MerkleTreeWithHistory { if (_root == 0) { return false; } - uint256 i = currentRootIndex; + uint32 i = currentRootIndex; do { if (_root == roots[i]) { return true; diff --git a/contracts/Mixer.sol b/contracts/Mixer.sol index 7c703be..4fa301f 100644 --- a/contracts/Mixer.sol +++ b/contracts/Mixer.sol @@ -35,7 +35,7 @@ contract Mixer is MerkleTreeWithHistory { _; } - event Deposit(uint256 indexed commitment, uint256 leafIndex, uint256 timestamp); + event Deposit(uint256 indexed commitment, uint32 leafIndex, uint256 timestamp); event Withdrawal(address to, uint256 nullifierHash, address indexed relayer, uint256 fee); /** @@ -48,7 +48,7 @@ contract Mixer is MerkleTreeWithHistory { constructor( IVerifier _verifier, uint256 _denomination, - uint8 _merkleTreeHeight, + uint32 _merkleTreeHeight, address _operator ) MerkleTreeWithHistory(_merkleTreeHeight) public { require(_denomination > 0, "denomination should be greater than 0"); @@ -64,7 +64,7 @@ contract Mixer is MerkleTreeWithHistory { function deposit(uint256 _commitment) public payable { require(!isDepositsDisabled, "deposits are disabled"); require(!commitments[_commitment], "The commitment has been submitted"); - uint256 insertedIndex = _insert(_commitment); + uint32 insertedIndex = _insert(_commitment); commitments[_commitment] = true; _processDeposit();