소스 검색

New commands: remove-bucket-operator, set-bucket-family-metadata

Leszek Wiesner 3 년 전
부모
커밋
71ccd6f266

+ 16 - 0
distributor-node/scripts/data/family-metadata.json

@@ -0,0 +1,16 @@
+{
+  "region": "eu-west",
+  "description": "Western Europe",
+  "boundary": [
+    { "longitude": 0.935664253776034, "latitude": 61.70157919955392 },
+    { "longitude": 7.077063962609969, "latitude": 37.40179586925884 },
+    { "longitude": 27.46754964469303, "latitude": 32.88770433956931 },
+    { "longitude": 40.68423960078124, "latitude": 48.39367044189657 },
+    { "longitude": 32.14019766910849, "latitude": 54.63502471598309 },
+    { "longitude": 28.56450578831937, "latitude": 59.09093283322235 },
+    { "longitude": 30.75892533489921, "latitude": 70.1670216697313 },
+    { "longitude": 19.2385951319647, "latitude": 73.4978175093038 },
+    { "longitude": -9.158590783812665, "latitude": 67.80006125371919 },
+    { "longitude": 0.935664253776034, "latitude": 61.70157919955392 }
+  ]
+}

+ 0 - 0
distributor-node/scripts/example-metadata.json → distributor-node/scripts/data/operator-metadata.json


+ 5 - 3
distributor-node/scripts/test-commands.sh

@@ -9,7 +9,7 @@ CLI=../bin/run
 
 ${CLI} dev:init
 ${CLI} leader:set-buckets-per-bag-limit -l 10
-FAMILY_ID=`${CLI} leader:create-bucket-family ${CONFIG}`
+FAMILY_ID=`${CLI} leader:create-bucket-family`
 BUCKET_ID=`${CLI} leader:create-bucket -f ${FAMILY_ID} -a yes`
 ${CLI} leader:update-bag -b static:council -f ${FAMILY_ID} -a ${BUCKET_ID}
 ${CLI} leader:update-bucket-status -f ${FAMILY_ID} -B ${BUCKET_ID}  --acceptingBags yes
@@ -20,10 +20,12 @@ ${CLI} leader:invite-bucket-operator -f ${FAMILY_ID} -B ${BUCKET_ID} -w 0
 ${CLI} leader:cancel-invitation -f ${FAMILY_ID} -B ${BUCKET_ID} -w 0
 ${CLI} leader:invite-bucket-operator -f ${FAMILY_ID} -B ${BUCKET_ID} -w 0
 ${CLI} operator:accept-invitation -f ${FAMILY_ID} -B ${BUCKET_ID} -w 0
-${CLI} operator:set-metadata -f ${FAMILY_ID} -B ${BUCKET_ID} -w 0 -i ./example-metadata.json
+${CLI} operator:set-metadata -f ${FAMILY_ID} -B ${BUCKET_ID} -w 0 -i ./data/operator-metadata.json
+${CLI} leader:remove-bucket-operator -f ${FAMILY_ID} -B ${BUCKET_ID} -w 0
+${CLI} leader:set-bucket-family-metadata -f ${FAMILY_ID} -i ./data/family-metadata.json
 
 # Deletion commands tested separately, since bucket operator removal is not yet supported
-FAMILY_TO_DELETE_ID=`${CLI} leader:create-bucket-family ${CONFIG}`
+FAMILY_TO_DELETE_ID=`${CLI} leader:create-bucket-family`
 BUCKET_TO_DELETE_ID=`${CLI} leader:create-bucket -f ${FAMILY_TO_DELETE_ID} -a yes`
 ${CLI} leader:delete-bucket -f ${FAMILY_TO_DELETE_ID} -B ${BUCKET_TO_DELETE_ID}
 ${CLI} leader:delete-bucket-family -f ${FAMILY_TO_DELETE_ID}

+ 38 - 0
distributor-node/src/commands/leader/remove-bucket-operator.ts

@@ -0,0 +1,38 @@
+import AccountsCommandBase from '../../command-base/accounts'
+import DefaultCommandBase, { flags } from '../../command-base/default'
+
+export default class LeaderRemoveBucketOperator extends AccountsCommandBase {
+  static description = `Remove distribution bucket operator (distribution group worker).
+  Requires distribution working group leader permissions.`
+
+  static flags = {
+    bucketId: flags.integer({
+      char: 'B',
+      description: 'Distribution bucket id',
+      required: true,
+    }),
+    familyId: flags.integer({
+      char: 'f',
+      description: 'Distribution bucket family id',
+      required: true,
+    }),
+    workerId: flags.integer({
+      char: 'w',
+      description: 'ID of the operator (distribution working group worker) to remove from the bucket',
+      required: true,
+    }),
+    ...DefaultCommandBase.flags,
+  }
+
+  async run(): Promise<void> {
+    const { bucketId, familyId, workerId } = this.parse(LeaderRemoveBucketOperator).flags
+    const leadKey = await this.getDistributorLeadKey()
+
+    this.log(`Removing distribution bucket operator (bucket: ${bucketId}, worker: ${workerId})...`)
+    await this.sendAndFollowTx(
+      await this.getDecodedPair(leadKey),
+      this.api.tx.storage.removeDistributionBucketOperator(familyId, bucketId, workerId)
+    )
+    this.log('Bucket operator succesfully removed!')
+  }
+}

