Skip to content

Commit

Permalink
refactor(core-p2p): getPeerConfig return plugins information (#3755)
Browse files Browse the repository at this point in the history
  • Loading branch information
air1one authored Jun 1, 2020
1 parent 67ae0c8 commit a124655
Show file tree
Hide file tree
Showing 6 changed files with 151 additions and 146 deletions.
104 changes: 0 additions & 104 deletions __tests__/unit/core-kernel/utils/transform-plugins.test.ts

This file was deleted.

19 changes: 19 additions & 0 deletions __tests__/unit/core-p2p/socket-server/controllers/peer.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,34 @@ describe("PeerController", () => {
getLastDownloadedBlock: jest.fn(),
};
const createProcessor = jest.fn();
const appPlugins = [ { package: "@arkecosystem/core-api", options: {} } ];
const coreApiServiceProvider = {
name: () => "core-api",
configDefaults: () => ({
server: { http: { port: 4003 } }
}),
};
const serviceProviders = { "@arkecosystem/core-api": coreApiServiceProvider, };
const configRepository = { get: () => appPlugins }; // get("app.plugins")
const serviceProviderRepository = { get: (plugin) => serviceProviders[plugin] };
const appGet = {
[Container.Identifiers.BlockchainService]: blockchain,
[Container.Identifiers.TransactionPoolProcessorFactory]: createProcessor,
[Container.Identifiers.ConfigRepository]: configRepository,
[Container.Identifiers.ServiceProviderRepository]: serviceProviderRepository,
};
const config = { getOptional: jest.fn().mockReturnValue(["127.0.0.1"]) }; // remoteAccess
const app = {
get: (key) => appGet[key],
getTagged: () => config,
version: () => "3.0.9",
resolve: () => ({
from: () => ({
merge: () => ({
all: () => ({ server: { http: { port: "4003" } } })
})
})
})
};

beforeAll(() => {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { Managers } from "@arkecosystem/crypto";
import { getPeerConfig } from "@arkecosystem/core-p2p/src/socket-server/utils/get-peer-config";
import { Container } from "@arkecosystem/core-kernel";

describe("getPeerConfig", () => {
const mockConfig = {
Expand All @@ -13,7 +14,56 @@ describe("getPeerConfig", () => {
jest.spyOn(Managers.configManager, "get").mockImplementation((key) => mockConfig[key]);

const version = "3.0.9";
const app = { version: () => version };
const appPlugins = [
{ package: "@arkecosystem/core-api", options: {} },
{ package: "@arkecosystem/core-webhooks" },
];
const coreApiServiceProvider = {
name: () => "core-api",
configDefaults: () => ({
server: { http: { port: 4003 } }
}),
};
const coreWebhooksServiceProvider = {
name: () => "core-webhooks",
configDefaults: () => ({}),
};
const serviceProviders = {
"@arkecosystem/core-api": coreApiServiceProvider,
"@arkecosystem/core-webhooks": coreWebhooksServiceProvider,
}
const configRepository = { get: () => appPlugins }; // get("app.plugins")
const serviceProviderRepository = { get: (plugin) => serviceProviders[plugin] };
const appGet = {
[Container.Identifiers.ConfigRepository]: configRepository,
[Container.Identifiers.ServiceProviderRepository]: serviceProviderRepository,
}
const app = {
version: () => version,
get: (key) => appGet[key],
resolve: () => ({
from: () => ({
merge: () => ({
all: () => ({
server: {
http: {
port: "4003"
}
}
})
})
}),
discover: () => ({
merge: () => ({
all: () => ({
server: {
port: "4004"
}
})
})
})
})
};

it("should return own config from config manager", () => {
expect(getPeerConfig(app as any)).toEqual({
Expand All @@ -28,7 +78,16 @@ describe("getPeerConfig", () => {
symbol: mockConfig["network.client.symbol"],
},
},
plugins: {},
plugins: {
"@arkecosystem/core-api": {
enabled: true,
port: 4003
},
"@arkecosystem/core-webhooks": {
enabled: true,
port: 4004
}
},
});
});
});
1 change: 0 additions & 1 deletion packages/core-kernel/src/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import { isWhitelisted } from "./is-whitelisted";
import { calculateRound, isNewRound } from "./round-calculator";
export * as Search from "./search";
import { calculate } from "./supply-calculator";
export * as Plugins from "./transform-plugins";

export * from "@arkecosystem/utils";
export * from "./expiration-calculator";
Expand Down
34 changes: 0 additions & 34 deletions packages/core-kernel/src/utils/transform-plugins.ts

This file was deleted.

76 changes: 71 additions & 5 deletions packages/core-p2p/src/socket-server/utils/get-peer-config.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,70 @@
// import { Plugins } from "@arkecosystem/core-kernel";
import { Contracts } from "@arkecosystem/core-kernel";
import { Contracts, Container, Services, Providers, Utils } from "@arkecosystem/core-kernel";
import { Managers } from "@arkecosystem/crypto";

type PluginConfig = { package: string; options: any };

const transformPlugins = (plugins: PluginConfig[]): Contracts.P2P.PeerPlugins => {
const result: Contracts.P2P.PeerPlugins = {};

for (const pluginConfig of plugins) {
const name = pluginConfig.package;
const options = pluginConfig.options?.server?.http
|| pluginConfig.options?.server?.https
|| pluginConfig.options?.server
|| pluginConfig.options
|| {}; // lots of options for server configuration... TODO review see if it can be cleaner

const port = Number(options.port);

if (isNaN(port) || name.includes("core-p2p")) {
continue;
}

result[name] = {
enabled: true, // default to true because "enabled" flag is in different place based on which plugin
port,
};
}

return result;
};

const getPluginsConfig = (plugins: PluginConfig[], app: Contracts.Kernel.Application) => {
return plugins.map(plugin => {
const serviceProvider: Providers.ServiceProvider = app
.get<Providers.ServiceProviderRepository>(Container.Identifiers.ServiceProviderRepository)
.get(plugin.package);

const serviceProviderName: string | undefined = serviceProvider.name();

Utils.assert.defined<string>(serviceProviderName);

const hasDefaults: boolean = Object.keys(serviceProvider.configDefaults()).length > 0;

if (hasDefaults) {
const pluginConfig = {
package: plugin.package,
options: app
.resolve(Providers.PluginConfiguration)
.from(serviceProviderName, serviceProvider.configDefaults())
.merge(plugin.options || {})
.all()
};
return pluginConfig;
}

const pluginConfig = {
package: plugin.package,
options: app
.resolve(Providers.PluginConfiguration)
.discover(serviceProviderName)
.merge(plugin.options || {})
.all()
};
return pluginConfig;
});
}

export const getPeerConfig = (app: Contracts.Kernel.Application): Contracts.P2P.PeerConfig => {
return {
version: app.version(),
Expand All @@ -15,8 +78,11 @@ export const getPeerConfig = (app: Contracts.Kernel.Application): Contracts.P2P.
symbol: Managers.configManager.get("network.client.symbol"),
},
},
plugins: {},
// todo: review and re-enable
// plugins: Plugins.transformPlugins(appConfig.config.plugins),
plugins: transformPlugins(
getPluginsConfig(
app.get<Services.Config.ConfigRepository>(Container.Identifiers.ConfigRepository).get("app.plugins"),
app
)
),
};
};

0 comments on commit a124655

Please sign in to comment.