From 5beefd1f0a5ffb97ac60cbcd9de3102b3f6568a2 Mon Sep 17 00:00:00 2001 From: ALPAC-4 Date: Mon, 6 Jan 2025 12:22:18 +0900 Subject: [PATCH] fix: fetch latest consensus timestamp --- src/db/controller/client.ts | 57 +++++++++++++++++++------------------ src/lib/restClient.ts | 22 ++++++++++++++ 2 files changed, 51 insertions(+), 28 deletions(-) diff --git a/src/db/controller/client.ts b/src/db/controller/client.ts index b8f6916..9cf6702 100644 --- a/src/db/controller/client.ts +++ b/src/db/controller/client.ts @@ -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 { - 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) @@ -38,20 +26,7 @@ export class ClientController { chainId: string, clientId: string ): Promise { - 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 }, @@ -163,4 +138,30 @@ export class ClientController { return false }) } + + public static async fetchClient( + rest: RESTClient, + chainId: string, + clientId: string + ): Promise { + 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 + ), + } + } } diff --git a/src/lib/restClient.ts b/src/lib/restClient.ts index 5901939..93ba30f 100644 --- a/src/lib/restClient.ts +++ b/src/lib/restClient.ts @@ -55,6 +55,12 @@ class IbcAPI extends IbcAPI_ { ) } + async lastConsensusState(clientId: string) { + return this.c.get( + `/ibc/core/client/v1/consensus_states/${clientId}/revision/0/height/0?latest_height=true` + ) + } + async getConnection(connectionId: string): Promise { return this.c.get( `/ibc/core/connection/v1/connections/${connectionId}` @@ -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 + } +}