Skip to content

Commit

Permalink
Revert Finschia#956 on x/auth relevant checks
Browse files Browse the repository at this point in the history
  • Loading branch information
0Tech committed Apr 12, 2023
1 parent a9a4332 commit 2156936
Show file tree
Hide file tree
Showing 11 changed files with 19 additions and 136 deletions.
4 changes: 2 additions & 2 deletions simapp/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -384,8 +384,8 @@ func NewSimApp(
upgrade.NewAppModule(app.UpgradeKeeper),
evidence.NewAppModule(app.EvidenceKeeper),
params.NewAppModule(app.ParamsKeeper),
tokenmodule.NewAppModule(appCodec, app.TokenKeeper, app.AccountKeeper),
collectionmodule.NewAppModule(appCodec, app.CollectionKeeper, app.AccountKeeper),
tokenmodule.NewAppModule(appCodec, app.TokenKeeper),
collectionmodule.NewAppModule(appCodec, app.CollectionKeeper),
authzmodule.NewAppModule(appCodec, app.AuthzKeeper, app.AccountKeeper, app.BankKeeper, app.interfaceRegistry),
)

Expand Down
4 changes: 0 additions & 4 deletions x/collection/expected_keepers.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,4 @@ type (
NewID(ctx sdk.Context) string
HasID(ctx sdk.Context, id string) bool
}

AuthKeeper interface {
HasAccount(sdk.Context, sdk.AccAddress) bool
}
)
36 changes: 3 additions & 33 deletions x/collection/keeper/grpc_query.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,27 +17,17 @@ import (
)

type queryServer struct {
keeper Keeper
authKeeper collection.AuthKeeper
keeper Keeper
}

