Skip to content

Commit

Permalink
Patch aggregate queries (#87)
Browse files Browse the repository at this point in the history
Fetching aggregates without a limit parameter could return a 500 error under certain circumstances.
Solution: Adding a hard limit to 50 in the SDK (while still adding a limit parameter for end user to increase it if needed)

Co-authored-by: Roman Gascoin <[email protected]>
  • Loading branch information
BjrInt and Rgascoin committed Oct 24, 2022
1 parent ce764e2 commit 508cc87
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 78 deletions.
18 changes: 9 additions & 9 deletions src/messages/aggregate/get.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ type AggregateGetConfiguration = {
APIServer?: string;
address: string;
keys?: Array<string>;
limit?: number;
};

/**
Expand All @@ -18,20 +19,19 @@ type AggregateGetConfiguration = {
*
* @param configuration The configuration used to get the message, including the API endpoint.
*/
export async function Get<T>(
{ APIServer = DEFAULT_API_V2, address = "", keys = [] }: AggregateGetConfiguration = {
APIServer: DEFAULT_API_V2,
address: "",
keys: [],
},
): Promise<T> {
const _keys = keys.length === 0 ? null : keys.join(",");
export async function Get<T>({
APIServer = DEFAULT_API_V2,
address = "",
keys = [],
limit = 50,
}: AggregateGetConfiguration): Promise<T> {
const response = await axios.get<AggregateGetResponse<T>>(
`${stripTrailingSlash(APIServer)}/api/v0/aggregates/${address}.json`,
{
socketPath: getSocketPath(),
params: {
keys: _keys,
keys: keys.join(",") || undefined,
limit,
},
},
);
Expand Down
17 changes: 4 additions & 13 deletions src/messages/post/get.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,21 +65,12 @@ export async function Get<T>(configuration: PostGetConfiguration): Promise<PostQ
types: configuration.types,
pagination: configuration.pagination,
page: configuration.page,
refs: configuration.refs.join(",") || undefined,
addresses: configuration.addresses.join(",") || undefined,
tags: configuration.tags.join(",") || undefined,
hashes: configuration.hashes.join(",") || undefined,
};

if (configuration.refs?.length > 0) {
params.refs = configuration.refs.join(",");
}
if (configuration.addresses?.length > 0) {
params.addresses = configuration.addresses.join(",");
}
if (configuration.tags?.length > 0) {
params.tags = configuration.tags.join(",");
}
if (configuration.hashes?.length > 0) {
params.hashes = configuration.hashes.join(",");
}

const response = await axios.get<PostQueryResponse<T>>(
`${stripTrailingSlash(configuration.APIServer)}/api/v0/posts.json`,
{
Expand Down
69 changes: 16 additions & 53 deletions tests/messages/aggregate/get.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,62 +2,25 @@ import { aggregate } from "../../index";
import { DEFAULT_API_V2 } from "../../../src/global";

describe("Aggregate message retrieve test", () => {
it("should retrieve an existing aggregate message", async () => {
type T = {
satoshi: {
A: number;
};
};
const key = "satoshi";
const address = "0x629fBDA22F485720617C8f1209692484C0359D43";

const message = await aggregate.Get<T>({
APIServer: DEFAULT_API_V2,
address: address,
keys: [key],
});

const expected = {
A: 1,
};

expect(message.satoshi).toStrictEqual(expected);
it("should failed to retrieve an aggregate message", async () => {
try {
await aggregate.Get({
APIServer: DEFAULT_API_V2,
address: "0xa1B3bb7d2332383D96b7796B908fB7f7F3c2Be10",
keys: ["satoshi"],
});
expect(true).toStrictEqual(false);
} catch (e: any) {
expect(e.request.res.statusCode).toStrictEqual(404);
}
});

it("should retrieve an existing aggregate message without specifies side params", async () => {
type T = {
satoshi: {
A: number;
};
};
const address = "0x629fBDA22F485720617C8f1209692484C0359D43";

const message = await aggregate.Get<T>({
address: address,
it("should print the CCN list correctly (testing #87)", async () => {
const message = await aggregate.Get({
address: "0xa1B3bb7d2332383D96b7796B908fB7f7F3c2Be10",
keys: ["corechannel"],
});

const expected = {
A: 1,
};

expect(message.satoshi).toStrictEqual(expected);
});

it("should failed to retrieve an aggregate message", async () => {
type T = {
satoshi: {
A: number;
};
};
const key = "satoshi";
const address = "0x629xBDA22F485720617C8f1209692484C0358D43";

await expect(
aggregate.Get<T>({
APIServer: DEFAULT_API_V2,
address: address,
keys: [key],
}),
).rejects.toThrow();
expect(message).toHaveProperty("corechannel");
});
});
4 changes: 1 addition & 3 deletions tests/messages/aggregate/publish.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,9 @@ import { ItemType } from "../../../src/messages/message";
import { DEFAULT_API_V2 } from "../../../src/global";
import { aggregate, ethereum } from "../../index";

const mnemonic = "exit canvas recycle vault excite battle short roof unlock limb attract device";

describe("Aggregate message publish test", () => {
it("should publish an aggregate message", async () => {
const account = ethereum.ImportAccountFromMnemonic(mnemonic);
const { account } = ethereum.NewAccount();
const key = "satoshi";

const content: { A: number } = {
Expand Down

0 comments on commit 508cc87

Please sign in to comment.