Skip to content

Commit

Permalink
refactor instantiate logic
Browse files Browse the repository at this point in the history
  • Loading branch information
SebastianElvis committed Jan 8, 2025
1 parent a9393e7 commit feb7adc
Show file tree
Hide file tree
Showing 15 changed files with 141 additions and 292 deletions.
22 changes: 3 additions & 19 deletions contrib/images/ibcsim-bcd/setup-bcd.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

display_usage() {
echo "Missing parameters. Please check if all parameters were specified."
echo "Usage: setup-bcd.sh [CHAIN_ID] [CHAIN_DIR] [RPC_PORT] [P2P_PORT] [PROFILING_PORT] [GRPC_PORT] [BABYLON_CONTRACT_CODE_FILE] [BTCSTAKING_CONTRACT_CODE_FILE] [BTCFINALITY_CONTRACT_CODE_FILE] [INSTANTIATING_CFG]"
echo "Example: setup-bcd.sh test-chain-id ./data 26657 26656 6060 9090 ./babylon_contract.wasm '{"btc_confirmation_depth":1,"checkpoint_finalization_timeout":2,"network":"Regtest","babylon_tag":"bbn0", "notify_cosmos_zone":false, "btc_staking_code_id":2}'"
echo "Usage: setup-bcd.sh [CHAIN_ID] [CHAIN_DIR] [RPC_PORT] [P2P_PORT] [PROFILING_PORT] [GRPC_PORT] [BABYLON_CONTRACT_CODE_FILE] [BTCSTAKING_CONTRACT_CODE_FILE] [BTCFINALITY_CONTRACT_CODE_FILE]"
echo "Example: setup-bcd.sh test-chain-id ./data 26657 26656 6060 9090 ./babylon_contract.wasm"
exit 1
}

Expand Down Expand Up @@ -35,7 +35,6 @@ GRPCPORT=$6
BABYLON_CONTRACT_CODE_FILE=$7
BTCSTAKING_CONTRACT_CODE_FILE=$8
BTCFINALITY_CONTRACT_CODE_FILE=$9
INSTANTIATING_CFG=${10}

# ensure the binary exists
if ! command -v $BINARY &>/dev/null; then
Expand Down Expand Up @@ -88,21 +87,6 @@ sed -i 's#"tcp://0.0.0.0:1317"#"tcp://0.0.0.0:1318"#g' $CHAINDIR/$CHAINID/config
sed -i 's/"bond_denom": "stake"/"bond_denom": "'"$DENOM"'"/g' $CHAINDIR/$CHAINID/config/genesis.json
# sed -i '' 's#index-events = \[\]#index-events = \["message.action","send_packet.packet_src_channel","send_packet.packet_sequence"\]#g' $CHAINDIR/$CHAINID/config/app.toml

## Script for getting contract addresses
## TODO(euphrates): pass a gov prop on setting the Babylon / BTC staking contract addresses
# babylonContractAddr=$(bcd query wasm list-contract-by-code 1 -o json | jq -r '.contracts[0]')
# btcStakingContractAddr=$(bcd query wasm list-contract-by-code 2 -o json | jq -r '.contracts[0]')
# echo "babylonContractAddr is $babylonContractAddr"
# echo "btcStakingContractAddr is $btcStakingContractAddr"

# update contract address in genesis
babylonContractAddr=bbnc14hj2tavq8fpesdwxxcu44rty3hh90vhujrvcmstl4zr3txmfvw9syx25zf
btcStakingContractAddr=bbnc1nc5tatafv6eyq7llkr2gv50ff9e22mnf70qgjlv737ktmt4eswrqgn0kq0
btcFinalityContractAddr=bbnc17p9rzwnnfxcjp32un9ug7yhhzgtkhvl9jfksztgw5uh69wac2pgssg3nft
sed -i 's/"babylon_contract_address": ""/"babylon_contract_address": "'"$babylonContractAddr"'"/g' $CHAINDIR/$CHAINID/config/genesis.json
sed -i 's/"btc_staking_contract_address": ""/"btc_staking_contract_address": "'"$btcStakingContractAddr"'"/g' $CHAINDIR/$CHAINID/config/genesis.json
sed -i 's/"btc_finality_contract_address": ""/"btc_finality_contract_address": "'"$btcFinalityContractAddr"'"/g' $CHAINDIR/$CHAINID/config/genesis.json

