Skip to content

Commit

Permalink
fix: submission-interval (#42)
Browse files Browse the repository at this point in the history
* fix update

* fix typo
  • Loading branch information
JSHan94 authored Jun 21, 2024
1 parent c7960f9 commit 24722cd
Show file tree
Hide file tree
Showing 5 changed files with 73 additions and 34 deletions.
2 changes: 1 addition & 1 deletion src/lib/query.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,4 +75,4 @@ export async function getLatestOutputFromExecutor(): Promise<OutputResponse> {
const axiosInstance = AxiosSingleton.getInstance()
const res = await axiosInstance.get(url)
return res.data
}
}
90 changes: 65 additions & 25 deletions src/worker/bridgeExecutor/monitor/l2.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { ExecutorOutputEntity, ExecutorWithdrawalTxEntity } from '../../../orm'
import { Monitor } from './monitor'
import { EntityManager } from 'typeorm'
import { BlockInfo } from 'initia-l2'
import { BlockInfo, OutputInfo } from 'initia-l2'
import { getDB } from '../db'
import { RPCClient } from '../../../lib/rpc'
import winston from 'winston'
Expand Down Expand Up @@ -115,48 +115,85 @@ export class L2Monitor extends Monitor {
return true
}

async checkSubmissionInterval(): Promise<boolean> {
const lastOutputSubmitted = await getLastOutputInfo(this.bridgeId)
if (lastOutputSubmitted) {
const lastOutputSubmittedTime =
lastOutputSubmitted.output_proposal.l1_block_time
const bridgeInfo = await getBridgeInfo(this.bridgeId)
const submissionInterval =
bridgeInfo.bridge_config.submission_interval.seconds.toNumber()
if (
this.getCurTimeSec() <
this.dateToSeconds(lastOutputSubmittedTime) +
Math.floor(submissionInterval * config.SUBMISSION_THRESHOLD)
async checkSubmissionInterval(
lastOutputSubmitted: OutputInfo | null,
lastOutputFromDB: ExecutorOutputEntity | null
): Promise<boolean> {
// if no output from DB, create output (first output)
if (!lastOutputFromDB) {
this.logger.info(
`[checkSubmissionInterval - ${this.name()}] No output from DB`
)
return false
return true
}
return true
}

async handleOutput(manager: EntityManager): Promise<void> {
if (!(await this.checkSubmissionInterval())) {
if (this.currentHeight % 10 === 0)
// if no output submitted, wait for submission
if (!lastOutputSubmitted) return false

// if output index from db is greater, wait for submission
if (lastOutputSubmitted.output_index < lastOutputFromDB.outputIndex) {
this.logger.info(
`[checkSubmissionInterval - ${this.name()}] Output index not matched`
)
return false
}

const lastOutputSubmittedTime =
lastOutputSubmitted.output_proposal.l1_block_time
const bridgeInfo = await getBridgeInfo(this.bridgeId)
const submissionInterval =
bridgeInfo.bridge_config.submission_interval.seconds.toNumber()
const targetTimeSec =
this.dateToSeconds(lastOutputSubmittedTime) +
Math.floor(submissionInterval * config.SUBMISSION_THRESHOLD)

// if submission interval not reached, wait for submission
if (this.getCurTimeSec() < targetTimeSec) {
if (this.currentHeight % 10 === 0) {
this.logger.info(
`[handleOutput - ${this.name()}] Submission interval not reached`
`[checkSubmissionInterval - ${this.name()}] need to wait for submission interval ${targetTimeSec - this.getCurTimeSec()} seconds`
)
return
}

return false
}

const lastOutput = await this.helper.getLastOutputFromDB(
// if submission interval reached, create output
this.logger.info(
`[checkSubmissionInterval - ${this.name()}] Submission interval reached! try to create output...`
)
return true
}

async handleOutput(manager: EntityManager): Promise<void> {
const lastOutputSubmitted = await getLastOutputInfo(this.bridgeId)
const lastOutputFromDB = await this.helper.getLastOutputFromDB(
manager,
ExecutorOutputEntity
)

const lastOutputEndBlockNumber = lastOutput ? lastOutput.endBlockNumber : 0
const lastOutputIndex = lastOutput ? lastOutput.outputIndex : 0
if (
!(await this.checkSubmissionInterval(
lastOutputSubmitted,
lastOutputFromDB
))
)
return

const lastOutputEndBlockNumber = lastOutputSubmitted
? lastOutputSubmitted.output_proposal.l2_block_number
: 0
const lastOutputIndex = lastOutputSubmitted
? lastOutputSubmitted.output_index
: 0

const startBlockNumber = lastOutputEndBlockNumber + 1
const endBlockNumber = this.currentHeight
const outputIndex = lastOutputIndex + 1

if (startBlockNumber > endBlockNumber) {
this.logger.info(
`[handleOutput - ${this.name()}] No new block to process`
`[handleOutput - ${this.name()}] No new block to process ${startBlockNumber - endBlockNumber} block remaining...`
)
return
}
Expand Down Expand Up @@ -186,6 +223,9 @@ export class L2Monitor extends Monitor {
endBlockNumber
)

this.logger.info(
`output entity created: block height (${startBlockNumber} - ${endBlockNumber})`
)
await this.helper.saveEntity(manager, ExecutorOutputEntity, outputEntity)
}

Expand Down
2 changes: 1 addition & 1 deletion src/worker/bridgeExecutor/monitor/monitor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ export abstract class Monitor {
})

this.syncedHeight = state?.height || 0

if (!state) {
if (this.name() === BOT_NAME.EXECUTOR_L1_MONITOR) {
this.syncedHeight = config.EXECUTOR_L1_MONITOR_HEIGHT
Expand Down
3 changes: 1 addition & 2 deletions src/worker/challenger/challenger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -200,13 +200,12 @@ export class Challenger {
: (await getOutputInfoByIndex(this.bridgeId, outputIndex - 1))
.output_proposal.l2_block_number + 1
const endBlockNumber = output.output_proposal.l2_block_number

const state = await manager.getRepository(StateEntity).findOne({
where: { name: 'challenger_l2_monitor' }
})
if (!state || state.height < endBlockNumber) return null


const blockInfo = await config.l2lcd.tendermint.blockInfo(endBlockNumber)

const txEntities = await this.helper.getWithdrawalTxs(
Expand Down
10 changes: 5 additions & 5 deletions src/worker/common/name.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
export const BOT_NAME = {
EXECUTOR_L1_MONITOR: 'executor_l1_monitor',
EXECUTOR_L2_MONITOR: 'executor_l2_monitor',
CHALLENGER_L1_MONITOR: 'challenger_l1_monitor',
CHALLENGER_L2_MONITOR: 'challenger_l2_monitor',
}
EXECUTOR_L1_MONITOR: 'executor_l1_monitor',
EXECUTOR_L2_MONITOR: 'executor_l2_monitor',
CHALLENGER_L1_MONITOR: 'challenger_l1_monitor',
CHALLENGER_L2_MONITOR: 'challenger_l2_monitor'
}

0 comments on commit 24722cd

Please sign in to comment.