Browse Source

query node - workers isActive sign addition

ondratra 3 years ago
parent
commit
a382f99e14

+ 3 - 2
query-node/mappings/src/common.ts

@@ -259,10 +259,11 @@ export function getWorkingGroupModuleName(group: WorkingGroup): WorkingGroupModu
 export async function getWorker(
   store: DatabaseManager,
   groupName: WorkingGroupModuleName,
-  runtimeId: WorkerId | number
+  runtimeId: WorkerId | number,
+  relations: string[] = []
 ): Promise<Worker> {
   const workerDbId = `${groupName}-${runtimeId}`
-  const worker = await store.get(Worker, { where: { id: workerDbId } })
+  const worker = await store.get(Worker, { where: { id: workerDbId }, relations })
   if (!worker) {
     inconsistentState(`Expected worker not found by id ${workerDbId}`)
   }

+ 15 - 3
query-node/mappings/src/workingGroups.ts

@@ -338,7 +338,7 @@ async function handleWorkingGroupMetadataAction(
 async function handleTerminatedWorker({ store, event }: EventContext & StoreContext): Promise<void> {
   const [workerId, optPenalty, optRationale] = new WorkingGroups.TerminatedWorkerEvent(event).params
   const group = await getWorkingGroup(store, event)
-  const worker = await getWorker(store, group.name as WorkingGroupModuleName, workerId)
+  const worker = await getWorker(store, group.name as WorkingGroupModuleName, workerId, ['application'])
   const eventTime = new Date(event.blockTimestamp)
 
   const EventConstructor = worker.isLead ? TerminatedLeaderEvent : TerminatedWorkerEvent
@@ -359,6 +359,7 @@ async function handleTerminatedWorker({ store, event }: EventContext & StoreCont
   worker.stake = new BN(0)
   worker.rewardPerBlock = new BN(0)
   worker.updatedAt = eventTime
+  worker.isActive = isWorkerActive(worker)
 
   await store.save<Worker>(worker)
 }
@@ -373,6 +374,14 @@ export async function findLeaderSetEventByTxHash(store: DatabaseManager, txHash?
   return leaderSetEvent
 }
 
+// expects `worker.application` to be available
+function isWorkerActive(worker: Worker): boolean {
+  return (
+    worker.application.status.isTypeOf === 'ApplicationStatusAccepted' &&
+    worker.status.isTypeOf === 'WorkerStatusActive'
+  )
+}
+
 // Mapping functions
 export async function workingGroups_OpeningAdded({ store, event }: EventContext & StoreContext): Promise<void> {
   const [
@@ -536,6 +545,7 @@ export async function workingGroups_OpeningFilled({ store, event }: EventContext
               entry: openingFilledEvent,
               rewardPerBlock: opening.rewardPerBlock,
             })
+            worker.isActive = isWorkerActive(worker)
             await store.save<Worker>(worker)
             return worker
           }
@@ -787,7 +797,7 @@ export async function workingGroups_NewMissedRewardLevelReached({
 export async function workingGroups_WorkerExited({ store, event }: EventContext & StoreContext): Promise<void> {
   const [workerId] = new WorkingGroups.WorkerExitedEvent(event).params
   const group = await getWorkingGroup(store, event)
-  const worker = await getWorker(store, group.name as WorkingGroupModuleName, workerId)
+  const worker = await getWorker(store, group.name as WorkingGroupModuleName, workerId, ['application'])
   const eventTime = new Date(event.blockTimestamp)
 
   const workerExitedEvent = new WorkerExitedEvent({
@@ -807,6 +817,7 @@ export async function workingGroups_WorkerExited({ store, event }: EventContext
   worker.rewardPerBlock = new BN(0)
   worker.missingRewardAmount = undefined
   worker.updatedAt = eventTime
+  worker.isActive = isWorkerActive(worker)
 
   await store.save<Worker>(worker)
 }
@@ -907,7 +918,7 @@ export async function workingGroups_StakeDecreased({ store, event }: EventContex
 export async function workingGroups_WorkerStartedLeaving({ store, event }: EventContext & StoreContext): Promise<void> {
   const [workerId, optRationale] = new WorkingGroups.WorkerStartedLeavingEvent(event).params
   const group = await getWorkingGroup(store, event)
-  const worker = await getWorker(store, group.name as WorkingGroupModuleName, workerId)
+  const worker = await getWorker(store, group.name as WorkingGroupModuleName, workerId, ['application'])
   const eventTime = new Date(event.blockTimestamp)
 
   const workerStartedLeavingEvent = new WorkerStartedLeavingEvent({
@@ -923,6 +934,7 @@ export async function workingGroups_WorkerStartedLeaving({ store, event }: Event
   status.workerStartedLeavingEventId = workerStartedLeavingEvent.id
   worker.status = status
   worker.updatedAt = eventTime
+  worker.isActive = isWorkerActive(worker)
 
   await store.save<Worker>(worker)
 }

+ 3 - 0
query-node/schemas/workingGroups.graphql

@@ -52,6 +52,9 @@ type Worker @entity {
   "Whether the worker is also the working group lead"
   isLead: Boolean!
 
+  "Whether the worker is currently active"
+  isActive: Boolean!
+
   "Current role stake (in JOY)"
   stake: BigInt!