Skip to content

Commit b92fad3

Browse files
committed
add tests for cron module
1 parent aae46b5 commit b92fad3

File tree

18 files changed

+1020
-86
lines changed

18 files changed

+1020
-86
lines changed

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@ require (
7474
github.com/cosmos/cosmos-proto v1.0.0-beta.5
7575
github.com/cosmos/rosetta v0.50.4
7676
github.com/gogo/protobuf v1.3.2
77+
github.com/golang/mock v1.6.0
7778
github.com/hashicorp/go-metrics v0.5.3
7879
github.com/scrtlabs/tm-secret-enclave v1.12.3-implicit-hash
7980
golang.org/x/xerrors v0.0.0-20231012003039-104605ab7028
@@ -139,7 +140,6 @@ require (
139140
github.com/gogo/googleapis v1.4.1 // indirect
140141
github.com/golang/glog v1.2.3 // indirect
141142
github.com/golang/groupcache v0.0.0-20210331224755-41bb18bfe9da // indirect
142-
github.com/golang/mock v1.6.0 // indirect
143143
github.com/golang/snappy v0.0.4 // indirect
144144
github.com/google/btree v1.1.3 // indirect
145145
github.com/google/flatbuffers v2.0.8+incompatible // indirect

proto/secret/cron/schedule.proto

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ message Schedule {
2424
// Last execution's block height
2525
uint64 last_execute_height = 4;
2626
// Stage when messages will be executed
27-
ExecutionStage execution_stage = 5;
27+
// ExecutionStage execution_stage = 5;
2828
}
2929

3030
// Defines the contract and the message to pass

testutil/common/nullify/nullify.go

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
// Package nullify provides methods to init nil values structs for test assertion.
2+
package nullify
3+
4+
import (
5+
"reflect"
6+
"unsafe"
7+
8+
sdk "github.com/cosmos/cosmos-sdk/types"
9+
)
10+
11+
var (
12+
coinType = reflect.TypeOf(sdk.Coin{})
13+
coinsType = reflect.TypeOf(sdk.Coins{})
14+
)
15+
16+
// Fill analyze all struct fields and slices with
17+
// reflection and initialize the nil and empty slices,
18+
// structs, and pointers.
19+
func Fill(x interface{}) interface{} {
20+
v := reflect.Indirect(reflect.ValueOf(x))
21+
switch v.Kind() {
22+
case reflect.Slice:
23+
for i := 0; i < v.Len(); i++ {
24+
obj := v.Index(i)
25+
objPt := reflect.NewAt(obj.Type(), unsafe.Pointer(obj.UnsafeAddr())).Interface()
26+
objPt = Fill(objPt)
27+
obj.Set(reflect.ValueOf(objPt))
28+
}
29+
case reflect.Struct:
30+
for i := 0; i < v.NumField(); i++ {
31+
f := reflect.Indirect(v.Field(i))
32+
if !f.CanSet() {
33+
continue
34+
}
35+
switch f.Kind() {
36+
case reflect.Slice:
37+
f.Set(reflect.MakeSlice(f.Type(), 0, 0))
38+
case reflect.Struct:
39+
switch f.Type() {
40+
case coinType:
41+
coin := reflect.New(coinType).Interface()
42+
s := reflect.ValueOf(coin).Elem()
43+
f.Set(s)
44+
case coinsType:
45+
coins := reflect.New(coinsType).Interface()
46+
s := reflect.ValueOf(coins).Elem()
47+
f.Set(s)
48+
default:
49+
objPt := reflect.NewAt(f.Type(), unsafe.Pointer(f.UnsafeAddr())).Interface()
50+
s := Fill(objPt)
51+
f.Set(reflect.ValueOf(s))
52+
}
53+
}
54+
}
55+
}
56+
return reflect.Indirect(v).Interface()
57+
}

testutil/cron/keeper/cron.go

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
package keeper
2+
3+
import (
4+
"sync"
5+
"testing"
6+
7+
"cosmossdk.io/log"
8+
metrics2 "cosmossdk.io/store/metrics"
9+
db2 "github.com/cosmos/cosmos-db"
10+
authtypes "github.com/cosmos/cosmos-sdk/x/auth/types"
11+
12+
"cosmossdk.io/store"
13+
storetypes "cosmossdk.io/store/types"
14+
tmproto "github.com/cometbft/cometbft/proto/tendermint/types"
15+
"github.com/cosmos/cosmos-sdk/codec"
16+
codectypes "github.com/cosmos/cosmos-sdk/codec/types"
17+
sdk "github.com/cosmos/cosmos-sdk/types"
18+
"github.com/stretchr/testify/require"
19+
20+
"github.com/scrtlabs/SecretNetwork/x/cron/keeper"
21+
"github.com/scrtlabs/SecretNetwork/x/cron/types"
22+
)
23+
24+
var configOnce sync.Once
25+
26+
func CronKeeper(t testing.TB, accountKeeper types.AccountKeeper) (*keeper.Keeper, sdk.Context) {
27+
configOnce.Do(func() {
28+
config := sdk.GetConfig()
29+
config.SetBech32PrefixForAccount("secret", "secretpub")
30+
config.SetBech32PrefixForValidator("secretvaloper", "secretvaloperpub")
31+
config.SetBech32PrefixForConsensusNode("secretvalcons", "secretvalconspub")
32+
config.Seal()
33+
})
34+
35+
storeKey := storetypes.NewKVStoreKey(types.StoreKey)
36+
memStoreKey := storetypes.NewMemoryStoreKey(types.MemStoreKey)
37+
38+
db := db2.NewMemDB()
39+
stateStore := store.NewCommitMultiStore(db, log.NewNopLogger(), metrics2.NewNoOpMetrics())
40+
stateStore.MountStoreWithDB(storeKey, storetypes.StoreTypeIAVL, db)
41+
stateStore.MountStoreWithDB(memStoreKey, storetypes.StoreTypeMemory, nil)
42+
require.NoError(t, stateStore.LoadLatestVersion())
43+
44+
registry := codectypes.NewInterfaceRegistry()
45+
cdc := codec.NewProtoCodec(registry)
46+
47+
k := keeper.NewKeeper(
48+
cdc,
49+
storeKey,
50+
memStoreKey,
51+
accountKeeper,
52+
authtypes.NewModuleAddress(types.ModuleName).String(),
53+
)
54+
55+
ctx := sdk.NewContext(stateStore, tmproto.Header{}, false, log.NewNopLogger())
56+
57+
// Initialize params
58+
err := k.SetParams(ctx, types.DefaultParams())
59+
require.NoError(t, err)
60+
61+
return k, ctx
62+
}

testutil/mocks/cron/types/expected_keepers.go

Lines changed: 89 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

testutil/testutil.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
// Package testutil provides testing utilities for the SecretNetwork project.
2+
package testutil
3+
4+
// TestOwnerAddress is a test address used in tests
5+
const TestOwnerAddress = "secret1g9v8s0uzndfrq7l0l5t8h8q3q3q3q3q3q3q3q3"

x/compute/module.go

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,7 @@ package compute
33
import (
44
"context"
55
"crypto/sha256"
6-
"encoding/hex"
76
"encoding/json"
8-
"fmt"
97

108
"github.com/grpc-ecosystem/grpc-gateway/runtime"
119
"github.com/spf13/cobra"
@@ -227,7 +225,6 @@ func (am AppModule) EndBlock(c context.Context) error {
227225
}
228226
hash := sha256.Sum256(cron_data)
229227

230-
fmt.Printf("Hash of the executed msgs in the next round: %+v\n", hex.EncodeToString(hash[:]))
231228
err = tmenclave.SetImplicitHash(hash[:])
232229
if err != nil {
233230
ctx.Logger().Error("Failed to set implicit hash %+v", err)

x/cron/genesis.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import (
1111
func InitGenesis(ctx sdk.Context, k keeper.Keeper, genState types.GenesisState) {
1212
// Set all the schedules
1313
for _, elem := range genState.ScheduleList {
14-
err := k.AddSchedule(ctx, elem.Name, elem.Period, elem.Msgs, elem.ExecutionStage)
14+
err := k.AddSchedule(ctx, elem.Name, elem.Period, elem.Msgs)
1515
if err != nil {
1616
panic(err)
1717
}

x/cron/genesis_test.go

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package cron_test
2+
3+
import (
4+
"testing"
5+
6+
"github.com/stretchr/testify/require"
7+
8+
"github.com/scrtlabs/SecretNetwork/testutil/common/nullify"
9+
"github.com/scrtlabs/SecretNetwork/testutil/cron/keeper"
10+
"github.com/scrtlabs/SecretNetwork/x/cron"
11+
"github.com/scrtlabs/SecretNetwork/x/cron/types"
12+
)
13+
14+
func TestGenesis(t *testing.T) {
15+
k, ctx := keeper.CronKeeper(t, nil)
16+
17+
genesisState := types.GenesisState{
18+
Params: types.DefaultParams(),
19+
ScheduleList: []types.Schedule{
20+
{
21+
Name: "a",
22+
Period: 5,
23+
Msgs: nil,
24+
LastExecuteHeight: uint64(ctx.BlockHeight()), //nolint:gosec
25+
},
26+
},
27+
}
28+
29+
cron.InitGenesis(ctx, *k, genesisState)
30+
got := cron.ExportGenesis(ctx, *k)
31+
require.NotNil(t, got)
32+
33+
nullify.Fill(&genesisState)
34+
nullify.Fill(got)
35+
36+
require.Equal(t, genesisState.Params, got.Params)
37+
require.ElementsMatch(t, genesisState.ScheduleList, got.ScheduleList)
38+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package keeper_test
2+
3+
import (
4+
"testing"
5+
6+
testkeeper "github.com/scrtlabs/SecretNetwork/testutil/cron/keeper"
7+
8+
"github.com/stretchr/testify/require"
9+
10+
"github.com/scrtlabs/SecretNetwork/x/cron/types"
11+
)
12+
13+
func TestParamsQuery(t *testing.T) {
14+
keeper, ctx := testkeeper.CronKeeper(t, nil)
15+
params := types.DefaultParams()
16+
err := keeper.SetParams(ctx, params)
17+
require.NoError(t, err)
18+
19+
response, err := keeper.Params(ctx, &types.QueryParamsRequest{})
20+
require.NoError(t, err)
21+
require.Equal(t, &types.QueryParamsResponse{Params: params}, response)
22+
}

0 commit comments

Comments
 (0)