Skip to content

Commit

Permalink
feat(phase): register validator uptime points on phase ended
Browse files Browse the repository at this point in the history
  • Loading branch information
bdeneux committed Dec 21, 2022
1 parent 7b3013b commit 1b2f16f
Show file tree
Hide file tree
Showing 2 changed files with 101 additions and 1 deletion.
7 changes: 6 additions & 1 deletion app/actor/subscription/actor.go
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,12 @@ func (a *Actor) handleNewTweetEvent(when time.Time, data map[string]interface{})
}

func (a *Actor) handlePhaseEnded(phase *nemeton.Phase) {
// TODO: update validator uptime
err := a.store.CompleteValidatorsUptimeForPhase(a.ctx, phase)
if err != nil {
log.Panic().Err(err).Msg("❌⏱️ An error occurs fetch uptime validators.")
return
}
log.Info().Int("phaseNumber", phase.Number).Msg("✅ Uptime points for phase has been set.")
}

func (a *Actor) handlePhaseStarted(phase *nemeton.Phase) {
Expand Down
95 changes: 95 additions & 0 deletions app/nemeton/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -447,3 +447,98 @@ func (s *Store) GetPreviousPhaseByBlock(ctx context.Context, height int64) (*Pha
var phase Phase
return &phase, res.Decode(&phase)
}

// CompleteValidatorsUptimeForPhase is used to concat all missed blocks on a given phase and calculate the number of
// points rewarded.
func (s *Store) CompleteValidatorsUptimeForPhase(ctx context.Context, phase *Phase) error {
_, err := s.db.Collection(validatorsCollectionName).Aggregate(ctx, bson.A{
bson.M{"$lookup": bson.M{
"from": "phases",
"pipeline": bson.A{
bson.M{"$match": bson.M{"_id": phase.Number}},
bson.M{"$project": bson.M{"blocks": 1}},
},
"as": "currentPhase",
}},
bson.M{"$unwind": bson.M{"path": "$missedBlocks", "preserveNullAndEmptyArrays": true}},
bson.M{"$unwind": bson.M{"path": "$currentPhase"}},
bson.M{"$replaceRoot": bson.M{
"newRoot": bson.M{
"count": bson.M{
"$switch": bson.M{
"branches": bson.A{
bson.M{
"case": bson.M{"$and": bson.A{bson.M{"$gte": bson.A{"$missedBlocks.from", "$currentPhase.blocks.from"}}, bson.M{"$lte": bson.A{"$missedBlocks.to", "$currentPhase.blocks.to"}}}},
"then": bson.M{"$subtract": bson.A{"$missedBlocks.to", "$missedBlocks.from"}},
},
bson.M{
"case": bson.M{"$and": bson.A{
bson.M{"$lt": bson.A{"$missedBlocks.from", "$currentPhase.blocks.from"}},
bson.M{"$lte": bson.A{"$missedBlocks.to", "$currentPhase.blocks.to"}},
bson.M{"$gte": bson.A{"$missedBlocks.to", "$currentPhase.blocks.from"}},
}},
"then": bson.M{"$subtract": bson.A{"$missedBlocks.to", "$currentPhase.blocks.from"}},
},
bson.M{
"case": bson.M{"$and": bson.A{
bson.M{"$gte": bson.A{"$missedBlocks.from", "$currentPhase.blocks.from"}},
bson.M{"$gt": bson.A{"$missedBlocks.to", "$currentPhase.blocks.to"}},
bson.M{"$lte": bson.A{"$missedBlocks.from", "$currentPhase.blocks.to"}},
}},
"then": bson.M{"$subtract": bson.A{"$currentPhase.blocks.to", "$missedBlocks.from"}},
},
},
"default": 0,
},
},
"currentPhase": "$currentPhase",
"validator": "$_id",
"points": "$points",
},
}},
bson.M{"$group": bson.M{
"_id": "$validator",
"totalMissedBlock": bson.M{"$sum": "$count"},
"phase": bson.M{"$mergeObjects": "$currentPhase"},
"points": bson.M{"$first": "$points"},
}},
bson.M{"$addFields": bson.M{
"uptime": bson.M{"$subtract": bson.A{
bson.M{
"$pow": bson.A{2501, bson.M{
"$multiply": bson.A{0.01, bson.M{
"$subtract": bson.A{100, bson.M{"$divide": bson.A{
bson.M{"$multiply": bson.A{100, "$totalMissedBlock"}},
bson.M{"$subtract": bson.A{"$phase.blocks.to", "$phase.blocks.from"}},
}}},
}},
}},
},
1,
}},
}},
bson.M{"$addFields": bson.M{
"newPoints": bson.M{"$add": bson.A{"$points", "$uptime"}},
}},
bson.M{"$merge": bson.M{
"into": "validators",
"on": "_id",
"let": bson.M{"uptime": "$uptime", "newPoints": "$newPoints"},
"whenMatched": bson.A{
bson.M{
"$set": bson.M{
"tasks.1.3.points": "$$uptime",
"tasks.1.3.completed": true,
"points": "$$newPoints",
},
},
},
"whenNotMatched": "discard",
}},
})
if err != nil {
return err
}

return nil
}

0 comments on commit 1b2f16f

Please sign in to comment.