tornado-trees/contracts/mocks/TornadoTreesMock.sol

129 lines
4.0 KiB
Solidity
Raw Permalink Normal View History

2021-02-01 14:40:32 +01:00
// SPDX-License-Identifier: MIT
pragma solidity ^0.6.0;
pragma experimental ABIEncoderV2;
import "../TornadoTrees.sol";
2021-02-03 18:59:35 +01:00
import "../interfaces/ITornadoTreesV1.sol";
2021-02-10 18:45:19 +01:00
import "../interfaces/IBatchTreeUpdateVerifier.sol";
2021-02-01 14:40:32 +01:00
contract TornadoTreesMock is TornadoTrees {
uint256 public currentBlock;
constructor(
2021-02-03 18:59:35 +01:00
address _governance,
ITornadoTreesV1 _tornadoTreesV1,
2021-02-06 14:13:22 +01:00
SearchParams memory _searchParams
2021-02-26 12:01:34 +01:00
) public TornadoTrees(_governance, _tornadoTreesV1, _searchParams) {}
2021-02-01 14:40:32 +01:00
function setBlockNumber(uint256 _blockNumber) public {
currentBlock = _blockNumber;
}
function blockNumber() public view override returns (uint256) {
return currentBlock == 0 ? block.number : currentBlock;
}
2021-03-06 13:05:54 +01:00
function findArrayLengthMock(
ITornadoTreesV1 _tornadoTreesV1,
string memory _type,
uint256 _from,
uint256 _step
) public view returns (uint256) {
return findArrayLength(_tornadoTreesV1, _type, _from, _step);
}
2021-02-01 14:40:32 +01:00
function register(
address _instance,
bytes32 _commitment,
bytes32 _nullifier,
uint256 _depositBlockNumber,
uint256 _withdrawBlockNumber
) public {
setBlockNumber(_depositBlockNumber);
2021-02-05 21:28:51 +01:00
registerDeposit(_instance, _commitment);
2021-02-04 18:47:47 +01:00
2021-02-01 14:40:32 +01:00
setBlockNumber(_withdrawBlockNumber);
2021-02-05 21:28:51 +01:00
registerWithdrawal(_instance, _nullifier);
2021-02-01 14:40:32 +01:00
}
2021-02-10 21:22:46 +01:00
function updateRoots(bytes32 _depositRoot, bytes32 _withdrawalRoot) public {
depositRoot = _depositRoot;
withdrawalRoot = _withdrawalRoot;
}
2021-02-01 14:40:32 +01:00
function updateDepositTreeMock(
bytes32 _oldRoot,
bytes32 _newRoot,
uint32 _pathIndices,
TreeLeaf[] calldata _events
) public pure returns (uint256) {
bytes memory data = new bytes(BYTES_SIZE);
assembly {
mstore(add(data, 0x44), _pathIndices)
mstore(add(data, 0x40), _newRoot)
mstore(add(data, 0x20), _oldRoot)
}
for (uint256 i = 0; i < CHUNK_SIZE; i++) {
(bytes32 hash, address instance, uint32 depositBlock) = (_events[i].hash, _events[i].instance, _events[i].block);
assembly {
mstore(add(add(data, mul(ITEM_SIZE, i)), 0x7c), depositBlock)
mstore(add(add(data, mul(ITEM_SIZE, i)), 0x78), instance)
mstore(add(add(data, mul(ITEM_SIZE, i)), 0x64), hash)
}
}
return uint256(sha256(data)) % SNARK_FIELD;
}
function updateDepositTreeMock2(
bytes32 _oldRoot,
bytes32 _newRoot,
uint32 _pathIndices,
TreeLeaf[] calldata _events
) public pure returns (bytes memory) {
bytes memory data = new bytes(BYTES_SIZE);
assembly {
mstore(add(data, 0x44), _pathIndices)
mstore(add(data, 0x40), _newRoot)
mstore(add(data, 0x20), _oldRoot)
}
for (uint256 i = 0; i < CHUNK_SIZE; i++) {
(bytes32 hash, address instance, uint32 depositBlock) = (_events[i].hash, _events[i].instance, _events[i].block);
assembly {
mstore(add(add(data, mul(ITEM_SIZE, i)), 0x7c), depositBlock)
mstore(add(add(data, mul(ITEM_SIZE, i)), 0x78), instance)
mstore(add(add(data, mul(ITEM_SIZE, i)), 0x64), hash)
}
}
return data;
}
2021-03-22 18:12:50 +01:00
function getRegisteredDeposits() external view returns (bytes32[] memory _deposits) {
uint256 count = depositsLength - lastProcessedDepositLeaf;
_deposits = new bytes32[](count);
for (uint256 i = 0; i < count; i++) {
_deposits[i] = deposits[lastProcessedDepositLeaf + i];
}
}
function getRegisteredWithdrawals() external view returns (bytes32[] memory _withdrawals) {
uint256 count = withdrawalsLength - lastProcessedWithdrawalLeaf;
_withdrawals = new bytes32[](count);
for (uint256 i = 0; i < count; i++) {
_withdrawals[i] = withdrawals[lastProcessedWithdrawalLeaf + i];
}
}
2021-03-22 18:25:42 +01:00
function findArrayLength(
ITornadoTreesV1 _tornadoTreesV1,
string memory _type,
uint256 _from, // most likely array length after the proposal has passed
uint256 _step // optimal step size to find first match, approximately equals dispersion
) internal view override returns (uint256) {
if (_from == 0 && _step == 0) {
return 0;
}
return super.findArrayLength(_tornadoTreesV1, _type, _from, _step);
}
2021-02-01 14:40:32 +01:00
}