Skip to content

Commit

Permalink
chore: update skyway validator nonces
Browse files Browse the repository at this point in the history
When a claim is set to observed, update all validator nonces if they
have a lower last observed nonce.
  • Loading branch information
maharifu committed Sep 11, 2024
1 parent 1f93b67 commit c8d56d7
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 0 deletions.
7 changes: 7 additions & 0 deletions x/skyway/keeper/attestation.go
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,13 @@ func (k Keeper) TryAttestation(ctx context.Context, att *types.Attestation) erro
return err
}

// Update all validator nonces to the claim nonce that was just
// processed
err = k.updateValidatorNoncesIfHigher(ctx, claim.GetChainReferenceId(), claim.GetSkywayNonce())
if err != nil {
return err
}

break
}
}
Expand Down
28 changes: 28 additions & 0 deletions x/skyway/keeper/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -480,3 +480,31 @@ func (k Keeper) overrideNonce(ctx context.Context, chainReferenceId string, nonc
logger.Info("Updated last observed nonce successfully")
return nil
}

// updateValidatorNoncesIfHigher updates all validator nonces to `newNonce` only
// if it is higher than the current record
func (k Keeper) updateValidatorNoncesIfHigher(
ctx context.Context,
chainReferenceId string,
newNonce uint64,
) error {
logger := liblog.FromKeeper(ctx, k).WithComponent("update-validator-nonces-if-higher")

store := k.GetStore(ctx, chainReferenceId)
prefixStore := prefix.NewStore(store, types.LastEventNonceByValidatorKey)

err := k.IterateValidatorLastEventNonces(ctx, chainReferenceId, func(key []byte, nonce uint64) bool {
if newNonce > nonce {
prefixStore.Set(key, types.UInt64Bytes(newNonce))
}

return false
})
if err != nil {
logger.WithError(err).Warn("Failed to update validator skyway nonces")
return err
}

logger.Info("Updated validator nonces to highest successfully")
return nil
}

0 comments on commit c8d56d7

Please sign in to comment.