mirror of
https://github.com/planetmint/planetmint-go.git
synced 2025-03-30 15:08:28 +00:00

* added a MqttMonitor module with levelDB and periodic cleanup * initialized in the app * passed to dao keeper * added conversion methods (string2unixtime, byte ToJSON) * removed obsolete keeper code * maded RDDLToken.Factor public * added explicit mqtt client to the monitor module * restart mqtt connection in mqttmonitor on connection loss * adjusted mqttmock structure to be compatible * added some linter exclusions to let the monitor tool pass * created a MockMqttMonitor interface and mock object * used this to pass tests * made the MockMqttMonitor a global object so that it can be easily mocked * removed MockMqttMonitor from the app/keeper initialization * adjusted test cases to register "active machines" to the mqttmonitor * added mutex in mocks to protect against data races * defined mocks for the dao tests * clear separation between interface and mqtt-Monitor * added another waiting block to ensure the tx went through (multi-threading issue, race condition) during tests this failed sometimes * added memstorage to test instead of a file based DB Signed-off-by: Jürgen Eckel <juergen@riddleandcode.com>
89 lines
3.2 KiB
Go
89 lines
3.2 KiB
Go
package keeper
|
|
|
|
import (
|
|
"testing"
|
|
|
|
tmdb "github.com/cometbft/cometbft-db"
|
|
"github.com/cometbft/cometbft/libs/log"
|
|
tmproto "github.com/cometbft/cometbft/proto/tendermint/types"
|
|
"github.com/cosmos/cosmos-sdk/codec"
|
|
codectypes "github.com/cosmos/cosmos-sdk/codec/types"
|
|
"github.com/cosmos/cosmos-sdk/store"
|
|
storetypes "github.com/cosmos/cosmos-sdk/store/types"
|
|
sdk "github.com/cosmos/cosmos-sdk/types"
|
|
authtypes "github.com/cosmos/cosmos-sdk/x/auth/types"
|
|
govtypes "github.com/cosmos/cosmos-sdk/x/gov/types"
|
|
typesparams "github.com/cosmos/cosmos-sdk/x/params/types"
|
|
"github.com/golang/mock/gomock"
|
|
"github.com/planetmint/planetmint-go/monitor"
|
|
monitormocks "github.com/planetmint/planetmint-go/monitor/mocks"
|
|
"github.com/planetmint/planetmint-go/x/dao/keeper"
|
|
daotestutil "github.com/planetmint/planetmint-go/x/dao/testutil"
|
|
"github.com/planetmint/planetmint-go/x/dao/types"
|
|
"github.com/stretchr/testify/require"
|
|
)
|
|
|
|
func DaoKeeper(t testing.TB) (*keeper.Keeper, sdk.Context) {
|
|
monitor.SetMqttMonitorInstance(&monitormocks.MockMQTTMonitorClientI{})
|
|
storeKey := sdk.NewKVStoreKey(types.StoreKey)
|
|
memStoreKey := storetypes.NewMemoryStoreKey(types.MemStoreKey)
|
|
challengeStoreKey := storetypes.NewMemoryStoreKey(types.ChallengeKey)
|
|
mintRequestHashStoreKey := storetypes.NewMemoryStoreKey(types.MintRequestHashKey)
|
|
mintRequestAddressStoreKey := storetypes.NewMemoryStoreKey(types.MintRequestAddressKey)
|
|
|
|
db := tmdb.NewMemDB()
|
|
stateStore := store.NewCommitMultiStore(db)
|
|
stateStore.MountStoreWithDB(storeKey, storetypes.StoreTypeIAVL, db)
|
|
stateStore.MountStoreWithDB(memStoreKey, storetypes.StoreTypeMemory, nil)
|
|
stateStore.MountStoreWithDB(challengeStoreKey, storetypes.StoreTypeIAVL, db)
|
|
stateStore.MountStoreWithDB(mintRequestHashStoreKey, storetypes.StoreTypeIAVL, db)
|
|
stateStore.MountStoreWithDB(mintRequestAddressStoreKey, storetypes.StoreTypeIAVL, db)
|
|
|
|
require.NoError(t, stateStore.LoadLatestVersion())
|
|
|
|
registry := codectypes.NewInterfaceRegistry()
|
|
cdc := codec.NewProtoCodec(registry)
|
|
|
|
paramsSubspace := typesparams.NewSubspace(cdc,
|
|
types.Amino,
|
|
storeKey,
|
|
memStoreKey,
|
|
"DaoParams",
|
|
)
|
|
|
|
ctx := sdk.NewContext(stateStore, tmproto.Header{}, false, log.NewNopLogger())
|
|
|
|
ctrl := gomock.NewController(t)
|
|
bk := daotestutil.NewMockBankKeeper(ctrl)
|
|
|
|
bk.EXPECT().MintCoins(gomock.Any(), gomock.Any(), gomock.Any()).Return(nil).AnyTimes()
|
|
bk.EXPECT().BurnCoins(gomock.Any(), gomock.Any(), gomock.Any()).Return(nil).AnyTimes()
|
|
bk.EXPECT().SendCoinsFromModuleToAccount(gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any()).Return(nil).AnyTimes()
|
|
bk.EXPECT().SendCoinsFromAccountToModule(gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any()).Return(nil).AnyTimes()
|
|
bk.EXPECT().GetBalance(gomock.Any(), gomock.Any(), gomock.Any()).Return(sdk.NewCoin("crddl", sdk.NewInt(10000))).AnyTimes()
|
|
|
|
k := keeper.NewKeeper(
|
|
cdc,
|
|
storeKey,
|
|
memStoreKey,
|
|
challengeStoreKey,
|
|
mintRequestHashStoreKey,
|
|
mintRequestAddressStoreKey,
|
|
nil, // TODO: mount and add store/key
|
|
paramsSubspace,
|
|
bk,
|
|
nil,
|
|
nil,
|
|
authtypes.NewModuleAddress(govtypes.ModuleName).String(),
|
|
"",
|
|
)
|
|
|
|
// Initialize params
|
|
err := k.SetParams(ctx, types.DefaultParams())
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
|
|
return k, ctx
|
|
}
|