mirror of
https://github.com/tornadocash/tornado-core.git
synced 2024-10-01 01:06:17 -04:00
make the relayer great again
This commit is contained in:
parent
a8331eea08
commit
ee2772328c
@ -31,6 +31,8 @@ template Withdraw(levels, rounds) {
|
||||
signal input root;
|
||||
signal input nullifierHash;
|
||||
signal input receiver; // not taking part in any computations
|
||||
signal input relayer; // not taking part in any computations
|
||||
signal input fee; // not taking part in any computations
|
||||
|
||||
signal private input nullifier;
|
||||
signal private input secret;
|
||||
@ -55,7 +57,11 @@ template Withdraw(levels, rounds) {
|
||||
// Most likely it is not required, but it's better to stay on the safe side and it only takes 2 constraints
|
||||
// Squares are used to prevent optimizer from removing those constraints
|
||||
signal receiverSquare;
|
||||
signal feeSquare;
|
||||
signal relayerSquare;
|
||||
receiverSquare <== receiver * receiver;
|
||||
feeSquare <== fee * fee;
|
||||
relayerSquare <== relayer * relayer;
|
||||
}
|
||||
|
||||
component main = Withdraw(16, 220);
|
||||
|
@ -25,86 +25,14 @@ contract ETHMixer is Mixer, GSNRecipient {
|
||||
) Mixer(_verifier, _mixDenomination, _merkleTreeHeight, _emptyElement, _operator) public {
|
||||
}
|
||||
|
||||
function _processWithdraw(address payable _receiver) internal {
|
||||
_receiver.transfer(mixDenomination);
|
||||
function _processWithdraw(address payable _receiver, address payable _relayer, uint256 _fee) internal {
|
||||
_receiver.transfer(mixDenomination - _fee);
|
||||
if (_fee > 0) {
|
||||
_relayer.transfer(_fee);
|
||||
}
|
||||
}
|
||||
|
||||
function _processDeposit() internal {
|
||||
require(msg.value == mixDenomination, "Please send `mixDenomination` ETH along with transaction");
|
||||
}
|
||||
|
||||
function withdrawViaRelayer(uint256[2] memory a, uint256[2][2] memory b, uint256[2] memory c, uint256[3] memory input) public {
|
||||
uint256 root = input[0];
|
||||
uint256 nullifierHash = input[1];
|
||||
address receiver = address(input[2]);
|
||||
require(!nullifierHashes[nullifierHash], "The note has been already spent");
|
||||
|
||||
require(isKnownRoot(root), "Cannot find your merkle root"); // Make sure to use a recent one
|
||||
require(verifier.verifyProof(a, b, c, input), "Invalid withdraw proof");
|
||||
nullifierHashes[nullifierHash] = true;
|
||||
emit Withdraw(receiver, nullifierHash, tx.origin);
|
||||
// we will process withdraw in postRelayedCall func
|
||||
}
|
||||
|
||||
// gsn related stuff
|
||||
// this func is called by a Relayer via the RelayerHub before sending a tx
|
||||
function acceptRelayedCall(
|
||||
address relay,
|
||||
address from,
|
||||
bytes memory encodedFunction,
|
||||
uint256 transactionFee,
|
||||
uint256 gasPrice,
|
||||
uint256 gasLimit,
|
||||
uint256 nonce,
|
||||
bytes memory approvalData,
|
||||
uint256 maxPossibleCharge
|
||||
) public view returns (uint256, bytes memory) {
|
||||
// think of a withdraw dry-run
|
||||
if (!compareBytesWithSelector(encodedFunction, this.withdrawViaRelayer.selector)) {
|
||||
return (1, "Only withdrawViaRelayer can be called");
|
||||
}
|
||||
bytes memory recipient;
|
||||
assembly {
|
||||
let dataPointer := add(encodedFunction, 32)
|
||||
let recipientPointer := mload(add(dataPointer, 324)) // 4 + (8 * 32) + (32) + (32) == selector + proof + root + nullifier
|
||||
mstore(recipient, 32) // save array length
|
||||
mstore(add(recipient, 32), recipientPointer) // save recipient address
|
||||
}
|
||||
return (0, recipient);
|
||||
}
|
||||
|
||||
// this func is called by RelayerHub right before calling a target func
|
||||
function preRelayedCall(bytes calldata /*context*/) external returns (bytes32) {}
|
||||
|
||||
event Debug(uint actualCharge, bytes context, address recipient);
|
||||
// this func is called by RelayerHub right after calling a target func
|
||||
function postRelayedCall(bytes memory context, bool /*success*/, uint actualCharge, bytes32 /*preRetVal*/) public {
|
||||
IRelayHub relayHub = IRelayHub(getHubAddr());
|
||||
address payable recipient;
|
||||
assembly {
|
||||
recipient := mload(add(context, 32))
|
||||
}
|
||||
emit Debug(actualCharge, context, recipient);
|
||||
|
||||
recipient.transfer(mixDenomination - actualCharge);
|
||||
relayHub.depositFor.value(actualCharge)(address(this));
|
||||
// or we can send actualCharge somewhere else...
|
||||
}
|
||||
|
||||
function compareBytesWithSelector(bytes memory data, bytes4 sel) internal pure returns (bool) {
|
||||
return data[0] == sel[0]
|
||||
&& data[1] == sel[1]
|
||||
&& data[2] == sel[2]
|
||||
&& data[3] == sel[3];
|
||||
}
|
||||
|
||||
function withdrawFundsFromHub(uint256 amount, address payable dest) external {
|
||||
require(msg.sender == operator, "unauthorized");
|
||||
IRelayHub(getHubAddr()).withdraw(amount, dest);
|
||||
}
|
||||
|
||||
function upgradeRelayHub(address newRelayHub) external {
|
||||
require(msg.sender == operator, "unauthorized");
|
||||
_upgradeRelayHub(newRelayHub);
|
||||
}
|
||||
}
|
||||
|
@ -14,7 +14,7 @@ pragma solidity ^0.5.8;
|
||||
import "./MerkleTreeWithHistory.sol";
|
||||
|
||||
contract IVerifier {
|
||||
function verifyProof(uint256[2] memory a, uint256[2][2] memory b, uint256[2] memory c, uint256[3] memory input) public returns(bool);
|
||||
function verifyProof(uint256[2] memory a, uint256[2][2] memory b, uint256[2] memory c, uint256[5] memory input) public returns(bool);
|
||||
}
|
||||
|
||||
contract Mixer is MerkleTreeWithHistory {
|
||||
@ -29,7 +29,7 @@ contract Mixer is MerkleTreeWithHistory {
|
||||
uint256 public mixDenomination;
|
||||
|
||||
event Deposit(uint256 indexed commitment, uint256 leafIndex, uint256 timestamp);
|
||||
event Withdraw(address to, uint256 nullifierHash, address indexed relayer);
|
||||
event Withdraw(address to, uint256 nullifierHash, address indexed relayer, uint fee);
|
||||
|
||||
/**
|
||||
@dev The constructor
|
||||
@ -75,17 +75,20 @@ contract Mixer is MerkleTreeWithHistory {
|
||||
- the receiver of funds
|
||||
- optional fee that goes to the transaction sender (usually a relay)
|
||||
*/
|
||||
function withdraw(uint256[2] memory a, uint256[2][2] memory b, uint256[2] memory c, uint256[3] memory input) public {
|
||||
function withdraw(uint256[2] memory a, uint256[2][2] memory b, uint256[2] memory c, uint256[5] memory input) public {
|
||||
uint256 root = input[0];
|
||||
uint256 nullifierHash = input[1];
|
||||
address payable receiver = address(input[2]);
|
||||
address payable relayer = address(input[3]);
|
||||
uint256 fee = input[4];
|
||||
require(fee < mixDenomination, "Fee exceeds transfer value");
|
||||
require(!nullifierHashes[nullifierHash], "The note has been already spent");
|
||||
|
||||
require(isKnownRoot(root), "Cannot find your merkle root"); // Make sure to use a recent one
|
||||
require(verifier.verifyProof(a, b, c, input), "Invalid withdraw proof");
|
||||
nullifierHashes[nullifierHash] = true;
|
||||
_processWithdraw(receiver);
|
||||
emit Withdraw(receiver, nullifierHash, receiver);
|
||||
_processWithdraw(receiver, relayer, fee);
|
||||
emit Withdraw(receiver, nullifierHash, relayer, fee);
|
||||
}
|
||||
|
||||
function toggleDeposits() external {
|
||||
@ -103,5 +106,5 @@ contract Mixer is MerkleTreeWithHistory {
|
||||
}
|
||||
|
||||
function _processDeposit() internal {}
|
||||
function _processWithdraw(address payable _receiver) internal {}
|
||||
function _processWithdraw(address payable _receiver, address payable _relayer, uint256 _fee) internal {}
|
||||
}
|
||||
|
@ -13,6 +13,6 @@ module.exports = function(deployer, network, accounts) {
|
||||
await ETHMixer.link(MiMC, miMC.address)
|
||||
const mixer = await deployer.deploy(ETHMixer, verifier.address, ETH_AMOUNT, MERKLE_TREE_HEIGHT, EMPTY_ELEMENT, accounts[0])
|
||||
console.log('ETHMixer\'s address ', mixer.address)
|
||||
const tx = await mixer.initialize()
|
||||
// const tx = await mixer.initialize()
|
||||
})
|
||||
}
|
||||
|
1337
package-lock.json
generated
1337
package-lock.json
generated
File diff suppressed because it is too large
Load Diff
@ -33,9 +33,9 @@
|
||||
"eslint": "^6.2.2",
|
||||
"ganache-cli": "^6.4.5",
|
||||
"snarkjs": "^0.1.16",
|
||||
"truffle": "^5.0.27",
|
||||
"truffle-artifactor": "^4.0.23",
|
||||
"truffle-contract": "^4.0.24",
|
||||
"truffle": "^5.0.37",
|
||||
"truffle-artifactor": "^4.0.30",
|
||||
"truffle-contract": "^4.0.31",
|
||||
"truffle-hdwallet-provider": "^1.0.14",
|
||||
"web3": "^1.0.0-beta.55",
|
||||
"web3-utils": "^1.0.0-beta.55",
|
||||
|
@ -45,7 +45,7 @@ function getRandomReceiver() {
|
||||
return receiver
|
||||
}
|
||||
|
||||
contract('GSN support', accounts => {
|
||||
contract.skip('GSN support', accounts => {
|
||||
let mixer
|
||||
let gsnMixer
|
||||
let relayHubAddress
|
||||
@ -93,7 +93,7 @@ contract('GSN support', accounts => {
|
||||
})
|
||||
|
||||
describe('#withdrawViaRelayer', () => {
|
||||
it.only('should work', async () => {
|
||||
it('should work', async () => {
|
||||
const gasPrice = toBN('20000000000')
|
||||
const relayerTxFee = 10 // 20%
|
||||
const deposit = generateDeposit()
|
||||
|
Loading…
Reference in New Issue
Block a user