Skip to content

Commit

Permalink
AddPrivilege; KeeperTestSuite
Browse files Browse the repository at this point in the history
  • Loading branch information
catShaark committed Aug 9, 2024
1 parent f4eb43b commit a0701f4
Show file tree
Hide file tree
Showing 6 changed files with 49 additions and 100 deletions.
2 changes: 2 additions & 0 deletions app/test_helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,8 @@ func Setup(
panic(err)
}
genesisState[feemarkettypes.ModuleName] = app.AppCodec().MustMarshalJSON(feemarketGenesis)
} else {
genesisState[feemarkettypes.ModuleName] = app.AppCodec().MustMarshalJSON(feemarkettypes.DefaultGenesisState())
}

stateBytes, err := json.MarshalIndent(genesisState, "", " ")
Expand Down
49 changes: 24 additions & 25 deletions x/asset/keeper/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,13 @@ import (

type (
Keeper struct {
cdc codec.BinaryCodec
storeKey storetypes.StoreKey
memKey storetypes.StoreKey
paramstore paramtypes.Subspace
bankKeeper types.BankKeeper
ak types.AccountKeeper
PrivilegesMap map[string]types.PrivilegeI
cdc codec.BinaryCodec
storeKey storetypes.StoreKey
memKey storetypes.StoreKey
paramstore paramtypes.Subspace
bankKeeper types.BankKeeper
ak types.AccountKeeper
PrivilegeManager map[string]types.PrivilegeI
}
)

Expand All @@ -35,33 +35,32 @@ func NewKeeper(
ps paramtypes.Subspace,
bankKeeper types.BankKeeper,
ak types.AccountKeeper,
privileges ...types.PrivilegeI,
) *Keeper {
// set KeyTable if it has not already been set
if !ps.HasKeyTable() {
ps = ps.WithKeyTable(types.ParamKeyTable())
}

privilegesMap := map[string]types.PrivilegeI{}
for _, priv := range privileges {
if _, ok := privilegesMap[priv.Name()]; ok {
continue
}

privilegesMap[priv.Name()] = priv
// regiester the privilege's interfaces
priv.RegisterInterfaces()
return &Keeper{
cdc: cdc,
storeKey: storeKey,
memKey: memKey,
paramstore: ps,
bankKeeper: bankKeeper,
ak: ak,
PrivilegeManager: map[string]types.PrivilegeI{},
}
}

return &Keeper{
cdc: cdc,
storeKey: storeKey,
memKey: memKey,
paramstore: ps,
bankKeeper: bankKeeper,
ak: ak,
PrivilegesMap: privilegesMap,
func (k Keeper) AddPrivilege(priv types.PrivilegeI) error {
if _, ok := k.PrivilegeManager[priv.Name()]; ok {
return fmt.Errorf("privilege %s already exists", priv.Name())
}

k.PrivilegeManager[priv.Name()] = priv
// regiester the privilege's interfaces
priv.RegisterInterfaces()
return nil
}

func (k Keeper) Logger(ctx sdk.Context) log.Logger {
Expand Down
87 changes: 14 additions & 73 deletions x/asset/keeper/keeper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,94 +2,35 @@ package keeper_test

import (
"testing"
"time"

"github.com/cosmos/cosmos-sdk/baseapp"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/evmos/ethermint/crypto/ethsecp256k1"
"github.com/stretchr/testify/require"
"github.com/stretchr/testify/suite"
"github.com/tendermint/tendermint/crypto/tmhash"
tmproto "github.com/tendermint/tendermint/proto/tendermint/types"
tmversion "github.com/tendermint/tendermint/proto/tendermint/version"
"github.com/tendermint/tendermint/version"

govkeeper "github.com/cosmos/cosmos-sdk/x/gov/keeper"
"github.com/realiotech/realio-network/app"
realiotypes "github.com/realiotech/realio-network/types"
"github.com/realiotech/realio-network/x/asset/keeper"
"github.com/realiotech/realio-network/x/asset/types"

"github.com/realiotech/realio-network/testutil"
)

type KeeperTestSuite struct {
suite.Suite
app *app.RealioNetwork
ctx sdk.Context
queryClient types.QueryClient
testUser1Acc sdk.AccAddress
testUser1Address string
testUser2Acc sdk.AccAddress
testUser2Address string
testUser3Acc sdk.AccAddress
testUser3Address string
}
app *app.RealioNetwork
ctx sdk.Context

func (suite *KeeperTestSuite) SetupTest() {
suite.DoSetupTest(suite.T())
assetKeeper *keeper.Keeper
govkeeper govkeeper.Keeper
msgServer types.MsgServer
}

func (suite *KeeperTestSuite) DoSetupTest(t *testing.T) {
checkTx := false

// user 1 key
suite.testUser1Acc = testutil.GenAddress()
suite.testUser1Address = suite.testUser1Acc.String()

// user 2 key
suite.testUser2Acc = testutil.GenAddress()
suite.testUser2Address = suite.testUser2Acc.String()

// user 3 key
suite.testUser3Acc = testutil.GenAddress()
suite.testUser3Address = suite.testUser3Acc.String()

// consensus key
priv, err := ethsecp256k1.GenerateKey()
require.NoError(t, err)
consAddress := sdk.ConsAddress(priv.PubKey().Address())

// init app
suite.app = app.Setup(checkTx, nil, 1)

// Set Context
suite.ctx = suite.app.BaseApp.NewContext(checkTx, tmproto.Header{
Height: 1,
ChainID: realiotypes.TestnetChainID,
Time: time.Now().UTC(),
ProposerAddress: consAddress.Bytes(),

Version: tmversion.Consensus{
Block: version.BlockProtocol,
},
LastBlockId: tmproto.BlockID{
Hash: tmhash.Sum([]byte("block_id")),
PartSetHeader: tmproto.PartSetHeader{
Total: 11,
Hash: tmhash.Sum([]byte("partset_header")),
},
},
AppHash: tmhash.Sum([]byte("app")),
DataHash: tmhash.Sum([]byte("data")),
EvidenceHash: tmhash.Sum([]byte("evidence")),
ValidatorsHash: tmhash.Sum([]byte("validators")),
NextValidatorsHash: tmhash.Sum([]byte("next_validators")),
ConsensusHash: tmhash.Sum([]byte("consensus")),
LastResultsHash: tmhash.Sum([]byte("last_result")),
})
func (suite *KeeperTestSuite) SetupTest() {
app := app.Setup(false, nil, 3)

queryHelper := baseapp.NewQueryServerTestHelper(suite.ctx, suite.app.InterfaceRegistry())
types.RegisterQueryServer(queryHelper, suite.app.AssetKeeper)
suite.queryClient = types.NewQueryClient(queryHelper)
suite.app = app
suite.ctx = app.BaseApp.NewContext(false, tmproto.Header{Height: app.LastBlockHeight() + 1})
suite.assetKeeper = &app.AssetKeeper
suite.govkeeper = app.GovKeeper
suite.msgServer = keeper.NewMsgServerImpl(app.AssetKeeper)
}

func TestKeeperTestSuite(t *testing.T) {
Expand Down
4 changes: 2 additions & 2 deletions x/asset/keeper/msg_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ func (k msgServer) UnassignPrivilege(goCtx context.Context, msg *types.MsgUnassi

tm, found := k.GetTokenManagement(ctx, msg.TokenId)
if !found {
return nil, errorsmod.Wrapf(sdkerrors.ErrNotFound, "token with denom %s is not exists", msg.TokenId)
return nil, errorsmod.Wrapf(sdkerrors.ErrNotFound, "token with denom %s doesn't exists", msg.TokenId)
}
if tm.Manager != msg.Manager {
return nil, errorsmod.Wrapf(sdkerrors.ErrUnauthorized, "sender is not token manager")
Expand Down Expand Up @@ -220,7 +220,7 @@ func (k msgServer) ExecutePrivilege(goCtx context.Context, msg *types.MsgExecute
return nil, errorsmod.Wrapf(sdkerrors.ErrInvalidRequest, "user does not not have %s privilege", privName)
}

privImplementation, ok := k.PrivilegesMap[privName]
privImplementation, ok := k.PrivilegeManager[privName]
if !ok {
return nil, errorsmod.Wrapf(sdkerrors.ErrInvalidRequest, "privilege name %s is not registered yet", privName)
}
Expand Down
7 changes: 7 additions & 0 deletions x/asset/keeper/msg_server_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package keeper_test

// import (
// "fmt"
// "testing"

// )
File renamed without changes.

0 comments on commit a0701f4

Please sign in to comment.