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

IPFS Plugin - move config into envs + method options #1060

Merged
merged 22 commits into from
Sep 15, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
f82b5fa
exploratory
pileks Jul 18, 2022
793e464
Add basic env options to ipfs plugin
pileks Jul 20, 2022
d20a142
remove ipfs plugin config
pileks Jul 20, 2022
f2199f8
use provider when checking for file existence
pileks Jul 20, 2022
622d08a
fix broken tests, add envs where needed
pileks Jul 21, 2022
1937a72
add fallback providers to ipfs plugin method options
pileks Jul 21, 2022
c56d690
lint fix
pileks Jul 21, 2022
3c2a562
IPFS plugin timeout test
pileks Jul 21, 2022
17baf6d
fix ethereum e2e tests
pileks Jul 21, 2022
37180db
ipfs resolver timeout tests
pileks Jul 21, 2022
ac3110c
ipfs plugin test method options provider
pileks Jul 21, 2022
3b83f06
fix failing tests
pileks Jul 21, 2022
076b92d
ipfs plugin fallback provider in method options, tests
pileks Jul 22, 2022
137958b
Merge remote-tracking branch 'origin/origin-dev' into pileks-ipfs-con…
pileks Aug 9, 2022
fa06a4c
remove providers from getClientwithEnsAndIpfs
pileks Aug 9, 2022
b93178e
Merge remote-tracking branch 'origin/origin-dev' into pileks-ipfs-con…
pileks Aug 17, 2022
a2d1fa5
Merge remote-tracking branch 'origin/origin-dev' into pileks-ipfs-con…
krisbitney Sep 7, 2022
bddda80
updated default client config after merge
krisbitney Sep 7, 2022
82ca1a7
updated @polywrap/react tests to use ipfs plugin env instead of plugi…
krisbitney Sep 7, 2022
28c6943
Merge remote-tracking branch 'origin/origin-dev' into pileks-ipfs-con…
pileks Sep 7, 2022
9fc0444
rename skipCheckIfExistsBeforeGetFile to skipCheckIfExists
pileks Sep 7, 2022
ce9809b
set default timeout for ipfs plugin actions
pileks Sep 7, 2022
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
13 changes: 10 additions & 3 deletions packages/cli/src/lib/helpers/client.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { PluginRegistration } from "@polywrap/core-js";
import { PluginRegistration, Env } from "@polywrap/core-js";
import { ensResolverPlugin } from "@polywrap/ens-resolver-plugin-js";
import {
ethereumPlugin,
Expand All @@ -18,6 +18,8 @@ interface SimpleClientConfig {
export function getSimpleClient(config: SimpleClientConfig): PolywrapClient {
const { ensAddress, ethProvider, ipfsProvider } = config;
const plugins: PluginRegistration[] = [];
const envs: Env[] = [];

if (ensAddress) {
plugins.push({
uri: "wrap://ens/ens-resolver.polywrap.eth",
Expand Down Expand Up @@ -45,10 +47,15 @@ export function getSimpleClient(config: SimpleClientConfig): PolywrapClient {
if (ipfsProvider) {
plugins.push({
uri: "wrap://ens/ipfs.polywrap.eth",
plugin: ipfsPlugin({
plugin: ipfsPlugin({}),
});

envs.push({
uri: "wrap://ens/ipfs.polywrap.eth",
env: {
provider: ipfsProvider,
fallbackProviders: defaultIpfsProviders,
}),
},
});
}
return new PolywrapClient({ plugins });
Expand Down
17 changes: 13 additions & 4 deletions packages/cli/src/lib/test-env/client-config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {
} from "@polywrap/ethereum-plugin-js";
import { ipfsPlugin } from "@polywrap/ipfs-plugin-js";
import { ensAddresses } from "@polywrap/test-env-js";
import { Env } from "@polywrap/core-js";

export async function getTestEnvClientConfig(): Promise<
Partial<PolywrapClientConfig>
Expand Down Expand Up @@ -41,10 +42,7 @@ export async function getTestEnvClientConfig(): Promise<
},
{
uri: "wrap://ens/ipfs.polywrap.eth",
plugin: ipfsPlugin({
provider: ipfsProvider,
fallbackProviders: defaultIpfsProviders,
}),
plugin: ipfsPlugin({}),
},
{
uri: "wrap://ens/ens-resolver.polywrap.eth",
Expand All @@ -56,7 +54,18 @@ export async function getTestEnvClientConfig(): Promise<
},
];

const envs: Env[] = [
{
uri: "wrap://ens/ipfs.polywrap.eth",
env: {
provider: ipfsProvider,
fallbackProviders: defaultIpfsProviders,
},
},
];

return {
plugins,
envs,
};
}
5 changes: 5 additions & 0 deletions packages/interfaces/ipfs/src/schema.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,11 @@ type Options {
"""
provider: String

"""
Fallback IPFS providers
"""
fallbackProviders: [String!]

"""
Disable querying providers in parallel when resolving URIs
"""
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,13 @@ export const getDefaultClientConfig = (): ClientConfig<Uri> => {
provider: "https://api.thegraph.com",
},
},
{
uri: new Uri("wrap://ens/ipfs.polywrap.eth"),
env: {
provider: defaultIpfsProviders[0],
fallbackProviders: defaultIpfsProviders.slice(1),
},
},
],
redirects: [
{
Expand All @@ -52,10 +59,7 @@ export const getDefaultClientConfig = (): ClientConfig<Uri> => {
// IPFS is required for downloading Polywrap packages
{
uri: new Uri("wrap://ens/ipfs.polywrap.eth"),
plugin: ipfsPlugin({
provider: defaultIpfsProviders[0],
fallbackProviders: defaultIpfsProviders.slice(1),
}),
plugin: ipfsPlugin({}),
},
// ENS is required for resolving domain to IPFS hashes
{
Expand Down
4 changes: 2 additions & 2 deletions packages/js/client/scripts/extractPluginConfigs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,11 @@ const plugins: PluginConfigSource[] = [
name: "Ipfs",
module: "@polywrap/ipfs-plugin-js",
uri: "wrap://ens/ipfs.polywrap.eth",
config: "IpfsPluginConfig",
config: "NoConfig",
files: [
{
name: "build/index.d.ts",
interfaces: ["IpfsPluginConfig"],
types: ["NoConfig"]
},
],
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ export const getClientWithEnsAndIpfs = async (
return createPolywrapClient(
{
ethereum: { connections },
ipfs: { provider: providers.ipfs },
ipfs: {},
ens: {
addresses: {
testnet: ensAddresses.ensAddress,
Expand Down
5 changes: 1 addition & 4 deletions packages/js/client/src/pluginConfigs/Ipfs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,4 @@
/// Types generated from @polywrap/ipfs-plugin-js build files:
/// build/index.d.ts

export interface IpfsPluginConfig {
provider: string;
fallbackProviders?: string[];
}
export type NoConfig = Record<string, never>;
4 changes: 2 additions & 2 deletions packages/js/client/src/pluginConfigs/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@
/* eslint-disable @typescript-eslint/no-explicit-any */
/* eslint-disable prettier/prettier */

import { IpfsPluginConfig } from "./Ipfs";
import { NoConfig } from "./Ipfs";
import { EthereumPluginConfig } from "./Ethereum";
import { EnsResolverPluginConfig } from "./Ens";

interface PluginConfigs {
ipfs?: IpfsPluginConfig;
ipfs?: NoConfig;
ethereum?: EthereumPluginConfig;
ens?: EnsResolverPluginConfig;
}
Expand Down
14 changes: 10 additions & 4 deletions packages/js/plugins/ethereum/src/__tests__/e2e.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,17 +63,23 @@ describe("Ethereum Plugin", () => {
});

client = new PolywrapClient({
envs: [
{
uri: "wrap://ens/ipfs.polywrap.eth",
env: {
provider: providers.ipfs,
fallbackProviders: defaultIpfsProviders,
},
},
],
plugins: [
{
uri: "wrap://ens/ethereum.polywrap.eth",
plugin: ethereumPlugin({ connections }),
},
{
uri: "wrap://ens/ipfs.polywrap.eth",
plugin: ipfsPlugin({
provider: providers.ipfs,
fallbackProviders: defaultIpfsProviders,
}),
plugin: ipfsPlugin({}),
},
{
uri: "wrap://ens/ens-resolver.polywrap.eth",
Expand Down
173 changes: 171 additions & 2 deletions packages/js/plugins/ipfs/src/__tests__/e2e.spec.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { InvokeResult } from "@polywrap/core-js";
import { PolywrapClient } from "@polywrap/client-js";
import {
initTestEnvironment,
Expand Down Expand Up @@ -29,9 +30,15 @@ describe("IPFS Plugin", () => {
plugins: [
{
uri: "wrap://ens/ipfs.polywrap.eth",
plugin: ipfsPlugin({
plugin: ipfsPlugin({}),
},
],
envs: [
{
uri: "wrap://ens/ipfs.polywrap.eth",
env: {
provider: providers.ipfs,
}),
},
},
],
});
Expand Down Expand Up @@ -87,4 +94,166 @@ describe("IPFS Plugin", () => {

expect(contentsBuffer).toEqual(addedFileBuffer);
});

it("Should timeout within a specified amount of time - env and options", async () => {
const createRacePromise = (
timeout: number
): Promise<InvokeResult<Uint8Array>> => {
return new Promise<InvokeResult<Uint8Array>>((resolve) =>
setTimeout(() => {
resolve({
data: Uint8Array.from([1, 2, 3, 4]),
error: undefined,
});
}, timeout)
);
};

const altClient = new PolywrapClient({
plugins: [
{
uri: "wrap://ens/ipfs.polywrap.eth",
plugin: ipfsPlugin({}),
},
],
envs: [
{
uri: "wrap://ens/ipfs.polywrap.eth",
env: {
provider: providers.ipfs,
timeout: 1000,
},
},
],
});

const nonExistentFileCid = "Qmaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa";

const catPromise = Ipfs_Module.cat({ cid: nonExistentFileCid }, altClient);

let racePromise = createRacePromise(1100);

const result = await Promise.race([catPromise, racePromise]);

expect(result).toBeTruthy();
expect(result.data).toBeFalsy();
expect(result.error).toBeTruthy();
expect(result.error?.stack).toMatch("Timeout has been reached");
expect(result.error?.stack).toMatch("Timeout: 1000");

const catPromiseWithTimeoutOverride = Ipfs_Module.cat(
{
cid: nonExistentFileCid,
options: { timeout: 500 },
},
altClient
);

racePromise = createRacePromise(600);

const resultForOverride = await Promise.race([
catPromiseWithTimeoutOverride,
racePromise,
]);

expect(resultForOverride).toBeTruthy();
expect(resultForOverride.data).toBeFalsy();
expect(resultForOverride.error).toBeTruthy();
expect(resultForOverride.error?.stack).toMatch("Timeout has been reached");
expect(resultForOverride.error?.stack).toMatch("Timeout: 500");
});

it("Should use provider from method options", async () => {
const clientWithBadProvider = new PolywrapClient({
plugins: [
{
uri: "wrap://ens/ipfs.polywrap.eth",
plugin: ipfsPlugin({}),
},
],
envs: [
{
uri: "wrap://ens/ipfs.polywrap.eth",
env: {
provider: "this-provider-doesnt-exist",
},
},
],
});

const catResult = await Ipfs_Module.cat(
{
cid: sampleFileIpfsInfo.hash.toString(),
options: { provider: providers.ipfs },
},
clientWithBadProvider
);

expect(catResult.error).toBeFalsy();
expect(catResult.data).toEqual(sampleFileBuffer);

const resolveResult = await Ipfs_Module.resolve(
{
cid: sampleFileIpfsInfo.hash.toString(),
options: { provider: providers.ipfs },
},
clientWithBadProvider
);

expect(resolveResult.error).toBeFalsy();
expect(resolveResult.data).toEqual({
cid: `/ipfs/${sampleFileIpfsInfo.hash.toString()}`,
provider: providers.ipfs,
});
});

it("Should use fallback provider from method options", async () => {
const clientWithBadProvider = new PolywrapClient({
plugins: [
{
uri: "wrap://ens/ipfs.polywrap.eth",
plugin: ipfsPlugin({}),
},
],
envs: [
{
uri: "wrap://ens/ipfs.polywrap.eth",
env: {
provider: "this-provider-doesnt-exist",
},
},
],
});

const catResult = await Ipfs_Module.cat(
{
cid: sampleFileIpfsInfo.hash.toString(),
options: {
provider: "this-provider-also-doesnt-exist",
fallbackProviders: [providers.ipfs],
},
},
clientWithBadProvider
);

expect(catResult.error).toBeFalsy();
expect(catResult.data).toEqual(sampleFileBuffer);

const resolveResult = await Ipfs_Module.resolve(
{
cid: sampleFileIpfsInfo.hash.toString(),
options: {
provider: "this-provider-also-doesnt-exist",
fallbackProviders: [providers.ipfs],
},
},
clientWithBadProvider
);

expect(resolveResult.error).toBeFalsy();
expect(resolveResult.data).toEqual({
cid: `/ipfs/${sampleFileIpfsInfo.hash.toString()}`,
provider: providers.ipfs,
});
});
});
Loading