tree update fixes

This commit is contained in:
Alexey 2020-10-14 14:43:38 +03:00
parent 838da6eab3
commit e850e042ed
11 changed files with 1026861 additions and 55167 deletions

View File

@ -1,5 +1,6 @@
NET_ID=42
RPC_URL=https://kovan.infura.io
HTTP_RPC_URL=https://kovan.infura.io
WS_RPC_URL=wss://kovan.infura.io/ws/v3/
# ORACLE_RPC_URL should always point to the mainnet
ORACLE_RPC_URL=https://mainnet.infura.io
REDIS_URL=redis://127.0.0.1:6379

607379
TreeUpdate.json Normal file

File diff suppressed because one or more lines are too long

BIN
TreeUpdate_proving_key.bin Normal file

Binary file not shown.

2
keys/.gitignore vendored
View File

@ -1,2 +0,0 @@
# avoid committing non-prduction blobs
./*

File diff suppressed because it is too large Load Diff

Binary file not shown.

View File

@ -31,8 +31,7 @@
"uuid": "^8.3.0",
"web3": "^1.3.0",
"web3-core-promievent": "^1.3.0",
"web3-utils": "^1.2.2",
"why-is-node-running": "^2.2.0"
"web3-utils": "^1.2.2"
},
"devDependencies": {
"chai": "^4.2.0",

View File

@ -5,7 +5,8 @@ const jobType = require('./jobTypes')
module.exports = {
netId: Number(process.env.NET_ID) || 42,
redisUrl: process.env.REDIS_URL || 'redis://127.0.0.1:6379',
rpcUrl: process.env.RPC_URL || 'https://kovan.infura.io/',
httpRpcUrl: process.env.HTTP_RPC_URL,
wsRpcUrl: process.env.WS_RPC_URL,
oracleRpcUrl: process.env.ORACLE_RPC_URL || 'https://mainnet.infura.io/',
oracleAddress: '0xA2b8E7ee7c8a18ea561A5CF7C9C365592026E374',
minerAddress: '0x96c7B5c39542bae92b3cD39392a81De514c6E698', // each network has its own instance

View File

@ -1,11 +1,11 @@
const MerkleTree = require('fixed-merkle-tree')
const { redisUrl, rpcUrl, minerMerkleTreeHeight, minerAddress } = require('./config')
const { redisUrl, wsRpcUrl, minerMerkleTreeHeight, minerAddress } = require('./config')
const { poseidonHash2 } = require('./utils')
const { toBN } = require('web3-utils')
const Redis = require('ioredis')
const redis = new Redis(redisUrl)
const Web3 = require('web3')
const web3 = new Web3(rpcUrl)
const web3 = new Web3(wsRpcUrl)
const contract = new web3.eth.Contract(require('../abis/mining.abi.json'), minerAddress)
let tree, eventSubscription, blockSubscription
@ -28,7 +28,13 @@ async function processNewEvent(err, event) {
// return
}
console.log('New account event', event.returnValues)
console.log(
`New account event
Index: ${event.returnValues.index}
Commitment: ${event.returnValues.commitment}
Nullifier: ${event.returnValues.nullifier}
EncAcc: ${event.returnValues.encryptedAccount}`,
)
const { commitment, index } = event.returnValues
if (tree.elements().length === Number(index)) {
tree.insert(toBN(commitment))

View File

@ -14,7 +14,7 @@ const { poseidonHash2, getInstance, fromDecimals } = require('./utils')
const jobType = require('./jobTypes')
const {
netId,
rpcUrl,
httpRpcUrl,
redisUrl,
privateKey,
swapAddress,
@ -36,36 +36,38 @@ let controller
let swap
const redis = new Redis(redisUrl)
const redisSubscribe = new Redis(redisUrl)
const gasPriceOracle = new GasPriceOracle({ defaultRpc: rpcUrl })
const gasPriceOracle = new GasPriceOracle({ defaultRpc: httpRpcUrl })
const status = Object.freeze({
ACCEPTED: 'ACCEPTED',
SENT: 'SENT',
MINED: 'MINED',
CONFIRMED: 'CONFIRMED',
FAILED: 'FAILED'
FAILED: 'FAILED',
})
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 && currentJob && ['miningReward', 'miningWithdraw'].includes(currentJob.data.type)) {
if (currentTx && currentJob && ['MINING_REWARD', 'MINING_WITHDRAW'].includes(currentJob.data.type)) {
const { proof, args } = currentJob.data
if (toBN(args.account.inputRoot).eq(toBN(tree.root()))) {
console.log('Account root is up to date. Skipping Root Update operation...')
return
} else {
console.log('Account root is outdated. Starting Root Update operation...')
}
const update = await controller.treeUpdate(args.account.outputCommitment, tree)
const instance = new web3.eth.Contract(miningABI, minerAddress)
const data =
currentJob.data.type === 'miningReward'
currentJob.data.type === 'MINING_REWARD'
? instance.methods.reward(proof, args, update.proof, update.args).encodeABI()
: instance.methods.withdraw(proof, args, update.proof, update.args).encodeABI()
currentTx = await currentTx.replace({
await currentTx.replace({
to: minerAddress,
data,
})
@ -74,8 +76,8 @@ async function fetchTree() {
}
async function start() {
web3 = new Web3(rpcUrl)
txManager = new TxManager({ privateKey, rpcUrl })
web3 = new Web3(httpRpcUrl)
txManager = new TxManager({ privateKey, rpcUrl: httpRpcUrl, config: { CONFIRMATIONS: 6 } })
swap = new web3.eth.Contract(swapABI, swapAddress)
redisSubscribe.subscribe('treeUpdate', fetchTree)
await fetchTree()
@ -190,12 +192,12 @@ async function process(job) {
await updateStatus(status.ACCEPTED)
console.log(`Start processing a new ${job.data.type} job #${job.id}`)
await checkFee(job)
if (job.data.type !== jobType.TORNADO_WITHDRAW) {
// precheck if root is up to date
}
currentTx = await txManager.createTx(getTxObject(job))
if (job.data.type !== jobType.TORNADO_WITHDRAW) {
await fetchTree()
}
try {
await currentTx
.send()

View File

@ -3606,11 +3606,6 @@ shebang-regex@^1.0.0:
resolved "https://registry.yarnpkg.com/shebang-regex/-/shebang-regex-1.0.0.tgz#da42f49740c0b42db2ca9728571cb190c98efea3"
integrity sha1-2kL0l0DAtC2yypcoVxyxkMmO/qM=
siginfo@^2.0.0:
version "2.0.0"
resolved "https://registry.yarnpkg.com/siginfo/-/siginfo-2.0.0.tgz#32e76c70b79724e3bb567cb9d543eb858ccfaf30"
integrity sha512-ybx0WO1/8bSBLEWXZvEd7gMW3Sn3JFlW3TvX1nREbDLRNQNaeNN8WK0meBwPdAaOI7TtRRRJn/Es1zhrrCHu7g==
signal-exit@^3.0.0, signal-exit@^3.0.2:
version "3.0.3"
resolved "https://registry.yarnpkg.com/signal-exit/-/signal-exit-3.0.3.tgz#a1410c2edd8f077b08b4e253c8eacfcaf057461c"
@ -3670,11 +3665,6 @@ sshpk@^1.7.0:
safer-buffer "^2.0.2"
tweetnacl "~0.14.0"
stackback@0.0.2:
version "0.0.2"
resolved "https://registry.yarnpkg.com/stackback/-/stackback-0.0.2.tgz#1ac8a0d9483848d1695e418b6d031a3c3ce68e3b"
integrity sha1-Gsig2Ug4SNFpXkGLbQMaPDzmjjs=
standard-as-callback@^2.0.1:
version "2.0.1"
resolved "https://registry.yarnpkg.com/standard-as-callback/-/standard-as-callback-2.0.1.tgz#ed8bb25648e15831759b6023bdb87e6b60b38126"
@ -4410,14 +4400,6 @@ which@^1.2.9:
dependencies:
isexe "^2.0.0"
why-is-node-running@^2.2.0:
version "2.2.0"
resolved "https://registry.yarnpkg.com/why-is-node-running/-/why-is-node-running-2.2.0.tgz#fd0a73ea9303920fbb45457c6ecc392ebec90bd2"
integrity sha512-rxtN9D0lJaYyP92BR5yoyWecK2txBKmBIuS7GRbOPP5bXsT37/hBqcmTrlrt25DBr9p4WJb6c9LuYSJd89vHRQ==
dependencies:
siginfo "^2.0.0"
stackback "0.0.2"
wide-align@1.1.3:
version "1.1.3"
resolved "https://registry.yarnpkg.com/wide-align/-/wide-align-1.1.3.tgz#ae074e6bdc0c14a431e804e624549c633b000457"