-
Notifications
You must be signed in to change notification settings - Fork 41
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'main' into 1749-support-setting-a-NAV-attribute-on-meta…
…data-scopes
- Loading branch information
Showing
10 changed files
with
150 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
package app | ||
|
||
import ( | ||
"context" | ||
|
||
sdk "github.com/cosmos/cosmos-sdk/types" | ||
"github.com/cosmos/cosmos-sdk/x/group" | ||
) | ||
|
||
// GroupCheckerFunc convenient type to match the GroupChecker interface. | ||
type GroupCheckerFunc func(sdk.Context, sdk.AccAddress) bool | ||
|
||
// GroupPolicyQuerier provides functionality to query group policies. | ||
type GroupPolicyQuerier interface { | ||
GroupPolicyInfo(goCtx context.Context, request *group.QueryGroupPolicyInfoRequest) (*group.QueryGroupPolicyInfoResponse, error) | ||
} | ||
|
||
// IsGroupAddress checks if the account is a group address. | ||
func (t GroupCheckerFunc) IsGroupAddress(ctx sdk.Context, account sdk.AccAddress) bool { | ||
if account == nil { | ||
return false | ||
} | ||
return t(ctx, account) | ||
} | ||
|
||
// NewGroupCheckerFunc creates a new GroupChecker function for checking if an account is in a group. | ||
func NewGroupCheckerFunc(querier GroupPolicyQuerier) GroupCheckerFunc { | ||
return GroupCheckerFunc(func(ctx sdk.Context, account sdk.AccAddress) bool { | ||
msg := &group.QueryGroupPolicyInfoRequest{Address: account.String()} | ||
goCtx := sdk.WrapSDKContext(ctx) | ||
_, err := querier.GroupPolicyInfo(goCtx, msg) | ||
return err == nil | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
package app | ||
|
||
import ( | ||
"context" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
|
||
cerrs "cosmossdk.io/errors" | ||
sdk "github.com/cosmos/cosmos-sdk/types" | ||
"github.com/cosmos/cosmos-sdk/x/group" | ||
tmproto "github.com/tendermint/tendermint/proto/tendermint/types" | ||
) | ||
|
||
func TestNewGroupCheckerFunc(t *testing.T) { | ||
querier := NewMockGroupPolicyQuerier(true) | ||
checker := NewGroupCheckerFunc(querier) | ||
assert.NotNil(t, checker, "should return a group checker function") | ||
} | ||
|
||
func TestIsGroupAddress(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
querySuccess bool | ||
address sdk.AccAddress | ||
}{ | ||
{ | ||
name: "should be true with group address", | ||
querySuccess: true, | ||
address: sdk.AccAddress("test"), | ||
}, | ||
{ | ||
name: "should return false with non group address", | ||
querySuccess: false, | ||
address: sdk.AccAddress("test"), | ||
}, | ||
{ | ||
name: "should return false with nil address", | ||
querySuccess: false, | ||
address: nil, | ||
}, | ||
} | ||
|
||
for _, tc := range tests { | ||
t.Run(tc.name, func(t *testing.T) { | ||
querier := NewMockGroupPolicyQuerier(tc.querySuccess) | ||
checker := NewGroupCheckerFunc(querier) | ||
ctx := sdk.NewContext(nil, tmproto.Header{}, true, nil) | ||
success := checker.IsGroupAddress(ctx, tc.address) | ||
assert.Equal(t, tc.querySuccess, success, "should correctly detect if the supplied address is a group address") | ||
}) | ||
} | ||
} | ||
|
||
// MockGroupPolicyQuerier mocks the querier so a GroupKeeper isn't needed. | ||
type MockGroupPolicyQuerier struct { | ||
isGroupAddress bool | ||
} | ||
|
||
// NewMockGroupPolicyQuerier creates a new MockGroupPolicyQuerier. | ||
func NewMockGroupPolicyQuerier(isGroupAddress bool) *MockGroupPolicyQuerier { | ||
return &MockGroupPolicyQuerier{ | ||
isGroupAddress: isGroupAddress, | ||
} | ||
} | ||
|
||
// GroupPolicyInfo provides a stubbed implementation of the GroupPolicyInfo method. | ||
func (t MockGroupPolicyQuerier) GroupPolicyInfo(goCtx context.Context, request *group.QueryGroupPolicyInfoRequest) (*group.QueryGroupPolicyInfoResponse, error) { | ||
var err error | ||
if !t.isGroupAddress { | ||
err = cerrs.New("", 1, "") | ||
} | ||
return nil, err | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters