diff --git a/x/skyway/keeper/attestation.go b/x/skyway/keeper/attestation.go index 2a78b04f..ff93e87e 100644 --- a/x/skyway/keeper/attestation.go +++ b/x/skyway/keeper/attestation.go @@ -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 } } diff --git a/x/skyway/keeper/keeper.go b/x/skyway/keeper/keeper.go index 8e9a3f1a..05f6377f 100644 --- a/x/skyway/keeper/keeper.go +++ b/x/skyway/keeper/keeper.go @@ -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 +}