Repository: unionlabs/union
File: lib/chain-utils/src/cosmos.rs (Rust)

Code before:

            let height = commit.signed_header.header.height;

            let unbonding_period = std::time::Duration::new(
                params
                    .unbonding_time
                    .clone()
                    .unwrap()
                    .seconds
                    .try_into()
                    .unwrap(),
                params
                    .unbonding_time
                    .clone()
                    .unwrap()
                    .nanos
                    .try_into()
                    .unwrap(),
            );

            tendermint::client_state::ClientState {
                chain_id: self.chain_id(),
                // https://github.com/cometbft/cometbft/blob/da0e55604b075bac9e1d5866cb2e62eaae386dd9/light/verifier.go#L16
                trust_level: Fraction {
                    numerator: 1,
                    denominator: NonZeroU64::new(3).unwrap(),
                },
                // https://github.com/cosmos/relayer/blob/23d1e5c864b35d133cad6a0ef06970a2b1e1b03f/relayer/chains/cosmos/provider.go#L177
                trusting_period: unionlabs::google::protobuf::duration::Duration::new(
                    (unbonding_period * 85 / 100).as_secs().try_into().unwrap(),
                    (unbonding_period * 85 / 100)
                        .subsec_nanos()
                        .try_into()
                        .unwrap(),
                )
                .unwrap(),
                unbonding_period: unionlabs::google::protobuf::duration::Duration::new(
                    unbonding_period.as_secs().try_into().unwrap(),
                    unbonding_period.subsec_nanos().try_into().unwrap(),
                )
                .unwrap(),
                // https://github.com/cosmos/relayer/blob/23d1e5c864b35d133cad6a0ef06970a2b1e1b03f/relayer/chains/cosmos/provider.go#L177
                max_clock_drift: unionlabs::google::protobuf::duration::Duration::new(60 * 10, 0)
                    .unwrap(),
                frozen_height: None,
                latest_height: Height {
                    revision_number: self.chain_revision,
                    revision_height: height.value(),
                },
                proof_specs: SDK_SPECS.into(),
                upgrade_path: vec!["upgrade".into(), "upgradedIBCState".into()],

Review comment (bug):
Small nit, new is const, so you can do `promote!(option_unwrap!(NonZeroU64: NonZeroU64::new(3)))`, which will force the expression to be evaluated at compile time (promote) and fail if the value is zero (option_unwrap). These macros are both in unionlabs

Code after:
                .unwrap();

            let height = commit.signed_header.header.height;

            let unbonding_period = std::time::Duration::new(
                params
                    .unbonding_time
                    .clone()
                    .unwrap()
                    .seconds
                    .try_into()
                    .unwrap(),
                params
                    .unbonding_time
                    .clone()
                    .unwrap()
                    .nanos
                    .try_into()
                    .unwrap(),
            );

            tendermint::client_state::ClientState {
                chain_id: self.chain_id(),
                // https://github.com/cometbft/cometbft/blob/da0e55604b075bac9e1d5866cb2e62eaae386dd9/light/verifier.go#L16
                trust_level: Fraction {
                    numerator: 1,
                    denominator: promote!(NonZeroU64: option_unwrap!(NonZeroU64::new(3))),
                },
                // https://github.com/cosmos/relayer/blob/23d1e5c864b35d133cad6a0ef06970a2b1e1b03f/relayer/chains/cosmos/provider.go#L177
                trusting_period: unionlabs::google::protobuf::duration::Duration::new(
                    (unbonding_period * 85 / 100).as_secs().try_into().unwrap(),
                    (unbonding_period * 85 / 100)
                        .subsec_nanos()
                        .try_into()
                        .unwrap(),
                )
                .unwrap(),
                unbonding_period: unionlabs::google::protobuf::duration::Duration::new(
                    unbonding_period.as_secs().try_into().unwrap(),
                    unbonding_period.subsec_nanos().try_into().unwrap(),
                )
                .unwrap(),
                // https://github.com/cosmos/relayer/blob/23d1e5c864b35d133cad6a0ef06970a2b1e1b03f/relayer/chains/cosmos/provider.go#L177
                max_clock_drift: unionlabs::google::protobuf::duration::Duration::new(60 * 10, 0)
                    .unwrap(),
                frozen_height: None,
                latest_height: Height {
                    revision_number: self.chain_revision,
                    revision_height: height.value(),
                },
                proof_specs: SDK_SPECS.into(),