// NewQueryServer returns an implementation of the token QueryServer interface
// for the provided Keeper.
func NewQueryServer(keeper Keeper, authKeeper collection.AuthKeeper) collection.QueryServer {
func NewQueryServer(keeper Keeper) collection.QueryServer {
return &queryServer{
keeper: keeper,
authKeeper: authKeeper,
keeper: keeper,
}
}

func (s queryServer) validateExistenceOfAccountGRPC(ctx sdk.Context, addr sdk.AccAddress) error {
if !s.authKeeper.HasAccount(ctx, addr) {
return status.Error(codes.NotFound, sdkerrors.ErrUnknownAddress.Wrap(addr.String()).Error())
}

return nil
}

func (s queryServer) validateExistenceOfCollectionGRPC(ctx sdk.Context, id string) error {
if _, err := s.keeper.GetContract(ctx, id); err != nil {
return status.Error(codes.NotFound, err.Error())
Expand Down Expand Up @@ -94,15 +84,6 @@ func (s queryServer) Balance(c context.Context, req *collection.QueryBalanceRequ
}

ctx := sdk.UnwrapSDKContext(c)

if err := s.validateExistenceOfAccountGRPC(ctx, addr); err != nil {
return nil, err
}

if err := s.validateExistenceOfCollectionGRPC(ctx, req.ContractId); err != nil {
return nil, err
}

balance := s.keeper.GetBalance(ctx, req.ContractId, addr, req.TokenId)
coin := collection.Coin{
TokenId: req.TokenId,
Expand Down Expand Up @@ -604,10 +585,6 @@ func (s queryServer) GranteeGrants(c context.Context, req *collection.QueryGrant

ctx := sdk.UnwrapSDKContext(c)

if err := s.validateExistenceOfAccountGRPC(ctx, granteeAddr); err != nil {
return nil, err
}

if err := s.validateExistenceOfCollectionGRPC(ctx, req.ContractId); err != nil {
return nil, err
}
Expand Down Expand Up @@ -650,13 +627,6 @@ func (s queryServer) IsOperatorFor(c context.Context, req *collection.QueryIsOpe

ctx := sdk.UnwrapSDKContext(c)

if err := s.validateExistenceOfAccountGRPC(ctx, operator); err != nil {
return nil, err
}
if err := s.validateExistenceOfAccountGRPC(ctx, holder); err != nil {
return nil, err
}

if err := s.validateExistenceOfCollectionGRPC(ctx, req.ContractId); err != nil {
return nil, err
}
Expand Down
26 changes: 1 addition & 25 deletions x/collection/keeper/grpc_query_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,20 +55,10 @@ func (s *KeeperTestSuite) TestQueryBalance() {
contractID: s.contractID,
tokenID: tokenID,
},
"invalid token id": {
"valid token id": {
contractID: s.contractID,
address: s.vendor,
},
"address not found": {
contractID: s.contractID,
address: sdk.AccAddress("notfound"),
tokenID: tokenID,
},
"collection not found": {
contractID: "deadbeef",
address: s.vendor,
tokenID: tokenID,
},
}

for name, tc := range testCases {
Expand Down Expand Up @@ -935,10 +925,6 @@ func (s *KeeperTestSuite) TestQueryGranteeGrants() {
contractID: "deadbeef",
grantee: s.vendor,
},
"grantee not found": {
contractID: s.contractID,
grantee: sdk.AccAddress("notfound"),
},
}

for name, tc := range testCases {
Expand Down Expand Up @@ -997,16 +983,6 @@ func (s *KeeperTestSuite) TestQueryIsOperatorFor() {
operator: s.operator,
holder: s.vendor,
},
"operator not found": {
contractID: s.contractID,
operator: sdk.AccAddress("notfound"),
holder: s.customer,
},
"holder not found": {
contractID: s.contractID,
operator: s.operator,
holder: sdk.AccAddress("notfound"),
},
}

for name, tc := range testCases {
Expand Down
6 changes: 1 addition & 5 deletions x/collection/keeper/keeper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ func (s *KeeperTestSuite) SetupTest() {
s.goCtx = sdk.WrapSDKContext(s.ctx)
s.keeper = app.CollectionKeeper

s.queryServer = keeper.NewQueryServer(s.keeper, app.AccountKeeper)
s.queryServer = keeper.NewQueryServer(s.keeper)
s.msgServer = keeper.NewMsgServer(s.keeper)

s.depthLimit = 4
Expand All @@ -81,10 +81,6 @@ func (s *KeeperTestSuite) SetupTest() {
}
for i, address := range createRandomAccounts(len(addresses)) {
*addresses[i] = address

// create account
acc := app.AccountKeeper.NewAccountWithAddress(s.ctx, address)
app.AccountKeeper.SetAccount(s.ctx, acc)
}

s.balance = sdk.NewInt(1000000)
Expand Down
10 changes: 4 additions & 6 deletions x/collection/module/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,15 +78,13 @@ func (b AppModuleBasic) RegisterInterfaces(registry codectypes.InterfaceRegistry
type AppModule struct {
AppModuleBasic

keeper keeper.Keeper
authKeeper collection.AuthKeeper
keeper keeper.Keeper
}

// NewAppModule creates a new AppModule object
func NewAppModule(cdc codec.Codec, keeper keeper.Keeper, authKeeper collection.AuthKeeper) AppModule {
func NewAppModule(cdc codec.Codec, keeper keeper.Keeper) AppModule {
return AppModule{
keeper: keeper,
authKeeper: authKeeper,
keeper: keeper,
}
}

Expand All @@ -108,7 +106,7 @@ func (am AppModule) LegacyQuerierHandler(legacyQuerierCdc *codec.LegacyAmino) sd
// module-specific GRPC queries.
func (am AppModule) RegisterServices(cfg module.Configurator) {
collection.RegisterMsgServer(cfg.MsgServer(), keeper.NewMsgServer(am.keeper))
collection.RegisterQueryServer(cfg.QueryServer(), keeper.NewQueryServer(am.keeper, am.authKeeper))
collection.RegisterQueryServer(cfg.QueryServer(), keeper.NewQueryServer(am.keeper))

// m := keeper.NewMigrator(am.keeper)
// migrations := map[uint64]func(sdk.Context) error{}
Expand Down
4 changes: 0 additions & 4 deletions x/token/expected_keepers.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,4 @@ type (
InitGenesis(ctx sdk.Context, data *ClassGenesisState)
ExportGenesis(ctx sdk.Context) *ClassGenesisState
}

AuthKeeper interface {
HasAccount(ctx sdk.Context, addr sdk.AccAddress) bool
}
)
32 changes: 3 additions & 29 deletions x/token/keeper/grpc_query.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,29 +14,19 @@ import (
)

type queryServer struct {
keeper Keeper
authKeeper token.AuthKeeper
keeper Keeper
}

// NewQueryServer returns an implementation of the token QueryServer interface
// for the provided Keeper.
func NewQueryServer(keeper Keeper, authKeeper token.AuthKeeper) token.QueryServer {
func NewQueryServer(keeper Keeper) token.QueryServer {
return &queryServer{
keeper: keeper,
authKeeper: authKeeper,
keeper: keeper,
}
}

var _ token.QueryServer = queryServer{}

func (s queryServer) validateExistenceOfAccountGRPC(ctx sdk.Context, addr sdk.AccAddress) error {
if !s.authKeeper.HasAccount(ctx, addr) {
return status.Error(codes.NotFound, sdkerrors.ErrUnknownAddress.Wrap(addr.String()).Error())
}

return nil
}

func (s queryServer) validateExistenceOfClassGRPC(ctx sdk.Context, id string) error {
if _, err := s.keeper.GetClass(ctx, id); err != nil {
return status.Error(codes.NotFound, err.Error())
Expand All @@ -60,11 +50,6 @@ func (s queryServer) Balance(c context.Context, req *token.QueryBalanceRequest)
}

ctx := sdk.UnwrapSDKContext(c)

if err := s.validateExistenceOfAccountGRPC(ctx, addr); err != nil {
return nil, err
}

balance := s.keeper.GetBalance(ctx, req.ContractId, addr)

return &token.QueryBalanceResponse{Amount: balance}, nil
Expand Down Expand Up @@ -167,10 +152,6 @@ func (s queryServer) GranteeGrants(c context.Context, req *token.QueryGranteeGra

ctx := sdk.UnwrapSDKContext(c)

if err := s.validateExistenceOfAccountGRPC(ctx, grantee); err != nil {
return nil, err
}

if err := s.validateExistenceOfClassGRPC(ctx, req.ContractId); err != nil {
return nil, err
}
Expand Down Expand Up @@ -212,13 +193,6 @@ func (s queryServer) IsOperatorFor(c context.Context, req *token.QueryIsOperator

ctx := sdk.UnwrapSDKContext(c)

if err := s.validateExistenceOfAccountGRPC(ctx, operator); err != nil {
return nil, err
}
if err := s.validateExistenceOfAccountGRPC(ctx, holder); err != nil {
return nil, err
}

if err := s.validateExistenceOfClassGRPC(ctx, req.ContractId); err != nil {
return nil, err
}
Expand Down
18 changes: 0 additions & 18 deletions x/token/keeper/grpc_query_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,6 @@ func (s *KeeperTestSuite) TestQueryBalance() {
"invalid address": {
contractID: s.contractID,
},
"address not found": {
contractID: s.contractID,
address: sdk.AccAddress("notfound"),
},
}

for name, tc := range testCases {
Expand Down Expand Up @@ -245,10 +241,6 @@ func (s *KeeperTestSuite) TestQueryGranteeGrants() {
contractID: "fee1dead",
grantee: s.vendor,
},
"grantee not found": {
contractID: s.contractID,
grantee: sdk.AccAddress("notfound"),
},
}

for name, tc := range testCases {
Expand Down Expand Up @@ -307,16 +299,6 @@ func (s *KeeperTestSuite) TestQueryIsOperatorFor() {
operator: s.operator,
holder: s.vendor,
},
"operator not found": {
contractID: s.contractID,
operator: sdk.AccAddress("notfound"),
holder: s.customer,
},
"holder not found": {
contractID: s.contractID,
operator: s.operator,
holder: sdk.AccAddress("notfound"),
},
}

for name, tc := range testCases {
Expand Down
5 changes: 1 addition & 4 deletions x/token/keeper/keeper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ func (s *KeeperTestSuite) SetupTest() {
s.goCtx = sdk.WrapSDKContext(s.ctx)
s.keeper = app.TokenKeeper

s.queryServer = keeper.NewQueryServer(s.keeper, app.AccountKeeper)
s.queryServer = keeper.NewQueryServer(s.keeper)
s.msgServer = keeper.NewMsgServer(s.keeper)

addresses := []*sdk.AccAddress{
Expand All @@ -68,9 +68,6 @@ func (s *KeeperTestSuite) SetupTest() {
}
for i, address := range createRandomAccounts(len(addresses)) {
*addresses[i] = address

acc := app.AccountKeeper.NewAccountWithAddress(s.ctx, address)
app.AccountKeeper.SetAccount(s.ctx, acc)
}

s.balance = sdk.NewInt(1000)
Expand Down
10 changes: 4 additions & 6 deletions x/token/module/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,15 +78,13 @@ func (b AppModuleBasic) RegisterInterfaces(registry codectypes.InterfaceRegistry
type AppModule struct {
AppModuleBasic

keeper keeper.Keeper
authKeeper token.AuthKeeper
keeper keeper.Keeper
}

// NewAppModule creates a new AppModule object
func NewAppModule(cdc codec.Codec, keeper keeper.Keeper, authKeeper token.AuthKeeper) AppModule {
func NewAppModule(cdc codec.Codec, keeper keeper.Keeper) AppModule {
return AppModule{
keeper: keeper,
authKeeper: authKeeper,
keeper: keeper,
}
}

Expand All @@ -108,7 +106,7 @@ func (am AppModule) LegacyQuerierHandler(legacyQuerierCdc *codec.LegacyAmino) sd
// module-specific GRPC queries.
func (am AppModule) RegisterServices(cfg module.Configurator) {
token.RegisterMsgServer(cfg.MsgServer(), keeper.NewMsgServer(am.keeper))
token.RegisterQueryServer(cfg.QueryServer(), keeper.NewQueryServer(am.keeper, am.authKeeper))
token.RegisterQueryServer(cfg.QueryServer(), keeper.NewQueryServer(am.keeper))

// m := keeper.NewMigrator(am.keeper)
// migrations := map[uint64]func(sdk.Context) error{}
Expand Down

0 comments on commit 2156936

Please sign in to comment.