Parcourir la source

Merge pull request #2908 from Lezek123/giza-qn-bucket-ids

Giza query node: Support new distribution bucket ids
shamil-gadelshin il y a 3 ans
Parent
commit
1dc84c819b

+ 39 - 24
query-node/mappings/storage/index.ts

@@ -38,6 +38,9 @@ import {
   getDynamicBag,
   getDistributionBucketFamilyWithMetadata,
   getDistributionBucketOperatorWithMetadata,
+  distributionBucketId,
+  distributionOperatorId,
+  distributionBucketIdByFamilyAndIndex,
 } from './utils'
 
 // STORAGE BUCKETS
@@ -201,7 +204,7 @@ export async function storage_DynamicBagCreated({ event, store }: EventContext &
     owner: getDynamicBagOwner(bagId),
     storageBuckets: Array.from(storageBucketIdsSet).map((id) => new StorageBucket({ id: id.toString() })),
     distributionBuckets: Array.from(distributionBucketIdsSet).map(
-      (id) => new DistributionBucket({ id: id.toString() })
+      (id) => new DistributionBucket({ id: distributionBucketId(id) })
     ),
   })
   await store.save<StorageBag>(storageBag)
@@ -295,7 +298,8 @@ export async function storage_DistributionBucketCreated({ event, store }: EventC
 
   const family = await getById(store, DistributionBucketFamily, familyId.toString())
   const bucket = new DistributionBucket({
-    id: bucketId.toString(),
+    id: distributionBucketId(bucketId),
+    bucketIndex: bucketId.distribution_bucket_index.toNumber(),
     acceptingNewBags: acceptingNewBags.valueOf(),
     distributing: true, // Runtime default
     family,
@@ -308,28 +312,30 @@ export async function storage_DistributionBucketStatusUpdated({
   event,
   store,
 }: EventContext & StoreContext): Promise<void> {
-  const [, bucketId, acceptingNewBags] = new Storage.DistributionBucketStatusUpdatedEvent(event).params
+  const [bucketId, acceptingNewBags] = new Storage.DistributionBucketStatusUpdatedEvent(event).params
 
-  const bucket = await getById(store, DistributionBucket, bucketId.toString())
+  const bucket = await getById(store, DistributionBucket, distributionBucketId(bucketId))
   bucket.acceptingNewBags = acceptingNewBags.valueOf()
 
   await store.save<DistributionBucket>(bucket)
 }
 
 export async function storage_DistributionBucketDeleted({ event, store }: EventContext & StoreContext): Promise<void> {
-  const [, bucketId] = new Storage.DistributionBucketDeletedEvent(event).params
+  const [bucketId] = new Storage.DistributionBucketDeletedEvent(event).params
   // TODO: Cascade remove on db level (would require changes in Hydra / comitting autogenerated files)
   const distributionBucket = await store.get(DistributionBucket, {
-    where: { id: bucketId.toString() },
+    where: { id: distributionBucketId(bucketId) },
     relations: ['bags', 'bags.distributionBuckets'],
   })
   if (!distributionBucket) {
-    inconsistentState(`Distribution bucket by id ${bucketId.toString()} not found!`)
+    inconsistentState(`Distribution bucket by id ${distributionBucketId(bucketId)} not found!`)
   }
   // Remove relations
   await Promise.all(
     (distributionBucket.bags || []).map((bag) => {
-      bag.distributionBuckets = (bag.distributionBuckets || []).filter((bucket) => bucket.id !== bucketId.toString())
+      bag.distributionBuckets = (bag.distributionBuckets || []).filter(
+        (bucket) => bucket.id !== distributionBucketId(bucketId)
+      )
       return store.save<StorageBag>(bag)
     })
   )
@@ -340,11 +346,20 @@ export async function storage_DistributionBucketsUpdatedForBag({
   event,
   store,
 }: EventContext & StoreContext): Promise<void> {
-  const [bagId, , addedBucketsSet, removedBucketsSet] = new Storage.DistributionBucketsUpdatedForBagEvent(event).params
+  const [
+    bagId,
+    familyId,
+    addedBucketsIndices,
+    removedBucketsIndices,
+  ] = new Storage.DistributionBucketsUpdatedForBagEvent(event).params
   // Get or create bag
   const storageBag = await getBag(store, bagId, ['distributionBuckets'])
-  const removedBucketsIds = Array.from(removedBucketsSet).map((id) => id.toString())
-  const addedBucketsIds = Array.from(addedBucketsSet).map((id) => id.toString())
+  const removedBucketsIds = Array.from(removedBucketsIndices).map((bucketIndex) =>
+    distributionBucketIdByFamilyAndIndex(familyId, bucketIndex)
+  )
+  const addedBucketsIds = Array.from(addedBucketsIndices).map((bucketIndex) =>
+    distributionBucketIdByFamilyAndIndex(familyId, bucketIndex)
+  )
   storageBag.distributionBuckets = (storageBag.distributionBuckets || [])
     .filter((bucket) => !removedBucketsIds.includes(bucket.id))
     .concat(addedBucketsIds.map((id) => new DistributionBucket({ id })))
@@ -355,9 +370,9 @@ export async function storage_DistributionBucketModeUpdated({
   event,
   store,
 }: EventContext & StoreContext): Promise<void> {
-  const [, bucketId, distributing] = new Storage.DistributionBucketModeUpdatedEvent(event).params
+  const [bucketId, distributing] = new Storage.DistributionBucketModeUpdatedEvent(event).params
 
-  const bucket = await getById(store, DistributionBucket, bucketId.toString())
+  const bucket = await getById(store, DistributionBucket, distributionBucketId(bucketId))
   bucket.distributing = distributing.valueOf()
 
   await store.save<DistributionBucket>(bucket)
@@ -367,11 +382,11 @@ export async function storage_DistributionBucketOperatorInvited({
   event,
   store,
 }: EventContext & StoreContext): Promise<void> {
-  const [, bucketId, workerId] = new Storage.DistributionBucketOperatorInvitedEvent(event).params
+  const [bucketId, workerId] = new Storage.DistributionBucketOperatorInvitedEvent(event).params
 
-  const bucket = await getById(store, DistributionBucket, bucketId.toString())
+  const bucket = await getById(store, DistributionBucket, distributionBucketId(bucketId))
   const invitedOperator = new DistributionBucketOperator({
-    id: `${bucketId}-${workerId}`,
+    id: distributionOperatorId(bucketId, workerId),
     distributionBucket: bucket,
     status: DistributionBucketOperatorStatus.INVITED,
     workerId: workerId.toNumber(),
@@ -384,9 +399,9 @@ export async function storage_DistributionBucketInvitationCancelled({
   event,
   store,
 }: EventContext & StoreContext): Promise<void> {
-  const [, bucketId, workerId] = new Storage.DistributionBucketOperatorInvitedEvent(event).params
+  const [bucketId, workerId] = new Storage.DistributionBucketOperatorInvitedEvent(event).params
 
-  const invitedOperator = await getById(store, DistributionBucketOperator, `${bucketId}-${workerId}`)
+  const invitedOperator = await getById(store, DistributionBucketOperator, distributionOperatorId(bucketId, workerId))
 
   await store.remove<DistributionBucketOperator>(invitedOperator)
 }
@@ -395,9 +410,9 @@ export async function storage_DistributionBucketInvitationAccepted({
   event,
   store,
 }: EventContext & StoreContext): Promise<void> {
-  const [workerId, , bucketId] = new Storage.DistributionBucketInvitationAcceptedEvent(event).params
+  const [workerId, bucketId] = new Storage.DistributionBucketInvitationAcceptedEvent(event).params
 
-  const invitedOperator = await getById(store, DistributionBucketOperator, `${bucketId}-${workerId}`)
+  const invitedOperator = await getById(store, DistributionBucketOperator, distributionOperatorId(bucketId, workerId))
   invitedOperator.status = DistributionBucketOperatorStatus.ACTIVE
 
   await store.save<DistributionBucketOperator>(invitedOperator)
@@ -407,9 +422,9 @@ export async function storage_DistributionBucketMetadataSet({
   event,
   store,
 }: EventContext & StoreContext): Promise<void> {
-  const [workerId, , bucketId, metadataBytes] = new Storage.DistributionBucketMetadataSetEvent(event).params
+  const [workerId, bucketId, metadataBytes] = new Storage.DistributionBucketMetadataSetEvent(event).params
 
-  const operator = await getDistributionBucketOperatorWithMetadata(store, `${bucketId}-${workerId}`)
+  const operator = await getDistributionBucketOperatorWithMetadata(store, distributionOperatorId(bucketId, workerId))
   operator.metadata = await processDistributionOperatorMetadata(store, operator.metadata, metadataBytes)
 
   await store.save<DistributionBucketOperator>(operator)
@@ -419,11 +434,11 @@ export async function storage_DistributionBucketOperatorRemoved({
   event,
   store,
 }: EventContext & StoreContext): Promise<void> {
-  const [, bucketId, workerId] = new Storage.DistributionBucketOperatorRemovedEvent(event).params
+  const [bucketId, workerId] = new Storage.DistributionBucketOperatorRemovedEvent(event).params
 
   // TODO: Cascade remove on db level (would require changes in Hydra / comitting autogenerated files)
 
-  const operator = await getDistributionBucketOperatorWithMetadata(store, `${bucketId}-${workerId}`)
+  const operator = await getDistributionBucketOperatorWithMetadata(store, distributionOperatorId(bucketId, workerId))
   await store.remove<DistributionBucketOperator>(operator)
   if (operator.metadata) {
     await store.remove<DistributionBucketOperatorMetadata>(operator.metadata)

+ 28 - 3
query-node/mappings/storage/utils.ts

@@ -23,7 +23,16 @@ import { unsetAssetRelations } from '../content/utils'
 
 import { BTreeSet } from '@polkadot/types'
 import _ from 'lodash'
-import { DataObjectId, BagId, DynamicBagId, StaticBagId } from '@joystream/types/augment/all'
+import {
+  DataObjectId,
+  BagId,
+  DynamicBagId,
+  StaticBagId,
+  DistributionBucketId,
+  DistributionBucketFamilyId,
+  DistributionBucketIndex,
+  WorkerId,
+} from '@joystream/types/augment/all'
 import { Balance } from '@polkadot/types/interfaces'
 
 export async function getDataObjectsInBag(
@@ -60,7 +69,7 @@ export function getStaticBagOwner(bagId: StaticBagId): typeof StorageBagOwner {
   }
 }
 
-export function getDynamicBagOwner(bagId: DynamicBagId) {
+export function getDynamicBagOwner(bagId: DynamicBagId): typeof StorageBagOwner {
   if (bagId.isChannel) {
     const owner = new StorageBagOwnerChannel()
     owner.channelId = bagId.asChannel.toNumber()
@@ -94,7 +103,7 @@ export function getDynamicBagId(bagId: DynamicBagId): string {
   }
 }
 
-export function getBagId(bagId: BagId) {
+export function getBagId(bagId: BagId): string {
   return bagId.isStatic ? getStaticBagId(bagId.asStatic) : getDynamicBagId(bagId.asDynamic)
 }
 
@@ -239,3 +248,19 @@ export async function removeDataObject(store: DatabaseManager, object: StorageDa
   await unsetAssetRelations(store, object)
   await store.remove<StorageDataObject>(object)
 }
+
+export function distributionBucketId(runtimeBucketId: DistributionBucketId): string {
+  const { distribution_bucket_family_id: familyId, distribution_bucket_index: bucketIndex } = runtimeBucketId
+  return distributionBucketIdByFamilyAndIndex(familyId, bucketIndex)
+}
+
+export function distributionBucketIdByFamilyAndIndex(
+  familyId: DistributionBucketFamilyId,
+  bucketIndex: DistributionBucketIndex
+): string {
+  return `${familyId.toString()}:${bucketIndex.toString()}`
+}
+
+export function distributionOperatorId(bucketId: DistributionBucketId, workerId: WorkerId): string {
+  return `${distributionBucketId(bucketId)}-${workerId.toString()}`
+}

+ 4 - 1
query-node/schemas/storage.graphql

@@ -273,12 +273,15 @@ type DistributionBucketOperator @entity {
 }
 
 type DistributionBucket @entity {
-  "Runtime bucket id"
+  "Runtime bucket id in {familyId}:{bucketIndex} format"
   id: ID!
 
   "Distribution family the bucket is part of"
   family: DistributionBucketFamily!
 
+  "Bucket index within the family"
+  bucketIndex: Int!
+
   "Distribution bucket operators (either active or invited)"
   operators: [DistributionBucketOperator!] @derivedFrom(field: "distributionBucket")