# Start
echo "Starting $BINARY..."
$BINARY --home $CHAINDIR/$CHAINID start --pruning=nothing --grpc-web.enable=false --grpc.address="0.0.0.0:$GRPCPORT" --log_level trace --trace --log_format 'plain' 2>&1 | tee $CHAINDIR/$CHAINID.log &
Expand All @@ -125,4 +109,4 @@ sleep 10

# Echo the command with expanded variables
echo "Instantiating contract $BABYLON_CONTRACT_CODE_FILE..."
$BINARY --home $CHAINDIR/$CHAINID tx wasm instantiate 1 "$INSTANTIATING_CFG" --admin=$(bcd --home $CHAINDIR/$CHAINID keys show user --keyring-backend test -a) --label "v0.0.1" $KEYRING --from user --chain-id $CHAINID --gas 20000000000 --gas-prices 0.001ustake --node http://localhost:$RPCPORT -y --amount 100000stake
$BINARY --home $CHAINDIR/$CHAINID tx babylon instantiate-babylon-contracts 1 2 3 "regtest" "01020304" 1 2 false "" "" "Test\ Consumer" "Test\ Consumer\ Description" "" --admin=$(bcd --home $CHAINDIR/$CHAINID keys show user --keyring-backend test -a) --label "v0.0.1" $KEYRING --from user --chain-id $CHAINID --gas 20000000000 --gas-prices 0.001ustake --node http://localhost:$RPCPORT -y --amount 100000stake
12 changes: 1 addition & 11 deletions contrib/images/ibcsim-bcd/wrapper.sh
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,7 @@ CONSUMER_KEY="bcd-key"
CONSUMER_CHAIN_ID="bcd-test"

# 1. Create a bcd testnet with Babylon contract
./setup-bcd.sh $CONSUMER_CHAIN_ID $CONSUMER_CONF 26657 26656 6060 9090 ./babylon_contract.wasm ./btc_staking.wasm ./btc_finality.wasm '{
"network": "regtest",
"babylon_tag": "01020304",
"btc_confirmation_depth": 1,
"checkpoint_finalization_timeout": 2,
"notify_cosmos_zone": false,
"btc_staking_code_id": 2,
"consumer_name": "Test Consumer",
"consumer_description": "Test Consumer Description",
"btc_finality_code_id": 3
}'
./setup-bcd.sh $CONSUMER_CHAIN_ID $CONSUMER_CONF 26657 26656 6060 9090 ./babylon_contract.wasm ./btc_staking.wasm ./btc_finality.wasm

sleep 10

Expand Down
43 changes: 16 additions & 27 deletions demo/app/app_test.go
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
package app

