Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Change RootSet approach to getRootAt #119

Merged
merged 4 commits into from
Jan 2, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions test-flow-generator/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@
"dependencies": {
"@metamask/eth-sig-util": "^4.0.1",
"http-server": "14.1.1",
"libzkbob-rs-node": "0.1.27",
"libzkbob-rs-wasm-web": "^0.7.0",
"libzkbob-rs-node": "0.2.2",
"libzkbob-rs-wasm-web": "0.8.0",
"node-polyfill-webpack-plugin": "^1.1.4",
"puppeteer": "^19.2.0",
"web3-utils": "1.8.0",
Expand Down
41 changes: 8 additions & 33 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -264,24 +264,6 @@
"@jridgewell/resolve-uri" "^3.0.3"
"@jridgewell/sourcemap-codec" "^1.4.10"

"@jridgewell/resolve-uri@^3.0.3":
version "3.1.0"
resolved "https://registry.yarnpkg.com/@jridgewell/resolve-uri/-/resolve-uri-3.1.0.tgz#2203b118c157721addfe69d47b70465463066d78"
integrity sha512-F2msla3tad+Mfht5cJq7LSXcdudKTWCVYUgw6pLFOOHSTtZlj6SWNYAp+AhuqLmWdBO2X5hPrLcu8cVP8fy28w==

"@jridgewell/sourcemap-codec@^1.4.10":
version "1.4.14"
resolved "https://registry.yarnpkg.com/@jridgewell/sourcemap-codec/-/sourcemap-codec-1.4.14.tgz#add4c98d341472a289190b424efbdb096991bb24"
integrity sha512-XPSJHWmi394fuUuzDnGz1wiKqWfo1yXecHQMRf2l6hztTO+nPru658AyDngaBe7isIxEkRsPR3FZh+s7iVa4Uw==

"@jridgewell/trace-mapping@0.3.9":
version "0.3.9"
resolved "https://registry.yarnpkg.com/@jridgewell/trace-mapping/-/trace-mapping-0.3.9.tgz#6534fd5933a53ba7cbf3a17615e273a0d1273ff9"
integrity sha512-3Belt6tdc8bPgAtbcmdtNJlirVoTmEb5e2gC94PnkwEW9jI6CAHUeoG85tjWP5WquqfavoMtMwiG4P926ZKKuQ==
dependencies:
"@jridgewell/resolve-uri" "^3.0.3"
"@jridgewell/sourcemap-codec" "^1.4.10"

"@metamask/eth-sig-util@^4.0.1":
version "4.0.1"
resolved "https://registry.yarnpkg.com/@metamask/eth-sig-util/-/eth-sig-util-4.0.1.tgz#3ad61f6ea9ad73ba5b19db780d40d9aae5157088"
Expand Down Expand Up @@ -3490,24 +3472,17 @@ libzeropool-rs-wasm-bundler@0.3.8:
resolved "https://registry.yarnpkg.com/libzeropool-rs-wasm-bundler/-/libzeropool-rs-wasm-bundler-0.3.8.tgz#2b55628a741f4bd0f776d87365c22ed0da63ba6c"
integrity sha512-b+W6M/gyeaoKZ4LJC2ThtZmXHVOoEtsa3foPezAyfdgbsyNGDHbSMBcqoTIXVHkP6ul7BKxjOP3xvfi3F6klTg==

libzkbob-rs-node@0.1.27:
version "0.1.27"
resolved "https://registry.yarnpkg.com/libzkbob-rs-node/-/libzkbob-rs-node-0.1.27.tgz#d482d45563672fdf2e5ed59039b414a92503513f"
integrity sha512-Y5xMQ4G6kr/YOTV5DMLVhNnrIN52oPVkJQt8j12sB7wGYRwHqFUUsUBXq0TnBkA73Ep+sVmCK/RI1GTVr3CoyQ==
dependencies:
cargo-cp-artifact "^0.1"

libzkbob-rs-node@0.2.1:
version "0.2.1"
resolved "https://registry.yarnpkg.com/libzkbob-rs-node/-/libzkbob-rs-node-0.2.1.tgz#58340fce40c5ba06641f3b3afb131ccfe5fef634"
integrity sha512-Qh2EXpXTVOi1CZ6bvbKjXRw9dkoXgq7ss/YX+fxpJGYRAa6UIyt+Lrv4jZrSddNOGMrlItkDFtNOsXZVfdFSRw==
libzkbob-rs-node@0.2.2:
version "0.2.2"
resolved "https://registry.yarnpkg.com/libzkbob-rs-node/-/libzkbob-rs-node-0.2.2.tgz#2b176b9c74fd24eb59c2f38cba42d227fc7fb152"
integrity sha512-8elJ5FoiZHhnm2Tnu8TLSzC0A4g7+t+UVSIIysn4ZVOVAgFkojTlSniZL9cqgTmjvMxZzySk5DCcKpa6pTp/WQ==
dependencies:
cargo-cp-artifact "^0.1"

libzkbob-rs-wasm-web@^0.7.0:
version "0.7.0"
resolved "https://registry.yarnpkg.com/libzkbob-rs-wasm-web/-/libzkbob-rs-wasm-web-0.7.0.tgz#2bd54bb0687a0194ce7c3e43ceecb55a378dba06"
integrity sha512-aIG5Hg7D7gy5z+Uq7CRGD4t9EPqENUB7+Tb+xSxOJSIcL8ZENUuCJGcLwcrw5YCZ1OOBoe3LU4wa2WbC8cWy2A==
libzkbob-rs-wasm-web@0.8.0:
version "0.8.0"
resolved "https://registry.yarnpkg.com/libzkbob-rs-wasm-web/-/libzkbob-rs-wasm-web-0.8.0.tgz#b133aa0f1a381567fde082662ce4c4e46ea7aa47"
integrity sha512-EIPeDyl2wmpSMx299LnG0UCqg5wy8sr7TqfCdgPDxA8kH5Gu92MF12KOOOx7sfLpoYZ965y7MbkkuUfz5OzVAg==

lighthouse-logger@^1.0.0:
version "1.3.0"
Expand Down
2 changes: 1 addition & 1 deletion zp-relayer/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
"express-winston": "4.2.0",
"gas-price-oracle": "0.5.1",
"ioredis": "5.2.4",
"libzkbob-rs-node": "0.2.1",
"libzkbob-rs-node": "0.2.2",
"node-fetch": "^2.6.1",
"promise-retry": "^2.0.1",
"web3": "1.7.4",
Expand Down
11 changes: 0 additions & 11 deletions zp-relayer/pool.ts
Original file line number Diff line number Diff line change
Expand Up @@ -147,11 +147,6 @@ class Pool {
return
}

// Set initial root
await this.state.roots.add({
0: INIT_ROOT,
})

const numTxs = Math.floor((contractIndex - localIndex) / OUTPLUSONE)
const missedIndices = Array(numTxs)
for (let i = 0; i < numTxs; i++) {
Expand Down Expand Up @@ -206,12 +201,6 @@ class Pool {
// Save nullifier in confirmed state
const nullifier = parser.getField('nullifier')
await this.state.nullifiers.add([web3.utils.hexToNumberString(nullifier)])

// Save root in confirmed state
const root = this.state.getMerkleRoot()
await this.state.roots.add({
[newPoolIndex]: root,
})
}
}

Expand Down
8 changes: 4 additions & 4 deletions zp-relayer/state/PoolState.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,18 @@ import { logger } from '@/services/appLogger'
import { OUTPLUSONE } from '@/utils/constants'
import { MerkleTree, TxStorage, MerkleProof, Constants, Helpers } from 'libzkbob-rs-node'
import { NullifierSet } from './nullifierSet'
import { RootSet } from './rootSet'
import { JobIdsMapping } from './jobIdsMapping'

export class PoolState {
private tree: MerkleTree
private txs: TxStorage
public nullifiers: NullifierSet
public roots: RootSet
public jobIdsMapping: JobIdsMapping

constructor(private name: string, redis: Redis, path: string) {
this.tree = new MerkleTree(`${path}/${name}Tree.db`)
this.txs = new TxStorage(`${path}/${name}Txs.db`)
this.nullifiers = new NullifierSet(`${name}-nullifiers`, redis)
this.roots = new RootSet(`${name}-roots`, redis)
// This structure can be shared among different pool states
// So, use constant name
this.jobIdsMapping = new JobIdsMapping('job-id-mapping', redis)
Expand Down Expand Up @@ -87,7 +84,10 @@ export class PoolState {
return [outCommit, memo]
}

getMerkleRoot(): string {
getMerkleRoot(index?: number): string {
if (index) {
return this.tree.getRootAt(index)
k1rill-fedoseev marked this conversation as resolved.
Show resolved Hide resolved
}
return this.tree.getRoot()
}

Expand Down
23 changes: 0 additions & 23 deletions zp-relayer/state/rootSet.ts

This file was deleted.

39 changes: 8 additions & 31 deletions zp-relayer/validateTx.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,15 @@ import { Proof, SnarkProof } from 'libzkbob-rs-node'
import { logger } from './services/appLogger'
import config from './config'
import type { Limits, Pool } from './pool'
import { NullifierSet } from './state/nullifierSet'
import type { NullifierSet } from './state/nullifierSet'
import TokenAbi from './abi/token-abi.json'
import { web3 } from './services/web3'
import { numToHex, unpackSignature } from './utils/helpers'
import { recoverSaltedPermit } from './utils/EIP712SaltedPermit'
import { ZERO_ADDRESS } from './utils/constants'
import { TxPayload } from './queue/poolTxQueue'
import { getTxProofField, parseDelta } from './utils/proofInputs'
import { RootSet } from './state/rootSet'
import type { PoolState } from './state/PoolState'

const tokenContract = new web3.eth.Contract(TokenAbi as AbiItem[], config.tokenAddress)

Expand Down Expand Up @@ -194,33 +194,12 @@ async function getRecoveredAddress(
return recoveredAddress
}

async function checkRoot(
proofIndex: BN,
proofRoot: string,
poolSet: RootSet,
optimisticSet: RootSet,
contractFallback: (i: string) => Promise<string>
) {
const indexStr = proofIndex.toString(10)

// Lookup root in cache
let isPresent = true
let root = (await poolSet.get(indexStr)) || (await optimisticSet.get(indexStr))

// Get root from contract if not found in cache
if (root === null) {
logger.info('Getting root from contract...')
root = await contractFallback(indexStr)
isPresent = false
}

if (root !== proofRoot) {
return new TxValidationError(`Incorrect root at index ${indexStr}: given ${proofRoot}, expected ${root}`)
}
async function checkRoot(proofIndex: BN, proofRoot: string, state: PoolState) {
const index = proofIndex.toNumber()

// If recieved correct root from contract update cache (only confirmed state)
if (!isPresent) {
await poolSet.add({ [proofIndex.toNumber()]: root })
const stateRoot = state.getMerkleRoot(index)
if (stateRoot !== proofRoot) {
return new TxValidationError(`Incorrect root at index ${index}: given ${proofRoot}, expected ${stateRoot}`)
}

return null
Expand All @@ -239,9 +218,7 @@ export async function validateTx({ txType, rawMemo, txProof, depositSignature }:
await checkAssertion(() => checkRoot(
delta.transferIndex,
root,
pool.state.roots,
pool.optimisticState.roots,
i => pool.getContractMerkleRoot(i)
pool.optimisticState,
))
await checkAssertion(() => checkNullifier(nullifier, pool.state.nullifiers))
await checkAssertion(() => checkNullifier(nullifier, pool.optimisticState.nullifiers))
Expand Down
5 changes: 0 additions & 5 deletions zp-relayer/workers/poolTxWorker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,11 +91,6 @@ export async function createPoolTxWorker<T extends EstimationType>(
pool.optimisticState.updateState(commitIndex, outCommit, prefixedMemo)
logger.debug('Adding nullifier %s to OS', nullifier)
await pool.optimisticState.nullifiers.add([nullifier])
const poolIndex = (commitIndex + 1) * OUTPLUSONE
logger.debug('Adding root %s at %s to OS', rootAfter, poolIndex)
await pool.optimisticState.roots.add({
[poolIndex]: rootAfter,
})

const sentJob = await sentTxQueue.add(
txHash,
Expand Down
9 changes: 0 additions & 9 deletions zp-relayer/workers/sentTxWorker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,6 @@ async function clearOptimisticState() {
pool.optimisticState.rollbackTo(pool.state)
logger.info('Clearing optimistic nullifiers...')
await pool.optimisticState.nullifiers.clear()
logger.info('Clearing optimistic roots...')
await pool.optimisticState.roots.clear()

const root1 = pool.state.getMerkleRoot()
const root2 = pool.optimisticState.getMerkleRoot()
Expand Down Expand Up @@ -112,13 +110,6 @@ export async function createSentTxWorker<T extends EstimationType>(gasPrice: Gas
logger.info('Removing nullifier %s from OS', nullifier)
await pool.optimisticState.nullifiers.remove([nullifier])

// Add root to confirmed state and remove from optimistic one
const poolIndex = ((commitIndex + 1) * OUTPLUSONE).toString(10)
logger.info('Adding root %s %s to PS', poolIndex, root)
await pool.state.roots.add({ [poolIndex]: root })
logger.info('Removing root %s %s from OS', poolIndex, root)
await pool.optimisticState.roots.remove([poolIndex])

const node1 = pool.state.getCommitment(commitIndex)
const node2 = pool.optimisticState.getCommitment(commitIndex)
logger.info(`Assert commitments are equal: ${node1}, ${node2}`)
Expand Down