Skip to content

Commit

Permalink
Merge pull request #147 from tokamak-network/OR-1566-thanos-cannon-ad…
Browse files Browse the repository at this point in the history
…d-getMessageStatus-for-Portals

Or 1566 thanos cannon add get message status for portals
  • Loading branch information
nguyenzung authored May 2, 2024
2 parents 6475ed7 + 38e6f9e commit 302e1e3
Show file tree
Hide file tree
Showing 2 changed files with 99 additions and 4 deletions.
69 changes: 66 additions & 3 deletions packages/tokamak/sdk/src/portals.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,12 @@ import {
} from '@ethersproject/abstract-provider'
import { Signer } from '@ethersproject/abstract-signer'
import { BigNumber, PayableOverrides, ethers } from 'ethers'
import { sleep, DepositTx, toRpcHexString } from '@tokamak-network/core-utils'
import {
sleep,
DepositTx,
toRpcHexString,
predeploys,
} from '@tokamak-network/core-utils'

import {
DepositTransactionRequest,
Expand All @@ -18,6 +23,7 @@ import {
ProvenWithdrawal,
WithdrawalMessageInfo,
WithdrawalTransactionRequest,
MessageStatus,
} from './interfaces'
import {
calculateWithdrawalMessage,
Expand Down Expand Up @@ -190,6 +196,57 @@ export class Portals {
throw new Error(`timed out waiting for relayed deposit transaction`)
}

public async getMessageStatus(
txReceipt: TransactionReceipt
): Promise<MessageStatus> {
return txReceipt.to === predeploys.L2ToL1MessagePasser
? this.getL2ToL1MessageStatusByReceipt(txReceipt)
: this.getL1ToL2MessageStatusByReceipt(txReceipt)
}

public async getL1ToL2MessageStatusByReceipt(
txReceipt: TransactionReceipt
): Promise<MessageStatus> {
const l1BlockNumber = await this.getL1BlockNumber()
return txReceipt.blockNumber > l1BlockNumber
? MessageStatus.UNCONFIRMED_L1_TO_L2_MESSAGE
: MessageStatus.RELAYED
}

public async getL2ToL1MessageStatusByReceipt(
txReceipt: TransactionReceipt
): Promise<MessageStatus> {
const l2BlockNumber = txReceipt.blockNumber
if (l2BlockNumber > (await this.getL2BlockNumberInOO())) {
return MessageStatus.STATE_ROOT_NOT_PUBLISHED
}
const withdrawalMessageInfo = await this.calculateWithdrawalMessage(
txReceipt
)
const provenWithdrawal = await this.getProvenWithdrawal(
withdrawalMessageInfo.withdrawalHash
)
if (!provenWithdrawal || provenWithdrawal.timestamp.toNumber() === 0) {
return MessageStatus.READY_TO_PROVE
}

const BUFFER_TIME = 12
const currentTimestamp = Date.now()
const finalizedPeriod = await this.getChallengePeriodSeconds()
if (
currentTimestamp / 1000 - BUFFER_TIME <
provenWithdrawal.timestamp.toNumber() + finalizedPeriod
) {
return MessageStatus.IN_CHALLENGE_PERIOD
}
const finalizedStatus = await this.getFinalizedWithdrawalStatus(
withdrawalMessageInfo.withdrawalHash
)
return finalizedStatus
? MessageStatus.RELAYED
: MessageStatus.READY_FOR_RELAY
}

public async waitingDepositTransactionRelayedUsingL1Tx(
transactionHash: string,
opts?: {
Expand Down Expand Up @@ -222,7 +279,7 @@ export class Portals {
return promiseString
}

public async getL2BlockNumber(): Promise<number> {
public async getL2BlockNumberInOO(): Promise<number> {
return this.contracts.l1.L2OutputOracle.latestBlockNumber()
}

Expand Down Expand Up @@ -258,7 +315,7 @@ export class Portals {
let totalTimeMs = 0
while (totalTimeMs < (opts?.timeoutMs || Infinity)) {
const tick = Date.now()
if (l2BlockNumber <= (await this.getL2BlockNumber())) {
if (l2BlockNumber <= (await this.getL2BlockNumberInOO())) {
return
}
await sleep(opts?.pollIntervalMs || 1000)
Expand Down Expand Up @@ -316,6 +373,12 @@ export class Portals {
return this.contracts.l1.OptimismPortal.provenWithdrawals(withdrawalHash)
}

public async getFinalizedWithdrawalStatus(
withdrawalHash: string
): Promise<boolean> {
return this.contracts.l1.OptimismPortal.finalizedWithdrawals(withdrawalHash)
}

public async depositTransaction(
request: DepositTransactionRequest,
opts?: {
Expand Down
34 changes: 33 additions & 1 deletion packages/tokamak/sdk/tasks/portals.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import '@nomiclabs/hardhat-ethers'
import 'hardhat-deploy'
import { BigNumber, BytesLike, ethers } from 'ethers'

import { Portals, NumberLike, asL2Provider } from '../src'
import { Portals, NumberLike, asL2Provider, MessageStatus } from '../src'

console.log('Setup task...')

Expand Down Expand Up @@ -160,6 +160,8 @@ const depositViaOP = async (amount: NumberLike) => {
depositReceipt,
{}
)
const status = await portals.getMessageStatus(depositReceipt)
console.log('deposit status relayed:', status === MessageStatus.RELAYED)
console.log('relayedTxHash:', relayedTxHash)
const depositedTxReceipt = await l2Provider.getTransactionReceipt(
relayedTxHash
Expand Down Expand Up @@ -225,19 +227,33 @@ const withdrawViaBedrockMessagePasser = async (amount: NumberLike) => {
withdrawalReceipt
)
console.log('withdrawalMessageInfo:', withdrawalMessageInfo)

let status = await portals.getMessageStatus(withdrawalReceipt)
console.log('[Withdrawal Status] check publish L2 root:', status)

await portals.waitForWithdrawalTxReadyForRelay(withdrawalReceipt)

status = await portals.getMessageStatus(withdrawalReceipt)
console.log('[Withdrawal Status] check ready for proving:', status)

const proveTransaction = await portals.proveWithdrawalTransaction(
withdrawalMessageInfo
)
await proveTransaction.wait()

status = await portals.getMessageStatus(withdrawalReceipt)
console.log('[Withdrawal Status] check in challenging:', status)

await portals.waitForFinalization(withdrawalMessageInfo)
const finalizedTransaction = await portals.finalizeWithdrawalTransaction(
withdrawalMessageInfo
)
const finalizedTransactionReceipt = await finalizedTransaction.wait()
console.log('finalized transaction receipt:', finalizedTransactionReceipt)

status = await portals.getMessageStatus(withdrawalReceipt)
console.log('[Withdrawal Status] check relayed:', status)

const transferTx = await l2NativeTokenContract.transferFrom(
l1Contracts.OptimismPortal,
l1Wallet.address,
Expand Down Expand Up @@ -298,22 +314,38 @@ const withdrawViaBedrockMessagePasserV2 = async (amount: NumberLike) => {
})
const withdrawalReceipt = await withdrawalTx.wait()

let status = await portals.getL2ToL1MessageStatusByReceipt(withdrawalReceipt)
console.log('[Withdrawal Status] check publish L2 root:', status)

await portals.waitForWithdrawalTxReadyForRelayUsingL2Tx(
withdrawalReceipt.transactionHash
)

status = await portals.getL2ToL1MessageStatusByReceipt(withdrawalReceipt)
console.log('[Withdrawal Status] check ready for proving:', status)

const proveTransaction = await portals.proveWithdrawalTransactionUsingL2Tx(
withdrawalReceipt.transactionHash
)
await proveTransaction.wait()

status = await portals.getL2ToL1MessageStatusByReceipt(withdrawalReceipt)
console.log('[Withdrawal Status] check in challenging:', status)

await portals.waitForFinalizationUsingL2Tx(withdrawalReceipt.transactionHash)
status = await portals.getL2ToL1MessageStatusByReceipt(withdrawalReceipt)
console.log('[Withdrawal Status] check ready for relay:', status)

const finalizedTransaction =
await portals.finalizeWithdrawalTransactionUsingL2Tx(
withdrawalReceipt.transactionHash
)
const finalizedTransactionReceipt = await finalizedTransaction.wait()
console.log('finalized transaction receipt:', finalizedTransactionReceipt)

status = await portals.getL2ToL1MessageStatusByReceipt(withdrawalReceipt)
console.log('[Withdrawal Status] check relayed:', status)

const transferTx = await l2NativeTokenContract.transferFrom(
l1Contracts.OptimismPortal,
l1Wallet.address,
Expand Down

0 comments on commit 302e1e3

Please sign in to comment.