Skip to content

Commit

Permalink
fix: revert block validity check
Browse files Browse the repository at this point in the history
feat: check for nullifier uniqueness in block
  • Loading branch information
chancehudson authored and wanseob committed Dec 2, 2021
1 parent 9a49b48 commit 9f46d66
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 30 deletions.
21 changes: 20 additions & 1 deletion packages/coordinator/src/middlewares/default/block-generator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,28 @@ export class BlockGenerator extends GeneratorBase {
this.context.config.maxBytes - consumedBytes,
this.context.gasPrice.muln(this.context.config.priceMultiplier),
)
const usedNullifiers = {}
const validPendingsTxs = (
await Promise.all(
pendingTxs.map(async tx => {
const valid = await this.context.node.layer2.isValidTx(tx)
return valid ? tx : undefined
}),
)
).filter(tx => {
if (!tx) return false
const nullifiers = tx.inflow.map(({ nullifier }) => nullifier.toString())
for (const nullifier of nullifiers) {
if (usedNullifiers[nullifier]) {
return false
}
usedNullifiers[nullifier] = true
}
return true
}) as ZkTx[]
const txs = [] as ZkTx[]
// check each pending tx to make sure it doesn't break the dry patch
for (const tx of pendingTxs) {
for (const tx of validPendingsTxs) {
const { ok } = await this.dryRun([...txs, tx], pendingMassDeposits)
if (!ok) {
logger.info('Warning, transaction dry run not ok, skipping')
Expand Down
28 changes: 0 additions & 28 deletions packages/coordinator/src/middlewares/default/block-proposer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@ import { Block, MAX_MASS_DEPOSIT_COMMIT_GAS } from '@zkopru/core'
import { TransactionReceipt } from 'web3-core'
import { soliditySha3Raw } from 'web3-utils'
import { logger } from '@zkopru/utils'
import { FullValidator } from '@zkopru/core'
import { Bytes32, Address, Uint256 } from 'soltypes'
import { ProposerBase } from '../interfaces/proposer-base'

export class BlockProposer extends ProposerBase {
Expand Down Expand Up @@ -47,38 +45,12 @@ export class BlockProposer extends ProposerBase {
hash: block.header.parentBlock.toString(),
},
})
const parentHeader = await layer2.db.findOne('Header', {
where: {
hash: block.header.parentBlock.toString(),
},
})
if (!parentProposal) {
throw new Error('Unable to find parent proposal')
}
if (!parentProposal.proposalData && parentProposal.proposalNum !== 0) {
throw new Error('No proposal data for parent block')
}
// validate the block before proposing
const validator = new FullValidator(layer1, layer2)
const validationResult = await validator.validate(
{
proposer: Address.from(parentHeader.proposer),
parentBlock: Bytes32.from(parentHeader.parentBlock),
fee: Uint256.from(parentHeader.fee),
utxoRoot: Uint256.from(parentHeader.utxoRoot),
utxoIndex: Uint256.from(parentHeader.utxoIndex),
nullifierRoot: Bytes32.from(parentHeader.nullifierRoot),
withdrawalRoot: Uint256.from(parentHeader.withdrawalRoot),
withdrawalIndex: Uint256.from(parentHeader.withdrawalIndex),
txRoot: Bytes32.from(parentHeader.txRoot),
depositRoot: Bytes32.from(parentHeader.depositRoot),
migrationRoot: Bytes32.from(parentHeader.migrationRoot),
},
block,
)
if (validationResult.slashable) {
throw new Error('Block is slashable, aborting proposal')
}
const bytes = block.serializeBlock()
const blockData = `0x${bytes.toString('hex')}`
let proposeTx: any
Expand Down
3 changes: 2 additions & 1 deletion packages/coordinator/src/tx-pool.ts
Original file line number Diff line number Diff line change
Expand Up @@ -271,7 +271,8 @@ export class TxMemPool implements TxPoolInterface {
revert(txRoot: string) {
const txs = this.queued[txRoot]
if (txs) {
txs.forEach(this.addToTxPool.bind(this))
// TODO: validate transactions before trying to include again
// txs.forEach(this.addToTxPool.bind(this))
}
delete this.queued[txRoot]
}
Expand Down
17 changes: 17 additions & 0 deletions packages/core/src/context/layer2.ts
Original file line number Diff line number Diff line change
Expand Up @@ -395,6 +395,23 @@ export class L2Chain {
}
}
}

// 3. try to find invalid inflow
const nullifiers = zkTx.inflow.map(({ nullifier }) => nullifier.toString())
const utxos = await this.db.findMany('Utxo', {
where: {
nullifier: nullifiers,
usedAt: { ne: null },
},
})
if (utxos.length > 0) return false
const keyedNullifiers = {}
for (const nullifier of nullifiers) {
if (keyedNullifiers[nullifier]) {
return false
}
keyedNullifiers[nullifier] = true
}
return true
}

Expand Down

0 comments on commit 9f46d66

Please sign in to comment.