Skip to content

Commit

Permalink
fix: unused code & name
Browse files Browse the repository at this point in the history
  • Loading branch information
djm07073 committed Jun 21, 2024
1 parent 1858a2a commit 9b76612
Show file tree
Hide file tree
Showing 9 changed files with 43 additions and 52 deletions.
9 changes: 1 addition & 8 deletions contrib/launchtools/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"encoding/json"
"os"
"path"
"path/filepath"
"sync"

"cosmossdk.io/log"
Expand Down Expand Up @@ -147,12 +146,6 @@ func NewLauncher(
artifactsDir string,
) *LauncherContext {

logger := log.NewLogger(os.Stderr)
minitiaDir := filepath.Join(clientCtx.HomeDir, ".minitia")
if _, err := os.Stat(minitiaDir); err == nil {
logger.Error("minitia directory already exists, please remove it first")
os.Exit(1)
}
kr, err := keyring.New("minitia", keyring.BackendTest, clientCtx.HomeDir, nil, clientCtx.Codec)
if err != nil {
panic("failed to create keyring")
Expand All @@ -176,7 +169,7 @@ func NewLauncher(
}

return &LauncherContext{
log: logger,
log: log.NewLogger(os.Stderr),
mtx: new(sync.Mutex),
clientCtx: &nextClientCtx,
serverCtx: serverCtx,
Expand Down
17 changes: 5 additions & 12 deletions x/ophost/client/cli/tx.go
Original file line number Diff line number Diff line change
Expand Up @@ -122,56 +122,49 @@ func NewCreateBridge(ac address.Codec) *cobra.Command {
origConfig := BridgeCliConfig{}
err = json.Unmarshal(configBytes, &origConfig)
if err != nil {
fmt.Println("Error unmarshaling config file:", err)

Check warning on line 125 in x/ophost/client/cli/tx.go

View check run for this annotation

Codecov / codecov/patch

x/ophost/client/cli/tx.go#L125

Added line #L125 was not covered by tests
return err
}

submissionInterval, err := time.ParseDuration(origConfig.SubmissionInterval)
if err != nil {
fmt.Println("Error parsing submission interval:", err)
return err
}

finalizationPeriod, err := time.ParseDuration(origConfig.FinalizationPeriod)
if err != nil {
fmt.Println("Error parsing finalization period:", err)
return err
}

submissionStartTime, err := time.Parse(time.RFC3339, origConfig.SubmissionStartTime)
if err != nil {
fmt.Println("Error parsing submission start time:", err)
return err
}

fmt.Println("Original Config:", origConfig)

config := types.BridgeConfig{
Challengers: []string{origConfig.Challenger}, // Ensure Challenger is properly assigned
Proposer: origConfig.Proposer,
BatchInfo: types.BatchInfo{Submitter: origConfig.BatchInfo.Submitter, Chain: origConfig.BatchInfo.Chain}, // Ensure Submitter is properly assigned
SubmissionInterval: submissionInterval,
FinalizationPeriod: finalizationPeriod,
SubmissionStartTime: submissionStartTime,
Metadata: []byte(origConfig.Metadata),
BatchInfo: types.BatchInfo(origConfig.BatchInfo),
}

fmt.Println("Converted Config:", config)

if err = config.Validate(ac); err != nil {
fmt.Println("Error validating config:", err)

Check warning on line 155 in x/ophost/client/cli/tx.go

View check run for this annotation

Codecov / codecov/patch

x/ophost/client/cli/tx.go#L155

Added line #L155 was not covered by tests
return err
}

fromAddr, err := ac.BytesToString(clientCtx.GetFromAddress())
if err != nil {
fmt.Println("Error getting from address:", err)

Check warning on line 161 in x/ophost/client/cli/tx.go

View check run for this annotation

Codecov / codecov/patch

x/ophost/client/cli/tx.go#L161

Added line #L161 was not covered by tests
return err
}

msg := types.NewMsgCreateBridge(fromAddr, config)
if err = msg.Validate(ac); err != nil {
fmt.Println("Error validating message:", err)

Check warning on line 167 in x/ophost/client/cli/tx.go

View check run for this annotation

Codecov / codecov/patch

x/ophost/client/cli/tx.go#L167

Added line #L167 was not covered by tests
return err
}

Expand Down
2 changes: 1 addition & 1 deletion x/ophost/keeper/common_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -340,7 +340,7 @@ func (h *bridgeHook) BridgeCreated(
return nil
}

func (h *bridgeHook) BridgeChallengerUpdated(
func (h *bridgeHook) BridgeChallengersUpdated(
ctx context.Context,
bridgeId uint64,
bridgeConfig ophosttypes.BridgeConfig,
Expand Down
2 changes: 1 addition & 1 deletion x/ophost/keeper/msg_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -466,7 +466,7 @@ func (ms MsgServer) UpdateChallenger(ctx context.Context, req *types.MsgUpdateCh
return nil, govtypes.ErrInvalidSigner.Wrapf("invalid authority; expected %s or in %s, got %s", ms.authority, config.Challengers, req.Authority)
}

if err := ms.Keeper.bridgeHook.BridgeChallengerUpdated(ctx, bridgeId, config); err != nil {
if err := ms.Keeper.bridgeHook.BridgeChallengersUpdated(ctx, bridgeId, config); err != nil {
return nil, err
}

Expand Down
4 changes: 2 additions & 2 deletions x/ophost/types/bridge_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ func (config BridgeConfig) Validate(ac address.Codec) error {
}

if config.BatchInfo.Submitter == "" {
return errors.Wrapf(sdkerrors.ErrInvalidRequest, "submitter must be non-empty array")
return errors.Wrapf(sdkerrors.ErrInvalidRequest, "batch submitter must be set")
}

if !config.isValidChallengers() {
Expand Down Expand Up @@ -62,7 +62,7 @@ func (config BridgeConfig) ValidateWithNoAddrValidation() error {
}

if config.BatchInfo.Submitter == "" {
return errors.Wrapf(sdkerrors.ErrInvalidRequest, "submitter must be non-empty array")
return errors.Wrapf(sdkerrors.ErrInvalidRequest, "batch submitter must be set")
}

if config.FinalizationPeriod == time.Duration(0) {
Expand Down
5 changes: 1 addition & 4 deletions x/ophost/types/genesis.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,13 +73,10 @@ func ValidateGenesis(data *GenesisState, ac address.Codec) error {
return ErrEmptyBatchInfo
}
// last batchinfo should be same with bridgeconfig batchinfo
if bridge.BatchInfos[len(bridge.BatchInfos)-1].BatchInfo.Chain != bridge.BridgeConfig.BatchInfo.Chain ||
if bridge.BatchInfos[len(bridge.BatchInfos)-1].BatchInfo != bridge.BridgeConfig.BatchInfo ||
!bridge.BatchInfos[0].Output.IsEmpty() {
return ErrInvalidBatchInfo
}
if bridge.BatchInfos[len(bridge.BatchInfos)-1].BatchInfo.Submitter != bridge.BridgeConfig.BatchInfo.Submitter {
return ErrInvalidBatchInfo
}
}

if data.NextBridgeId < DefaultBridgeIdStart {
Expand Down
7 changes: 5 additions & 2 deletions x/ophost/types/hook/bridge_hook.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package hook

import (
"context"
"errors"

"cosmossdk.io/core/address"
sdk "github.com/cosmos/cosmos-sdk/types"
Expand Down Expand Up @@ -49,7 +50,9 @@ func (h BridgeHook) BridgeCreated(
}

Check warning on line 50 in x/ophost/types/hook/bridge_hook.go

View check run for this annotation

Codecov / codecov/patch

x/ophost/types/hook/bridge_hook.go#L49-L50

Added lines #L49 - L50 were not covered by tests
challengers = append(challengers, challenger)
}

if len(challengers) != 1 {
return errors.New("only one challenger is allowed on bridge creation")
}
sdkCtx := sdk.UnwrapSDKContext(ctx)
for _, permChannel := range metadata.PermChannels {
portID, channelID := permChannel.PortID, permChannel.ChannelID
Expand All @@ -66,7 +69,7 @@ func (h BridgeHook) BridgeCreated(
return nil
}

func (h BridgeHook) BridgeChallengerUpdated(
func (h BridgeHook) BridgeChallengersUpdated(
ctx context.Context,
bridgeId uint64,
bridgeConfig ophosttypes.BridgeConfig,
Expand Down
43 changes: 24 additions & 19 deletions x/ophost/types/hook/bridge_hook_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,29 +32,34 @@ func (k MockChannelKeeper) GetNextSequenceSend(ctx sdk.Context, portID, channelI
return seq, ok
}

type MockPermRelayerList struct {
Relayers []sdk.AccAddress
type MockPermissionRelayers struct {
Relayers []string
}

func (l MockPermRelayerList) Equals(relayer sdk.AccAddress) bool {
func (l MockPermissionRelayers) HasRelayer(relayer string) bool {
for _, r := range l.Relayers {
if r.Equals(relayer) {
if r == relayer {
return true
}
}
return false
}

type MockPermKeeper struct {
perms map[string]MockPermRelayerList
perms map[string]MockPermissionRelayers
ac address.Bech32Codec
}

func (k MockPermKeeper) HasPermission(ctx context.Context, portID, channelID string, relayer sdk.AccAddress) (bool, error) {
return k.perms[portID+"/"+channelID].Equals(relayer), nil
return k.perms[portID+"/"+channelID].HasRelayer(relayer.String()), nil
}

func (k MockPermKeeper) SetPermissionedRelayers(ctx context.Context, portID, channelID string, relayers []sdk.AccAddress) error {
k.perms[portID+"/"+channelID] = MockPermRelayerList{Relayers: relayers}
var relayersStr []string
for _, r := range relayers {
relayersStr = append(relayersStr, r.String())
}
k.perms[portID+"/"+channelID] = MockPermissionRelayers{Relayers: relayersStr}
return nil
}

Expand All @@ -66,7 +71,7 @@ func setup() (context.Context, hook.BridgeHook) {
"transfer/channel-2": 1,
},
}, MockPermKeeper{
perms: make(map[string]MockPermRelayerList),
perms: make(map[string]MockPermissionRelayers),
}, address.NewBech32Codec(sdk.GetConfig().GetBech32AccountAddrPrefix()))

ms := store.NewCommitMultiStore(dbm.NewMemDB(), log.NewNopLogger(), metrics.NewNoOpMetrics())
Expand Down Expand Up @@ -107,12 +112,12 @@ func Test_BridgeHook_BridgeCreated(t *testing.T) {

addr := acc_addr()
err = h.BridgeCreated(ctx, 1, ophosttypes.BridgeConfig{
Challengers: []string{addr[0].String(), addr[1].String()},
Challengers: []string{addr[0].String()},
Metadata: metadata,
})
require.NoError(t, err)

// cannot take non-1 sequence channel
// challenger is only one when creating bridge
err = h.BridgeCreated(ctx, 1, ophosttypes.BridgeConfig{
Challengers: []string{addr[0].String(), addr[1].String()},
Metadata: metadata2,
Expand All @@ -123,17 +128,13 @@ func Test_BridgeHook_BridgeCreated(t *testing.T) {
ok, err := h.IBCPermKeeper.HasPermission(ctx, "transfer", "channel-0", addr[0])
require.NoError(t, err)
require.True(t, ok)
ok, err = h.IBCPermKeeper.HasPermission(ctx, "transfer", "channel-1", addr[1])
require.NoError(t, err)
require.False(t, ok)

// check permission is applied
ok, err = h.IBCPermKeeper.HasPermission(ctx, "transfer", "channel-1", addr[0])
ok, err = h.IBCPermKeeper.HasPermission(ctx, "transfer", "channel-0", addr[1])
require.NoError(t, err)
require.False(t, ok)
}

func Test_BridgeHook_ChallengerUpdated(t *testing.T) {
func Test_BridgeHook_ChallengersUpdated(t *testing.T) {
ctx, h := setup()

metadata, err := json.Marshal(hook.PermsMetadata{
Expand All @@ -148,13 +149,13 @@ func Test_BridgeHook_ChallengerUpdated(t *testing.T) {

addr := acc_addr()
err = h.BridgeCreated(ctx, 1, ophosttypes.BridgeConfig{
Challengers: []string{addr[0].String(), addr[1].String()},
Challengers: []string{addr[0].String()},
Metadata: metadata,
})
require.NoError(t, err)

newAddr := acc_addr()
err = h.BridgeChallengerUpdated(ctx, 1, ophosttypes.BridgeConfig{
err = h.BridgeChallengersUpdated(ctx, 1, ophosttypes.BridgeConfig{
Challengers: []string{newAddr[0].String(), newAddr[1].String()},
Metadata: metadata,
})
Expand All @@ -168,6 +169,10 @@ func Test_BridgeHook_ChallengerUpdated(t *testing.T) {
ok, err = h.IBCPermKeeper.HasPermission(ctx, "transfer", "channel-0", newAddr[1])
require.NoError(t, err)
require.True(t, ok)

ok, err = h.IBCPermKeeper.HasPermission(ctx, "transfer", "channel-0", addr[0])
require.NoError(t, err)
require.False(t, ok)
}

func Test_BridgeHook_MetadataUpdated(t *testing.T) {
Expand All @@ -185,7 +190,7 @@ func Test_BridgeHook_MetadataUpdated(t *testing.T) {

addr := acc_addr()
err = h.BridgeCreated(ctx, 1, ophosttypes.BridgeConfig{
Challengers: []string{addr[0].String(), addr[1].String()},
Challengers: []string{addr[0].String()},
Metadata: metadata,
})
require.NoError(t, err)
Expand Down
6 changes: 3 additions & 3 deletions x/ophost/types/hooks.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ type BridgeHook interface {
bridgeId uint64,
bridgeConfig BridgeConfig,
) error
BridgeChallengerUpdated(
BridgeChallengersUpdated(
ctx context.Context,
bridgeId uint64,
bridgeConfig BridgeConfig,
Expand Down Expand Up @@ -52,13 +52,13 @@ func (hooks BridgeHooks) BridgeCreated(
return nil
}

func (hooks BridgeHooks) BridgeChallengerUpdated(
func (hooks BridgeHooks) BridgeChallengersUpdated(
ctx context.Context,
bridgeId uint64,
bridgeConfig BridgeConfig,
) error {
for _, h := range hooks {
if err := h.BridgeChallengerUpdated(ctx, bridgeId, bridgeConfig); err != nil {
if err := h.BridgeChallengersUpdated(ctx, bridgeId, bridgeConfig); err != nil {

Check warning on line 61 in x/ophost/types/hooks.go

View check run for this annotation

Codecov / codecov/patch

x/ophost/types/hooks.go#L61

Added line #L61 was not covered by tests
return err
}
}
Expand Down

0 comments on commit 9b76612

Please sign in to comment.