From 54e2c5f890cafbf0fc6f9c34195168a0b4b88109 Mon Sep 17 00:00:00 2001 From: poma Date: Fri, 1 Nov 2019 04:07:11 +0300 Subject: [PATCH] return inserted index from merkle tree --- contracts/MerkleTreeWithHistory.sol | 3 ++- contracts/Mixer.sol | 4 ++-- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/contracts/MerkleTreeWithHistory.sol b/contracts/MerkleTreeWithHistory.sol index 9d2f189..39145b9 100644 --- a/contracts/MerkleTreeWithHistory.sol +++ b/contracts/MerkleTreeWithHistory.sol @@ -56,7 +56,7 @@ contract MerkleTreeWithHistory { hash = R; } - function _insert(uint256 leaf) internal { + function _insert(uint256 leaf) internal returns(uint256 index) { uint32 current_index = next_index; require(current_index != 2**levels, "Merkle tree is full. No more leafs can be added"); next_index += 1; @@ -82,6 +82,7 @@ contract MerkleTreeWithHistory { current_root = (current_root + 1) % ROOT_HISTORY_SIZE; _roots[current_root] = current_level_hash; + return next_index - 1; } function isKnownRoot(uint256 root) public view returns(bool) { diff --git a/contracts/Mixer.sol b/contracts/Mixer.sol index 4f273ad..8da900e 100644 --- a/contracts/Mixer.sol +++ b/contracts/Mixer.sol @@ -65,11 +65,11 @@ contract Mixer is MerkleTreeWithHistory { function deposit(uint256 commitment) public payable { require(isDepositsEnabled, "deposits are disabled"); require(!commitments[commitment], "The commitment has been submitted"); - _insert(commitment); + uint256 insertedIndex = _insert(commitment); commitments[commitment] = true; _processDeposit(); - emit Deposit(commitment, next_index - 1, block.timestamp); + emit Deposit(commitment, insertedIndex, block.timestamp); } /** @dev this function is defined in a child contract */