tornado-trees/contracts/mocks/TornadoTreesMock.sol

89 lines
2.7 KiB
Solidity
Raw Normal View History

2021-02-01 16:40:32 +03:00
// SPDX-License-Identifier: MIT
pragma solidity ^0.6.0;
pragma experimental ABIEncoderV2;
import "../TornadoTrees.sol";
2021-02-03 20:59:35 +03:00
import "../interfaces/ITornadoTreesV1.sol";
2021-02-10 20:45:19 +03:00
import "../interfaces/IBatchTreeUpdateVerifier.sol";
2021-02-01 16:40:32 +03:00
contract TornadoTreesMock is TornadoTrees {
uint256 public currentBlock;
constructor(
2021-02-03 20:59:35 +03:00
address _governance,
address _tornadoProxy,
ITornadoTreesV1 _tornadoTreesV1,
2021-02-10 20:45:19 +03:00
IBatchTreeUpdateVerifier _treeUpdateVerifier,
2021-02-06 16:13:22 +03:00
SearchParams memory _searchParams
) public TornadoTrees(_governance, _tornadoProxy, _tornadoTreesV1, _treeUpdateVerifier, _searchParams) {}
2021-02-01 16:40:32 +03:00
function setBlockNumber(uint256 _blockNumber) public {
currentBlock = _blockNumber;
}
function blockNumber() public view override returns (uint256) {
return currentBlock == 0 ? block.number : currentBlock;
}
function register(
address _instance,
bytes32 _commitment,
bytes32 _nullifier,
uint256 _depositBlockNumber,
uint256 _withdrawBlockNumber
) public {
setBlockNumber(_depositBlockNumber);
2021-02-05 23:28:51 +03:00
registerDeposit(_instance, _commitment);
2021-02-04 20:47:47 +03:00
2021-02-01 16:40:32 +03:00
setBlockNumber(_withdrawBlockNumber);
2021-02-05 23:28:51 +03:00
registerWithdrawal(_instance, _nullifier);
2021-02-01 16:40:32 +03: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;
}
}