Skip to content

Commit

Permalink
fix(observer): remove error return in IsAuthorized (#1250)
Browse files Browse the repository at this point in the history
* update function

* regenerate interfaces

* update for crosschain
  • Loading branch information
lumtis authored Oct 10, 2023
1 parent 996f35d commit 23d7724
Show file tree
Hide file tree
Showing 18 changed files with 34 additions and 54 deletions.
2 changes: 1 addition & 1 deletion testutil/keeper/mocks/crosschain/account.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion testutil/keeper/mocks/crosschain/bank.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion testutil/keeper/mocks/crosschain/fungible.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 3 additions & 13 deletions testutil/keeper/mocks/crosschain/observer.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion testutil/keeper/mocks/crosschain/staking.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion testutil/keeper/mocks/fungible/account.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion testutil/keeper/mocks/fungible/bank.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion testutil/keeper/mocks/fungible/evm.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion testutil/keeper/mocks/fungible/observer.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 4 additions & 6 deletions x/crosschain/keeper/keeper_chain_nonces.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,12 @@ import (
"context"
"fmt"

zetaObserverTypes "github.com/zeta-chain/zetacore/x/observer/types"

"github.com/cosmos/cosmos-sdk/store/prefix"
sdk "github.com/cosmos/cosmos-sdk/types"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
"github.com/cosmos/cosmos-sdk/types/query"
"github.com/zeta-chain/zetacore/x/crosschain/types"
observertypes "github.com/zeta-chain/zetacore/x/observer/types"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
)
Expand Down Expand Up @@ -108,12 +107,11 @@ func (k msgServer) NonceVoter(goCtx context.Context, msg *types.MsgNonceVoter) (
ctx := sdk.UnwrapSDKContext(goCtx)
chain := k.zetaObserverKeeper.GetParams(ctx).GetChainFromChainID(msg.ChainId)
if chain == nil {
return nil, zetaObserverTypes.ErrSupportedChains
return nil, observertypes.ErrSupportedChains
}

ok, err := k.zetaObserverKeeper.IsAuthorized(ctx, msg.Creator, chain)
if !ok {
return nil, err
if ok := k.zetaObserverKeeper.IsAuthorized(ctx, msg.Creator, chain); !ok {
return nil, observertypes.ErrNotAuthorizedPolicy
}
chainNonce, isFound := k.GetChainNonces(ctx, chain.ChainName.String())

Expand Down
5 changes: 2 additions & 3 deletions x/crosschain/keeper/keeper_cross_chain_tx_vote_inbound_tx.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,9 +77,8 @@ func (k msgServer) VoteOnObservedInboundTx(goCtx context.Context, msg *types.Msg
tssPub = tss.TssPubkey
}
// IsAuthorized does various checks against the list of observer mappers
ok, err := k.zetaObserverKeeper.IsAuthorized(ctx, msg.Creator, observationChain)
if !ok {
return nil, err
if ok := k.zetaObserverKeeper.IsAuthorized(ctx, msg.Creator, observationChain); !ok {
return nil, observerTypes.ErrNotAuthorizedPolicy
}

index := msg.Digest()
Expand Down
5 changes: 2 additions & 3 deletions x/crosschain/keeper/keeper_cross_chain_tx_vote_outbound_tx.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,9 +73,8 @@ func (k msgServer) VoteOnObservedOutboundTx(goCtx context.Context, msg *types.Ms
return nil, err
}
//Check is msg.Creator is authorized to vote
ok, err := k.zetaObserverKeeper.IsAuthorized(ctx, msg.Creator, observationChain)
if !ok {
return nil, err
if ok := k.zetaObserverKeeper.IsAuthorized(ctx, msg.Creator, observationChain); !ok {
return nil, observerTypes.ErrNotAuthorizedPolicy
}

// Check if CCTX exists
Expand Down
9 changes: 4 additions & 5 deletions x/crosschain/keeper/keeper_gas_price.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import (
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
"github.com/cosmos/cosmos-sdk/types/query"
"github.com/zeta-chain/zetacore/x/crosschain/types"
zetaObserverTypes "github.com/zeta-chain/zetacore/x/observer/types"
observertypes "github.com/zeta-chain/zetacore/x/observer/types"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
)
Expand Down Expand Up @@ -127,11 +127,10 @@ func (k msgServer) GasPriceVoter(goCtx context.Context, msg *types.MsgGasPriceVo

chain := k.zetaObserverKeeper.GetParams(ctx).GetChainFromChainID(msg.ChainId)
if chain == nil {
return nil, zetaObserverTypes.ErrSupportedChains
return nil, observertypes.ErrSupportedChains
}
ok, err := k.zetaObserverKeeper.IsAuthorized(ctx, msg.Creator, chain)
if !ok {
return nil, err
if ok := k.zetaObserverKeeper.IsAuthorized(ctx, msg.Creator, chain); !ok {
return nil, observertypes.ErrNotAuthorizedPolicy
}
if chain == nil {
return nil, sdkerrors.Wrap(types.ErrUnsupportedChain, fmt.Sprintf("ChainID : %d ", msg.ChainId))
Expand Down
7 changes: 2 additions & 5 deletions x/crosschain/keeper/keeper_out_tx_tracker.go
Original file line number Diff line number Diff line change
Expand Up @@ -173,11 +173,8 @@ func (k msgServer) AddToOutTxTracker(goCtx context.Context, msg *types.MsgAddToO
adminPolicyAccount := k.zetaObserverKeeper.GetParams(ctx).GetAdminPolicyAccount(observertypes.Policy_Type_group1)
isAdmin := msg.Creator == adminPolicyAccount

isObserver, err := k.zetaObserverKeeper.IsAuthorized(ctx, msg.Creator, chain)
if err != nil {
ctx.Logger().Error("Error while checking if the account is an observer", err)
return nil, cosmoserrors.Wrap(observertypes.ErrNotAuthorized, fmt.Sprintf("error IsAuthorized %s", msg.Creator))
}
isObserver := k.zetaObserverKeeper.IsAuthorized(ctx, msg.Creator, chain)

// Sender needs to be either the admin policy account or an observer
if !(isAdmin || isObserver) {
return nil, cosmoserrors.Wrap(observertypes.ErrNotAuthorized, fmt.Sprintf("Creator %s", msg.Creator))
Expand Down
2 changes: 1 addition & 1 deletion x/crosschain/types/expected_keepers.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ type ZetaObserverKeeper interface {
SetLastObserverCount(ctx sdk.Context, lbc *zetaObserverTypes.LastObserverCount)
AddVoteToBallot(ctx sdk.Context, ballot zetaObserverTypes.Ballot, address string, observationType zetaObserverTypes.VoteType) (zetaObserverTypes.Ballot, error)
CheckIfFinalizingVote(ctx sdk.Context, ballot zetaObserverTypes.Ballot) (zetaObserverTypes.Ballot, bool)
IsAuthorized(ctx sdk.Context, address string, chain *common.Chain) (bool, error)
IsAuthorized(ctx sdk.Context, address string, chain *common.Chain) bool
FindBallot(ctx sdk.Context, index string, chain *common.Chain, observationType zetaObserverTypes.ObservationType) (ballot zetaObserverTypes.Ballot, isNew bool, err error)
AddBallotToList(ctx sdk.Context, ballot zetaObserverTypes.Ballot)
GetBlockHeader(ctx sdk.Context, hash []byte) (val common.BlockHeader, found bool)
Expand Down
8 changes: 4 additions & 4 deletions x/observer/keeper/keeper_utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,17 +32,17 @@ func (k Keeper) CheckIfFinalizingVote(ctx sdk.Context, ballot types.Ballot) (typ
}

// IsAuthorized checks whether a signer is authorized to sign , by checking their address against the observer mapper which contains the observer list for the chain and type
func (k Keeper) IsAuthorized(ctx sdk.Context, address string, chain *common.Chain) (bool, error) {
func (k Keeper) IsAuthorized(ctx sdk.Context, address string, chain *common.Chain) bool {
observerMapper, found := k.GetObserverMapper(ctx, chain)
if !found {
return false, errors.Wrap(types.ErrNotAuthorized, fmt.Sprintf("observer list not present for chain %s", chain.String()))
return false
}
for _, obs := range observerMapper.ObserverList {
if obs == address {
return true, nil
return true
}
}
return false, errors.Wrap(types.ErrNotAuthorized, fmt.Sprintf("address: %s", address))
return false
}

func (k Keeper) FindBallot(ctx sdk.Context, index string, chain *common.Chain, observationType types.ObservationType) (ballot types.Ballot, isNew bool, err error) {
Expand Down
5 changes: 2 additions & 3 deletions x/observer/keeper/msg_server_add_blame_vote.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,8 @@ func (k msgServer) AddBlameVote(goCtx context.Context, vote *types.MsgAddBlameVo
return nil, sdkerrors.Wrap(crosschainTypes.ErrUnsupportedChain, fmt.Sprintf("ChainID %d, Blame vote", vote.ChainId))
}
// IsAuthorized does various checks against the list of observer mappers
ok, err := k.IsAuthorized(ctx, vote.Creator, observationChain)
if !ok {
return nil, err
if ok := k.IsAuthorized(ctx, vote.Creator, observationChain); !ok {
return nil, types.ErrNotAuthorizedPolicy
}

index := vote.Digest()
Expand Down
5 changes: 2 additions & 3 deletions x/observer/keeper/msg_server_add_block_header.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,8 @@ func (k msgServer) AddBlockHeader(goCtx context.Context, msg *types.MsgAddBlockH

// check authorization for this chain
chain := common.GetChainFromChainID(msg.ChainId)
ok, err := k.IsAuthorized(ctx, msg.Creator, chain)
if !ok {
return nil, cosmoserrors.Wrap(types.ErrNotAuthorizedPolicy, err.Error())
if ok := k.IsAuthorized(ctx, msg.Creator, chain); !ok {
return nil, types.ErrNotAuthorizedPolicy
}

// add vote to ballot
Expand Down

0 comments on commit 23d7724

Please sign in to comment.