Skip to content

Commit

Permalink
Merge pull request #5002 from connext/lh_propose_fix
Browse files Browse the repository at this point in the history
fix: lh propose aggregate root handler
  • Loading branch information
liu-zhipeng authored Oct 13, 2023
2 parents 0bacc52 + 7f255e8 commit ac4a670
Show file tree
Hide file tree
Showing 6 changed files with 23 additions and 2 deletions.
10 changes: 10 additions & 0 deletions packages/adapters/database/src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -901,6 +901,16 @@ export const getAggregateRoot = async (
return aggregateRootId.split("-")[0] ?? undefined;
};

export const getBaseAggregateRootCount = async (
received_root: string,
_pool?: Pool | db.TxnClientForRepeatableRead,
): Promise<number | undefined> => {
const poolToUse = _pool ?? pool;
// Get the leaf count at the aggregated root
const root = await db.selectOne("aggregated_roots", { received_root }).run(poolToUse);
return root ? convertFromDbAggregatedRoot(root).index : undefined;
};

export const getAggregateRootCount = async (
aggreateRoot: string,
_pool?: Pool | db.TxnClientForRepeatableRead,
Expand Down
7 changes: 7 additions & 0 deletions packages/adapters/database/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ import {
getAggregateRoot,
getAggregateRootByRootAndDomain,
getAggregateRootCount,
getBaseAggregateRootCount,
getAggregateRoots,
getBaseAggregateRoot,
getMessageRootIndex,
Expand Down Expand Up @@ -203,6 +204,10 @@ export type Database = {
aggregateRoot: string,
_pool?: Pool | TxnClientForRepeatableRead,
) => Promise<number | undefined>;
getBaseAggregateRootCount: (
aggregateRoot: string,
_pool?: Pool | TxnClientForRepeatableRead,
) => Promise<number | undefined>;
getAggregateRoots: (count: number, _pool?: Pool | TxnClientForRepeatableRead) => Promise<string[]>;
getBaseAggregateRoot: (_pool?: Pool | TxnClientForRepeatableRead) => Promise<string | undefined>;
getCurrentProposedSnapshot: (_pool?: Pool | TxnClientForRepeatableRead) => Promise<Snapshot | undefined>;
Expand Down Expand Up @@ -374,6 +379,7 @@ export const getDatabase = async (databaseUrl: string, logger: Logger): Promise<
getAggregateRoot,
getAggregateRootByRootAndDomain,
getAggregateRootCount,
getBaseAggregateRootCount,
getAggregateRoots,
getBaseAggregateRoot,
getMessageRootIndex,
Expand Down Expand Up @@ -461,6 +467,7 @@ export const getDatabaseAndPool = async (
getAggregateRoot,
getAggregateRootByRootAndDomain,
getAggregateRootCount,
getBaseAggregateRootCount,
getAggregateRoots,
getBaseAggregateRoot,
getMessageRootIndex,
Expand Down
3 changes: 3 additions & 0 deletions packages/adapters/database/test/client.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ import {
getLatestMessageRoot,
getLatestAggregateRoots,
getAggregateRootCount,
getBaseAggregateRootCount,
getUnProcessedMessages,
getUnProcessedMessagesByIndex,
getUnProcessedMessagesByDomains,
Expand Down Expand Up @@ -1096,6 +1097,7 @@ describe("Database client", () => {
expect(await getMessageRootCount("", "", pool)).to.eq(undefined);
expect(await getMessageRootIndex("", "", pool)).to.eq(undefined);
expect(await getAggregateRootCount("", pool)).to.eq(undefined);
expect(await getBaseAggregateRootCount("", pool)).to.eq(undefined);
expect(await getAggregateRoot("", pool)).to.eq(undefined);
expect(await getLatestMessageRoot("", "", pool)).to.eq(undefined);
expect(await getLatestAggregateRoots("", 1, "DESC", pool)).to.be.deep.eq([]);
Expand Down Expand Up @@ -1130,6 +1132,7 @@ describe("Database client", () => {
).to.eventually.not.be.rejected;
await expect(getAggregateRoot(undefined as any, undefined as any)).to.eventually.not.be.rejected;
await expect(getAggregateRootCount(undefined as any, undefined as any)).to.eventually.not.be.rejected;
await expect(getBaseAggregateRootCount(undefined as any, undefined as any)).to.eventually.not.be.rejected;
await expect(getMessageRootIndex(undefined as any, undefined as any, undefined as any)).to.eventually.not.be
.rejected;
await expect(getMessageRootAggregatedFromIndex(undefined as any, undefined as any, undefined as any)).to.eventually
Expand Down
1 change: 1 addition & 0 deletions packages/adapters/database/test/mock.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ export const mockDatabase = (): Database => {
getUnProcessedMessagesByDomains: stub().resolves([]),
getAggregateRoot: stub().resolves(),
getAggregateRootCount: stub().resolves(),
getBaseAggregateRootCount: stub().resolves(),
getMessageRootIndex: stub().resolves(),
getMessageRootAggregatedFromIndex: stub().resolves(),
getMessageRootCount: stub().resolves(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ export const proposeSnapshot = async (snapshotId: string, snapshotRoots: string[
throw new NoBaseAggregateRoot();
}

const baseAggregateRootCount = await database.getAggregateRootCount(baseAggregateRoot);
const baseAggregateRootCount = await database.getBaseAggregateRootCount(baseAggregateRoot);
if (!baseAggregateRootCount) {
throw new NoBaseAggregateRootCount(baseAggregateRoot);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ describe("Operations: Propose", () => {

it("happy case should call propose snapshot succesfully", async () => {
(proposeCtxMock.adapters.database.getBaseAggregateRoot as SinonStub).resolves("0x");
(proposeCtxMock.adapters.database.getAggregateRootCount as SinonStub).resolves(1);
(proposeCtxMock.adapters.database.getBaseAggregateRootCount as SinonStub).resolves(1);
(proposeCtxMock.adapters.database.getAggregateRoots as SinonStub).resolves(["0x"]);
(proposeCtxMock.adapters.database.getPendingSnapshots as SinonStub).resolves([mock.entity.snapshotRoot()]);
encodeFunctionData.returns("0x");
Expand Down

0 comments on commit ac4a670

Please sign in to comment.