Skip to content

Commit

Permalink
Add poolId validation (#154)
Browse files Browse the repository at this point in the history
  • Loading branch information
Leonid Tyurin authored and akolotov committed Jan 24, 2023
1 parent e7bf186 commit c390321
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 1 deletion.
3 changes: 3 additions & 0 deletions zp-relayer/pool.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ class Pool {
public state: PoolState
public optimisticState: PoolState
public denominator: BN = toBN(1)
public poolId: BN = toBN(0)
public isInitialized = false

constructor() {
Expand Down Expand Up @@ -105,6 +106,8 @@ class Pool {
if (this.isInitialized) return

this.denominator = toBN(await this.PoolInstance.methods.denominator().call())
this.poolId = toBN(await this.PoolInstance.methods.pool_id().call())

await this.syncState(config.startBlock)
this.isInitialized = true
}
Expand Down
10 changes: 9 additions & 1 deletion zp-relayer/validateTx.ts
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,7 @@ async function getRecoveredAddress(
return recoveredAddress
}

async function checkRoot(proofIndex: BN, proofRoot: string, state: PoolState) {
function checkRoot(proofIndex: BN, proofRoot: string, state: PoolState) {
const index = proofIndex.toNumber()

const stateRoot = state.getMerkleRootAt(index)
Expand All @@ -207,6 +207,13 @@ async function checkRoot(proofIndex: BN, proofRoot: string, state: PoolState) {
return null
}

function checkPoolId(deltaPoolId: BN, contractPoolId: BN) {
if (deltaPoolId.eq(contractPoolId)) {
return null
}
return new TxValidationError(`Incorrect poolId: given ${deltaPoolId}, expected ${contractPoolId}`)
}

async function checkScreener(address: string, traceId?: string) {
if (config.screenerUrl === null || config.screenerToken === null) {
return null
Expand Down Expand Up @@ -259,6 +266,7 @@ export async function validateTx(
fee.toString(10)
)

await checkAssertion(() => checkPoolId(delta.poolId, pool.poolId))
await checkAssertion(() => checkRoot(delta.transferIndex, root, pool.optimisticState))
await checkAssertion(() => checkNullifier(nullifier, pool.state.nullifiers))
await checkAssertion(() => checkNullifier(nullifier, pool.optimisticState.nullifiers))
Expand Down

0 comments on commit c390321

Please sign in to comment.