tree prover, rebuild tree and resubmit pending transactions

This commit is contained in:
poma 2020-10-02 08:38:31 +03:00
parent 69a528795e
commit 90ed6cbe91
No known key found for this signature in database
GPG key ID: BA20CB01FE165657
6 changed files with 286 additions and 8 deletions

View file

@ -1,3 +1,4 @@
const fs = require('fs')
const Web3 = require('web3')
const { numberToHex, toBN } = require('web3-utils')
const MerkleTree = require('fixed-merkle-tree')
@ -6,27 +7,46 @@ const { GasPriceOracle } = require('gas-price-oracle')
const tornadoABI = require('../abis/tornadoABI.json')
const miningABI = require('../abis/mining.abi.json')
const swapABI = require('../abis/swap.abi.json')
const { queue } = require('./queue')
const { poseidonHash2 } = require('./utils')
const { rpcUrl, redisUrl, privateKey, updateConfig, rewardAccount, minerAddress } = require('../config')
const { rpcUrl, redisUrl, privateKey, updateConfig, swapAddress, rewardAccount, minerAddress } = require('../config')
const TxManager = require('./TxManager')
const { Controller } = require('tornado-cash-anonymity-mining')
let web3
let currentTx
let currentJob
let tree
let txManager
let controller
const redis = new Redis(redisUrl)
const redisSubscribe = new Redis(redisUrl)
const gasPriceOracle = new GasPriceOracle({ defaultRpc: rpcUrl })
async function fetchTree() {
console.log('got tree update')
const elements = await redis.get('tree:elements')
const convert = (_, val) => (typeof val === 'string' ? toBN(val) : val)
tree = MerkleTree.deserialize(JSON.parse(elements, convert), poseidonHash2)
if (currentTx) {
// todo replace
if (currentTx && currentJob && ['miningReward', 'miningWithdraw'].includes(currentJob.data.type)) {
const { proof, args } = currentJob.data.data
if (args.account.inputRoot === tree.root()) { // todo check type
return
}
const update = await controller.treeUpdate(args.account.outputCommitment, tree)
const instance = new web3.eth.Contract(tornadoABI, minerAddress)
const data = currentJob.data.type === 'miningReward' ?
instance.methods.reward(proof, args, update.proof, update.args).encodeABI() :
instance.methods.withdraw(proof, args, update.proof, update.args).encodeABI()
currentTx = await currentTx.replace({
to: minerAddress,
data,
})
console.log('replaced pending tx')
}
}
@ -34,9 +54,15 @@ async function start() {
web3 = new Web3(rpcUrl)
txManager = new TxManager({ privateKey, rpcUrl })
updateConfig({ rewardAccount: txManager.address })
queue.process(process)
redisSubscribe.subscribe('treeUpdate', fetchTree)
await fetchTree()
const provingKeys = {
treeUpdateCircuit: require('../keys/TreeUpdate.json'),
treeUpdateProvingKey: fs.readFileSync('./keys/TreeUpdate_proving_key.bin').buffer,
}
controller = new Controller({ provingKeys })
await controller.init()
queue.process(process)
console.log('Worker started')
}
@ -45,6 +71,13 @@ async function checkTornadoFee(/* contract, fee, refund*/) {
console.log('fast', fast)
}
async function checkMiningFee(points) {
const swap = new web3.eth.Contract(swapABI, swapAddress)
const TornAmount = await swap.getExpectedReturn(points).call()
// todo: use desired torn/eth rate and compute the same way as in tornado
}
async function process(job) {
switch (job.data.type) {
case 'tornadoWithdraw':