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

feat: add client expiration check to update client before expired #20

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 commits
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
53 changes: 53 additions & 0 deletions src/db/controller/client.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
import { ClientTable } from 'src/types'
import { DB } from '..'
import { insert } from '../utils'
import { mockServers } from 'src/test/testSetup'
import { ClientController } from './client'

describe('client controller', () => {
mockServers // to set set config file
test('client controller e2e', async () => {
const currentTimestamp = Math.floor(new Date().valueOf() / 1000)
// add clients for test
const testClients: ClientTable[] = [
{
chain_id: 'chain-1',
client_id: 'client-1',
counterparty_chain_id: 'chain-2',
revision_height: 1,
trusting_period: 3000,
last_update_time: currentTimestamp - 2500, // need update
},

{
chain_id: 'chain-1',
client_id: 'client-2',
counterparty_chain_id: 'chain-2',
revision_height: 1,
trusting_period: 3000,
last_update_time: currentTimestamp - 500, // do not need to update
},

{
chain_id: 'chain-1',
client_id: 'client-3',
counterparty_chain_id: 'chain-2',
revision_height: 1,
trusting_period: 3000,
last_update_time: currentTimestamp - 3500, // expired
},
]

testClients.map((client) => {
insert(DB, ClientController.tableName, client)
})

// get clients to update
const clientsToUpdate = ClientController.getClientsToUpdate('chain-1', [
'chain-2',
])

// check clients to update
expect(clientsToUpdate.map((v) => v.client_id)).toEqual(['client-1'])
})
})
62 changes: 58 additions & 4 deletions src/db/controller/client.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { DB } from '..'
import { insert, selectOne } from '../utils'
import { insert, select, selectOne, update } from '../utils'
import { Any } from 'cosmjs-types/google/protobuf/any'
import { UpdateClientEvent, ClientTable } from 'src/types'
import { Header } from 'cosmjs-types/ibc/lightclients/tendermint/v1/tendermint'
Expand Down Expand Up @@ -51,15 +51,35 @@ export class ClientController {
const client = await this.getClient(rest, chainId, clientId)

// update client
client.revision_height = parseInt(
const revisionHeight = parseInt(
event.consensusHeights.split(',')[0].split('-')[1]
)

if (revisionHeight > client.revision_height) {
client.revision_height = revisionHeight
}

if (header.signedHeader?.header?.time.seconds) {
client.last_update_time = Number(header.signedHeader.header.time.seconds)
const lastUpdateTime = Number(header.signedHeader.header.time.seconds)
if (lastUpdateTime > client.last_update_time) {
client.last_update_time = lastUpdateTime
}
ALPAC-4 marked this conversation as resolved.
Show resolved Hide resolved
Comment on lines +86 to +98
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue

Add explicit format checks for consensusHeights.

Based on IBC-Go spec, if event.consensusHeights is missing or invalid, the process must fail. Currently, there's no fallback or error thrown if the format doesn't match number-number.

 const splitted = event.consensusHeights.split(',')[0].split('-')
 if (splitted.length < 2) {
-  const revisionHeight = parseInt(splitted[1])
+  throw new Error('Invalid consensusHeights format. Expected "revision-height"')
 }
 const revisionHeight = parseInt(splitted[1])

Committable suggestion skipped: line range outside the PR's diff.

}

insert(DB, this.tableName, client)
update<ClientTable>(
DB,
this.tableName,
ALPAC-4 marked this conversation as resolved.
Show resolved Hide resolved
{
revision_height: client.revision_height,
last_update_time: client.last_update_time,
},
[
{
chain_id: client.chain_id,
client_id: client.client_id,
},
]
)
}

public static async getClient(
Expand All @@ -77,4 +97,38 @@ export class ClientController {

return client ?? this.addClient(rest, chainId, clientId)
}

public static getClientsToUpdate(
chainId: string,
counterpartyChainIds: string[]
): ClientTable[] {
const clients = select<ClientTable>(
DB,
this.tableName,
ALPAC-4 marked this conversation as resolved.
Show resolved Hide resolved
counterpartyChainIds.map((counterpartyChainId) => ({
chain_id: chainId,
counterparty_chain_id: counterpartyChainId,
}))
)

// check need updates
const currentTimestamp = new Date().valueOf() / 1000

return clients.filter((client) => {
// check expired
if (client.last_update_time + client.trusting_period < currentTimestamp) {
return false
}

// check need update
if (
client.last_update_time + client.trusting_period * 0.666 <
currentTimestamp
) {
return true
}

return false
})
}
}
2 changes: 1 addition & 1 deletion src/lib/eventParser.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ describe('event parser', () => {
timeoutTimestamp: 1730467768,
timeoutHeightRaw: '0-0',
timeoutTimestampRaw: '1730467768229000000',
ordering: undefined,
ordering: '',
ack: 'eyJyZXN1bHQiOiJBUT09In0=',
}

Expand Down
19 changes: 18 additions & 1 deletion src/lib/eventParser.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
import { Event } from '@cosmjs/tendermint-rpc/build/comet38/responses'
import { ChannelOpenCloseInfo, PacketFeeEvent, PacketInfo } from 'src/types'
import {
ChannelOpenCloseInfo,
PacketFeeEvent,
PacketInfo,
UpdateClientEvent,
} from 'src/types'

export function parsePacketEvent(event: Event, height: number): PacketInfo {
const connectionId = getConnection(event) as string
Expand Down Expand Up @@ -124,6 +129,18 @@ export function parsePacketFeeEvent(event: Event): PacketFeeEvent {
}
}

export function parseUpdateClientEvent(event: Event): UpdateClientEvent {
const clientId = find(event, 'client_id') as string
const header = find(event, 'header') as string
const consensusHeights = find(event, 'consensus_heights') as string

return {
clientId,
header,
consensusHeights,
}
}
Comment on lines +132 to +142
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🛠️ Refactor suggestion

Add validation for consensusHeights inside parse.

To avoid passing malformed data to feedUpdateClientEvent, consider validating or throwing an error if consensusHeights is missing or invalid.


function getConnection(event: Event): string | undefined {
return find(event, 'connection_id') || find(event, 'packet_connection')
}
Expand Down
13 changes: 6 additions & 7 deletions src/msgs/updateClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,12 +76,10 @@ async function getValidatorSet(
chain: ChainWorker,
height: number
): Promise<ValidatorSet> {
let block = await chain.rest.tendermint
.blockInfo(height)
.catch(() => undefined)
let header = await chain.rpc.header(height).catch(() => undefined)
let count = 0
while (block === undefined) {
block = await chain.rest.tendermint.blockInfo(height).catch((e) => {
while (header === undefined) {
header = await chain.rpc.header(height).catch((e) => {
if (count > 5) {
throw e
}
Expand All @@ -90,7 +88,7 @@ async function getValidatorSet(
await delay(100)
count++
}
const proposerAddress = block.block.header.proposer_address
const proposerAddress = header.header.proposer_address
// we need to query the header to find out who the proposer was, and pull them out
const validators = await chain.rpc.validatorsAll(height)
let totalVotingPower = BigInt(0)
Expand All @@ -117,7 +115,8 @@ async function getValidatorSet(
})

const proposer: Validator | undefined = mappedValidators.find(
(val) => Buffer.from(val.address).toString('base64') === proposerAddress
(val) =>
Buffer.from(val.address).toString('hex') === proposerAddress.toLowerCase()
ALPAC-4 marked this conversation as resolved.
Show resolved Hide resolved
)

return ValidatorSet.fromPartial({
Expand Down
28 changes: 27 additions & 1 deletion src/workers/chain.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,17 @@
import { RPCClient } from 'src/lib/rpcClient'
import { createLoggerWithPrefix } from 'src/lib/logger'
import { ChannelOpenCloseEvent, PacketEvent, PacketFeeEvent } from 'src/types'
import {
ChannelOpenCloseEvent,
PacketEvent,
PacketFeeEvent,
UpdateClientEvent,
} from 'src/types'
import {
parseChannelCloseEvent,
parseChannelOpenEvent,
parsePacketEvent,
parsePacketFeeEvent,
parseUpdateClientEvent,
} from 'src/lib/eventParser'
import { DB } from 'src/db'
import { SyncInfoController } from 'src/db/controller/syncInfo'
Expand All @@ -16,6 +22,7 @@ import { RESTClient } from 'src/lib/restClient'
import { ChannelController } from 'src/db/controller/channel'
import { PacketFeeController } from 'src/db/controller/packetFee'
import { PacketFee } from 'src/lib/config'
import { ClientController } from 'src/db/controller/client'

export class ChainWorker {
public latestHeight: number
Expand Down Expand Up @@ -138,11 +145,23 @@ class SyncWorker {
const packetEvents = events.map((e) => e.packetEvents).flat()
const channelOpenEvents = events.map((e) => e.channelOpenEvents).flat()
const packetFeeEvents = events.map((e) => e.packetFeeEvents).flat()
const updateClientEvents = events
.map((e) => e.updateClientEvents)
.flat()

this.logger.debug(
`Fetched block results for heights (${JSON.stringify(heights)})`
)

// `feedUpdateClient` does not need to be included in the db transaction
for (const event of updateClientEvents) {
await ClientController.feedUpdateClientEvent(
this.chain.rest,
this.chain.chainId,
event
)
}
ALPAC-4 marked this conversation as resolved.
Show resolved Hide resolved

let finish = false

const packetEventFeed = await PacketController.feedEvents(
Expand Down Expand Up @@ -196,6 +215,7 @@ class SyncWorker {
packetEvents: PacketEvent[]
channelOpenEvents: ChannelOpenCloseEvent[]
packetFeeEvents: PacketFeeEvent[]
updateClientEvents: UpdateClientEvent[]
}> {
this.logger.debug(`Fetch new block results (height - ${height})`)
const blockResult = await this.chain.rpc.blockResults(height)
Expand All @@ -204,6 +224,7 @@ class SyncWorker {
const packetEvents: PacketEvent[] = []
const channelOpenEvents: ChannelOpenCloseEvent[] = []
const packetFeeEvents: PacketFeeEvent[] = []
const updateClientEvents: UpdateClientEvent[] = []

txData.map((data) => {
for (const event of data.events) {
Expand Down Expand Up @@ -245,13 +266,18 @@ class SyncWorker {
if (event.type === 'incentivized_ibc_packet') {
packetFeeEvents.push(parsePacketFeeEvent(event))
}

if (event.type === 'update_client') {
updateClientEvents.push(parseUpdateClientEvent(event))
}
}
})

return {
packetEvents,
channelOpenEvents,
packetFeeEvents,
updateClientEvents,
}
}
}
Expand Down
26 changes: 14 additions & 12 deletions src/workers/wallet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ import { Logger } from 'winston'
import { ChannelController } from 'src/db/controller/channel'
import { State } from '@initia/initia.proto/ibc/core/channel/v1/channel'
import { PacketFee } from 'src/lib/config'
import { ClientController } from 'src/db/controller/client'

// TODO: add update client worker
export class WalletWorker {
private sequence?: number
private accountNumber?: number
Expand Down Expand Up @@ -163,26 +163,18 @@ export class WalletWorker {
await this.filterTimeoutPackets(timeoutPackets)
const filteredChannelOpenCloseEvents =
await this.filterChannelOpenCloseEvents(channelOpenEvents)
if (
filteredSendPackets.length === 0 &&
filteredWriteAckPackets.length === 0 &&
filteredTimeoutPackets.length === 0 &&
filteredChannelOpenCloseEvents.length === 0
) {
return
}

// create msgs

// generate update client msgs
// get unique client id
const connections = [
...filteredSendPackets.map((packet) => packet.dst_connection_id),
...filteredWriteAckPackets.map((packet) => packet.src_connection_id),
...filteredTimeoutPackets.map((packet) => packet.src_connection_id),
...filteredChannelOpenCloseEvents.map((event) => event.connection_id),
].filter((v, i, a) => a.indexOf(v) === i)

// get client ids from connections
const connectionClientMap: Record<string, string> = {}
await Promise.all(
connections.map(async (connection) => {
Expand All @@ -195,10 +187,18 @@ export class WalletWorker {
})
)

const clientIds = Object.values(connectionClientMap).filter(
(v, i, a) => a.indexOf(v) === i
// check clients that need to update
const clientsToUpdate = ClientController.getClientsToUpdate(
this.chain.chainId,
counterpartyChainIdsWithFeeFilter.map((f) => f.chainId)
)

// get unique client id
const clientIds = [
...Object.values(connectionClientMap),
...clientsToUpdate.map((c) => c.client_id),
].filter((v, i, a) => a.indexOf(v) === i)

// generate msgs
const updateClientMsgs: Record<
string,
Expand Down Expand Up @@ -303,6 +303,8 @@ export class WalletWorker {
...channelOpenMsgs,
]

if (msgs.length === 0) return

// init sequence
if (!this.sequence) {
await this.initAccInfo()
Expand Down
Loading