diff --git a/x/babylon/keeper/wasm.go b/x/babylon/keeper/wasm.go index 7fcf16b..20561ad 100644 --- a/x/babylon/keeper/wasm.go +++ b/x/babylon/keeper/wasm.go @@ -35,12 +35,37 @@ func (k Keeper) getBTCStakingContractAddr(ctx sdk.Context) sdk.AccAddress { return addr } -// SendBeginBlockMsg sends a BeginBlock sudo message to the BTC staking contract via sudo +func (k Keeper) getBTCFinalityContractAddr(ctx sdk.Context) sdk.AccAddress { + // get address of the BTC finality contract + addrStr := k.GetParams(ctx).BtcFinalityContractAddress + if len(addrStr) == 0 { + // the BTC finality contract address is not set yet, skip sending BeginBlockMsg + return nil + } + addr, err := sdk.AccAddressFromBech32(addrStr) + if err != nil { + // Although this is a programming error so we should panic, we emit + // a warning message to minimise the impact on the consumer chain's operation + k.Logger(ctx).Warn("the BTC finality contract address is malformed", "contract", addrStr, "error", err) + return nil + } + if !k.wasm.HasContractInfo(ctx, addr) { + // NOTE: it's possible that the default contract address does not correspond to + // any contract. We emit a warning message rather than panic to minimise the + // impact on the consumer chain's operation + k.Logger(ctx).Warn("the BTC finality contract address is not on-chain", "contract", addrStr) + return nil + } + + return addr +} + +// SendBeginBlockMsg sends a BeginBlock sudo message to the BTC finality contract via sudo func (k Keeper) SendBeginBlockMsg(c context.Context) error { ctx := sdk.UnwrapSDKContext(c) - // try to get and parse BTC staking contract - addr := k.getBTCStakingContractAddr(ctx) + // try to get and parse BTC finality contract + addr := k.getBTCFinalityContractAddr(ctx) if addr == nil { return nil } @@ -58,12 +83,12 @@ func (k Keeper) SendBeginBlockMsg(c context.Context) error { return k.doSudoCall(ctx, addr, msg) } -// SendEndBlockMsg sends a EndBlock sudo message to the BTC staking contract via sudo +// SendEndBlockMsg sends a EndBlock sudo message to the BTC finality contract via sudo func (k Keeper) SendEndBlockMsg(c context.Context) error { ctx := sdk.UnwrapSDKContext(c) - // try to get and parse BTC staking contract - addr := k.getBTCStakingContractAddr(ctx) + // try to get and parse BTC finality contract + addr := k.getBTCFinalityContractAddr(ctx) if addr == nil { return nil }