+ 45 - 0
distributor-node/src/commands/leader/set-bucket-family-metadata.ts

@@ -0,0 +1,45 @@
+import fs from 'fs'
+import AccountsCommandBase from '../../command-base/accounts'
+import DefaultCommandBase, { flags } from '../../command-base/default'
+import { ValidationService } from '../../services/validation/ValidationService'
+import { DistributionBucketFamilyMetadata, IDistributionBucketFamilyMetadata } from '@joystream/metadata-protobuf'
+
+export default class LeaderSetBucketFamilyMetadata extends AccountsCommandBase {
+  static description = `Set/update distribution bucket family metadata.
+  Requires distribution working group leader permissions.`
+
+  static flags = {
+    familyId: flags.integer({
+      char: 'f',
+      description: 'Distribution bucket family id',
+      required: true,
+    }),
+    input: flags.string({
+      char: 'i',
+      description: 'Path to JSON metadata file',
+      required: true,
+    }),
+    ...DefaultCommandBase.flags,
+  }
+
+  async run(): Promise<void> {
+    const { familyId, input } = this.parse(LeaderSetBucketFamilyMetadata).flags
+    const leadKey = await this.getDistributorLeadKey()
+
+    const validation = new ValidationService()
+    const metadata: IDistributionBucketFamilyMetadata = validation.validate(
+      'FamilyMetadata',
+      JSON.parse(fs.readFileSync(input).toString())
+    )
+
+    this.log(`Setting bucket family metadata (family: ${familyId})`, metadata)
+    await this.sendAndFollowTx(
+      await this.getDecodedPair(leadKey),
+      this.api.tx.storage.setDistributionBucketFamilyMetadata(
+        familyId,
+        '0x' + Buffer.from(DistributionBucketFamilyMetadata.encode(metadata).finish()).toString('hex')
+      )
+    )
+    this.log('Bucket family metadata succesfully set/updated!')
+  }
+}

+ 1 - 1
distributor-node/src/commands/operator/set-metadata.ts

@@ -21,7 +21,7 @@ export default class OperatorSetMetadata extends AccountsCommandBase {
     }),
     workerId: flags.integer({
       char: 'w',
-      description: 'ID of the invited operator (distribution group worker)',
+      description: 'ID of the operator (distribution group worker)',
       required: true,
     }),
     endpoint: flags.string({

+ 20 - 0
distributor-node/src/services/validation/schemas/familyMetadataSchema.ts

@@ -0,0 +1,20 @@
+import { JSONSchema4 } from 'json-schema'
+import { strictObject } from './utils'
+
+export const familyMetadataSchema: JSONSchema4 = {
+  type: 'object',
+  additionalProperties: false,
+  properties: {
+    region: { type: 'string' },
+    description: { type: 'string' },
+    boundary: {
+      type: 'array',
+      items: strictObject({
+        latitude: { type: 'number', minimum: -180, maximum: 180 },
+        longitude: { type: 'number', minimum: -180, maximum: 180 },
+      }),
+    },
+  },
+}
+
+export default familyMetadataSchema

+ 5 - 0
distributor-node/src/services/validation/schemas/index.ts

@@ -1,11 +1,14 @@
 import { ConfigJson } from '../../../types/generated/ConfigJson'
 import { OperatorMetadataJson } from '../../../types/generated/OperatorMetadataJson'
+import { FamilyMetadataJson } from '../../../types/generated/FamilyMetadataJson'
 import { configSchema } from './configSchema'
+import { familyMetadataSchema } from './familyMetadataSchema'
 import { operatorMetadataSchema } from './operatorMetadataSchema'
 
 export const schemas = {
   Config: configSchema,
   OperatorMetadata: operatorMetadataSchema,
+  FamilyMetadata: familyMetadataSchema,
 } as const
 
 export type SchemaKey = keyof typeof schemas & string
@@ -14,6 +17,8 @@ export type TypeBySchemaKey<T extends SchemaKey> = T extends 'Config'
   ? ConfigJson
   : T extends 'OperatorMetadata'
   ? OperatorMetadataJson
+  : T extends 'FamilyMetadata'
+  ? FamilyMetadataJson
   : never
 
 export default schemas

+ 15 - 0
distributor-node/src/types/generated/FamilyMetadataJson.d.ts

@@ -0,0 +1,15 @@
+/* tslint:disable */
+/**
+ * This file was automatically generated by json-schema-to-typescript.
+ * DO NOT MODIFY IT BY HAND. Instead, modify the source JSONSchema file,
+ * and run json-schema-to-typescript to regenerate this file.
+ */
+
+export interface FamilyMetadataJson {
+  region?: string
+  description?: string
+  boundary?: {
+    latitude: number
+    longitude: number
+  }[]
+}