Skip to content

Commit

Permalink
feat: update arkeo mdoule
Browse files Browse the repository at this point in the history
  • Loading branch information
shreyasbhat0 committed Jul 26, 2024
1 parent 783c6c4 commit 9105b70
Show file tree
Hide file tree
Showing 8 changed files with 172 additions and 85 deletions.
19 changes: 14 additions & 5 deletions x/arkeo/keeper/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"strings"

"cosmossdk.io/errors"
"cosmossdk.io/log"
storetypes "cosmossdk.io/store/types"
"github.com/cosmos/cosmos-sdk/codec"
sdk "github.com/cosmos/cosmos-sdk/types"
Expand All @@ -15,7 +16,6 @@ import (
paramtypes "github.com/cosmos/cosmos-sdk/x/params/types"
stakingkeeper "github.com/cosmos/cosmos-sdk/x/staking/keeper"
stakingtypes "github.com/cosmos/cosmos-sdk/x/staking/types"
"github.com/tendermint/tendermint/libs/log"

"github.com/arkeonetwork/arkeo/common"
"github.com/arkeonetwork/arkeo/common/cosmos"
Expand Down Expand Up @@ -55,7 +55,7 @@ type Keeper interface {
// passthrough funcs
SendCoins(ctx cosmos.Context, from, to cosmos.AccAddress, coins cosmos.Coins) error
AddCoins(ctx cosmos.Context, addr cosmos.AccAddress, coins cosmos.Coins) error
GetActiveValidators(ctx cosmos.Context) []stakingtypes.Validator
GetActiveValidators(ctx cosmos.Context) ([]stakingtypes.Validator, error)
GetAccount(ctx cosmos.Context, addr cosmos.AccAddress) cosmos.Account
StakingSetParams(ctx cosmos.Context, params stakingtypes.Params)

Expand Down Expand Up @@ -168,9 +168,13 @@ func (k KVStore) SetParams(ctx sdk.Context, params types.Params) {
k.paramstore.SetParamSet(ctx, &params)
}

// TODO: Check Thi Again
func (k KVStore) GetComputedVersion(ctx cosmos.Context) int64 {
versions := make(map[int64]int64) // maps are safe in blockchains, but should be okay in this case
validators := k.stakingKeeper.GetBondedValidatorsByPower(ctx)
validators, err := k.stakingKeeper.GetBondedValidatorsByPower(ctx)
if err != nil {
k.Logger(ctx).Error(err.Error())
}

// if there is only one validator, no need for consensus. Just return the
// validator's current version. This also helps makes
Expand All @@ -187,7 +191,12 @@ func (k KVStore) GetComputedVersion(ctx cosmos.Context) int64 {
if !val.IsBonded() {
continue
}
ver := k.GetVersionForAddress(ctx, val.GetOperator())

valBz, err := k.stakingKeeper.ValidatorAddressCodec().StringToBytes(val.GetOperator())
if err != nil {
k.Logger(ctx).Error(err.Error())
}
ver := k.GetVersionForAddress(ctx, valBz)
if _, ok := versions[ver]; !ok {
versions[ver] = 0
}
Expand Down Expand Up @@ -334,7 +343,7 @@ func (k KVStore) GetAccount(ctx cosmos.Context, addr cosmos.AccAddress) cosmos.A
return k.accountKeeper.GetAccount(ctx, addr)
}

func (k KVStore) GetActiveValidators(ctx cosmos.Context) []stakingtypes.Validator {
func (k KVStore) GetActiveValidators(ctx cosmos.Context) ([]stakingtypes.Validator, error) {
return k.stakingKeeper.GetBondedValidatorsByPower(ctx)
}

Expand Down
2 changes: 1 addition & 1 deletion x/arkeo/keeper/keeper_dummy.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,12 @@ import (
"github.com/arkeonetwork/arkeo/common/cosmos"
"github.com/arkeonetwork/arkeo/x/arkeo/types"

"cosmossdk.io/log"
"github.com/cosmos/cosmos-sdk/codec"
sdk "github.com/cosmos/cosmos-sdk/types"
authkeeper "github.com/cosmos/cosmos-sdk/x/auth/keeper"
bankkeeper "github.com/cosmos/cosmos-sdk/x/bank/keeper"
stakingtypes "github.com/cosmos/cosmos-sdk/x/staking/types"
"github.com/tendermint/tendermint/libs/log"
)

var kaboom = errors.New("Kaboom!!!")
Expand Down
131 changes: 91 additions & 40 deletions x/arkeo/keeper/keeper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,39 +3,43 @@ package keeper
import (
"testing"

arekoappParams "github.com/arkeonetwork/arkeo/app/params"
"github.com/arkeonetwork/arkeo/common"
"github.com/arkeonetwork/arkeo/common/cosmos"
"github.com/arkeonetwork/arkeo/testutil/utils"
"github.com/arkeonetwork/arkeo/x/arkeo/types"
"github.com/cosmos/cosmos-sdk/runtime"
"github.com/stretchr/testify/require"

"cosmossdk.io/log"
"cosmossdk.io/store"
storetypes "cosmossdk.io/store/types"
tmproto "github.com/cometbft/cometbft/proto/tendermint/types"
tmdb "github.com/cosmos/cosmos-db"
sdk "github.com/cosmos/cosmos-sdk/types"
authcodec "github.com/cosmos/cosmos-sdk/x/auth/codec"
authkeeper "github.com/cosmos/cosmos-sdk/x/auth/keeper"
authtypes "github.com/cosmos/cosmos-sdk/x/auth/types"
bankkeeper "github.com/cosmos/cosmos-sdk/x/bank/keeper"
banktypes "github.com/cosmos/cosmos-sdk/x/bank/types"
govtypes "github.com/cosmos/cosmos-sdk/x/gov/types"
paramskeeper "github.com/cosmos/cosmos-sdk/x/params/keeper"
typesparams "github.com/cosmos/cosmos-sdk/x/params/types"
stakingkeeper "github.com/cosmos/cosmos-sdk/x/staking/keeper"
stakingtypes "github.com/cosmos/cosmos-sdk/x/staking/types"
"github.com/tendermint/tendermint/libs/log"
tmproto "github.com/tendermint/tendermint/proto/tendermint/types"
tmdb "github.com/tendermint/tm-db"
)

func SetupKeeper(t testing.TB) (cosmos.Context, Keeper) {
storeKey := sdk.NewKVStoreKey(types.StoreKey)
storeKey := storetypes.NewKVStoreKey(types.StoreKey)
keyAcc := cosmos.NewKVStoreKey(authtypes.StoreKey)
keyBank := cosmos.NewKVStoreKey(banktypes.StoreKey)
keyStake := cosmos.NewKVStoreKey(stakingtypes.StoreKey)
keyParams := cosmos.NewKVStoreKey(typesparams.StoreKey)
tkeyParams := cosmos.NewTransientStoreKey(typesparams.TStoreKey)
memStoreKey := storetypes.NewMemoryStoreKey(types.MemStoreKey)

logger := log.NewNopLogger()
db := tmdb.NewMemDB()
stateStore := store.NewCommitMultiStore(db)
stateStore := store.NewCommitMultiStore(db, logger, nil)
stateStore.MountStoreWithDB(storeKey, storetypes.StoreTypeIAVL, db)
stateStore.MountStoreWithDB(keyAcc, storetypes.StoreTypeIAVL, db)
stateStore.MountStoreWithDB(keyBank, storetypes.StoreTypeIAVL, db)
Expand All @@ -44,7 +48,7 @@ func SetupKeeper(t testing.TB) (cosmos.Context, Keeper) {
stateStore.MountStoreWithDB(memStoreKey, storetypes.StoreTypeMemory, nil)
require.NoError(t, stateStore.LoadLatestVersion())

encodingConfig := MakeTestEncodingConfig()
encodingConfig := arekoappParams.MakeEncodingConfig()
types.RegisterInterfaces(encodingConfig.InterfaceRegistry)
cdc := utils.MakeTestMarshaler()
amino := encodingConfig.Amino
Expand All @@ -58,29 +62,53 @@ func SetupKeeper(t testing.TB) (cosmos.Context, Keeper) {

ctx := sdk.NewContext(stateStore, tmproto.Header{}, false, log.NewNopLogger())

pk := paramskeeper.NewKeeper(cdc, amino, keyParams, tkeyParams)
ak := authkeeper.NewAccountKeeper(cdc, keyAcc, pk.Subspace(authtypes.ModuleName), authtypes.ProtoBaseAccount, map[string][]string{
stakingtypes.BondedPoolName: {authtypes.Burner, authtypes.Staking},
stakingtypes.NotBondedPoolName: {authtypes.Burner, authtypes.Staking},
types.ModuleName: {authtypes.Minter, authtypes.Burner},
types.ReserveName: {},
types.ProviderName: {},
types.ContractName: {},
}, sdk.Bech32PrefixAccAddr)
ak.SetParams(ctx, authtypes.DefaultParams())

bk := bankkeeper.NewBaseKeeper(cdc, keyBank, ak, pk.Subspace(banktypes.ModuleName), nil)
_ = paramskeeper.NewKeeper(cdc, amino, keyParams, tkeyParams)
govModuleAddr := authtypes.NewModuleAddress(govtypes.ModuleName).String()
ak := authkeeper.NewAccountKeeper(
cdc,
runtime.NewKVStoreService(keyAcc),
authtypes.ProtoBaseAccount,
map[string][]string{
stakingtypes.BondedPoolName: {authtypes.Burner, authtypes.Staking},
stakingtypes.NotBondedPoolName: {authtypes.Burner, authtypes.Staking},
types.ModuleName: {authtypes.Minter, authtypes.Burner},
types.ReserveName: {},
types.ProviderName: {},
types.ContractName: {},
},
authcodec.NewBech32Codec(sdk.GetConfig().GetBech32AccountAddrPrefix()),
sdk.Bech32PrefixAccAddr,
govModuleAddr,
)

bk := bankkeeper.NewBaseKeeper(
cdc,
runtime.NewKVStoreService(keyBank),
ak,
nil,
govModuleAddr,
logger,
)
bk.SetParams(ctx, banktypes.DefaultParams())

sk := stakingkeeper.NewKeeper(cdc, keyStake, ak, bk, pk.Subspace(stakingtypes.ModuleName))
sk := stakingkeeper.NewKeeper(
cdc,
runtime.NewKVStoreService(keyStake),
ak,
bk,
govModuleAddr,
authcodec.NewBech32Codec(sdk.GetConfig().GetBech32ValidatorAddrPrefix()),
authcodec.NewBech32Codec(sdk.GetConfig().GetBech32ConsensusAddrPrefix()),
)

k := NewKVStore(
cdc,
storeKey,
memStoreKey,
paramsSubspace,
bk,
ak,
sk,
*sk,
)
k.SetVersion(ctx, common.GetCurrentVersion())

Expand All @@ -91,16 +119,17 @@ func SetupKeeper(t testing.TB) (cosmos.Context, Keeper) {
}

func SetupKeeperWithStaking(t testing.TB) (cosmos.Context, Keeper, stakingkeeper.Keeper) {
storeKey := sdk.NewKVStoreKey(types.StoreKey)
storeKey := storetypes.NewKVStoreKey(types.StoreKey)
keyAcc := cosmos.NewKVStoreKey(authtypes.StoreKey)
keyBank := cosmos.NewKVStoreKey(banktypes.StoreKey)
keyStake := cosmos.NewKVStoreKey(stakingtypes.StoreKey)
keyParams := cosmos.NewKVStoreKey(typesparams.StoreKey)
tkeyParams := cosmos.NewTransientStoreKey(typesparams.TStoreKey)
memStoreKey := storetypes.NewMemoryStoreKey(types.MemStoreKey)

logger := log.NewNopLogger()
db := tmdb.NewMemDB()
stateStore := store.NewCommitMultiStore(db)
stateStore := store.NewCommitMultiStore(db, logger, nil)
stateStore.MountStoreWithDB(storeKey, storetypes.StoreTypeIAVL, db)
stateStore.MountStoreWithDB(keyAcc, storetypes.StoreTypeIAVL, db)
stateStore.MountStoreWithDB(keyBank, storetypes.StoreTypeIAVL, db)
Expand All @@ -110,7 +139,7 @@ func SetupKeeperWithStaking(t testing.TB) (cosmos.Context, Keeper, stakingkeeper
stateStore.MountStoreWithDB(memStoreKey, storetypes.StoreTypeMemory, nil)
require.NoError(t, stateStore.LoadLatestVersion())

encodingConfig := MakeTestEncodingConfig()
encodingConfig := arekoappParams.MakeEncodingConfig()
types.RegisterInterfaces(encodingConfig.InterfaceRegistry)
cdc := utils.MakeTestMarshaler()
amino := encodingConfig.Amino
Expand All @@ -124,22 +153,44 @@ func SetupKeeperWithStaking(t testing.TB) (cosmos.Context, Keeper, stakingkeeper

ctx := sdk.NewContext(stateStore, tmproto.Header{}, false, log.NewNopLogger())

pk := paramskeeper.NewKeeper(cdc, amino, keyParams, tkeyParams)
ak := authkeeper.NewAccountKeeper(cdc, keyAcc, pk.Subspace(authtypes.ModuleName), authtypes.ProtoBaseAccount, map[string][]string{
stakingtypes.BondedPoolName: {authtypes.Burner, authtypes.Staking},
stakingtypes.NotBondedPoolName: {authtypes.Burner, authtypes.Staking},
types.ModuleName: {authtypes.Minter, authtypes.Burner},
types.ReserveName: {},
types.ProviderName: {},
types.ContractName: {},
}, sdk.Bech32PrefixAccAddr)
ak.SetParams(ctx, authtypes.DefaultParams())

bk := bankkeeper.NewBaseKeeper(cdc, keyBank, ak, pk.Subspace(banktypes.ModuleName), nil)
govModuleAddr := authtypes.NewModuleAddress(govtypes.ModuleName).String()
_ = paramskeeper.NewKeeper(cdc, amino, keyParams, tkeyParams)
ak := authkeeper.NewAccountKeeper(
cdc,
runtime.NewKVStoreService(keyAcc),
authtypes.ProtoBaseAccount,
map[string][]string{
stakingtypes.BondedPoolName: {authtypes.Burner, authtypes.Staking},
stakingtypes.NotBondedPoolName: {authtypes.Burner, authtypes.Staking},
types.ModuleName: {authtypes.Minter, authtypes.Burner},
types.ReserveName: {},
types.ProviderName: {},
types.ContractName: {},
},
authcodec.NewBech32Codec(sdk.GetConfig().GetBech32AccountAddrPrefix()),
sdk.Bech32PrefixAccAddr,
govModuleAddr,
)

bk := bankkeeper.NewBaseKeeper(
cdc,
runtime.NewKVStoreService(keyBank),
ak,
nil,
govModuleAddr,
logger,
)
bk.SetParams(ctx, banktypes.DefaultParams())

sk := stakingkeeper.NewKeeper(cdc, keyStake, ak, bk, pk.Subspace(stakingtypes.ModuleName))
sk.SetParams(ctx, stakingtypes.DefaultParams())
sk := stakingkeeper.NewKeeper(
cdc,
runtime.NewKVStoreService(keyStake),
ak,
bk,
govModuleAddr,
authcodec.NewBech32Codec(sdk.GetConfig().GetBech32ValidatorAddrPrefix()),
authcodec.NewBech32Codec(sdk.GetConfig().GetBech32ConsensusAddrPrefix()),
)

k := NewKVStore(
cdc,
Expand All @@ -148,12 +199,12 @@ func SetupKeeperWithStaking(t testing.TB) (cosmos.Context, Keeper, stakingkeeper
paramsSubspace,
bk,
ak,
sk,
*sk,
)
k.SetVersion(ctx, common.GetCurrentVersion())

// Initialize params
k.SetParams(ctx, types.DefaultParams())

return ctx, *k, sk
return ctx, *k, *sk
}
Loading

0 comments on commit 9105b70

Please sign in to comment.