Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: update validator nonces in endblocker #1292

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions x/skyway/abci.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,12 @@ import (
"github.com/palomachain/paloma/x/skyway/types"
)

const updateValidatorNoncesPeriod = 50

// EndBlocker is called at the end of every block
func EndBlocker(ctx context.Context, k keeper.Keeper, cc *libcons.ConsensusChecker) {
sdkCtx := sdk.UnwrapSDKContext(ctx)

logger := liblog.FromKeeper(ctx, k).WithComponent("skyway-endblocker")
defer func() {
if r := recover(); r != nil {
Expand All @@ -42,6 +46,17 @@ func EndBlocker(ctx context.Context, k keeper.Keeper, cc *libcons.ConsensusCheck
if err != nil {
logger.WithError(err).Warn("Failed to prune attestations.")
}

if sdkCtx.BlockHeight()%updateValidatorNoncesPeriod == 0 {
// Update all validator nonces to the latest observed nonce, if
// suitable This makes sure validators don't get stuck waiting for
// events they can't get from the RPC, even though these events are
// already observed
err = k.UpdateValidatorNoncesToLatest(ctx, v)
if err != nil {
logger.WithError(err).Warn("Failed to update validator nonces.")
}
}
}

err = processGasEstimates(ctx, k, cc)
Expand Down
7 changes: 0 additions & 7 deletions x/skyway/keeper/attestation.go
Original file line number Diff line number Diff line change
Expand Up @@ -165,13 +165,6 @@ 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
19 changes: 12 additions & 7 deletions x/skyway/keeper/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -481,21 +481,26 @@ func (k Keeper) overrideNonce(ctx context.Context, chainReferenceId string, nonc
return nil
}

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

lastSkywayNonce, err := k.GetLastObservedSkywayNonce(ctx, chainReferenceId)
if err != nil {
logger.WithError(err).Warn("Failed to get last observed nonce.")
return err
}

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))
err = k.IterateValidatorLastEventNonces(ctx, chainReferenceId, func(key []byte, nonce uint64) bool {
if lastSkywayNonce > nonce {
prefixStore.Set(key, types.UInt64Bytes(lastSkywayNonce))
}

return false
Expand Down
Loading