initialise
This commit is contained in:
commit
9ceb004676
19
Dockerfile
Normal file
19
Dockerfile
Normal file
@ -0,0 +1,19 @@
|
||||
FROM node:14
|
||||
WORKDIR /app
|
||||
|
||||
RUN curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh -s -- -y
|
||||
ENV PATH="/root/.cargo/bin:${PATH}"
|
||||
RUN cargo install zkutil
|
||||
|
||||
COPY --from=tornadocash/tornado-trees \
|
||||
/app/artifacts/circuits/BatchTreeUpdate.params \
|
||||
/app/artifacts/circuits/BatchTreeUpdate \
|
||||
/app/artifacts/circuits/BatchTreeUpdate.dat \
|
||||
/app/artifacts/circuits/BatchTreeUpdate.r1cs \
|
||||
./snarks/
|
||||
|
||||
COPY package.json yarn.lock ./
|
||||
RUN yarn && yarn cache clean --force
|
||||
COPY . .
|
||||
|
||||
CMD ["yarn", "start"]
|
22
LICENSE
Normal file
22
LICENSE
Normal file
@ -0,0 +1,22 @@
|
||||
The MIT License (MIT)
|
||||
|
||||
Copyright (c) 2018 Truffle
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights
|
||||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
copies of the Software, and to permit persons to whom the Software is
|
||||
furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in all
|
||||
copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
SOFTWARE.
|
||||
|
31
README.md
Normal file
31
README.md
Normal file
@ -0,0 +1,31 @@
|
||||
# Root updater [![Build Status](https://github.com/tornadocash/tornado-root-updater/workflows/build/badge.svg)](https://github.com/tornadocash/tornado-root-updater/actions) [![Docker Image Version (latest semver)](https://img.shields.io/docker/v/tornadocash/tornado-root-updater?logo=docker&logoColor=%23FFFFFF&sort=semver)](https://hub.docker.com/repository/docker/tornadocash/tornado-root-updater)
|
||||
|
||||
For Tornado Cash to enable mining, it needs more metadata than is currently available: it needs to know the block number for each Tornado Cash deposit and withdrawal. Since the current version of Tornado cash is immutable the mining system uses a special proxy to collect this data. When users make deposits and withdrawal all necessary data is recorded to the [proxy](https://github.com/tornadocash/tornado-anonymity-mining/blob/master/contracts/TornadoProxy.sol) contract. In order to be used in zkSnark proof, this data should be added to the special Merkle trees (deposits Merkle tree and withdrawals Merkle tree). So long as someone out there does this, the system works smoothly and trustlessly.
|
||||
|
||||
This software helps to upload deposit and withdrawal metadata from Tornado Cash anonymity mining [proxy](https://github.com/tornadocash/tornado-anonymity-mining/blob/master/contracts/TornadoProxy.sol) into the [TornadoTrees](https://github.com/tornadocash/tornado-anonymity-mining/blob/master/contracts/TornadoTrees.sol) contract that handles the Merkle trees.
|
||||
|
||||
Keep in mind, it could cost some ether to do so.
|
||||
|
||||
## Usage with docker
|
||||
|
||||
```shell script
|
||||
wget https://raw.githubusercontent.com/tornadocash/tornado-root-updater/master/docker-compose.yml
|
||||
wget https://raw.githubusercontent.com/tornadocash/tornado-root-updater/master/.env.example -O .env
|
||||
vi .env # update env vars
|
||||
docker-compose up -d
|
||||
```
|
||||
|
||||
## Usage for development
|
||||
|
||||
```shell script
|
||||
brew install redis
|
||||
redis-server
|
||||
|
||||
yarn
|
||||
cp .env.example .env
|
||||
vi .env
|
||||
# download and unzip snark keys from https://tornado-trees-circuit.s3-eu-west-1.amazonaws.com/tornado-trees-circuit.tar.gz to `snarks` directory
|
||||
yarn start
|
||||
```
|
||||
|
||||
Caches events from both mining and tornado cash instances
|
702
abi/tornadoTrees.json
Normal file
702
abi/tornadoTrees.json
Normal file
@ -0,0 +1,702 @@
|
||||
[
|
||||
{
|
||||
"inputs": [
|
||||
{
|
||||
"internalType": "address",
|
||||
"name": "_governance",
|
||||
"type": "address"
|
||||
},
|
||||
{
|
||||
"internalType": "contract ITornadoTreesV1",
|
||||
"name": "_tornadoTreesV1",
|
||||
"type": "address"
|
||||
},
|
||||
{
|
||||
"components": [
|
||||
{
|
||||
"internalType": "uint256",
|
||||
"name": "depositsFrom",
|
||||
"type": "uint256"
|
||||
},
|
||||
{
|
||||
"internalType": "uint256",
|
||||
"name": "depositsStep",
|
||||
"type": "uint256"
|
||||
},
|
||||
{
|
||||
"internalType": "uint256",
|
||||
"name": "withdrawalsFrom",
|
||||
"type": "uint256"
|
||||
},
|
||||
{
|
||||
"internalType": "uint256",
|
||||
"name": "withdrawalsStep",
|
||||
"type": "uint256"
|
||||
}
|
||||
],
|
||||
"internalType": "struct TornadoTrees.SearchParams",
|
||||
"name": "_searchParams",
|
||||
"type": "tuple"
|
||||
}
|
||||
],
|
||||
"stateMutability": "nonpayable",
|
||||
"type": "constructor"
|
||||
},
|
||||
{
|
||||
"anonymous": false,
|
||||
"inputs": [
|
||||
{
|
||||
"indexed": false,
|
||||
"internalType": "address",
|
||||
"name": "instance",
|
||||
"type": "address"
|
||||
},
|
||||
{
|
||||
"indexed": true,
|
||||
"internalType": "bytes32",
|
||||
"name": "hash",
|
||||
"type": "bytes32"
|
||||
},
|
||||
{
|
||||
"indexed": false,
|
||||
"internalType": "uint256",
|
||||
"name": "block",
|
||||
"type": "uint256"
|
||||
},
|
||||
{
|
||||
"indexed": false,
|
||||
"internalType": "uint256",
|
||||
"name": "index",
|
||||
"type": "uint256"
|
||||
}
|
||||
],
|
||||
"name": "DepositData",
|
||||
"type": "event"
|
||||
},
|
||||
{
|
||||
"anonymous": false,
|
||||
"inputs": [
|
||||
{
|
||||
"indexed": false,
|
||||
"internalType": "address",
|
||||
"name": "instance",
|
||||
"type": "address"
|
||||
},
|
||||
{
|
||||
"indexed": true,
|
||||
"internalType": "bytes32",
|
||||
"name": "hash",
|
||||
"type": "bytes32"
|
||||
},
|
||||
{
|
||||
"indexed": false,
|
||||
"internalType": "uint256",
|
||||
"name": "block",
|
||||
"type": "uint256"
|
||||
},
|
||||
{
|
||||
"indexed": false,
|
||||
"internalType": "uint256",
|
||||
"name": "index",
|
||||
"type": "uint256"
|
||||
}
|
||||
],
|
||||
"name": "WithdrawalData",
|
||||
"type": "event"
|
||||
},
|
||||
{
|
||||
"inputs": [],
|
||||
"name": "BYTES_SIZE",
|
||||
"outputs": [
|
||||
{
|
||||
"internalType": "uint256",
|
||||
"name": "",
|
||||
"type": "uint256"
|
||||
}
|
||||
],
|
||||
"stateMutability": "view",
|
||||
"type": "function"
|
||||
},
|
||||
{
|
||||
"inputs": [],
|
||||
"name": "CHUNK_SIZE",
|
||||
"outputs": [
|
||||
{
|
||||
"internalType": "uint256",
|
||||
"name": "",
|
||||
"type": "uint256"
|
||||
}
|
||||
],
|
||||
"stateMutability": "view",
|
||||
"type": "function"
|
||||
},
|
||||
{
|
||||
"inputs": [],
|
||||
"name": "CHUNK_TREE_HEIGHT",
|
||||
"outputs": [
|
||||
{
|
||||
"internalType": "uint256",
|
||||
"name": "",
|
||||
"type": "uint256"
|
||||
}
|
||||
],
|
||||
"stateMutability": "view",
|
||||
"type": "function"
|
||||
},
|
||||
{
|
||||
"inputs": [],
|
||||
"name": "ITEM_SIZE",
|
||||
"outputs": [
|
||||
{
|
||||
"internalType": "uint256",
|
||||
"name": "",
|
||||
"type": "uint256"
|
||||
}
|
||||
],
|
||||
"stateMutability": "view",
|
||||
"type": "function"
|
||||
},
|
||||
{
|
||||
"inputs": [],
|
||||
"name": "SNARK_FIELD",
|
||||
"outputs": [
|
||||
{
|
||||
"internalType": "uint256",
|
||||
"name": "",
|
||||
"type": "uint256"
|
||||
}
|
||||
],
|
||||
"stateMutability": "view",
|
||||
"type": "function"
|
||||
},
|
||||
{
|
||||
"inputs": [],
|
||||
"name": "blockNumber",
|
||||
"outputs": [
|
||||
{
|
||||
"internalType": "uint256",
|
||||
"name": "",
|
||||
"type": "uint256"
|
||||
}
|
||||
],
|
||||
"stateMutability": "view",
|
||||
"type": "function"
|
||||
},
|
||||
{
|
||||
"inputs": [],
|
||||
"name": "depositRoot",
|
||||
"outputs": [
|
||||
{
|
||||
"internalType": "bytes32",
|
||||
"name": "",
|
||||
"type": "bytes32"
|
||||
}
|
||||
],
|
||||
"stateMutability": "view",
|
||||
"type": "function"
|
||||
},
|
||||
{
|
||||
"inputs": [
|
||||
{
|
||||
"internalType": "uint256",
|
||||
"name": "",
|
||||
"type": "uint256"
|
||||
}
|
||||
],
|
||||
"name": "deposits",
|
||||
"outputs": [
|
||||
{
|
||||
"internalType": "bytes32",
|
||||
"name": "",
|
||||
"type": "bytes32"
|
||||
}
|
||||
],
|
||||
"stateMutability": "view",
|
||||
"type": "function"
|
||||
},
|
||||
{
|
||||
"inputs": [],
|
||||
"name": "depositsLength",
|
||||
"outputs": [
|
||||
{
|
||||
"internalType": "uint256",
|
||||
"name": "",
|
||||
"type": "uint256"
|
||||
}
|
||||
],
|
||||
"stateMutability": "view",
|
||||
"type": "function"
|
||||
},
|
||||
{
|
||||
"inputs": [],
|
||||
"name": "depositsV1Length",
|
||||
"outputs": [
|
||||
{
|
||||
"internalType": "uint256",
|
||||
"name": "",
|
||||
"type": "uint256"
|
||||
}
|
||||
],
|
||||
"stateMutability": "view",
|
||||
"type": "function"
|
||||
},
|
||||
{
|
||||
"inputs": [
|
||||
{
|
||||
"internalType": "contract ITornadoTreesV1",
|
||||
"name": "_tornadoTreesV1",
|
||||
"type": "address"
|
||||
},
|
||||
{
|
||||
"internalType": "string",
|
||||
"name": "_type",
|
||||
"type": "string"
|
||||
},
|
||||
{
|
||||
"internalType": "uint256",
|
||||
"name": "index",
|
||||
"type": "uint256"
|
||||
}
|
||||
],
|
||||
"name": "elementExists",
|
||||
"outputs": [
|
||||
{
|
||||
"internalType": "bool",
|
||||
"name": "success",
|
||||
"type": "bool"
|
||||
}
|
||||
],
|
||||
"stateMutability": "view",
|
||||
"type": "function"
|
||||
},
|
||||
{
|
||||
"inputs": [
|
||||
{
|
||||
"internalType": "contract ITornadoTreesV1",
|
||||
"name": "_tornadoTreesV1",
|
||||
"type": "address"
|
||||
},
|
||||
{
|
||||
"internalType": "string",
|
||||
"name": "_type",
|
||||
"type": "string"
|
||||
},
|
||||
{
|
||||
"internalType": "uint256",
|
||||
"name": "_from",
|
||||
"type": "uint256"
|
||||
},
|
||||
{
|
||||
"internalType": "uint256",
|
||||
"name": "_step",
|
||||
"type": "uint256"
|
||||
}
|
||||
],
|
||||
"name": "findArrayLength",
|
||||
"outputs": [
|
||||
{
|
||||
"internalType": "uint256",
|
||||
"name": "",
|
||||
"type": "uint256"
|
||||
}
|
||||
],
|
||||
"stateMutability": "view",
|
||||
"type": "function"
|
||||
},
|
||||
{
|
||||
"inputs": [],
|
||||
"name": "getRegisteredDeposits",
|
||||
"outputs": [
|
||||
{
|
||||
"internalType": "bytes32[]",
|
||||
"name": "_deposits",
|
||||
"type": "bytes32[]"
|
||||
}
|
||||
],
|
||||
"stateMutability": "view",
|
||||
"type": "function"
|
||||
},
|
||||
{
|
||||
"inputs": [],
|
||||
"name": "getRegisteredWithdrawals",
|
||||
"outputs": [
|
||||
{
|
||||
"internalType": "bytes32[]",
|
||||
"name": "_withdrawals",
|
||||
"type": "bytes32[]"
|
||||
}
|
||||
],
|
||||
"stateMutability": "view",
|
||||
"type": "function"
|
||||
},
|
||||
{
|
||||
"inputs": [],
|
||||
"name": "governance",
|
||||
"outputs": [
|
||||
{
|
||||
"internalType": "address",
|
||||
"name": "",
|
||||
"type": "address"
|
||||
}
|
||||
],
|
||||
"stateMutability": "view",
|
||||
"type": "function"
|
||||
},
|
||||
{
|
||||
"inputs": [
|
||||
{
|
||||
"internalType": "address",
|
||||
"name": "_tornadoProxy",
|
||||
"type": "address"
|
||||
},
|
||||
{
|
||||
"internalType": "contract IBatchTreeUpdateVerifier",
|
||||
"name": "_treeUpdateVerifier",
|
||||
"type": "address"
|
||||
}
|
||||
],
|
||||
"name": "initialize",
|
||||
"outputs": [],
|
||||
"stateMutability": "nonpayable",
|
||||
"type": "function"
|
||||
},
|
||||
{
|
||||
"inputs": [],
|
||||
"name": "lastProcessedDepositLeaf",
|
||||
"outputs": [
|
||||
{
|
||||
"internalType": "uint256",
|
||||
"name": "",
|
||||
"type": "uint256"
|
||||
}
|
||||
],
|
||||
"stateMutability": "view",
|
||||
"type": "function"
|
||||
},
|
||||
{
|
||||
"inputs": [],
|
||||
"name": "lastProcessedWithdrawalLeaf",
|
||||
"outputs": [
|
||||
{
|
||||
"internalType": "uint256",
|
||||
"name": "",
|
||||
"type": "uint256"
|
||||
}
|
||||
],
|
||||
"stateMutability": "view",
|
||||
"type": "function"
|
||||
},
|
||||
{
|
||||
"inputs": [],
|
||||
"name": "previousDepositRoot",
|
||||
"outputs": [
|
||||
{
|
||||
"internalType": "bytes32",
|
||||
"name": "",
|
||||
"type": "bytes32"
|
||||
}
|
||||
],
|
||||
"stateMutability": "view",
|
||||
"type": "function"
|
||||
},
|
||||
{
|
||||
"inputs": [],
|
||||
"name": "previousWithdrawalRoot",
|
||||
"outputs": [
|
||||
{
|
||||
"internalType": "bytes32",
|
||||
"name": "",
|
||||
"type": "bytes32"
|
||||
}
|
||||
],
|
||||
"stateMutability": "view",
|
||||
"type": "function"
|
||||
},
|
||||
{
|
||||
"inputs": [
|
||||
{
|
||||
"internalType": "address",
|
||||
"name": "_instance",
|
||||
"type": "address"
|
||||
},
|
||||
{
|
||||
"internalType": "bytes32",
|
||||
"name": "_commitment",
|
||||
"type": "bytes32"
|
||||
}
|
||||
],
|
||||
"name": "registerDeposit",
|
||||
"outputs": [],
|
||||
"stateMutability": "nonpayable",
|
||||
"type": "function"
|
||||
},
|
||||
{
|
||||
"inputs": [
|
||||
{
|
||||
"internalType": "address",
|
||||
"name": "_instance",
|
||||
"type": "address"
|
||||
},
|
||||
{
|
||||
"internalType": "bytes32",
|
||||
"name": "_nullifierHash",
|
||||
"type": "bytes32"
|
||||
}
|
||||
],
|
||||
"name": "registerWithdrawal",
|
||||
"outputs": [],
|
||||
"stateMutability": "nonpayable",
|
||||
"type": "function"
|
||||
},
|
||||
{
|
||||
"inputs": [
|
||||
{
|
||||
"internalType": "address",
|
||||
"name": "_tornadoProxy",
|
||||
"type": "address"
|
||||
}
|
||||
],
|
||||
"name": "setTornadoProxyContract",
|
||||
"outputs": [],
|
||||
"stateMutability": "nonpayable",
|
||||
"type": "function"
|
||||
},
|
||||
{
|
||||
"inputs": [
|
||||
{
|
||||
"internalType": "contract IBatchTreeUpdateVerifier",
|
||||
"name": "_treeUpdateVerifier",
|
||||
"type": "address"
|
||||
}
|
||||
],
|
||||
"name": "setVerifierContract",
|
||||
"outputs": [],
|
||||
"stateMutability": "nonpayable",
|
||||
"type": "function"
|
||||
},
|
||||
{
|
||||
"inputs": [],
|
||||
"name": "tornadoProxy",
|
||||
"outputs": [
|
||||
{
|
||||
"internalType": "address",
|
||||
"name": "",
|
||||
"type": "address"
|
||||
}
|
||||
],
|
||||
"stateMutability": "view",
|
||||
"type": "function"
|
||||
},
|
||||
{
|
||||
"inputs": [],
|
||||
"name": "tornadoTreesV1",
|
||||
"outputs": [
|
||||
{
|
||||
"internalType": "contract ITornadoTreesV1",
|
||||
"name": "",
|
||||
"type": "address"
|
||||
}
|
||||
],
|
||||
"stateMutability": "view",
|
||||
"type": "function"
|
||||
},
|
||||
{
|
||||
"inputs": [],
|
||||
"name": "treeUpdateVerifier",
|
||||
"outputs": [
|
||||
{
|
||||
"internalType": "contract IBatchTreeUpdateVerifier",
|
||||
"name": "",
|
||||
"type": "address"
|
||||
}
|
||||
],
|
||||
"stateMutability": "view",
|
||||
"type": "function"
|
||||
},
|
||||
{
|
||||
"inputs": [
|
||||
{
|
||||
"internalType": "bytes",
|
||||
"name": "_proof",
|
||||
"type": "bytes"
|
||||
},
|
||||
{
|
||||
"internalType": "bytes32",
|
||||
"name": "_argsHash",
|
||||
"type": "bytes32"
|
||||
},
|
||||
{
|
||||
"internalType": "bytes32",
|
||||
"name": "_currentRoot",
|
||||
"type": "bytes32"
|
||||
},
|
||||
{
|
||||
"internalType": "bytes32",
|
||||
"name": "_newRoot",
|
||||
"type": "bytes32"
|
||||
},
|
||||
{
|
||||
"internalType": "uint32",
|
||||
"name": "_pathIndices",
|
||||
"type": "uint32"
|
||||
},
|
||||
{
|
||||
"components": [
|
||||
{
|
||||
"internalType": "bytes32",
|
||||
"name": "hash",
|
||||
"type": "bytes32"
|
||||
},
|
||||
{
|
||||
"internalType": "address",
|
||||
"name": "instance",
|
||||
"type": "address"
|
||||
},
|
||||
{
|
||||
"internalType": "uint32",
|
||||
"name": "block",
|
||||
"type": "uint32"
|
||||
}
|
||||
],
|
||||
"internalType": "struct TornadoTrees.TreeLeaf[256]",
|
||||
"name": "_events",
|
||||
"type": "tuple[256]"
|
||||
}
|
||||
],
|
||||
"name": "updateDepositTree",
|
||||
"outputs": [],
|
||||
"stateMutability": "nonpayable",
|
||||
"type": "function"
|
||||
},
|
||||
{
|
||||
"inputs": [
|
||||
{
|
||||
"internalType": "bytes",
|
||||
"name": "_proof",
|
||||
"type": "bytes"
|
||||
},
|
||||
{
|
||||
"internalType": "bytes32",
|
||||
"name": "_argsHash",
|
||||
"type": "bytes32"
|
||||
},
|
||||
{
|
||||
"internalType": "bytes32",
|
||||
"name": "_currentRoot",
|
||||
"type": "bytes32"
|
||||
},
|
||||
{
|
||||
"internalType": "bytes32",
|
||||
"name": "_newRoot",
|
||||
"type": "bytes32"
|
||||
},
|
||||
{
|
||||
"internalType": "uint32",
|
||||
"name": "_pathIndices",
|
||||
"type": "uint32"
|
||||
},
|
||||
{
|
||||
"components": [
|
||||
{
|
||||
"internalType": "bytes32",
|
||||
"name": "hash",
|
||||
"type": "bytes32"
|
||||
},
|
||||
{
|
||||
"internalType": "address",
|
||||
"name": "instance",
|
||||
"type": "address"
|
||||
},
|
||||
{
|
||||
"internalType": "uint32",
|
||||
"name": "block",
|
||||
"type": "uint32"
|
||||
}
|
||||
],
|
||||
"internalType": "struct TornadoTrees.TreeLeaf[256]",
|
||||
"name": "_events",
|
||||
"type": "tuple[256]"
|
||||
}
|
||||
],
|
||||
"name": "updateWithdrawalTree",
|
||||
"outputs": [],
|
||||
"stateMutability": "nonpayable",
|
||||
"type": "function"
|
||||
},
|
||||
{
|
||||
"inputs": [
|
||||
{
|
||||
"internalType": "bytes32",
|
||||
"name": "_depositRoot",
|
||||
"type": "bytes32"
|
||||
},
|
||||
{
|
||||
"internalType": "bytes32",
|
||||
"name": "_withdrawalRoot",
|
||||
"type": "bytes32"
|
||||
}
|
||||
],
|
||||
"name": "validateRoots",
|
||||
"outputs": [],
|
||||
"stateMutability": "view",
|
||||
"type": "function"
|
||||
},
|
||||
{
|
||||
"inputs": [],
|
||||
"name": "withdrawalRoot",
|
||||
"outputs": [
|
||||
{
|
||||
"internalType": "bytes32",
|
||||
"name": "",
|
||||
"type": "bytes32"
|
||||
}
|
||||
],
|
||||
"stateMutability": "view",
|
||||
"type": "function"
|
||||
},
|
||||
{
|
||||
"inputs": [
|
||||
{
|
||||
"internalType": "uint256",
|
||||
"name": "",
|
||||
"type": "uint256"
|
||||
}
|
||||
],
|
||||
"name": "withdrawals",
|
||||
"outputs": [
|
||||
{
|
||||
"internalType": "bytes32",
|
||||
"name": "",
|
||||
"type": "bytes32"
|
||||
}
|
||||
],
|
||||
"stateMutability": "view",
|
||||
"type": "function"
|
||||
},
|
||||
{
|
||||
"inputs": [],
|
||||
"name": "withdrawalsLength",
|
||||
"outputs": [
|
||||
{
|
||||
"internalType": "uint256",
|
||||
"name": "",
|
||||
"type": "uint256"
|
||||
}
|
||||
],
|
||||
"stateMutability": "view",
|
||||
"type": "function"
|
||||
},
|
||||
{
|
||||
"inputs": [],
|
||||
"name": "withdrawalsV1Length",
|
||||
"outputs": [
|
||||
{
|
||||
"internalType": "uint256",
|
||||
"name": "",
|
||||
"type": "uint256"
|
||||
}
|
||||
],
|
||||
"stateMutability": "view",
|
||||
"type": "function"
|
||||
}
|
||||
]
|
154842
cache/deposit.json
vendored
Normal file
154842
cache/deposit.json
vendored
Normal file
File diff suppressed because it is too large
Load Diff
107466
cache/withdrawal.json
vendored
Normal file
107466
cache/withdrawal.json
vendored
Normal file
File diff suppressed because it is too large
Load Diff
20
docker-compose.yml
Normal file
20
docker-compose.yml
Normal file
@ -0,0 +1,20 @@
|
||||
version: '2'
|
||||
|
||||
services:
|
||||
app:
|
||||
image: tornadocash/tornado-root-updater
|
||||
depends_on: [redis]
|
||||
restart: always
|
||||
env_file: .env
|
||||
environment:
|
||||
REDIS_URL: redis://redis/0
|
||||
|
||||
redis:
|
||||
image: redis
|
||||
restart: always
|
||||
command: [redis-server, --appendonly, 'yes']
|
||||
volumes:
|
||||
- redis:/data
|
||||
|
||||
volumes:
|
||||
redis:
|
30
package.json
Normal file
30
package.json
Normal file
@ -0,0 +1,30 @@
|
||||
{
|
||||
"name": "root-updater",
|
||||
"version": "2.0.0",
|
||||
"description": "",
|
||||
"main": "index.js",
|
||||
"scripts": {
|
||||
"lint": "eslint --ext .js --ignore-path .gitignore .",
|
||||
"start": "node src/index.js"
|
||||
},
|
||||
"keywords": [],
|
||||
"author": "Roman Semenov <semenov.roma@gmail.com>",
|
||||
"license": "ISC",
|
||||
"dependencies": {
|
||||
"@makerdao/multicall": "^0.12.0",
|
||||
"circomlib": "git+https://github.com/tornadocash/circomlib.git#d20d53411d1bef61f38c99a8b36d5d0cc4836aa1",
|
||||
"cron": "^1.8.2",
|
||||
"dotenv": "^8.2.0",
|
||||
"ethers": "^5.0.31",
|
||||
"fixed-merkle-tree": "^0.3.4",
|
||||
"ioredis": "^4.17.3",
|
||||
"snarkjs": "^0.3.59",
|
||||
"torn-token": "^1.0.0",
|
||||
"tornado-trees": "^0.0.5",
|
||||
"tx-manager": "^0.2.9"
|
||||
},
|
||||
"devDependencies": {
|
||||
"babel-eslint": "^10.1.0",
|
||||
"eslint": "^7.5.0"
|
||||
}
|
||||
}
|
106
src/events.js
Normal file
106
src/events.js
Normal file
@ -0,0 +1,106 @@
|
||||
const { getTornadoTrees, redis, getProvider } = require('./singletons')
|
||||
const { action } = require('./utils')
|
||||
const { aggregate } = require('@makerdao/multicall')
|
||||
const ethers = require('ethers')
|
||||
const abi = new ethers.utils.AbiCoder()
|
||||
|
||||
const config = {
|
||||
rpcUrl: process.env.RPC_URL,
|
||||
multicallAddress: '0xeefba1e63905ef1d7acba5a8513c70307c1ce441',
|
||||
}
|
||||
|
||||
async function getTornadoTreesEvents(type, fromBlock, toBlock) {
|
||||
const eventName = type === action.DEPOSIT ? 'DepositData' : 'WithdrawalData'
|
||||
const events = await getProvider().getLogs({
|
||||
address: getTornadoTrees().address,
|
||||
topics: getTornadoTrees().filters[eventName]().topics,
|
||||
fromBlock,
|
||||
toBlock,
|
||||
})
|
||||
return events
|
||||
.map((e) => {
|
||||
const { instance, hash, block, index } = getTornadoTrees().interface.parseLog(e).args
|
||||
const encodedData = abi.encode(
|
||||
['address', 'bytes32', 'uint256'],
|
||||
[instance, hash, block],
|
||||
)
|
||||
return {
|
||||
instance,
|
||||
hash,
|
||||
block: block.toNumber(),
|
||||
index: index.toNumber(),
|
||||
sha3: ethers.utils.keccak256(encodedData),
|
||||
}
|
||||
})
|
||||
.sort((a, b) => a.index - b.index)
|
||||
}
|
||||
|
||||
async function getEventsWithCache(type) {
|
||||
const currentBlock = await getProvider().getBlockNumber()
|
||||
let lastBlock = Number(await redis.get(`${type}LastBlock`) || 0) + 1
|
||||
// if (currentBlock <= lastBlock) {
|
||||
// throw new Error('Current block is lower than last block')
|
||||
// }
|
||||
let cachedEvents = (await redis.lrange(type, 0, -1)).map((e) => JSON.parse(e))
|
||||
if (cachedEvents.length === 0) {
|
||||
cachedEvents = require(`../cache/${type}.json`)
|
||||
if (cachedEvents.length > 0) {
|
||||
lastBlock = cachedEvents.slice(-1)[0].block + 1
|
||||
await redis.rpush(type, cachedEvents.map((e) => JSON.stringify(e)))
|
||||
}
|
||||
}
|
||||
const newEvents = await getTornadoTreesEvents(type, lastBlock, currentBlock)
|
||||
if (newEvents.length > 0) {
|
||||
await redis.rpush(type, newEvents.map((e) => JSON.stringify(e)))
|
||||
}
|
||||
await redis.set(`${type}LastBlock`, currentBlock)
|
||||
return cachedEvents.concat(newEvents)
|
||||
}
|
||||
|
||||
async function getPendingEventHashes(type, from, to) {
|
||||
const calls = []
|
||||
const target = (await getTornadoTrees()).address
|
||||
const method = type === action.DEPOSIT ? 'deposits' : 'withdrawals'
|
||||
for (let i = from; i < to; i++) {
|
||||
calls.push({
|
||||
target,
|
||||
call: [`${method}(uint256)(bytes32)`, i],
|
||||
returns: [[i]],
|
||||
})
|
||||
}
|
||||
const result = await aggregate(calls, config)
|
||||
return Object.values(result.results.original)
|
||||
}
|
||||
|
||||
async function getEvents(type) {
|
||||
const committedMethod = type === action.DEPOSIT ? 'lastProcessedDepositLeaf' : 'lastProcessedWithdrawalLeaf'
|
||||
const committedCount = await getTornadoTrees()[committedMethod]()
|
||||
|
||||
const pendingLengthMethod = type === action.DEPOSIT ? 'depositsLength' : 'withdrawalsLength'
|
||||
const pendingLength = await getTornadoTrees()[pendingLengthMethod]()
|
||||
|
||||
const pendingEventHashes = await getPendingEventHashes(type, committedCount, pendingLength)
|
||||
|
||||
const events = await getEventsWithCache(type)
|
||||
|
||||
const committedEvents = events.slice(0, committedCount)
|
||||
const pendingEvents = pendingEventHashes.map((e) => events.find(a => a.sha3 === e))
|
||||
|
||||
if (pendingEvents.some((e) => e === undefined)) {
|
||||
pendingEvents.forEach((e, i) => {
|
||||
if (e === undefined) {
|
||||
console.log('Unknown event', pendingEventHashes[i])
|
||||
}
|
||||
})
|
||||
throw new Error('Tree contract expects unknown tornado event')
|
||||
}
|
||||
|
||||
return {
|
||||
committedEvents,
|
||||
pendingEvents,
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
getEvents,
|
||||
}
|
17
src/index.js
Normal file
17
src/index.js
Normal file
@ -0,0 +1,17 @@
|
||||
require('dotenv').config()
|
||||
const cron = require('cron')
|
||||
const { getEvents } = require('./events')
|
||||
const { updateTree } = require('./update')
|
||||
const { action } = require('./utils')
|
||||
|
||||
async function main() {
|
||||
// todo retry
|
||||
for (const type of Object.values(action)) {
|
||||
const { committedEvents, pendingEvents } = await getEvents(type)
|
||||
console.log(`There are ${pendingEvents.length} unprocessed ${type}s`)
|
||||
await updateTree(committedEvents, pendingEvents, type)
|
||||
}
|
||||
console.log('Done')
|
||||
}
|
||||
|
||||
cron.job(process.env.CRON_EXPRESSION, main, null, true, null, null, true)
|
40
src/singletons.js
Normal file
40
src/singletons.js
Normal file
@ -0,0 +1,40 @@
|
||||
require('dotenv').config()
|
||||
const ethers = require('ethers')
|
||||
const { TxManager } = require('tx-manager')
|
||||
const tornadoTreesAbi = require('../abi/tornadoTrees.json')
|
||||
const Redis = require('ioredis')
|
||||
const redis = new Redis(process.env.REDIS_URL)
|
||||
const config = require('torn-token')
|
||||
let tornadoTrees
|
||||
let provider
|
||||
|
||||
const txManager = new TxManager({
|
||||
privateKey: process.env.PRIVATE_KEY,
|
||||
rpcUrl: process.env.RPC_URL,
|
||||
broadcastNodes: process.env.BROADCAST_NODES ? process.env.BROADCAST_NODES.split(',') : undefined,
|
||||
config: {
|
||||
CONFIRMATIONS: process.env.CONFIRMATION_BLOCKS,
|
||||
MAX_GAS_PRICE: process.env.GAS_PRICE,
|
||||
},
|
||||
})
|
||||
|
||||
function getProvider() {
|
||||
if (!provider) {
|
||||
provider = new ethers.providers.JsonRpcProvider(process.env.RPC_URL)
|
||||
}
|
||||
return provider
|
||||
}
|
||||
|
||||
function getTornadoTrees() {
|
||||
if (!tornadoTrees) {
|
||||
tornadoTrees = new ethers.Contract(process.env.TORNADO_TREES || config.tornadoTrees.address, tornadoTreesAbi, getProvider())
|
||||
}
|
||||
return tornadoTrees
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
redis,
|
||||
getTornadoTrees,
|
||||
getProvider,
|
||||
txManager,
|
||||
}
|
45
src/update.js
Normal file
45
src/update.js
Normal file
@ -0,0 +1,45 @@
|
||||
require('dotenv').config()
|
||||
const { getTornadoTrees, txManager, getProvider } = require('./singletons')
|
||||
const { action, getExplorer, poseidonHash, poseidonHash2, toFixedHex } = require('./utils')
|
||||
const ethers = require('ethers')
|
||||
const BigNumber = ethers.BigNumber
|
||||
const { parseUnits } = ethers.utils
|
||||
const tornadoTrees = require('tornado-trees')
|
||||
const MerkleTree = require('fixed-merkle-tree')
|
||||
|
||||
const { INSERT_BATCH_SIZE, GAS_PRICE } = process.env
|
||||
|
||||
async function updateTree(committedEvents, pendingEvents, type) {
|
||||
const netId = (await getProvider().getNetwork()).chainId
|
||||
const leaves = committedEvents.map((e) => poseidonHash([e.instance, e.hash, e.block]))
|
||||
const tree = new MerkleTree(20, leaves, { hashFunction: poseidonHash2 })
|
||||
const rootMethod = type === action.DEPOSIT ? 'depositRoot' : 'withdrawalRoot'
|
||||
const root = toFixedHex(await getTornadoTrees()[rootMethod]())
|
||||
if (!BigNumber.from(root).eq(tree.root())) {
|
||||
throw new Error(`Invalid ${type} root! Contract: ${BigNumber.from(root).toHexString()}, local: ${tree.root().toHexString()}`)
|
||||
}
|
||||
while (pendingEvents.length >= INSERT_BATCH_SIZE) {
|
||||
const chunk = pendingEvents.splice(0, INSERT_BATCH_SIZE)
|
||||
|
||||
console.log('Generating snark proof')
|
||||
const { input, args } = tornadoTrees.batchTreeUpdate(tree, chunk)
|
||||
const proof = await tornadoTrees.prove(input, './snarks/BatchTreeUpdate')
|
||||
|
||||
console.log('Sending update tx')
|
||||
const method = type === action.DEPOSIT ? 'updateDepositTree' : 'updateWithdrawalTree'
|
||||
const txData = await getTornadoTrees().populateTransaction[method](proof, ...args, { gasPrice: parseUnits(GAS_PRICE, 'gwei') })
|
||||
const tx = txManager.createTx(txData)
|
||||
|
||||
const receiptPromise = tx
|
||||
.send()
|
||||
.on('transactionHash', (hash) => console.log(`Transaction: ${getExplorer(netId)}/tx/${hash}`))
|
||||
.on('mined', (receipt) => console.log('Mined in block', receipt.blockNumber))
|
||||
.on('confirmations', (n) => console.log(`Got ${n} confirmations`))
|
||||
|
||||
await receiptPromise // todo optional
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
updateTree,
|
||||
}
|
31
src/utils.js
Normal file
31
src/utils.js
Normal file
@ -0,0 +1,31 @@
|
||||
const { BigNumber } = require('ethers')
|
||||
const { poseidon } = require('circomlib')
|
||||
|
||||
/** BigNumber to hex string of specified length */
|
||||
const toFixedHex = (number, length = 32) =>
|
||||
'0x' +
|
||||
(number instanceof Buffer
|
||||
? number.toString('hex')
|
||||
: BigNumber.from(number).toHexString().slice(2)
|
||||
).padStart(length * 2, '0')
|
||||
|
||||
const poseidonHash = (items) => BigNumber.from(poseidon(items))
|
||||
const poseidonHash2 = (a, b) => poseidonHash([a, b])
|
||||
|
||||
const action = Object.freeze({ DEPOSIT: 'deposit', WITHDRAWAL: 'withdrawal' })
|
||||
|
||||
const prefix = {
|
||||
1: '',
|
||||
5: 'goerli.',
|
||||
42: 'kovan.',
|
||||
}
|
||||
|
||||
const getExplorer = (netId) => `https://${prefix[netId]}etherscan.io`
|
||||
|
||||
module.exports = {
|
||||
toFixedHex,
|
||||
poseidonHash,
|
||||
poseidonHash2,
|
||||
getExplorer,
|
||||
action,
|
||||
}
|
0
test/updater.test.js
Normal file
0
test/updater.test.js
Normal file
Loading…
Reference in New Issue
Block a user