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 timeout update example plugin #12

Merged
merged 8 commits into from
Aug 15, 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
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
bin
.ergomatic.toml
ergomatic.yaml
cov
.vscode
3 changes: 2 additions & 1 deletion deno.jsonc
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
{
"imports": {
"std/": "https://deno.land/std@0.196.0/",
"zod": "https://deno.land/x/zod@v3.21.4/mod.ts",
"zod/": "https://deno.land/x/zod@v3.21.4/",
"lodash.merge": "npm:lodash.merge@4.6.2",
"yargs": "https://deno.land/x/yargs@v17.7.2-deno/deno.ts",
"dirs/": "https://deno.land/x/dir@1.5.1/",
"@fleet-sdk/common": "npm:@fleet-sdk/common@0.1.3",
"axios": "npm:axios@1.4.0"
},
Expand Down
69 changes: 69 additions & 0 deletions deno.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 7 additions & 0 deletions ergomatic.example.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
logLevel: DEBUG
plugins:
- id: example_plugin
enabled: true # defaults to true, can be set to false to disable plugin.
config:
tokenId: c7e22029868ecba3d43c385bb42b8a1c96d0114ad5261ec7e0d27a6f24254f92
exitAtPage: 3
26 changes: 23 additions & 3 deletions plugins/example_plugin/mod.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ import { Plugin, PluginDescriptor } from "../../src/plugins/mod.ts";
export const EXAMPLE_PLUGIN_ID = "example_plugin";

interface ExamplePluginConfig {
someValue: number;
tokenId: string;
exitAtPage: number;
}

export class ExamplePlugin extends Plugin<ExamplePluginConfig> {
Expand All @@ -16,11 +17,30 @@ export class ExamplePlugin extends Plugin<ExamplePluginConfig> {
};
}

onStart(): Promise<void> {
async onStart(): Promise<void> {
this.logger.info(
`Example plugin started with config: ${JSON.stringify(this.config)}`,
);

return Promise.resolve();
const { tokenId, exitAtPage } = this.config;
let currentPage = 0;

for await (const page of this.blockchainClient.getBoxesByTokenId(tokenId)) {
currentPage++;

this.logger.info(
`Got page ${currentPage} of boxes for token ${tokenId}`,
);

this.logger.info(`there was ${page.length} boxes in this page`);

if (currentPage === exitAtPage) {
this.logger.info(
`Exiting at page ${currentPage} of boxes for token ${tokenId}`,
);

break;
}
}
}
}
7 changes: 7 additions & 0 deletions src/_testing.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { mergeUserConfigAndValidate } from "./config.ts";

export function testConfig() {
return mergeUserConfigAndValidate({
plugins: [{ enabled: true, id: "test-plugin" }],
});
}
4 changes: 4 additions & 0 deletions src/_utils.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
export function isTesting() {
return Deno.env.get("ERGOMATIC_TEST") === "true";
}

export function mbToBytes(mb: number) {
return mb * 1000000;
}
11 changes: 9 additions & 2 deletions src/blockchain/clients/explorer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,19 +12,25 @@ import { ErgomaticConfig } from "../../config.ts";
export class ExplorerClient implements BlockchainClient {
readonly #http: AxiosInstance;
#pageSize = 100;
#timeoutMs: number;

constructor(config: ErgomaticConfig) {
constructor(config: ErgomaticConfig, httpTimeoutMs: number = 10000) {
// axios timeout is incompatible with deno due to a missing nodejs API
// use signals for timeouts instead.
this.#timeoutMs = httpTimeoutMs;
this.#http = axios.create({
// let URL handle any possible trailing slash,etc in the configured endpoint.
baseURL: new URL("/api/v1", config.explorer.endpoint).href,
timeout: 10000, // explorer API can be slow
});
}

async submitTx(signedTx: SignedTransaction): Promise<TransactionId> {
const response = await this.#http.post(
"/mempool/transactions/submit",
signedTx,
{
signal: AbortSignal.timeout(this.#timeoutMs),
},
);

return response.data;
Expand All @@ -41,6 +47,7 @@ export class ExplorerClient implements BlockchainClient {
while (true) {
const { data } = await this.#http.get(`/boxes/byTokenId/${tokenId}`, {
params: { offset, limit: this.#pageSize },
signal: AbortSignal.timeout(this.#timeoutMs),
});
const { total, items } = data;

Expand Down
4 changes: 2 additions & 2 deletions src/config.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { z } from "zod";
import { z } from "zod/mod.ts";
import merge from "lodash.merge";

const pluginConfigEntrySchema = z.object({
id: z.string(),
enabled: z.boolean(),
enabled: z.boolean().default(true),
config: z.object({}).optional(),
});

Expand Down
14 changes: 5 additions & 9 deletions src/ergomatic_test.ts
Original file line number Diff line number Diff line change
@@ -1,23 +1,19 @@
import { beforeEach, describe, it } from "std/testing/bdd.ts";
import { Ergomatic } from "./ergomatic.ts";
import { PluginManager } from "./plugins/mod.ts";
import { ErgomaticConfig, mergeUserConfigAndValidate } from "./config.ts";
import { ErgomaticConfig } from "./config.ts";
import { assertSpyCalls, spy, stub } from "std/testing/mock.ts";
import { assertEquals } from "std/testing/asserts.ts";

function mkConfig() {
return mergeUserConfigAndValidate({
plugins: [{ enabled: true, id: "example_plugin" }],
});
}
import { testConfig } from "./_testing.ts";
import { mkTestPluginManager } from "./plugins/_testing.ts";

describe("Ergomatic", () => {
let config: ErgomaticConfig;
let pluginManager: PluginManager;

beforeEach(() => {
config = mkConfig();
pluginManager = new PluginManager(config);
config = testConfig();
pluginManager = mkTestPluginManager({ config }).pluginManager;
});

describe("start()", () => {
Expand Down
Loading
Loading