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 all 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
28 changes: 14 additions & 14 deletions src/db/controller/channel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ export class ChannelController {

return select<ChannelOpenCloseTable>(
DB,
this.tableName,
ChannelController.tableName,
wheres,
{ id: 'ASC' },
limit
Expand All @@ -103,23 +103,23 @@ export class ChannelController {

del<ChannelOpenCloseTable>(
DB,
this.tableName,
ChannelController.tableName,
events.map((v) => ({ id: v.id as number }))
)
}

public static updateInProgress(id?: number, inProgress = true) {
update<ChannelOpenCloseTable>(
DB,
this.tableName,
ChannelController.tableName,
{ in_progress: inProgress ? Bool.TRUE : Bool.FALSE },
[{ id }]
)
}

public static resetPacketInProgress(db?: Database) {
db = db ?? DB
update<ChannelOpenCloseTable>(db, this.tableName, {
update<ChannelOpenCloseTable>(db, ChannelController.tableName, {
in_progress: Bool.FALSE,
})
}
Expand Down Expand Up @@ -152,7 +152,7 @@ export class ChannelController {
}

return () => {
insert(DB, this.tableName, channelOnOpen) // store INIT state to the dst chain
insert(DB, ChannelController.tableName, channelOnOpen) // store INIT state to the dst chain
}
}

Expand Down Expand Up @@ -184,15 +184,15 @@ export class ChannelController {
}

return () => {
del<ChannelOpenCloseTable>(DB, this.tableName, [
del<ChannelOpenCloseTable>(DB, ChannelController.tableName, [
{
state: ChannelState.INIT,
counterparty_chain_id: connection.counterparty_chain_id,
counterparty_port_id: channelOnOpen.port_id,
counterparty_channel_id: channelOnOpen.channel_id,
},
]) // remove INIT from the dst chain
insert(DB, this.tableName, channelOnOpen) // store TRYOPEN state to the src chain
insert(DB, ChannelController.tableName, channelOnOpen) // store TRYOPEN state to the src chain
}
}

Expand Down Expand Up @@ -224,23 +224,23 @@ export class ChannelController {
}

return () => {
del<ChannelOpenCloseTable>(DB, this.tableName, [
del<ChannelOpenCloseTable>(DB, ChannelController.tableName, [
{
state: ChannelState.TRYOPEN,
counterparty_chain_id: channelOnOpen.chain_id,
counterparty_port_id: channelOnOpen.port_id,
counterparty_channel_id: channelOnOpen.channel_id,
},
]) // remove TRYOPEN from src chain
del<ChannelOpenCloseTable>(DB, this.tableName, [
del<ChannelOpenCloseTable>(DB, ChannelController.tableName, [
{
state: ChannelState.INIT,
counterparty_chain_id: chainId,
counterparty_port_id: channelOnOpen.counterparty_port_id,
counterparty_channel_id: channelOnOpen.counterparty_channel_id,
},
]) // remove INIT from dst chain
insert(DB, this.tableName, channelOnOpen) // store ACK state to the dst chain
insert(DB, ChannelController.tableName, channelOnOpen) // store ACK state to the dst chain
}
}

Expand All @@ -256,15 +256,15 @@ export class ChannelController {
event.channelOpenCloseInfo.dstConnectionId
)
return () => {
del<ChannelOpenCloseTable>(DB, this.tableName, [
del<ChannelOpenCloseTable>(DB, ChannelController.tableName, [
{
state: ChannelState.TRYOPEN,
counterparty_chain_id: chainId,
counterparty_port_id: event.channelOpenCloseInfo.dstPortId,
counterparty_channel_id: event.channelOpenCloseInfo.dstChannelId,
},
]) // remove TRYOPEN from src chain
del<ChannelOpenCloseTable>(DB, this.tableName, [
del<ChannelOpenCloseTable>(DB, ChannelController.tableName, [
{
state: ChannelState.ACK,
counterparty_chain_id: connection.counterparty_chain_id,
Expand Down Expand Up @@ -302,7 +302,7 @@ export class ChannelController {
}

return () => {
insert(DB, this.tableName, channelOnOpen)
insert(DB, ChannelController.tableName, channelOnOpen)

// Mark all packets as timed out
PacketController.updateTimeout(
Expand All @@ -318,7 +318,7 @@ export class ChannelController {
event: ChannelOpenCloseEvent
): () => void {
return () => {
del<ChannelOpenCloseTable>(DB, this.tableName, [
del<ChannelOpenCloseTable>(DB, ChannelController.tableName, [
{
state: ChannelState.CLOSE,
chain_id: chainId,
Expand Down
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'])
})
})
98 changes: 92 additions & 6 deletions src/db/controller/client.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import { DB } from '..'
import { insert, selectOne } from '../utils'
import { del, 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'
import { RESTClient } from 'src/lib/restClient'
import { PacketController } from './packet'

export class ClientController {
static tableName = 'client'
Expand All @@ -27,7 +28,38 @@ export class ClientController {
last_update_time: 0,
}

insert(DB, this.tableName, client)
insert(DB, ClientController.tableName, client)

return client
}

public static async replaceClient(
rest: RESTClient,
chainId: string,
clientId: string
): Promise<ClientTable> {
const state = await rest.ibc.getClientState(clientId)

const client: ClientTable = {
chain_id: chainId,
client_id: clientId,
counterparty_chain_id: state.client_state.chain_id,
trusting_period: parseInt(
state.client_state.trusting_period.replace('s', '')
),
revision_height: parseInt(
state.client_state.latest_height.revision_height
),
last_update_time: 0,
}

del(DB, ClientController.tableName, [
{ chain_id: chainId, client_id: clientId },
])
insert(DB, ClientController.tableName, client)

// to recheck expired packets
PacketController.resetPacketInProgress()

return client
}
Expand All @@ -51,15 +83,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,
ClientController.tableName,
{
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 @@ -68,7 +120,7 @@ export class ClientController {
clientId: string
): Promise<ClientTable> {
// get client
const client = selectOne<ClientTable>(DB, this.tableName, [
const client = selectOne<ClientTable>(DB, ClientController.tableName, [
{
chain_id: chainId,
client_id: clientId,
Expand All @@ -77,4 +129,38 @@ export class ClientController {

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

public static getClientsToUpdate(
chainId: string,
counterpartyChainIds: string[]
): ClientTable[] {
const clients = select<ClientTable>(
DB,
ClientController.tableName,
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
})
}
ALPAC-4 marked this conversation as resolved.
Show resolved Hide resolved
}
18 changes: 11 additions & 7 deletions src/db/controller/connection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ export class ConnectionController {
counterparty_client_id: connectionInfo.connection.counterparty.client_id,
}

insert(DB, this.tableName, connection)
insert(DB, ConnectionController.tableName, connection)

return connection
}
Expand All @@ -37,12 +37,16 @@ export class ConnectionController {
chainId: string,
connectionId: string
): Promise<ConnectionTable> {
const connection = selectOne<ConnectionTable>(DB, this.tableName, [
{
chain_id: chainId,
connection_id: connectionId,
},
])
const connection = selectOne<ConnectionTable>(
DB,
ConnectionController.tableName,
[
{
chain_id: chainId,
connection_id: connectionId,
},
]
)

return connection ?? this.addConnection(rest, chainId, connectionId)
}
Expand Down
Loading
Loading