Skip to content

Commit

Permalink
Merge the devel branch into the main branch, v2.0.0
Browse files Browse the repository at this point in the history
This merge contains the following set of changes:
- Remove /proof_tx endpoint (#59)
- Add merkle root index validation (#60)
- Add endpoints for parameters hash (#65)
- Improvements in user tx validation (#69)
- Update ZkBobPool ABI (#71)
- Configuration ability fo DB files dir (#79)
- Bunch of improvements to robust transaction sending (#78)
- Maintenance of merkle root for more robust transaction verification (#76)
- Add fallback in root lookup method (#86)
  • Loading branch information
akolotov authored Nov 1, 2022
2 parents 3900473 + d584079 commit c894e4c
Show file tree
Hide file tree
Showing 32 changed files with 637 additions and 282 deletions.
5 changes: 1 addition & 4 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,4 @@ prover.js
# Log file
zp.log
yarn-error.log
zp-relayer/optimisticTree.db
zp-relayer/optimisticTxs.db
zp-relayer/poolTree.db
zp-relayer/poolTxs.db
zp-relayer/POOL_STATE
9 changes: 7 additions & 2 deletions CONFIGURATION.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,21 @@
| RELAYER_GAS_LIMIT | Gas limit for pool transactions | integer |
| RELAYER_FEE | Minimal accepted relayer fee (in tokens | integer |
| MAX_NATIVE_AMOUNT_FAUCET | Maximal amount of faucet value (in ETH) | integer |
| TREE_UPDATE_PARAMS_PATH | Local path to tree update parameters | string |
| TREE_UPDATE_PARAMS_PATH | Local path to tree update circuit parameters | string |
| TRANSFER_PARAMS_PATH | Local path to transfer circuit parameters | string |
| TX_VK_PATH | Local path to transaction curcuit verification key | string |
| STATE_DIR_PATH | Path to persistent state files related to tree and transactions storage. Default: `./POOL_STATE` | string |
| GAS_PRICE_FALLBACK | Default fallback gas price | integer |
| GAS_PRICE_ESTIMATION_TYPE | Gas price estimation type | `web3` / `gas-price-oracle` / `eip1559-gas-estimation` / `polygon-gasstation-v2` |
| GAS_PRICE_SPEED_TYPE | This parameter specifies the desirable transaction speed | `instant` / `fast` / `standard` / `low` |
| GAS_PRICE_FACTOR | A value that will multiply the gas price of the oracle to convert it to gwei. If the oracle API returns gas prices in gwei then this can be set to `1`. Also, it could be used to intentionally pay more gas than suggested by the oracle to guarantee the transaction verification. E.g. `1.25` or `1.5`. | integer |
| GAS_PRICE_UPDATE_INTERVAL | Interval in milliseconds used to get the updated gas price value using specified estimation type | integer |
| MAX_FEE_PER_GAS_LIMIT | Max limit on `maxFeePerGas` parameter for each transaction in wei | integer |
| START_BLOCK | The block number used to start searching for events when the relayer instance is run for the first time | integer
| EVENTS_PROCESSING_BATCH_SIZE | Batch size for one `eth_getLogs` request when reprocessing old logs. Defaults to `10000` | integer
| RELAYER_LOG_LEVEL | Log level | Winston log level |
| RELAYER_REDIS_URL | Url to redis instance | URL |
| RPC_URL | Url to RPC node | URL |
| SENT_TX_DELAY | Delay in milliseconds for sentTxWorker to verify submitted transactions | integer
| SENT_TX_DELAY | Delay in milliseconds for sentTxWorker to verify submitted transactions | integer |
| PERMIT_DEADLINE_THRESHOLD_INITIAL | Minimum time threshold in seconds for permit signature deadline to be valid (before initial transaction submition) | integer |
| PERMIT_DEADLINE_THRESHOLD_RESEND | Minimum time threshold in seconds for permit signature deadline to be valid (for re-send attempts) | integer |
3 changes: 0 additions & 3 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -6765,6 +6765,3 @@ yocto-queue@^0.1.0:
dependencies:
borsh "^0.5.0"
buffer "^6.0.3"
stream-http "^3.2.0"
web3 "1.7.4"
webpack "^5.46.0"
10 changes: 4 additions & 6 deletions zp-memo-parser/memo.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
import BN from 'bn.js'
import { Buffer } from 'buffer'
import { deserialize, BinaryReader } from 'borsh'
import { toBN } from 'web3-utils'

type Option<T> = T | null

Expand All @@ -13,16 +11,16 @@ export enum TxType {
}

interface DefaultTxData {
fee: BN
fee: string
}

export interface WithdrawTxData extends DefaultTxData {
nativeAmount: BN
nativeAmount: string
reciever: Uint8Array
}

export interface PermittableDepositTxData extends DefaultTxData {
deadline: BN
deadline: string
holder: Uint8Array
}

Expand Down Expand Up @@ -97,7 +95,7 @@ function getNoteHashes(rawHashes: Buffer, num: number, maxNotes: number): Uint8A
export function getTxData(data: Buffer, txType: Option<TxType>): TxData {
function readU64(offset: number) {
let uint = data.readBigUInt64BE(offset)
return toBN(uint.toString())
return uint.toString(10)
}
let offset = 0
const fee = readU64(offset)
Expand Down
5 changes: 1 addition & 4 deletions zp-memo-parser/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,6 @@
},
"dependencies": {
"borsh": "^0.5.0",
"buffer": "^6.0.3",
"stream-http": "^3.2.0",
"webpack": "^5.46.0",
"web3": "1.7.4"
"buffer": "^6.0.3"
}
}
60 changes: 59 additions & 1 deletion zp-relayer/abi/pool-abi.json
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,25 @@
"name": "OwnershipTransferred",
"type": "event"
},
{
"anonymous": false,
"inputs": [
{
"indexed": false,
"internalType": "address",
"name": "user",
"type": "address"
},
{
"indexed": false,
"internalType": "uint8",
"name": "tier",
"type": "uint8"
}
],
"name": "UpdateTier",
"type": "event"
},
{
"inputs": [
{
Expand Down Expand Up @@ -170,6 +189,11 @@
"internalType": "uint256",
"name": "depositCap",
"type": "uint256"
},
{
"internalType": "uint8",
"name": "tier",
"type": "uint8"
}
],
"internalType": "struct ZkBobAccounting.Limits",
Expand Down Expand Up @@ -317,6 +341,11 @@
},
{
"inputs": [
{
"internalType": "uint8",
"name": "_tier",
"type": "uint8"
},
{
"internalType": "uint256",
"name": "_tvlCap",
Expand Down Expand Up @@ -361,6 +390,24 @@
"stateMutability": "nonpayable",
"type": "function"
},
{
"inputs": [
{
"internalType": "uint8",
"name": "_tier",
"type": "uint8"
},
{
"internalType": "address[]",
"name": "_users",
"type": "address[]"
}
],
"name": "setUsersTier",
"outputs": [],
"stateMutability": "nonpayable",
"type": "function"
},
{
"inputs": [],
"name": "token",
Expand Down Expand Up @@ -421,7 +468,18 @@
"type": "function"
},
{
"inputs": [],
"inputs": [
{
"internalType": "address",
"name": "_operator",
"type": "address"
},
{
"internalType": "address",
"name": "_to",
"type": "address"
}
],
"name": "withdrawFee",
"outputs": [],
"stateMutability": "nonpayable",
Expand Down
5 changes: 1 addition & 4 deletions zp-relayer/clear.sh
Original file line number Diff line number Diff line change
@@ -1,4 +1 @@
rm -rf ./optimisticTree.db
rm -rf ./optimisticTxs.db
rm -rf ./poolTree.db
rm -rf ./poolTxs.db
rm -rf ./POOL_STATE
5 changes: 5 additions & 0 deletions zp-relayer/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,18 +17,23 @@ const config = {
relayerFee: toBN(process.env.RELAYER_FEE as string),
maxFaucet: toBN(process.env.MAX_NATIVE_AMOUNT_FAUCET as string),
treeUpdateParamsPath: process.env.TREE_UPDATE_PARAMS_PATH || './params/tree_params.bin',
transferParamsPath: process.env.TRANSFER_PARAMS_PATH || './params/transfer_params.bin',
txVKPath: process.env.TX_VK_PATH || './params/transfer_verification_key.json',
stateDirPath: process.env.STATE_DIR_PATH || './POOL_STATE',
gasPriceFallback: process.env.GAS_PRICE_FALLBACK as string,
gasPriceEstimationType: (process.env.GAS_PRICE_ESTIMATION_TYPE as EstimationType) || 'web3',
gasPriceSpeedType: (process.env.GAS_PRICE_SPEED_TYPE as GasPriceKey) || 'fast',
gasPriceFactor: parseInt(process.env.GAS_PRICE_FACTOR || '1'),
gasPriceUpdateInterval: parseInt(process.env.GAS_PRICE_UPDATE_INTERVAL || '5000'),
maxFeeLimit: process.env.MAX_FEE_PER_GAS_LIMIT ? toBN(process.env.MAX_FEE_PER_GAS_LIMIT) : null,
startBlock: parseInt(process.env.START_BLOCK || '0'),
eventsProcessingBatchSize: parseInt(process.env.EVENTS_PROCESSING_BATCH_SIZE || '10000'),
logLevel: process.env.RELAYER_LOG_LEVEL || 'debug',
redisUrl: process.env.RELAYER_REDIS_URL,
rpcUrl: process.env.RPC_URL as string,
sentTxDelay: parseInt(process.env.SENT_TX_DELAY || '30000'),
permitDeadlineThresholdInitial: parseInt(process.env.PERMIT_DEADLINE_THRESHOLD_INITIAL || '300'),
permitDeadlineThresholdResend: parseInt(process.env.PERMIT_DEADLINE_THRESHOLD_RESEND || '10'),
}

export default config
42 changes: 16 additions & 26 deletions zp-relayer/endpoints.ts
Original file line number Diff line number Diff line change
@@ -1,41 +1,17 @@
import fs from 'fs'
import { Request, Response, NextFunction } from 'express'
import { pool } from './pool'
import { logger } from './services/appLogger'
import { poolTxQueue } from './queue/poolTxQueue'
import config from './config'
import { proveTx } from './prover'
import {
checkGetLimits,
checkGetTransactions,
checkGetTransactionsV2,
checkMerkleRootErrors,
checkSendTransactionErrors,
checkSendTransactionsErrors,
} from './validation/validation'

const txProof = (() => {
let txProofNum = 0
return async (req: Request, res: Response, next: NextFunction) => {
try {
logger.debug('Proving tx...')
const { pub, sec } = req.body
if (logger.isDebugEnabled()) {
const TX_PROOFS_DIR = 'tx_proofs'
if (!fs.existsSync(TX_PROOFS_DIR)) {
fs.mkdirSync(TX_PROOFS_DIR, { recursive: true })
}
fs.writeFileSync(`${TX_PROOFS_DIR}/object${txProofNum}.json`, JSON.stringify([pub, sec], null, 2))
txProofNum += 1
}
const proof = await proveTx(pub, sec)
logger.debug('Tx proved')
res.json(proof)
} catch (err) {
next(err)
}
}
})()

async function sendTransactions(req: Request, res: Response, next: NextFunction) {
const errors = checkSendTransactionsErrors(req.body)
if (errors) {
Expand Down Expand Up @@ -73,6 +49,13 @@ async function sendTransaction(req: Request, res: Response, next: NextFunction)
}

async function merkleRoot(req: Request, res: Response, next: NextFunction) {
const errors = checkMerkleRootErrors(req.params)
if (errors) {
logger.info('Request errors: %o', errors)
res.status(400).json({ errors })
return
}

const index = req.params.index
const root = await pool.getContractMerkleRoot(index)
res.json(root)
Expand Down Expand Up @@ -180,12 +163,18 @@ async function getLimits(req: Request, res: Response) {
res.json(limitsFetch)
}

function getParamsHash(type: 'tree' | 'transfer') {
const hash = type === 'tree' ? pool.treeParamsHash : pool.transferParamsHash
return (req: Request, res: Response) => {
res.json({ hash })
}
}

function root(req: Request, res: Response) {
return res.sendStatus(200)
}

export default {
txProof,
sendTransaction,
sendTransactions,
merkleRoot,
Expand All @@ -195,5 +184,6 @@ export default {
relayerInfo,
getFee,
getLimits,
getParamsHash,
root,
}
1 change: 1 addition & 0 deletions zp-relayer/init.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ export async function init() {
const gasPriceService = new GasPrice(web3, config.gasPriceUpdateInterval, config.gasPriceEstimationType, {
speedType: config.gasPriceSpeedType,
factor: config.gasPriceFactor,
maxFeeLimit: config.maxFeeLimit,
})
await gasPriceService.start()
const workerMutex = new Mutex()
Expand Down
23 changes: 0 additions & 23 deletions zp-relayer/nullifierSet.ts

This file was deleted.

4 changes: 2 additions & 2 deletions zp-relayer/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "zp-relayer",
"version": "1.2.1",
"version": "2.0.0",
"main": "build/index.js",
"types": "build/index.d.ts",
"scripts": {
Expand All @@ -10,7 +10,7 @@
"dev:worker": "ts-node poolTxWorker.ts",
"start:dev": "ts-node index.ts",
"start:prod": "node index.js",
"test": "ts-mocha --timeout 1000000 test/**/*.test.ts"
"test": "ts-mocha --paths --timeout 1000000 test/**/*.test.ts"
},
"dependencies": {
"@metamask/eth-sig-util": "^4.0.1",
Expand Down
Loading

0 comments on commit c894e4c

Please sign in to comment.