Skip to content

Commit

Permalink
fix(register): ensure missedblocks propulated
Browse files Browse the repository at this point in the history
  • Loading branch information
amimart committed Dec 23, 2022
1 parent 148554c commit 2e3dc96
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 1 deletion.
11 changes: 11 additions & 0 deletions app/actor/subscription/actor.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package subscription

import (
"context"
"fmt"
"time"

"okp4/nemeton-leaderboard/app/util"
Expand All @@ -28,6 +29,7 @@ type Actor struct {
ctx context.Context
eventPID *actor.PID
offsetStore *offset.Store
lastHeight *int64
}

func NewSubscriber(mongoURI, dbName string, eventPID *actor.PID) (*Actor, error) {
Expand Down Expand Up @@ -117,6 +119,8 @@ func (a *Actor) handleNewBlockEvent(data map[string]interface{}) {

logger := log.With().Time("blockTime", e.Time).Int64("height", e.Height).Logger()
logger.Info().Msg("Handle NewBlock event")
a.lastHeight = &e.Height

consensusAddr := make([]types.ConsAddress, len(e.Signatures))
for i, signature := range e.Signatures {
consensusAddr[i] = signature.GetValidatorAddress()
Expand Down Expand Up @@ -184,6 +188,12 @@ func (a *Actor) handleValidatorRegisteredEvent(data map[string]interface{}) {
return
}

if a.lastHeight == nil {
log.Err(fmt.Errorf("doesn't have a last height")).
Interface("data", data).
Msg("🤕 Couldn't register validator")
}

if err := a.store.RegisterValidator(
context.Background(),
e.Valoper,
Expand All @@ -193,6 +203,7 @@ func (a *Actor) handleValidatorRegisteredEvent(data map[string]interface{}) {
e.Discord,
e.Country,
e.Twitter,
*a.lastHeight,
); err != nil {
log.Err(err).Interface("data", data).Msg("🤕 Couldn't register validator")
}
Expand Down
19 changes: 18 additions & 1 deletion app/nemeton/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -321,13 +321,30 @@ func (s *Store) RegisterValidator(
description stakingtypes.Description,
discord, country string,
twitter *string,
lastHeight int64,
) error {
validator, err := NewValidator(valoper, delegator, valcons, description, discord, country, twitter)
if err != nil {
return err
}

_, err = s.db.Collection(validatorsCollectionName).InsertOne(ctx, validator)
res, err := s.db.Collection(validatorsCollectionName).InsertOne(ctx, validator)
if err != nil {
return err
}

_, err = s.db.Collection(validatorsCollectionName).UpdateOne(
ctx,
bson.M{"_id": res.InsertedID},
bson.M{
"missedBlocks": bson.A{
bson.M{
"from": 1,
"to": lastHeight + 1,
},
},
},
)
return err
}

Expand Down

0 comments on commit 2e3dc96

Please sign in to comment.