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

fix: change aggregate root queries #5139

Merged
merged 1 commit into from
Nov 7, 2023
Merged
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
25 changes: 15 additions & 10 deletions packages/adapters/database/src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ import {
StableSwapTransfer,
StableSwapLpBalance,
RootMessageStatus,
convertFromDbPropagatedRoot,
convertFromDbSpokeOptimisticRoot,
} from "@connext/nxtp-utils";
import { Pool } from "pg";
Expand Down Expand Up @@ -935,9 +934,14 @@ export const getAggregateRootCount = async (
_pool?: Pool | db.TxnClientForRepeatableRead,
): Promise<number | undefined> => {
const poolToUse = _pool ?? pool;

const idExp = `${aggregateRoot}%`;

// Get the leaf count at the aggregated root
const root = await db.selectOne("propagated_roots", { aggregate_root: aggregateRoot }).run(poolToUse);
return root ? convertFromDbPropagatedRoot(root).count : undefined;
const root = await db
.selectOne("aggregated_roots", { id: dc.like(idExp) }, { order: { by: "domain_index", direction: "DESC" } })
.run(poolToUse);
return root ? convertFromDbAggregatedRoot(root).index + 1 : undefined;
};

export const getAggregateRoots = async (
Expand Down Expand Up @@ -980,15 +984,16 @@ export const getLatestMessageRoot = async (
): Promise<RootMessage | undefined> => {
const poolToUse = _pool ?? pool;

type rootPropagatedSQL = s.root_messages.SQL | s.propagated_roots.SQL;
type rootPropagatedSelectable = s.root_messages.Selectable & { author: s.propagated_roots.Selectable };
type rootAggregatedSQL = s.root_messages.SQL | s.aggregated_roots.SQL;
type rootAggregatedelectable = s.root_messages.Selectable & { author: s.aggregated_roots.Selectable };
const idExp = `${aggregate_root}%`;

const root = await db.sql<
rootPropagatedSQL,
rootPropagatedSelectable[]
>`select * from ${"root_messages"} where ${"root"} in (select received_root from aggregated_roots where domain_index < (select leaf_count from propagated_roots where ${{
aggregate_root,
}})) and ${{
rootAggregatedSQL,
rootAggregatedelectable[]
>`select * from ${"root_messages"} where ${"root"} in (select received_root from aggregated_roots where id <= (select id from aggregated_roots where ${{
id: dc.like(idExp),
}} order by id desc limit 1)) and ${{
spoke_domain,
}} order by ${"leaf_count"} desc nulls last limit 1`.run(poolToUse);
return root.length > 0 ? convertFromDbRootMessage(root[0]) : undefined;
Expand Down
11 changes: 2 additions & 9 deletions packages/adapters/database/test/client.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1017,22 +1017,15 @@ describe("Database client", () => {
messages.push(rootMessage);
const m = mock.entity.aggregatedRoot();
m.index = _i;
m.id = _i.toString();
m.receivedRoot = rootMessage.root;
roots.push(m);
const propRoot = mock.entity.propagatedRoot();
propRoot.count = _i + 1;
propRoots.push(propRoot);
}

await saveSentRootMessages(messages, pool);
await saveAggregatedRoots(roots, pool);
await savePropagatedRoots(propRoots, pool);

const dbRoots = await getLatestMessageRoot(
mock.entity.rootMessage().spokeDomain,
propRoots[batchSize - 1].aggregate,
pool,
);
const dbRoots = await getLatestMessageRoot(mock.entity.rootMessage().spokeDomain, roots[batchSize - 1].id, pool);
dbRoots ? (dbRoots.count = batchSize - 1) : undefined;
expect(dbRoots).to.deep.eq(messages[batchSize - 1]);
});
Expand Down
Loading