Browse Source

Store the blocks where the `ElectionRound` ends

Theophile Sandoz 3 years ago
parent
commit
d3eda01eb2
2 changed files with 19 additions and 6 deletions
  1. 10 6
      query-node/mappings/src/council.ts
  2. 9 0
      query-node/schemas/council.graphql

+ 10 - 6
query-node/mappings/src/council.ts

@@ -1,4 +1,4 @@
-import { EventContext, StoreContext, DatabaseManager } from '@joystream/hydra-common'
+import { EventContext, StoreContext, DatabaseManager, SubstrateEvent } from '@joystream/hydra-common'
 import { CURRENT_NETWORK, deserializeMetadata, genericEventFields } from './common'
 import { CURRENT_NETWORK, deserializeMetadata, genericEventFields } from './common'
 import BN from 'bn.js'
 import BN from 'bn.js'
 import { FindConditions, SelectQueryBuilder } from 'typeorm'
 import { FindConditions, SelectQueryBuilder } from 'typeorm'
@@ -222,12 +222,16 @@ async function updateCouncilStage(
 async function startNextElectionRound(
 async function startNextElectionRound(
   store: DatabaseManager,
   store: DatabaseManager,
   electedCouncil: ElectedCouncil,
   electedCouncil: ElectedCouncil,
-  blockNumber: number,
+  event: SubstrateEvent,
   electionProblem?: ElectionProblem
   electionProblem?: ElectionProblem
 ): Promise<ElectionRound> {
 ): Promise<ElectionRound> {
   // finish last election round
   // finish last election round
   const lastElectionRound = await getCurrentElectionRound(store)
   const lastElectionRound = await getCurrentElectionRound(store)
   lastElectionRound.isFinished = true
   lastElectionRound.isFinished = true
+  lastElectionRound.endedAtBlock = event.blockNumber
+  lastElectionRound.endedAtTime = new Date(event.blockTimestamp)
+  lastElectionRound.endedAtNetwork = CURRENT_NETWORK
+
   lastElectionRound.nextElectedCouncil = electedCouncil
   lastElectionRound.nextElectedCouncil = electedCouncil
 
 
   // save last election
   // save last election
@@ -249,7 +253,7 @@ async function startNextElectionRound(
 
 
   const stage = new CouncilStageAnnouncing()
   const stage = new CouncilStageAnnouncing()
   stage.candidatesCount = new BN(0)
   stage.candidatesCount = new BN(0)
-  await updateCouncilStage(store, stage, blockNumber, electionProblem)
+  await updateCouncilStage(store, stage, event.blockNumber, electionProblem)
 
 
   return electionRound
   return electionRound
 }
 }
@@ -307,7 +311,7 @@ export async function council_AnnouncingPeriodStarted({ event, store }: EventCon
 
 
   // restart elections
   // restart elections
   const electedCouncil = await getCurrentElectedCouncil(store)
   const electedCouncil = await getCurrentElectedCouncil(store)
-  await startNextElectionRound(store, electedCouncil, event.blockNumber)
+  await startNextElectionRound(store, electedCouncil, event)
 }
 }
 
 
 /*
 /*
@@ -328,7 +332,7 @@ export async function council_NotEnoughCandidates({ event, store }: EventContext
 
 
   // restart elections
   // restart elections
   const electedCouncil = await getCurrentElectedCouncil(store)
   const electedCouncil = await getCurrentElectedCouncil(store)
-  await startNextElectionRound(store, electedCouncil, event.blockNumber, ElectionProblem.NOT_ENOUGH_CANDIDATES)
+  await startNextElectionRound(store, electedCouncil, event, ElectionProblem.NOT_ENOUGH_CANDIDATES)
 }
 }
 
 
 /*
 /*
@@ -528,7 +532,7 @@ export async function council_NewCouncilNotElected({ event, store }: EventContex
 
 
   // restart elections
   // restart elections
   const electedCouncil = await getCurrentElectedCouncil(store)
   const electedCouncil = await getCurrentElectedCouncil(store)
-  await startNextElectionRound(store, electedCouncil, event.blockNumber, ElectionProblem.NEW_COUNCIL_NOT_ELECTED)
+  await startNextElectionRound(store, electedCouncil, event, ElectionProblem.NEW_COUNCIL_NOT_ELECTED)
 }
 }
 
 
 /*
 /*

+ 9 - 0
query-node/schemas/council.graphql

@@ -229,6 +229,15 @@ type ElectionRound @entity {
   "Sign if election has already finished."
   "Sign if election has already finished."
   isFinished: Boolean!
   isFinished: Boolean!
 
 
+  "Block number at which the election ended."
+  endedAtBlock: Int
+
+  "Time at which the election ended."
+  endedAtTime: DateTime
+
+  "Network running at the time the election ended."
+  endedAtNetwork: Network
+
   "Vote cast in the election round."
   "Vote cast in the election round."
   castVotes: [CastVote!]! @derivedFrom(field: "electionRound")
   castVotes: [CastVote!]! @derivedFrom(field: "electionRound")