import (
"encoding/json"
"fmt"
"strconv"
"testing"
"time"

"cosmossdk.io/log"
"github.com/CosmWasm/wasmd/x/wasm"
wasmkeeper "github.com/CosmWasm/wasmd/x/wasm/keeper"
wasmtypes "github.com/CosmWasm/wasmd/x/wasm/types"
"github.com/babylonlabs-io/babylon-sdk/x/babylon/client/cli"
babylonkeeper "github.com/babylonlabs-io/babylon-sdk/x/babylon/keeper"
"github.com/babylonlabs-io/babylon-sdk/x/babylon/types"
abci "github.com/cometbft/cometbft/abci/types"
Expand Down Expand Up @@ -122,34 +123,22 @@ func TestInstantiateBabylonContracts(t *testing.T) {
btcFinalityContractCodeID := resp.CodeID
require.NoError(t, err)

// BTC staking init message
btcStakingInitMsg := map[string]interface{}{
"admin": consumerApp.BabylonKeeper.GetAuthority(),
}
btcStakingInitMsgBytes, err := json.Marshal(btcStakingInitMsg)
require.NoError(t, err)
// BTC finality init message
btcFinalityInitMsg := map[string]interface{}{
"admin": consumerApp.BabylonKeeper.GetAuthority(),
}
btcFinalityInitMsgBytes, err := json.Marshal(btcFinalityInitMsg)
msg, err := cli.ParseInstantiateArgs([]string{
strconv.FormatUint(babylonContractCodeID, 10),
strconv.FormatUint(btcStakingContractCodeID, 10),
strconv.FormatUint(btcFinalityContractCodeID, 10),
"regtest",
"01020304",
"1",
"2",
"false",
"test-consumer",
"test-consumer-description",
consumerApp.BabylonKeeper.GetAuthority(),
}, consumerApp.BabylonKeeper.GetAuthority())
require.NoError(t, err)

// instantiate Babylon contract
_, err = babylonMsgServer.InstantiateBabylonContracts(ctx, &types.MsgInstantiateBabylonContracts{
Network: "regtest",
BabylonContractCodeId: babylonContractCodeID,
BtcStakingContractCodeId: btcStakingContractCodeID,
BtcFinalityContractCodeId: btcFinalityContractCodeID,
BabylonTag: "01020304",
BtcConfirmationDepth: 1,
CheckpointFinalizationTimeout: 2,
NotifyCosmosZone: false,
BtcStakingMsg: btcStakingInitMsgBytes,
BtcFinalityMsg: btcFinalityInitMsgBytes,
ConsumerName: "test-consumer",
ConsumerDescription: "test-consumer-description",
Admin: babylonKeeper.GetAuthority(),
})
_, err = babylonMsgServer.InstantiateBabylonContracts(ctx, msg)
require.NoError(t, err)
}
2 changes: 0 additions & 2 deletions docs/proto/proto-docs.md
Original file line number Diff line number Diff line change
Expand Up @@ -174,8 +174,6 @@ type.
| `btc_confirmation_depth` | [uint32](#uint32) | | btc_confirmation_depth is the number of confirmations required for Bitcoin transactions |
| `checkpoint_finalization_timeout` | [uint32](#uint32) | | checkpoint_finalization_timeout is the timeout in blocks for checkpoint finalization |
| `notify_cosmos_zone` | [bool](#bool) | | notify_cosmos_zone indicates whether to notify the Cosmos zone of events |
| `btc_staking_msg` | [bytes](#bytes) | | btc_staking_msg is the initialization message for the BTC staking contract |
| `btc_finality_msg` | [bytes](#bytes) | | btc_finality_msg is the initialization message for the BTC finality contract |
| `consumer_name` | [string](#string) | | consumer_name is the name of this consumer chain |
| `consumer_description` | [string](#string) | | consumer_description is a description of this consumer chain |
| `admin` | [string](#string) | | admin is the address that controls the Babylon module |
Expand Down
11 changes: 3 additions & 8 deletions proto/babylonlabs/babylon/v1beta1/tx.proto
Original file line number Diff line number Diff line change
Expand Up @@ -51,17 +51,12 @@ message MsgInstantiateBabylonContracts {
uint32 checkpoint_finalization_timeout = 8;
// notify_cosmos_zone indicates whether to notify the Cosmos zone of events
bool notify_cosmos_zone = 9;
// btc_staking_msg is the initialization message for the BTC staking contract
bytes btc_staking_msg = 10;
// btc_finality_msg is the initialization message for the BTC finality
// contract
bytes btc_finality_msg = 11;
// consumer_name is the name of this consumer chain
string consumer_name = 12;
string consumer_name = 10;
// consumer_description is a description of this consumer chain
string consumer_description = 13;
string consumer_description = 11;
// admin is the address that controls the Babylon module
string admin = 14 [ (cosmos_proto.scalar) = "cosmos.AddressString" ];
string admin = 12 [ (cosmos_proto.scalar) = "cosmos.AddressString" ];
}

// MsgInstantiateBabylonContractsResponse is the Msg/InstantiateBabylonContracts
Expand Down
50 changes: 17 additions & 33 deletions tests/e2e/types/test_client.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
package types

import (
"encoding/json"
"fmt"
"strconv"
"testing"

"github.com/CosmWasm/wasmd/x/wasm/ibctesting"
wasmtypes "github.com/CosmWasm/wasmd/x/wasm/types"
"github.com/babylonlabs-io/babylon-sdk/demo/app"
"github.com/babylonlabs-io/babylon-sdk/x/babylon/client/cli"
bbntypes "github.com/babylonlabs-io/babylon-sdk/x/babylon/types"
abci "github.com/cometbft/cometbft/abci/types"
sdk "github.com/cosmos/cosmos-sdk/types"
Expand Down Expand Up @@ -104,41 +105,24 @@ func (p *TestConsumerClient) BootstrapContracts() (*ConsumerContract, error) {
btcStakingContractWasmId := p.Chain.StoreCodeFile("../testdata/btc_staking.wasm").CodeID
btcFinalityContractWasmId := p.Chain.StoreCodeFile("../testdata/btc_finality.wasm").CodeID

// Instantiate the contract
// TODO: parameterise
btcStakingInitMsg := map[string]interface{}{
"admin": p.GetSender().String(),
}
btcStakingInitMsgBytes, err := json.Marshal(btcStakingInitMsg)
// instantiate Babylon contracts
msgInstantiate, err := cli.ParseInstantiateArgs([]string{
strconv.FormatUint(babylonContractWasmId, 10),
strconv.FormatUint(btcStakingContractWasmId, 10),
strconv.FormatUint(btcFinalityContractWasmId, 10),
"regtest",
"01020304",
"1",
"2",
"false",
"test-consumer",
"test-consumer-description",
p.GetSender().String(),
}, p.GetSender().String())
if err != nil {
return nil, err
}
btcFinalityInitMsg := map[string]interface{}{
"admin": p.GetSender().String(),
}
btcFinalityInitMsgBytes, err := json.Marshal(btcFinalityInitMsg)
if err != nil {
return nil, err
}

// instantiate Babylon contract
msgInstantiate := bbntypes.MsgInstantiateBabylonContracts{
Signer: p.GetSender().String(),
BabylonContractCodeId: babylonContractWasmId,
BtcStakingContractCodeId: btcStakingContractWasmId,
BtcFinalityContractCodeId: btcFinalityContractWasmId,
Network: "regtest",
BabylonTag: "01020304",
BtcConfirmationDepth: 1,
CheckpointFinalizationTimeout: 2,
NotifyCosmosZone: false,
BtcStakingMsg: btcStakingInitMsgBytes,
BtcFinalityMsg: btcFinalityInitMsgBytes,
ConsumerName: "test-consumer",
ConsumerDescription: "test-consumer-description",
Admin: p.GetSender().String(),
}
_, err = p.Chain.SendMsgs(&msgInstantiate)
_, err = p.Chain.SendMsgs(msgInstantiate)
if err != nil {
return nil, err
}
Expand Down
Binary file modified tests/testdata/babylon_contract.wasm
Binary file not shown.
Binary file modified tests/testdata/btc_finality.wasm
Binary file not shown.
Binary file modified tests/testdata/btc_staking.wasm
Binary file not shown.
2 changes: 1 addition & 1 deletion tests/testdata/version.txt
Original file line number Diff line number Diff line change
@@ -1 +1 @@
84130eb706e1fb60e16cecf9ce8ade94b66ffa62
f71162b0dd42190387be3f911413e5f3e6513f09
25 changes: 7 additions & 18 deletions x/babylon/client/cli/tx.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package cli

import (
"encoding/hex"
"strconv"

"github.com/cosmos/cosmos-sdk/client"
Expand Down Expand Up @@ -30,8 +29,8 @@ func GetTxCmd() *cobra.Command {
return txCmd
}

// [babylon-contract-code-id] [btc-staking-contract-code-id] [btc-finality-contract-code-id] [btc-network] [babylon-tag] [btc-confirmation-depth] [checkpoint-finalization-timeout] [notify-cosmos-zone] [btc-staking-init-msg-hex] [btc-finality-init-msg-hex] [consumer-name] [consumer-description] [admin]
func parseInstantiateArgs(args []string, sender string) (*types.MsgInstantiateBabylonContracts, error) {
// [babylon-contract-code-id] [btc-staking-contract-code-id] [btc-finality-contract-code-id] [btc-network] [babylon-tag] [btc-confirmation-depth] [checkpoint-finalization-timeout] [notify-cosmos-zone] [consumer-name] [consumer-description] [admin]
func ParseInstantiateArgs(args []string, sender string) (*types.MsgInstantiateBabylonContracts, error) {
// get the id of the code to instantiate
babylonContractCodeID, err := strconv.ParseUint(args[0], 10, 64)
if err != nil {
Expand Down Expand Up @@ -60,17 +59,9 @@ func parseInstantiateArgs(args []string, sender string) (*types.MsgInstantiateBa
if err != nil {
return nil, err
}
btcStakingInitMsg, err := hex.DecodeString(args[8])
if err != nil {
return nil, err
}
btcFinalityInitMsg, err := hex.DecodeString(args[9])
if err != nil {
return nil, err
}
consumerName := args[10]
consumerDescription := args[11]
adminStr := args[12]
consumerName := args[8]
consumerDescription := args[9]
adminStr := args[10]

// build and sign the transaction, then broadcast to Tendermint
msg := types.MsgInstantiateBabylonContracts{
Expand All @@ -83,8 +74,6 @@ func parseInstantiateArgs(args []string, sender string) (*types.MsgInstantiateBa
BtcConfirmationDepth: uint32(btcConfirmationDepth),
CheckpointFinalizationTimeout: uint32(checkpointFinalizationTimeout),
NotifyCosmosZone: notifyCosmosZone,
BtcStakingMsg: btcStakingInitMsg,
BtcFinalityMsg: btcFinalityInitMsg,
ConsumerName: consumerName,
ConsumerDescription: consumerDescription,
Admin: adminStr,
Expand All @@ -98,13 +87,13 @@ func NewInstantiateBabylonContractsCmd() *cobra.Command {
Short: "Instantiate Babylon contracts",
Long: "Instantiate Babylon contracts",
Aliases: []string{"i"},
Args: cobra.ExactArgs(11),
Args: cobra.ExactArgs(13),
RunE: func(cmd *cobra.Command, args []string) error {
clientCtx, err := client.GetClientTxContext(cmd)
if err != nil {
return err
}
msg, err := parseInstantiateArgs(args, clientCtx.GetFromAddress().String())
msg, err := ParseInstantiateArgs(args, clientCtx.GetFromAddress().String())
if err != nil {
return err
}
Expand Down
22 changes: 17 additions & 5 deletions x/babylon/keeper/msg_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,27 +30,39 @@ func (ms msgServer) InstantiateBabylonContracts(goCtx context.Context, req *type
return nil, errorsmod.Wrapf(govtypes.ErrInvalidSigner, "only authority can override instantiated contracts; expected %s, got %s", ms.k.authority, req.Signer)
}

// if admin is not set, then use gov authority
admin := req.Admin
if admin == "" {
admin = ms.k.authority
}

// construct the init message
initMsg, err := types.NewInitMsg(
initMsgBytes, err := types.NewInitMsg(
req.Network,
req.BabylonTag,
req.BtcConfirmationDepth,
req.CheckpointFinalizationTimeout,
req.NotifyCosmosZone,
req.BtcStakingContractCodeId,
req.BtcStakingMsg,
req.BtcFinalityContractCodeId,
req.BtcFinalityMsg,
req.ConsumerName,
req.ConsumerDescription,
req.Admin,
admin,
)
if err != nil {
return nil, err
}
btcStakingInitMsg, err := types.NewBTCStakingInitMsg(admin)
if err != nil {
return nil, err
}
btcFinalityInitMsg, err := types.NewBTCFinalityInitMsg(admin)
if err != nil {
return nil, err
}

// instantiate the contracts
babylonContractAddr, btcStakingContractAddr, btcFinalityContractAddr, err := ms.k.InstantiateBabylonContracts(ctx, req.BabylonContractCodeId, initMsg)
babylonContractAddr, btcStakingContractAddr, btcFinalityContractAddr, err := ms.k.InstantiateBabylonContracts(ctx, req.BabylonContractCodeId, req.BtcStakingContractCodeId, req.BtcFinalityContractCodeId, initMsgBytes, btcStakingInitMsg, btcFinalityInitMsg)
if err != nil {
return nil, err
}
Expand Down
Loading

0 comments on commit feb7adc

Please sign in to comment.