Skip to content

Commit

Permalink
fix: fetch latest consensus timestamp
Browse files Browse the repository at this point in the history
  • Loading branch information
ALPAC-4 committed Jan 6, 2025
1 parent b621415 commit 5beefd1
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 28 deletions.
57 changes: 29 additions & 28 deletions src/db/controller/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,25 +8,13 @@ import { PacketController } from './packet'

export class ClientController {
static tableName = 'client'

public static async addClient(
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,
}
const client = await ClientController.fetchClient(rest, chainId, clientId)

insert(DB, ClientController.tableName, client)

Expand All @@ -38,20 +26,7 @@ export class ClientController {
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,
}
const client = await ClientController.fetchClient(rest, chainId, clientId)

del(DB, ClientController.tableName, [
{ chain_id: chainId, client_id: clientId },
Expand Down Expand Up @@ -163,4 +138,30 @@ export class ClientController {
return false
})
}

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

// get latest cons state
const consState = await rest.ibc.lastConsensusState(clientId)

return {
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: Math.floor(
new Date(consState.consensus_state.timestamp).valueOf() / 1000
),
}
}
}
22 changes: 22 additions & 0 deletions src/lib/restClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,12 @@ class IbcAPI extends IbcAPI_ {
)
}

async lastConsensusState(clientId: string) {
return this.c.get<ConsState>(
`/ibc/core/client/v1/consensus_states/${clientId}/revision/0/height/0?latest_height=true`
)
}

async getConnection(connectionId: string): Promise<ConnectionInfo> {
return this.c.get<ConnectionInfo>(
`/ibc/core/connection/v1/connections/${connectionId}`
Expand Down Expand Up @@ -103,3 +109,19 @@ interface NextSequence {
revision_height: string
}
}

interface ConsState {
consensus_state: {
'@type': string
timestamp: string
root: {
hash: string
}
next_validators_hash: string
}
proof: null | string
proof_height: {
revision_number: string
revision_height: string
}
}

0 comments on commit 5beefd1

Please sign in to comment.