Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make tx channel-upgrade init safer #147

Merged
merged 2 commits into from
Oct 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 12 additions & 14 deletions cmd/tx.go
Original file line number Diff line number Diff line change
Expand Up @@ -254,16 +254,9 @@ func channelUpgradeInitCmd(ctx *config.Context) *cobra.Command {
}

// check cp state
if unsafe, err := cmd.Flags().GetBool(flagUnsafe); err != nil {
permitUnsafe, err := cmd.Flags().GetBool(flagUnsafe)
if err != nil {
return err
} else if !unsafe {
if height, err := cp.LatestHeight(); err != nil {
return err
} else if chann, err := cp.QueryChannel(core.NewQueryContext(cmd.Context(), height)); err != nil {
return err
} else if state := chann.Channel.State; state >= chantypes.FLUSHING && state <= chantypes.FLUSHCOMPLETE {
return fmt.Errorf("stop channel upgrade initialization because the counterparty is in %v state", state)
}
}

// get ordering from flags
Expand All @@ -286,11 +279,16 @@ func channelUpgradeInitCmd(ctx *config.Context) *cobra.Command {
return err
}

return core.InitChannelUpgrade(chain, chantypes.UpgradeFields{
Ordering: ordering,
ConnectionHops: connHops,
Version: version,
})
return core.InitChannelUpgrade(
chain,
cp,
chantypes.UpgradeFields{
Ordering: ordering,
ConnectionHops: connHops,
Version: version,
},
permitUnsafe,
)
},
}

Expand Down
32 changes: 31 additions & 1 deletion core/channel-upgrade.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,10 +75,40 @@ func (action UpgradeAction) String() string {
}

// InitChannelUpgrade builds `MsgChannelUpgradeInit` based on the specified UpgradeFields and sends it to the specified chain.
func InitChannelUpgrade(chain *ProvableChain, upgradeFields chantypes.UpgradeFields) error {
func InitChannelUpgrade(chain, cp *ProvableChain, upgradeFields chantypes.UpgradeFields, permitUnsafe bool) error {
logger := GetChannelLogger(chain.Chain)
defer logger.TimeTrack(time.Now(), "InitChannelUpgrade")

if h, err := chain.LatestHeight(); err != nil {
logger.Error("failed to get the latest height", err)
return err
} else if cpH, err := cp.LatestHeight(); err != nil {
logger.Error("failed to get the latest height of the counterparty chain", err)
return err
} else if chann, cpChann, err := QueryChannelPair(
NewQueryContext(context.TODO(), h),
NewQueryContext(context.TODO(), cpH),
chain,
cp,
false,
); err != nil {
logger.Error("failed to query for the channel pair", err)
return err
} else if chann.Channel.State != chantypes.OPEN || cpChann.Channel.State != chantypes.OPEN {
logger = &log.RelayLogger{Logger: logger.With(
"channel_state", chann.Channel.State,
"cp_channel_state", cpChann.Channel.State,
)}

if permitUnsafe {
logger.Info("unsafe channel upgrade is permitted")
} else {
err := errors.New("unsafe channel upgrade initialization")
logger.Error("unsafe channel upgrade is not permitted", err)
return err
}
}

addr, err := chain.GetAddress()
if err != nil {
logger.Error("failed to get address", err)
Expand Down
4 changes: 4 additions & 0 deletions tests/cases/docker-compose-test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ services:
retries: 5
networks:
- *network-common
environment:
IBC_CHANNEL_UPGRADE_TIMEOUT: 20000000000
tendermint-chain1: &tendermint-chain1
container_name: tendermint-chain1
image: tendermint-chain1:${TAG}
Expand All @@ -35,6 +37,8 @@ services:
retries: 5
networks:
- *network-common
environment:
IBC_CHANNEL_UPGRADE_TIMEOUT: 20000000000
tendermint-chain0-mock:
<<: *tendermint-chain0
container_name: tendermint-chain0-mock
Expand Down
1 change: 0 additions & 1 deletion tests/chains/tendermint/scripts/entrypoint.sh
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
#!/bin/sh

export IBC_AUTHORITY=`jq -r .address /root/${CHAINDIR}/${CHAINID}/key_seed.json`
export IBC_CHANNEL_UPGRADE_TIMEOUT=20000000000 # 20sec = 20_000_000_000nsec

simd --home /root/${CHAINDIR}/${CHAINID} start --pruning=nothing --grpc.address="0.0.0.0:${GRPCPORT}"
Loading