refactor: functions order

This commit is contained in:
Alexey 2021-10-21 19:46:29 +03:00
parent 0ecd02620d
commit ebf1b1a1b4
No known key found for this signature in database
GPG Key ID: C77958099D784E76

View File

@ -98,10 +98,6 @@ contract TornadoPool is MerkleTreeWithHistory, IERC20Receiver, ReentrancyGuard,
super._initialize(); super._initialize();
} }
function configureLimits(uint256 _minimalWithdrawalAmount, uint256 _maximumDepositAmount) public onlyGovernance {
_configureLimits(_minimalWithdrawalAmount, _maximumDepositAmount);
}
function transact(Proof memory _args, ExtData memory _extData) public { function transact(Proof memory _args, ExtData memory _extData) public {
if (_extData.extAmount > 0) { if (_extData.extAmount > 0) {
// for deposits from L2 // for deposits from L2
@ -112,39 +108,36 @@ contract TornadoPool is MerkleTreeWithHistory, IERC20Receiver, ReentrancyGuard,
_transact(_args, _extData); _transact(_args, _extData);
} }
function _transact(Proof memory _args, ExtData memory _extData) internal nonReentrant { function register(Account memory _account) public {
require(isKnownRoot(_args.root), "Invalid merkle root"); require(_account.owner == msg.sender, "only owner can be registered");
for (uint256 i = 0; i < _args.inputNullifiers.length; i++) { _register(_account);
require(!isSpent(_args.inputNullifiers[i]), "Input is already spent"); }
}
require(uint256(_args.extDataHash) == uint256(keccak256(abi.encode(_extData))) % FIELD_SIZE, "Incorrect external data hash");
require(_args.publicAmount == calculatePublicAmount(_extData.extAmount, _extData.fee), "Invalid public amount");
require(verifyProof(_args), "Invalid transaction proof");
for (uint256 i = 0; i < _args.inputNullifiers.length; i++) { function registerAndTransact(
nullifierHashes[_args.inputNullifiers[i]] = true; Account memory _account,
} Proof memory _proofArgs,
ExtData memory _extData
) public {
register(_account);
transact(_proofArgs, _extData);
}
if (_extData.extAmount < 0) { function onTokenBridged(
require(_extData.recipient != address(0), "Can't withdraw to zero address"); IERC6777 _token,
if (_extData.isL1Withdrawal) { uint256 _amount,
token.transferAndCall(omniBridge, uint256(-_extData.extAmount), abi.encodePacked(l1Unwrapper, _extData.recipient)); bytes calldata _data
} else { ) external override {
token.transfer(_extData.recipient, uint256(-_extData.extAmount)); (Proof memory _args, ExtData memory _extData) = abi.decode(_data, (Proof, ExtData));
} require(_token == token, "provided token is not supported");
require(uint256(-_extData.extAmount) >= minimalWithdrawalAmount, "amount is less than minimalWithdrawalAmount"); // prevents ddos attack to Bridge require(msg.sender == omniBridge, "only omni bridge");
} require(_amount >= uint256(_extData.extAmount), "amount from bridge is incorrect");
if (_extData.fee > 0) { require(token.balanceOf(address(this)) >= uint256(_extData.extAmount) + lastBalance, "bridge did not send enough tokens");
token.transfer(_extData.relayer, _extData.fee); require(uint256(_extData.extAmount) <= maximumDepositAmount, "amount is larger than maximumDepositAmount");
} _transact(_args, _extData);
}
lastBalance = token.balanceOf(address(this)); function configureLimits(uint256 _minimalWithdrawalAmount, uint256 _maximumDepositAmount) public onlyGovernance {
_insert(_args.outputCommitments[0], _args.outputCommitments[1]); _configureLimits(_minimalWithdrawalAmount, _maximumDepositAmount);
emit NewCommitment(_args.outputCommitments[0], nextIndex - 2, _extData.encryptedOutput1);
emit NewCommitment(_args.outputCommitments[1], nextIndex - 1, _extData.encryptedOutput2);
for (uint256 i = 0; i < _args.inputNullifiers.length; i++) {
emit NewNullifier(_args.inputNullifiers[i]);
}
} }
function calculatePublicAmount(int256 _extAmount, uint256 _fee) public pure returns (uint256) { function calculatePublicAmount(int256 _extAmount, uint256 _fee) public pure returns (uint256) {
@ -207,36 +200,43 @@ contract TornadoPool is MerkleTreeWithHistory, IERC20Receiver, ReentrancyGuard,
} }
} }
function register(Account memory _account) public {
require(_account.owner == msg.sender, "only owner can be registered");
_register(_account);
}
function _register(Account memory _account) internal { function _register(Account memory _account) internal {
emit PublicKey(_account.owner, _account.publicKey); emit PublicKey(_account.owner, _account.publicKey);
} }
function registerAndTransact( function _transact(Proof memory _args, ExtData memory _extData) internal nonReentrant {
Account memory _account, require(isKnownRoot(_args.root), "Invalid merkle root");
Proof memory _proofArgs, for (uint256 i = 0; i < _args.inputNullifiers.length; i++) {
ExtData memory _extData require(!isSpent(_args.inputNullifiers[i]), "Input is already spent");
) public { }
register(_account); require(uint256(_args.extDataHash) == uint256(keccak256(abi.encode(_extData))) % FIELD_SIZE, "Incorrect external data hash");
transact(_proofArgs, _extData); require(_args.publicAmount == calculatePublicAmount(_extData.extAmount, _extData.fee), "Invalid public amount");
} require(verifyProof(_args), "Invalid transaction proof");
function onTokenBridged( for (uint256 i = 0; i < _args.inputNullifiers.length; i++) {
IERC6777 _token, nullifierHashes[_args.inputNullifiers[i]] = true;
uint256 _amount, }
bytes calldata _data
) external override { if (_extData.extAmount < 0) {
(Proof memory _args, ExtData memory _extData) = abi.decode(_data, (Proof, ExtData)); require(_extData.recipient != address(0), "Can't withdraw to zero address");
require(_token == token, "provided token is not supported"); if (_extData.isL1Withdrawal) {
require(msg.sender == omniBridge, "only omni bridge"); token.transferAndCall(omniBridge, uint256(-_extData.extAmount), abi.encodePacked(l1Unwrapper, _extData.recipient));
require(_amount >= uint256(_extData.extAmount), "amount from bridge is incorrect"); } else {
require(token.balanceOf(address(this)) >= uint256(_extData.extAmount) + lastBalance, "bridge did not send enough tokens"); token.transfer(_extData.recipient, uint256(-_extData.extAmount));
require(uint256(_extData.extAmount) <= maximumDepositAmount, "amount is larger than maximumDepositAmount"); }
_transact(_args, _extData); require(uint256(-_extData.extAmount) >= minimalWithdrawalAmount, "amount is less than minimalWithdrawalAmount"); // prevents ddos attack to Bridge
}
if (_extData.fee > 0) {
token.transfer(_extData.relayer, _extData.fee);
}
lastBalance = token.balanceOf(address(this));
_insert(_args.outputCommitments[0], _args.outputCommitments[1]);
emit NewCommitment(_args.outputCommitments[0], nextIndex - 2, _extData.encryptedOutput1);
emit NewCommitment(_args.outputCommitments[1], nextIndex - 1, _extData.encryptedOutput2);
for (uint256 i = 0; i < _args.inputNullifiers.length; i++) {
emit NewNullifier(_args.inputNullifiers[i]);
}
} }
function _configureLimits(uint256 _minimalWithdrawalAmount, uint256 _maximumDepositAmount) internal { function _configureLimits(uint256 _minimalWithdrawalAmount, uint256 _maximumDepositAmount) internal {