Skip to content

Commit

Permalink
Merge pull request #60 from penumbra-zone/ibc-channels-height
Browse files Browse the repository at this point in the history
Add counterparty height to IBC Channels
  • Loading branch information
ejmg authored Jan 18, 2024
2 parents 0bfd2a3 + b64eb54 commit decf002
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 2 deletions.
54 changes: 53 additions & 1 deletion src/app/api/ibc/channels/route.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import util from "util";
import db from "@/lib/db";
import { type NextRequest } from "next/server";

Expand Down Expand Up @@ -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...");
Expand All @@ -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));
Expand Down
14 changes: 13 additions & 1 deletion src/components/ibc/channels/columns.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,6 @@ export const columns : Array<ColumnDef<ChannelsColumns>> = [
{
id: "counterpartyId",
accessorFn: (row) => row[2].value,
// TODO: There can be multiple connections for a given channel, something to handle/worry about later.
header: () => <div className="font-semibold text-gray-800 text-center">Counterparty Chain</div>,
cell: ({ getValue }) => {
const counterpartyId = getValue() as string;
Expand All @@ -59,4 +58,17 @@ export const columns : Array<ColumnDef<ChannelsColumns>> = [
);
},
},
{
id: "counterpartyHeight",
accessorFn: (row) => row[4].value,
header: () => <div className="font-semibold text-gray-800 text-center">Counterparty Height</div>,
cell: ({ getValue }) => {
const counterpartyHeight = getValue() as string;
return (
<div className="text-center">
<p>{counterpartyHeight}</p>
</div>
);
},
},
];

0 comments on commit decf002

Please sign in to comment.