refactor: deterministic tests and add gas tests (#13539)

* refactor: x/auth deterministic tests

* changes

* x/bank refactor

* changes

* review changes

* refactor `x/staking`

* fix tests

* fix tests

* fix tests

* review changes

* Update testutil/testdata/grpc_query.go

* Update testutil/testdata/grpc_query.go

Co-authored-by: Aleksandr Bezobchuk <alexanderbez@users.noreply.github.com>
Co-authored-by: Amaury <1293565+amaurym@users.noreply.github.com>
This commit is contained in:
atheeshp
2022-10-25 06:36:47 +00:00
committed by GitHub
co-authored by Aleksandr Bezobchuk Amaury
parent b809f7129e
commit 9f3575a10f
5 changed files with 182 additions and 579 deletions
+39
View File
@@ -5,8 +5,17 @@ import (
"fmt"
"github.com/cosmos/gogoproto/proto"
"github.com/stretchr/testify/require"
grpc "google.golang.org/grpc"
"github.com/cosmos/cosmos-sdk/codec/types"
sdk "github.com/cosmos/cosmos-sdk/types"
)
var (
// iterCount defines the number of iterations to run on each query to test
// determinism.
iterCount = 1000
)
type QueryImpl struct{}
@@ -51,3 +60,33 @@ var _ types.UnpackInterfacesMessage = &TestAnyResponse{}
func (m *TestAnyResponse) UnpackInterfaces(unpacker types.AnyUnpacker) error {
return m.HasAnimal.UnpackInterfaces(unpacker)
}
// DeterministicIterations is a function to handle deterministic query requests. It tests 2 things:
// 1. That the response is always the same when calling the
// grpc query `iterCount` times (defaults to 1000).
// 2. That the gas consumption of the query is the same. When
// `gasOverwrite` is set to true, we also check that this consumed
// gas value is equal to the hardcoded `gasConsumed`.
func DeterministicIterations[request proto.Message, response proto.Message](
ctx sdk.Context,
require *require.Assertions,
req request,
grpcFn func(context.Context, request, ...grpc.CallOption) (response, error),
gasConsumed uint64,
gasOverwrite bool,
) {
before := ctx.GasMeter().GasConsumed()
prevRes, err := grpcFn(ctx, req)
require.NoError(err)
if gasOverwrite { // to handle regressions, i.e. check that gas consumption didn't change
gasConsumed = ctx.GasMeter().GasConsumed() - before
}
for i := 0; i < iterCount; i++ {
before := ctx.GasMeter().GasConsumed()
res, err := grpcFn(ctx, req)
require.Equal(ctx.GasMeter().GasConsumed()-before, gasConsumed)
require.NoError(err)
require.Equal(res, prevRes)
}
}