From b64eb540bd361181076d084da0a362e04dcadf27 Mon Sep 17 00:00:00 2001 From: ejmg Date: Thu, 18 Jan 2024 17:03:35 -0600 Subject: [PATCH] added counterparty height to ibc channels page/endpoint --- src/app/api/ibc/channels/route.ts | 54 ++++++++++++++++++++++++- src/components/ibc/channels/columns.tsx | 14 ++++++- 2 files changed, 66 insertions(+), 2 deletions(-) diff --git a/src/app/api/ibc/channels/route.ts b/src/app/api/ibc/channels/route.ts index 858c36b..d694adc 100644 --- a/src/app/api/ibc/channels/route.ts +++ b/src/app/api/ibc/channels/route.ts @@ -1,3 +1,4 @@ +import util from "util"; import db from "@/lib/db"; import { type NextRequest } from "next/server"; @@ -79,6 +80,55 @@ export async function GET(_req: NextRequest) { console.log("Successfully queried for associated IBC Connections.", connections[0].attributes); + // NOTE: Entry #5000 in why Prisma is being ripped out, please ignore the precariously bad and borderline incorrect code, this will be removed in due time. + console.log("Searching for client updates associated with IBC Channels..."); + const counterpartyHeights = await db.events.findMany({ + select: { + attributes: { + select: { + key: true, + value: true, + }, + where: { + OR: [ + { + key: { + equals: "consensus_height", + }, + }, + { + key: { + equals: "client_id", + }, + }, + ], + }, + }, + }, + where: { + type: { + equals: "update_client", + }, + }, + orderBy: { + block_id: "desc", + }, + }); + + console.log("Successfully queried for client updates.", util.inspect(counterpartyHeights, { showHidden: false, depth: null, colors: true })); + + const uniqueClients : Array<{ clientId: string, consensusHeight: string }> = []; + + counterpartyHeights.forEach(({ attributes }) => { + const _client = attributes[0].key === "client_id" ? attributes[0].value : attributes[1].value; + const _height = attributes[0].key === "consensus_height" ? attributes[0].value : attributes[1].value; + if (!uniqueClients.find(({ clientId }) => clientId===_client) && _client !== null && _height !== null) { + uniqueClients.push({ clientId: _client, consensusHeight: _height }); + } + }); + + console.log("Unique clients", uniqueClients); + // NOTE: this is why i should remove Prisma. I don't care if there is a way to massage the database into making this unecessary or that I could skip over it using RAWQUERY; the fact that this is what you are forced to do either of these for something so simple is ridiculous. // included in this is that I would have an entire third query if I wasn't relying on the way `client_id`s are built off of `client_type`s console.log("Merging query data..."); @@ -88,11 +138,13 @@ export async function GET(_req: NextRequest) { // Precondition: An attribute with key "client_id" should never have a value of null. const clientID = connections.find(({ attributes }) => !!attributes.find(({ key, value }) => key==="connection_id" && connectionId===value))?.attributes.find(({ key }) => key==="client_id") as {key: string, value: string}; channel.attributes.push(clientID); + const consensusHeight = uniqueClients.find(({ clientId: _clientID }) => clientID.value===_clientID)?.consensusHeight as string; + channel.attributes.push({ key: "consensusHeight", value: consensusHeight}); // NOTE: Abusing the fact that `ibc_types::core::client::ClientId` is built from `ibc_types::core::client::ClientType` by hyphenating an id_counter. // I don't want to rely this on the longrun. // TODO: Verify whether we want counterparty_client_id or not. const idx = clientID.value.lastIndexOf("-"); - const counterpartyID = clientID.value.substring(0, idx); + const counterpartyID = clientID.value.substring(0, idx); channel.attributes.push({ key: "client_type", value: counterpartyID }); // Enforce ordering on attributes for the consuming table on the client. channel.attributes.sort((a, b) => a.key.toUpperCase() < b.key.toUpperCase() ? -1 : (a.key.toUpperCase() > b.key.toUpperCase() ? 1 : 0)); diff --git a/src/components/ibc/channels/columns.tsx b/src/components/ibc/channels/columns.tsx index d5493fb..1460d37 100644 --- a/src/components/ibc/channels/columns.tsx +++ b/src/components/ibc/channels/columns.tsx @@ -48,7 +48,6 @@ export const columns : Array> = [ { id: "counterpartyId", accessorFn: (row) => row[2].value, - // TODO: There can be multiple connections for a given channel, something to handle/worry about later. header: () =>
Counterparty Chain
, cell: ({ getValue }) => { const counterpartyId = getValue() as string; @@ -59,4 +58,17 @@ export const columns : Array> = [ ); }, }, + { + id: "counterpartyHeight", + accessorFn: (row) => row[4].value, + header: () =>
Counterparty Height
, + cell: ({ getValue }) => { + const counterpartyHeight = getValue() as string; + return ( +
+

{counterpartyHeight}

+
+ ); + }, + }, ]; \ No newline at end of file