From 9aa7151d3de0d59d78acf9d103fb5e75b1f88cf4 Mon Sep 17 00:00:00 2001 From: Charly Date: Tue, 12 Mar 2024 12:04:52 +0100 Subject: [PATCH] imp: add flat fee for queries to `VerifyMembership` (#5927) Co-authored-by: Damian Nolan --- modules/core/02-client/keeper/grpc_query.go | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/modules/core/02-client/keeper/grpc_query.go b/modules/core/02-client/keeper/grpc_query.go index 9ba5241fe57..3646abdcb79 100644 --- a/modules/core/02-client/keeper/grpc_query.go +++ b/modules/core/02-client/keeper/grpc_query.go @@ -369,6 +369,7 @@ func (k Keeper) VerifyMembership(c context.Context, req *types.QueryVerifyMember } ctx := sdk.UnwrapSDKContext(c) + // cache the context to ensure clientState.VerifyMembership does not change state cachedCtx, _ := ctx.CacheContext() @@ -386,6 +387,13 @@ func (k Keeper) VerifyMembership(c context.Context, req *types.QueryVerifyMember return nil, status.Error(codes.FailedPrecondition, errorsmod.Wrapf(types.ErrClientNotActive, "cannot verify membership using client (%s) with status %s", req.ClientId, clientStatus).Error()) } + // consume flat gas fee for proof verification queries. + // NOTE: consuming gas prior to method invocation also provides protection against recursive calls reaching stack overflow + ctx.GasMeter().ConsumeGas( + 3*ctx.KVGasConfig().ReadCostPerByte*uint64(len(req.Proof)), + "verify membership query", + ) + if err := clientState.VerifyMembership(cachedCtx, k.ClientStore(cachedCtx, req.ClientId), k.cdc, req.ProofHeight, req.TimeDelay, req.BlockDelay, req.Proof, req.MerklePath, req.Value); err != nil { k.Logger(ctx).Debug("proof verification failed", "key", req.MerklePath, "error", err) return &types.QueryVerifyMembershipResponse{