From 75a1faf2af2b80ed364c4dd1bc16dba48bb35dab Mon Sep 17 00:00:00 2001 From: pshenmic Date: Mon, 3 Apr 2023 15:49:33 +0300 Subject: [PATCH 01/18] fix(dashmate): parse string as object --- packages/dashmate/src/commands/config/set.js | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/packages/dashmate/src/commands/config/set.js b/packages/dashmate/src/commands/config/set.js index 80f984f7be0..ff8c88f769a 100644 --- a/packages/dashmate/src/commands/config/set.js +++ b/packages/dashmate/src/commands/config/set.js @@ -20,7 +20,11 @@ class ConfigSetCommand extends ConfigBaseCommand { optionValue = null; } - config.set(optionPath, optionValue); + if (typeof config.get(optionPath) === 'object') { + config.set(optionPath, JSON.parse(optionValue)); + } else { + config.set(optionPath, optionValue); + } // eslint-disable-next-line no-console console.log(`${optionPath} set to ${config.get(optionPath)}`); From 0840754ff70e6109d64a29ffc526df5b025a3d8b Mon Sep 17 00:00:00 2001 From: pshenmic Date: Mon, 10 Apr 2023 16:50:58 +0400 Subject: [PATCH 02/18] fix: fix masternodes lists --- .../SimplifiedMasternodeListDAPIAddressProvider.js | 4 +++- scripts/configure_test_suite_network.sh | 2 +- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/packages/js-dapi-client/lib/dapiAddressProvider/SimplifiedMasternodeListDAPIAddressProvider.js b/packages/js-dapi-client/lib/dapiAddressProvider/SimplifiedMasternodeListDAPIAddressProvider.js index 673b3320493..fa238c87771 100644 --- a/packages/js-dapi-client/lib/dapiAddressProvider/SimplifiedMasternodeListDAPIAddressProvider.js +++ b/packages/js-dapi-client/lib/dapiAddressProvider/SimplifiedMasternodeListDAPIAddressProvider.js @@ -19,7 +19,9 @@ class SimplifiedMasternodeListDAPIAddressProvider { */ async getLiveAddress() { const sml = await this.smlProvider.getSimplifiedMNList(); - const validMasternodeList = sml.getValidMasternodesList(); + const validMasternodeList = sml.getValidMasternodesList() + // Keep only HP masternodes + .filter((smlEntry) => smlEntry.nType === 1); const addressesByRegProTxHashes = {}; let allowSelfSignedCertificate; diff --git a/scripts/configure_test_suite_network.sh b/scripts/configure_test_suite_network.sh index d9776969c93..653e600d127 100755 --- a/scripts/configure_test_suite_network.sh +++ b/scripts/configure_test_suite_network.sh @@ -56,7 +56,7 @@ FEATURE_FLAGS_OWNER_PRIVATE_KEY=$(yq .feature_flags_hd_private_key "$CONFIG") MASTERNODE_NAME=$(grep "$DAPI_SEED" "$INVENTORY" | awk '{print $1;}') MASTERNODE_OWNER_PRO_REG_TX_HASH=$(grep "$DAPI_SEED" "$INVENTORY" | awk -F "=" '{print $6;}') -MASTERNODE_OWNER_MASTER_PRIVATE_KEY=$(yq .masternodes."$MASTERNODE_NAME".owner.private_key "$CONFIG") +MASTERNODE_OWNER_MASTER_PRIVATE_KEY=$(yq .hp_masternodes."$MASTERNODE_NAME".owner.private_key "$CONFIG") if [[ "$NETWORK_STRING" == "devnet"* ]]; then NETWORK=devnet From 80742bc7e07e4431857e4e71c3c58a1a827d1441 Mon Sep 17 00:00:00 2001 From: pshenmic Date: Fri, 14 Apr 2023 10:41:58 +0300 Subject: [PATCH 03/18] feat: always JSON.parse on config set --- packages/dashmate/src/commands/config/set.js | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/packages/dashmate/src/commands/config/set.js b/packages/dashmate/src/commands/config/set.js index ff8c88f769a..99d9f31ca45 100644 --- a/packages/dashmate/src/commands/config/set.js +++ b/packages/dashmate/src/commands/config/set.js @@ -20,11 +20,7 @@ class ConfigSetCommand extends ConfigBaseCommand { optionValue = null; } - if (typeof config.get(optionPath) === 'object') { - config.set(optionPath, JSON.parse(optionValue)); - } else { - config.set(optionPath, optionValue); - } + config.set(optionPath, JSON.parse(optionValue)); // eslint-disable-next-line no-console console.log(`${optionPath} set to ${config.get(optionPath)}`); From 1f19aa400eea7f60046ce6a48df567e15b2c6f0c Mon Sep 17 00:00:00 2001 From: pshenmic Date: Fri, 14 Apr 2023 11:14:44 +0300 Subject: [PATCH 04/18] feat: check schema for value type --- packages/dashmate/src/commands/config/set.js | 21 +++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) diff --git a/packages/dashmate/src/commands/config/set.js b/packages/dashmate/src/commands/config/set.js index 99d9f31ca45..ab98e1d0008 100644 --- a/packages/dashmate/src/commands/config/set.js +++ b/packages/dashmate/src/commands/config/set.js @@ -1,4 +1,5 @@ const ConfigBaseCommand = require('../../oclif/command/ConfigBaseCommand'); +const configJsonSchema = require("../../../configs/schema/configJsonSchema"); class ConfigSetCommand extends ConfigBaseCommand { /** @@ -20,7 +21,25 @@ class ConfigSetCommand extends ConfigBaseCommand { optionValue = null; } - config.set(optionPath, JSON.parse(optionValue)); + const path = optionPath.split('.') + + let schema = configJsonSchema + + for (const e of path) { + schema = schema.properties[e] + } + + let schemaType = schema.type + + if (Array.isArray(schemaType)) { + schemaType = schemaType[0] + } + + if (schemaType === 'object' || schemaType === 'array') { + config.set(optionPath, JSON.parse(optionValue)); + } else { + config.set(optionPath, optionValue); + } // eslint-disable-next-line no-console console.log(`${optionPath} set to ${config.get(optionPath)}`); From f3286e70dfcd5a429c8aba698e06ffa20bacc3de Mon Sep 17 00:00:00 2001 From: pshenmic Date: Fri, 14 Apr 2023 12:09:34 +0300 Subject: [PATCH 05/18] fix(dashmate): add support for definitions --- packages/dashmate/src/commands/config/set.js | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/packages/dashmate/src/commands/config/set.js b/packages/dashmate/src/commands/config/set.js index ab98e1d0008..2537b118c4e 100644 --- a/packages/dashmate/src/commands/config/set.js +++ b/packages/dashmate/src/commands/config/set.js @@ -21,12 +21,20 @@ class ConfigSetCommand extends ConfigBaseCommand { optionValue = null; } + // check for existence + config.get(optionPath) + const path = optionPath.split('.') let schema = configJsonSchema for (const e of path) { - schema = schema.properties[e] + if (schema['$ref']) { + const [, , definitionName] = schema['$ref'].split('/') + schema = configJsonSchema.definitions[definitionName].properties + } else { + schema = schema.properties[e] + } } let schemaType = schema.type From 395c2df8275f870486b9158f8e691b4c626248af Mon Sep 17 00:00:00 2001 From: pshenmic Date: Fri, 14 Apr 2023 12:13:45 +0300 Subject: [PATCH 06/18] chore(dashmate): lint fix --- packages/dashmate/src/commands/config/set.js | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/packages/dashmate/src/commands/config/set.js b/packages/dashmate/src/commands/config/set.js index 2537b118c4e..8fb1d000ed5 100644 --- a/packages/dashmate/src/commands/config/set.js +++ b/packages/dashmate/src/commands/config/set.js @@ -1,5 +1,5 @@ const ConfigBaseCommand = require('../../oclif/command/ConfigBaseCommand'); -const configJsonSchema = require("../../../configs/schema/configJsonSchema"); +const configJsonSchema = require('../../../configs/schema/configJsonSchema'); class ConfigSetCommand extends ConfigBaseCommand { /** @@ -22,25 +22,25 @@ class ConfigSetCommand extends ConfigBaseCommand { } // check for existence - config.get(optionPath) + config.get(optionPath); - const path = optionPath.split('.') + const path = optionPath.split('.'); - let schema = configJsonSchema + let schema = configJsonSchema; for (const e of path) { - if (schema['$ref']) { - const [, , definitionName] = schema['$ref'].split('/') - schema = configJsonSchema.definitions[definitionName].properties + if (schema.$ref) { + const [, , definitionName] = schema.$ref.split('/'); + schema = configJsonSchema.definitions[definitionName].properties; } else { - schema = schema.properties[e] + schema = schema.properties[e]; } } - let schemaType = schema.type + let schemaType = schema.type; if (Array.isArray(schemaType)) { - schemaType = schemaType[0] + [schemaType] = schemaType; } if (schemaType === 'object' || schemaType === 'array') { From 7a0b237e20410a3d4213a3392e0851acbb0dba5c Mon Sep 17 00:00:00 2001 From: pshenmic Date: Mon, 5 Jun 2023 18:19:09 +0700 Subject: [PATCH 07/18] feat(dashmate): add derefence --- packages/dashmate/package.json | 1 + packages/dashmate/src/commands/config/set.js | 3 ++- yarn.lock | 1 + 3 files changed, 4 insertions(+), 1 deletion(-) diff --git a/packages/dashmate/package.json b/packages/dashmate/package.json index 647708320f2..3b1333e22a7 100644 --- a/packages/dashmate/package.json +++ b/packages/dashmate/package.json @@ -47,6 +47,7 @@ }, "homepage": "https://github.com/dashevo/dashmate#readme", "dependencies": { + "@apidevtools/json-schema-ref-parser": "^8.0.0", "@dashevo/bls": "~1.2.9", "@dashevo/dashcore-lib": "~0.20.5", "@dashevo/dashd-rpc": "^18.2.0", diff --git a/packages/dashmate/src/commands/config/set.js b/packages/dashmate/src/commands/config/set.js index 8fb1d000ed5..26531415ef6 100644 --- a/packages/dashmate/src/commands/config/set.js +++ b/packages/dashmate/src/commands/config/set.js @@ -1,5 +1,6 @@ const ConfigBaseCommand = require('../../oclif/command/ConfigBaseCommand'); const configJsonSchema = require('../../../configs/schema/configJsonSchema'); +const $RefParser = require('@apidevtools/json-schema-ref-parser'); class ConfigSetCommand extends ConfigBaseCommand { /** @@ -26,7 +27,7 @@ class ConfigSetCommand extends ConfigBaseCommand { const path = optionPath.split('.'); - let schema = configJsonSchema; + let schema = await $RefParser.dereference(configJsonSchema) for (const e of path) { if (schema.$ref) { diff --git a/yarn.lock b/yarn.lock index 6f2f8d54052..ebd8eeaaaba 100644 --- a/yarn.lock +++ b/yarn.lock @@ -6363,6 +6363,7 @@ __metadata: version: 0.0.0-use.local resolution: "dashmate@workspace:packages/dashmate" dependencies: + "@apidevtools/json-schema-ref-parser": ^8.0.0 "@dashevo/bls": ~1.2.9 "@dashevo/dashcore-lib": ~0.20.5 "@dashevo/dashd-rpc": ^18.2.0 From 9fc2aed32c625c382eb47daeb4b8b98ec09abba0 Mon Sep 17 00:00:00 2001 From: pshenmic Date: Mon, 5 Jun 2023 18:19:28 +0700 Subject: [PATCH 08/18] feat(dashmate): add set spec --- .../test/unit/commands/config/set.spec.js | 71 +++++++++++++++++++ 1 file changed, 71 insertions(+) create mode 100644 packages/dashmate/test/unit/commands/config/set.spec.js diff --git a/packages/dashmate/test/unit/commands/config/set.spec.js b/packages/dashmate/test/unit/commands/config/set.spec.js new file mode 100644 index 00000000000..d100769377d --- /dev/null +++ b/packages/dashmate/test/unit/commands/config/set.spec.js @@ -0,0 +1,71 @@ +const ConfigSetCommand = require('../../../../src/commands/config/set'); +const Config = require("../../../../src/config/Config"); +const baseConfig = require("../../../../configs/system/base"); + +describe('Set Command Status', () => { + let config + + beforeEach(async function it() { + config = new Config('config', baseConfig) + }); + + describe('#platform', () => { + it('should just work', async () => { + const command = new ConfigSetCommand(); + + const flags = {} + + await command.runWithDependencies({option: 'core.docker.image', value: 'fake_image'}, flags, config); + }); + + it('should throw on unknown path', async () => { + const command = new ConfigSetCommand(); + + const flags = {} + + // invalid path + try { + await command.runWithDependencies({option: 'fakePath', value: 'fake'}, flags, config); + + expect.fail('should throw error'); + } catch (e) { + expect(e.name).to.equal('InvalidOptionPathError'); + } + }); + + it('should allow setting array of values', async () => { + const command = new ConfigSetCommand(); + + const flags = {} + + // invalid json + try { + await command.runWithDependencies({option: 'core.rpc.allowIps', value: 'fake_image'}, flags, config); + + expect.fail('should throw error'); + } catch (e) { + expect(e.name).to.equal('SyntaxError'); + } + + // type mismatch + try { + await command.runWithDependencies({option: 'core.rpc.allowIps', value: [1337, 36484]}, flags, config); + + expect.fail('should throw error'); + } catch (e) { + console.error(e) + expect(e.name).to.equal('SyntaxError'); + } + + await command.runWithDependencies({option: 'core.rpc.allowIps', value: '["1337", "36484"]'}, flags, config); + }); + + it.only('should allow replacing part of the json', async () => { + const command = new ConfigSetCommand(); + + const flags = {} + + await command.runWithDependencies({option: 'docker', value: '{"network":{"subnet":"127.0.0.1/24"}}'}, flags, config); + }); + }); +}); From 8a56d20932c6c588d23571d13bf477a9fb5feecc Mon Sep 17 00:00:00 2001 From: pshenmic Date: Mon, 5 Jun 2023 18:37:59 +0700 Subject: [PATCH 09/18] feat(dashmate): lint fix --- packages/dashmate/src/commands/config/set.js | 4 +-- .../test/unit/commands/config/set.spec.js | 31 +++++++++---------- 2 files changed, 17 insertions(+), 18 deletions(-) diff --git a/packages/dashmate/src/commands/config/set.js b/packages/dashmate/src/commands/config/set.js index 26531415ef6..0f13e41c310 100644 --- a/packages/dashmate/src/commands/config/set.js +++ b/packages/dashmate/src/commands/config/set.js @@ -1,6 +1,6 @@ +const $RefParser = require('@apidevtools/json-schema-ref-parser'); const ConfigBaseCommand = require('../../oclif/command/ConfigBaseCommand'); const configJsonSchema = require('../../../configs/schema/configJsonSchema'); -const $RefParser = require('@apidevtools/json-schema-ref-parser'); class ConfigSetCommand extends ConfigBaseCommand { /** @@ -27,7 +27,7 @@ class ConfigSetCommand extends ConfigBaseCommand { const path = optionPath.split('.'); - let schema = await $RefParser.dereference(configJsonSchema) + let schema = await $RefParser.dereference(configJsonSchema); for (const e of path) { if (schema.$ref) { diff --git a/packages/dashmate/test/unit/commands/config/set.spec.js b/packages/dashmate/test/unit/commands/config/set.spec.js index d100769377d..d200e486d2c 100644 --- a/packages/dashmate/test/unit/commands/config/set.spec.js +++ b/packages/dashmate/test/unit/commands/config/set.spec.js @@ -1,31 +1,31 @@ const ConfigSetCommand = require('../../../../src/commands/config/set'); -const Config = require("../../../../src/config/Config"); -const baseConfig = require("../../../../configs/system/base"); +const Config = require('../../../../src/config/Config'); +const baseConfig = require('../../../../configs/system/base'); describe('Set Command Status', () => { - let config + let config; - beforeEach(async function it() { - config = new Config('config', baseConfig) + beforeEach(async () => { + config = new Config('config', baseConfig); }); describe('#platform', () => { it('should just work', async () => { const command = new ConfigSetCommand(); - const flags = {} + const flags = {}; - await command.runWithDependencies({option: 'core.docker.image', value: 'fake_image'}, flags, config); + await command.runWithDependencies({ option: 'core.docker.image', value: 'fake_image' }, flags, config); }); it('should throw on unknown path', async () => { const command = new ConfigSetCommand(); - const flags = {} + const flags = {}; // invalid path try { - await command.runWithDependencies({option: 'fakePath', value: 'fake'}, flags, config); + await command.runWithDependencies({ option: 'fakePath', value: 'fake' }, flags, config); expect.fail('should throw error'); } catch (e) { @@ -36,11 +36,11 @@ describe('Set Command Status', () => { it('should allow setting array of values', async () => { const command = new ConfigSetCommand(); - const flags = {} + const flags = {}; // invalid json try { - await command.runWithDependencies({option: 'core.rpc.allowIps', value: 'fake_image'}, flags, config); + await command.runWithDependencies({ option: 'core.rpc.allowIps', value: 'fake_image' }, flags, config); expect.fail('should throw error'); } catch (e) { @@ -49,23 +49,22 @@ describe('Set Command Status', () => { // type mismatch try { - await command.runWithDependencies({option: 'core.rpc.allowIps', value: [1337, 36484]}, flags, config); + await command.runWithDependencies({ option: 'core.rpc.allowIps', value: [1337, 36484] }, flags, config); expect.fail('should throw error'); } catch (e) { - console.error(e) expect(e.name).to.equal('SyntaxError'); } - await command.runWithDependencies({option: 'core.rpc.allowIps', value: '["1337", "36484"]'}, flags, config); + await command.runWithDependencies({ option: 'core.rpc.allowIps', value: '["1337", "36484"]' }, flags, config); }); it.only('should allow replacing part of the json', async () => { const command = new ConfigSetCommand(); - const flags = {} + const flags = {}; - await command.runWithDependencies({option: 'docker', value: '{"network":{"subnet":"127.0.0.1/24"}}'}, flags, config); + await command.runWithDependencies({ option: 'docker', value: '{"network":{"subnet":"127.0.0.1/24"}}' }, flags, config); }); }); }); From 09604fe9d494d9a0eadb5713a54f0aace1e0c8ee Mon Sep 17 00:00:00 2001 From: pshenmic Date: Mon, 5 Jun 2023 18:40:08 +0700 Subject: [PATCH 10/18] feat(dashmate): enable --- packages/dashmate/test/unit/commands/config/set.spec.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/dashmate/test/unit/commands/config/set.spec.js b/packages/dashmate/test/unit/commands/config/set.spec.js index d200e486d2c..1de944ce507 100644 --- a/packages/dashmate/test/unit/commands/config/set.spec.js +++ b/packages/dashmate/test/unit/commands/config/set.spec.js @@ -59,7 +59,7 @@ describe('Set Command Status', () => { await command.runWithDependencies({ option: 'core.rpc.allowIps', value: '["1337", "36484"]' }, flags, config); }); - it.only('should allow replacing part of the json', async () => { + it('should allow replacing part of the json', async () => { const command = new ConfigSetCommand(); const flags = {}; From 941ee2c10d8868511898b9018c91634a777d896d Mon Sep 17 00:00:00 2001 From: pshenmic Date: Mon, 5 Jun 2023 21:36:38 +0700 Subject: [PATCH 11/18] feat(dashmate): commit pnp cjs --- .pnp.cjs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/.pnp.cjs b/.pnp.cjs index 6ca6f82f01f..33cfaacc4f9 100755 --- a/.pnp.cjs +++ b/.pnp.cjs @@ -1,4 +1,4 @@ -#!/usr/bin/env node + #!/usr/bin/env node /* eslint-disable */ "use strict"; @@ -8447,6 +8447,7 @@ function $$SETUP_STATE(hydrateRuntimeState, basePath) { "packageLocation": "./packages/dashmate/",\ "packageDependencies": [\ ["dashmate", "workspace:packages/dashmate"],\ + ["@apidevtools/json-schema-ref-parser", "npm:8.0.0"],\ ["@dashevo/bls", "npm:1.2.9"],\ ["@dashevo/dashcore-lib", "npm:0.20.6"],\ ["@dashevo/dashd-rpc", "npm:18.2.0"],\ From 610fcbc314ef4e4e546ccce56a8f1587491d708a Mon Sep 17 00:00:00 2001 From: pshenmic Date: Mon, 5 Jun 2023 21:45:20 +0700 Subject: [PATCH 12/18] feat(dashmate): stringify optionValue --- .pnp.cjs | 2 +- packages/dashmate/src/commands/config/set.js | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/.pnp.cjs b/.pnp.cjs index 33cfaacc4f9..58f460ac86b 100755 --- a/.pnp.cjs +++ b/.pnp.cjs @@ -1,4 +1,4 @@ - #!/usr/bin/env node +#!/usr/bin/env node /* eslint-disable */ "use strict"; diff --git a/packages/dashmate/src/commands/config/set.js b/packages/dashmate/src/commands/config/set.js index 0f13e41c310..61c528aec44 100644 --- a/packages/dashmate/src/commands/config/set.js +++ b/packages/dashmate/src/commands/config/set.js @@ -51,7 +51,7 @@ class ConfigSetCommand extends ConfigBaseCommand { } // eslint-disable-next-line no-console - console.log(`${optionPath} set to ${config.get(optionPath)}`); + console.log(`${optionPath} set to ${JSON.stringify(config.get(optionPath))}`); } } From a0e46676c7f7f2f98b2585d7cb8d7407e01ef968 Mon Sep 17 00:00:00 2001 From: pshenmic Date: Tue, 6 Jun 2023 15:11:14 +0700 Subject: [PATCH 13/18] feat(dashmate): remove redundant $ref condition --- packages/dashmate/src/commands/config/set.js | 7 +------ 1 file changed, 1 insertion(+), 6 deletions(-) diff --git a/packages/dashmate/src/commands/config/set.js b/packages/dashmate/src/commands/config/set.js index 61c528aec44..511875216ba 100644 --- a/packages/dashmate/src/commands/config/set.js +++ b/packages/dashmate/src/commands/config/set.js @@ -30,12 +30,7 @@ class ConfigSetCommand extends ConfigBaseCommand { let schema = await $RefParser.dereference(configJsonSchema); for (const e of path) { - if (schema.$ref) { - const [, , definitionName] = schema.$ref.split('/'); - schema = configJsonSchema.definitions[definitionName].properties; - } else { - schema = schema.properties[e]; - } + schema = schema.properties[e]; } let schemaType = schema.type; From 0645b50d829fb57e483a7ac780407a5ef946424c Mon Sep 17 00:00:00 2001 From: pshenmic Date: Tue, 6 Jun 2023 16:13:06 +0700 Subject: [PATCH 14/18] feat(dashmate): replace with getPropertyDefinitionByPath.js --- packages/dashmate/src/commands/config/set.js | 10 ++-- .../src/util/getPropertyDefinitionByPath.js | 47 +++++++++++++++++++ 2 files changed, 52 insertions(+), 5 deletions(-) create mode 100644 packages/dashmate/src/util/getPropertyDefinitionByPath.js diff --git a/packages/dashmate/src/commands/config/set.js b/packages/dashmate/src/commands/config/set.js index 511875216ba..3a3b7eab8f4 100644 --- a/packages/dashmate/src/commands/config/set.js +++ b/packages/dashmate/src/commands/config/set.js @@ -1,6 +1,7 @@ const $RefParser = require('@apidevtools/json-schema-ref-parser'); const ConfigBaseCommand = require('../../oclif/command/ConfigBaseCommand'); const configJsonSchema = require('../../../configs/schema/configJsonSchema'); +const getPropertyDefinitionByPath = require('../../util/getPropertyDefinitionByPath'); class ConfigSetCommand extends ConfigBaseCommand { /** @@ -25,12 +26,11 @@ class ConfigSetCommand extends ConfigBaseCommand { // check for existence config.get(optionPath); - const path = optionPath.split('.'); + const configSchema = await $RefParser.dereference(configJsonSchema); + const schema = getPropertyDefinitionByPath(configSchema, optionPath); - let schema = await $RefParser.dereference(configJsonSchema); - - for (const e of path) { - schema = schema.properties[e]; + if (!schema) { + throw new Error(`Could not find schema for option path ${optionPath}`); } let schemaType = schema.type; diff --git a/packages/dashmate/src/util/getPropertyDefinitionByPath.js b/packages/dashmate/src/util/getPropertyDefinitionByPath.js new file mode 100644 index 00000000000..0409c0a5a55 --- /dev/null +++ b/packages/dashmate/src/util/getPropertyDefinitionByPath.js @@ -0,0 +1,47 @@ +/** + * Get user property definition + * + * @typeof {getPropertyDefinitionByPath} + * @param {Object} documentDefinition + * @param {string} path + * + * @return {Object} + */ +function getPropertyDefinitionByPath(documentDefinition, path) { + const [currentSegment, ...rest] = path.split('.'); + + const { [currentSegment]: propertyDefinition } = (documentDefinition.properties || {}); + + // nothing found return nothing + if (!propertyDefinition) { + return undefined; + } + + // if there is nothing to lookup for next + // return currently found property definition + if (rest.length === 0) { + return propertyDefinition; + } + + const { type } = propertyDefinition; + + if (type === 'array') { + const { items: itemsDefinition } = propertyDefinition; + + if (itemsDefinition.type === 'object') { + return getPropertyDefinitionByPath(itemsDefinition, rest.join('.')); + } + } + + if (type === 'object') { + // rince and repeat + return getPropertyDefinitionByPath(propertyDefinition, rest.join('.')); + } + + // the `rest` is not empty + // but definition is not an object nor array + // nothing to lookup for + return undefined; +} + +module.exports = getPropertyDefinitionByPath; From 33be452d13505e5c77c138d9a638bbacec6a4986 Mon Sep 17 00:00:00 2001 From: pshenmic Date: Tue, 6 Jun 2023 17:18:17 +0700 Subject: [PATCH 15/18] feat(dashmate): replace with JSON.parse() --- .pnp.cjs | 1 - packages/dashmate/package.json | 1 - packages/dashmate/src/commands/config/set.js | 31 ++----- .../src/config/errors/InvalidOptionError.js | 4 +- .../test/unit/commands/config/set.spec.js | 82 ++++++++++++------- yarn.lock | 1 - 6 files changed, 63 insertions(+), 57 deletions(-) diff --git a/.pnp.cjs b/.pnp.cjs index 58f460ac86b..6ca6f82f01f 100755 --- a/.pnp.cjs +++ b/.pnp.cjs @@ -8447,7 +8447,6 @@ function $$SETUP_STATE(hydrateRuntimeState, basePath) { "packageLocation": "./packages/dashmate/",\ "packageDependencies": [\ ["dashmate", "workspace:packages/dashmate"],\ - ["@apidevtools/json-schema-ref-parser", "npm:8.0.0"],\ ["@dashevo/bls", "npm:1.2.9"],\ ["@dashevo/dashcore-lib", "npm:0.20.6"],\ ["@dashevo/dashd-rpc", "npm:18.2.0"],\ diff --git a/packages/dashmate/package.json b/packages/dashmate/package.json index 3b1333e22a7..647708320f2 100644 --- a/packages/dashmate/package.json +++ b/packages/dashmate/package.json @@ -47,7 +47,6 @@ }, "homepage": "https://github.com/dashevo/dashmate#readme", "dependencies": { - "@apidevtools/json-schema-ref-parser": "^8.0.0", "@dashevo/bls": "~1.2.9", "@dashevo/dashcore-lib": "~0.20.5", "@dashevo/dashd-rpc": "^18.2.0", diff --git a/packages/dashmate/src/commands/config/set.js b/packages/dashmate/src/commands/config/set.js index 3a3b7eab8f4..7b12c92d744 100644 --- a/packages/dashmate/src/commands/config/set.js +++ b/packages/dashmate/src/commands/config/set.js @@ -1,7 +1,4 @@ -const $RefParser = require('@apidevtools/json-schema-ref-parser'); const ConfigBaseCommand = require('../../oclif/command/ConfigBaseCommand'); -const configJsonSchema = require('../../../configs/schema/configJsonSchema'); -const getPropertyDefinitionByPath = require('../../util/getPropertyDefinitionByPath'); class ConfigSetCommand extends ConfigBaseCommand { /** @@ -18,35 +15,21 @@ class ConfigSetCommand extends ConfigBaseCommand { flags, config, ) { - if (optionValue === 'null') { - // eslint-disable-next-line no-param-reassign - optionValue = null; - } - // check for existence config.get(optionPath); - const configSchema = await $RefParser.dereference(configJsonSchema); - const schema = getPropertyDefinitionByPath(configSchema, optionPath); - - if (!schema) { - throw new Error(`Could not find schema for option path ${optionPath}`); - } - - let schemaType = schema.type; + let value; - if (Array.isArray(schemaType)) { - [schemaType] = schemaType; + try { + value = JSON.parse(optionValue); + } catch (e) { + value = optionValue; } - if (schemaType === 'object' || schemaType === 'array') { - config.set(optionPath, JSON.parse(optionValue)); - } else { - config.set(optionPath, optionValue); - } + config.set(optionPath, value); // eslint-disable-next-line no-console - console.log(`${optionPath} set to ${JSON.stringify(config.get(optionPath))}`); + console.log(`${optionPath} set to ${optionValue}`); } } diff --git a/packages/dashmate/src/config/errors/InvalidOptionError.js b/packages/dashmate/src/config/errors/InvalidOptionError.js index 77f8578708b..f1292b2eef3 100644 --- a/packages/dashmate/src/config/errors/InvalidOptionError.js +++ b/packages/dashmate/src/config/errors/InvalidOptionError.js @@ -1,6 +1,6 @@ const AbstractError = require('../../errors/AbstractError'); -class InvalidOptionPathError extends AbstractError { +class InvalidOptionError extends AbstractError { /** * @param {string} path * @param {*} value @@ -37,4 +37,4 @@ class InvalidOptionPathError extends AbstractError { } } -module.exports = InvalidOptionPathError; +module.exports = InvalidOptionError; diff --git a/packages/dashmate/test/unit/commands/config/set.spec.js b/packages/dashmate/test/unit/commands/config/set.spec.js index 1de944ce507..2ae58255ff1 100644 --- a/packages/dashmate/test/unit/commands/config/set.spec.js +++ b/packages/dashmate/test/unit/commands/config/set.spec.js @@ -3,6 +3,7 @@ const Config = require('../../../../src/config/Config'); const baseConfig = require('../../../../configs/system/base'); describe('Set Command Status', () => { + const flags = {}; let config; beforeEach(async () => { @@ -10,61 +11,86 @@ describe('Set Command Status', () => { }); describe('#platform', () => { - it('should just work', async () => { + it('should allow setting strings', async () => { const command = new ConfigSetCommand(); - const flags = {}; - await command.runWithDependencies({ option: 'core.docker.image', value: 'fake_image' }, flags, config); }); - it('should throw on unknown path', async () => { + it('should allow setting null', async () => { const command = new ConfigSetCommand(); - const flags = {}; + await command.runWithDependencies({ option: 'description', value: null }, flags, config); - // invalid path - try { - await command.runWithDependencies({ option: 'fakePath', value: 'fake' }, flags, config); + expect(config.get('description')).to.equal(null); - expect.fail('should throw error'); - } catch (e) { - expect(e.name).to.equal('InvalidOptionPathError'); - } + await command.runWithDependencies({ option: 'description', value: 'null' }, flags, config); + + expect(config.get('description')).to.equal(null); + }); + + it('should allow setting numbers', async () => { + const command = new ConfigSetCommand(); + + await command.runWithDependencies({ option: 'platform.drive.abci.validatorSet.llmqType', value: 107 }, flags, config); + + expect(config.get('platform.drive.abci.validatorSet.llmqType')).to.equal(107); + + await command.runWithDependencies({ option: 'platform.drive.abci.validatorSet.llmqType', value: '107' }, flags, config); + + expect(config.get('platform.drive.abci.validatorSet.llmqType')).to.equal(107); + }); + + it('should allow setting booleans', async () => { + const command = new ConfigSetCommand(); + + await command.runWithDependencies({ option: 'dashmate.helper.api.enable', value: 'true' }, flags, config); + + expect(config.get('dashmate.helper.api.enable')).to.equal(true); + + await command.runWithDependencies({ option: 'dashmate.helper.api.enable', value: true }, flags, config); + + expect(config.get('dashmate.helper.api.enable')).to.equal(true); }); it('should allow setting array of values', async () => { const command = new ConfigSetCommand(); - const flags = {}; + await command.runWithDependencies({ option: 'core.rpc.allowIps', value: '["1337", "36484"]' }, flags, config); - // invalid json - try { - await command.runWithDependencies({ option: 'core.rpc.allowIps', value: 'fake_image' }, flags, config); + expect(config.get('core.rpc.allowIps')).to.deep.equal(['1337', '36484']); + }); - expect.fail('should throw error'); - } catch (e) { - expect(e.name).to.equal('SyntaxError'); - } + it('should allow replacing part of the json', async () => { + const command = new ConfigSetCommand(); + + await command.runWithDependencies({ option: 'docker', value: '{"network":{"subnet":"127.0.0.1/24"}}' }, flags, config); + }); + + it('should throw on unknown path', async () => { + const command = new ConfigSetCommand(); - // type mismatch + // invalid path try { - await command.runWithDependencies({ option: 'core.rpc.allowIps', value: [1337, 36484] }, flags, config); + await command.runWithDependencies({ option: 'fakePath', value: 'fake' }, flags, config); expect.fail('should throw error'); } catch (e) { - expect(e.name).to.equal('SyntaxError'); + expect(e.name).to.equal('InvalidOptionPathError'); } - - await command.runWithDependencies({ option: 'core.rpc.allowIps', value: '["1337", "36484"]' }, flags, config); }); - it('should allow replacing part of the json', async () => { + it('should throw if invalid json is passed', async () => { const command = new ConfigSetCommand(); - const flags = {}; + // invalid json + try { + await command.runWithDependencies({ option: 'core.rpc.allowIps', value: 'fake_image' }, flags, config); - await command.runWithDependencies({ option: 'docker', value: '{"network":{"subnet":"127.0.0.1/24"}}' }, flags, config); + expect.fail('should throw error'); + } catch (e) { + expect(e.name).to.equal('InvalidOptionError'); + } }); }); }); diff --git a/yarn.lock b/yarn.lock index ebd8eeaaaba..6f2f8d54052 100644 --- a/yarn.lock +++ b/yarn.lock @@ -6363,7 +6363,6 @@ __metadata: version: 0.0.0-use.local resolution: "dashmate@workspace:packages/dashmate" dependencies: - "@apidevtools/json-schema-ref-parser": ^8.0.0 "@dashevo/bls": ~1.2.9 "@dashevo/dashcore-lib": ~0.20.5 "@dashevo/dashd-rpc": ^18.2.0 From 7172c684b1d56916879925bf47be37d2f744179c Mon Sep 17 00:00:00 2001 From: pshenmic Date: Tue, 6 Jun 2023 17:21:27 +0700 Subject: [PATCH 16/18] feat(dashmate): add type mismatch case --- .../test/unit/commands/config/set.spec.js | 62 +++++++++++++++---- 1 file changed, 51 insertions(+), 11 deletions(-) diff --git a/packages/dashmate/test/unit/commands/config/set.spec.js b/packages/dashmate/test/unit/commands/config/set.spec.js index 2ae58255ff1..27d953fa5f1 100644 --- a/packages/dashmate/test/unit/commands/config/set.spec.js +++ b/packages/dashmate/test/unit/commands/config/set.spec.js @@ -14,17 +14,23 @@ describe('Set Command Status', () => { it('should allow setting strings', async () => { const command = new ConfigSetCommand(); - await command.runWithDependencies({ option: 'core.docker.image', value: 'fake_image' }, flags, config); + await command.runWithDependencies({ + option: 'core.docker.image', value: 'fake_image', + }, flags, config); }); it('should allow setting null', async () => { const command = new ConfigSetCommand(); - await command.runWithDependencies({ option: 'description', value: null }, flags, config); + await command.runWithDependencies({ + option: 'description', value: null, + }, flags, config); expect(config.get('description')).to.equal(null); - await command.runWithDependencies({ option: 'description', value: 'null' }, flags, config); + await command.runWithDependencies({ + option: 'description', value: 'null', + }, flags, config); expect(config.get('description')).to.equal(null); }); @@ -32,11 +38,17 @@ describe('Set Command Status', () => { it('should allow setting numbers', async () => { const command = new ConfigSetCommand(); - await command.runWithDependencies({ option: 'platform.drive.abci.validatorSet.llmqType', value: 107 }, flags, config); + await command.runWithDependencies({ + option: 'platform.drive.abci.validatorSet.llmqType', + value: 107, + }, flags, config); expect(config.get('platform.drive.abci.validatorSet.llmqType')).to.equal(107); - await command.runWithDependencies({ option: 'platform.drive.abci.validatorSet.llmqType', value: '107' }, flags, config); + await command.runWithDependencies({ + option: 'platform.drive.abci.validatorSet.llmqType', + value: '107', + }, flags, config); expect(config.get('platform.drive.abci.validatorSet.llmqType')).to.equal(107); }); @@ -44,11 +56,15 @@ describe('Set Command Status', () => { it('should allow setting booleans', async () => { const command = new ConfigSetCommand(); - await command.runWithDependencies({ option: 'dashmate.helper.api.enable', value: 'true' }, flags, config); + await command.runWithDependencies({ + option: 'dashmate.helper.api.enable', value: 'true', + }, flags, config); expect(config.get('dashmate.helper.api.enable')).to.equal(true); - await command.runWithDependencies({ option: 'dashmate.helper.api.enable', value: true }, flags, config); + await command.runWithDependencies({ + option: 'dashmate.helper.api.enable', value: true, + }, flags, config); expect(config.get('dashmate.helper.api.enable')).to.equal(true); }); @@ -56,7 +72,9 @@ describe('Set Command Status', () => { it('should allow setting array of values', async () => { const command = new ConfigSetCommand(); - await command.runWithDependencies({ option: 'core.rpc.allowIps', value: '["1337", "36484"]' }, flags, config); + await command.runWithDependencies({ + option: 'core.rpc.allowIps', value: '["1337", "36484"]', + }, flags, config); expect(config.get('core.rpc.allowIps')).to.deep.equal(['1337', '36484']); }); @@ -64,7 +82,10 @@ describe('Set Command Status', () => { it('should allow replacing part of the json', async () => { const command = new ConfigSetCommand(); - await command.runWithDependencies({ option: 'docker', value: '{"network":{"subnet":"127.0.0.1/24"}}' }, flags, config); + await command.runWithDependencies({ + option: 'docker', + value: '{"network":{"subnet":"127.0.0.1/24"}}', + }, flags, config); }); it('should throw on unknown path', async () => { @@ -72,7 +93,9 @@ describe('Set Command Status', () => { // invalid path try { - await command.runWithDependencies({ option: 'fakePath', value: 'fake' }, flags, config); + await command.runWithDependencies({ + option: 'fakePath', value: 'fake', + }, flags, config); expect.fail('should throw error'); } catch (e) { @@ -85,7 +108,24 @@ describe('Set Command Status', () => { // invalid json try { - await command.runWithDependencies({ option: 'core.rpc.allowIps', value: 'fake_image' }, flags, config); + await command.runWithDependencies({ + option: 'core.rpc.allowIps', value: 'fake_image', + }, flags, config); + + expect.fail('should throw error'); + } catch (e) { + expect(e.name).to.equal('InvalidOptionError'); + } + }); + + it('should throw on type mismatch', async () => { + const command = new ConfigSetCommand(); + + // invalid json + try { + await command.runWithDependencies({ + option: 'dashmate.helper.api.enable', value: 120, + }, flags, config); expect.fail('should throw error'); } catch (e) { From 035ed89a8b11723c54d1eb4e7e52447a2494c198 Mon Sep 17 00:00:00 2001 From: pshenmic Date: Tue, 6 Jun 2023 18:52:21 +0700 Subject: [PATCH 17/18] chore(dashmate): remove unused --- .../src/util/getPropertyDefinitionByPath.js | 47 ------------------- 1 file changed, 47 deletions(-) delete mode 100644 packages/dashmate/src/util/getPropertyDefinitionByPath.js diff --git a/packages/dashmate/src/util/getPropertyDefinitionByPath.js b/packages/dashmate/src/util/getPropertyDefinitionByPath.js deleted file mode 100644 index 0409c0a5a55..00000000000 --- a/packages/dashmate/src/util/getPropertyDefinitionByPath.js +++ /dev/null @@ -1,47 +0,0 @@ -/** - * Get user property definition - * - * @typeof {getPropertyDefinitionByPath} - * @param {Object} documentDefinition - * @param {string} path - * - * @return {Object} - */ -function getPropertyDefinitionByPath(documentDefinition, path) { - const [currentSegment, ...rest] = path.split('.'); - - const { [currentSegment]: propertyDefinition } = (documentDefinition.properties || {}); - - // nothing found return nothing - if (!propertyDefinition) { - return undefined; - } - - // if there is nothing to lookup for next - // return currently found property definition - if (rest.length === 0) { - return propertyDefinition; - } - - const { type } = propertyDefinition; - - if (type === 'array') { - const { items: itemsDefinition } = propertyDefinition; - - if (itemsDefinition.type === 'object') { - return getPropertyDefinitionByPath(itemsDefinition, rest.join('.')); - } - } - - if (type === 'object') { - // rince and repeat - return getPropertyDefinitionByPath(propertyDefinition, rest.join('.')); - } - - // the `rest` is not empty - // but definition is not an object nor array - // nothing to lookup for - return undefined; -} - -module.exports = getPropertyDefinitionByPath; From 9c7ad7851cb9cae186d2d676fa03471677f55002 Mon Sep 17 00:00:00 2001 From: pshenmic Date: Tue, 6 Jun 2023 18:54:41 +0700 Subject: [PATCH 18/18] chore(dashmate): fix test desc --- packages/dashmate/test/unit/commands/config/set.spec.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/dashmate/test/unit/commands/config/set.spec.js b/packages/dashmate/test/unit/commands/config/set.spec.js index 27d953fa5f1..6e95dc01309 100644 --- a/packages/dashmate/test/unit/commands/config/set.spec.js +++ b/packages/dashmate/test/unit/commands/config/set.spec.js @@ -2,7 +2,7 @@ const ConfigSetCommand = require('../../../../src/commands/config/set'); const Config = require('../../../../src/config/Config'); const baseConfig = require('../../../../configs/system/base'); -describe('Set Command Status', () => { +describe('Config set command', () => { const flags = {}; let config;