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

query: custom query for params #77

Draft
wants to merge 3 commits into
base: main
Choose a base branch
from
Draft
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
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
4 changes: 3 additions & 1 deletion demo/app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -493,7 +493,9 @@ func NewConsumerApp(
bbnkeeper.NewDefaultCustomMsgHandler(app.BabylonKeeper),
)
})
wasmOpts = append(wasmOpts, messageHandler,
wasmOpts = append(wasmOpts,
// add support for the custom message handlers
messageHandler,
// add support for the custom queries
wasmkeeper.WithQueryHandlerDecorator(bbnkeeper.NewQueryDecorator(app.BabylonKeeper)),
)
Expand Down
57 changes: 30 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,36 @@ 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)

// get params and verify contract addresses are set correctly
params := babylonKeeper.GetParams(ctx)
require.NotEmpty(t, params.BabylonContractAddress)
require.NotEmpty(t, params.BtcStakingContractAddress)
require.NotEmpty(t, params.BtcFinalityContractAddress)

// verify addresses are valid
_, err = sdk.AccAddressFromBech32(params.BabylonContractAddress)
require.NoError(t, err)
_, err = sdk.AccAddressFromBech32(params.BtcStakingContractAddress)
require.NoError(t, err)
_, err = sdk.AccAddressFromBech32(params.BtcFinalityContractAddress)
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
24 changes: 3 additions & 21 deletions tests/e2e/local_e2e_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (
"github.com/babylonlabs-io/babylon-sdk/demo/app"
appparams "github.com/babylonlabs-io/babylon-sdk/demo/app/params"
"github.com/babylonlabs-io/babylon-sdk/tests/e2e/types"
bbntypes "github.com/babylonlabs-io/babylon-sdk/x/babylon/types"
sdk "github.com/cosmos/cosmos-sdk/types"
ibctesting2 "github.com/cosmos/ibc-go/v8/testing"
"github.com/stretchr/testify/suite"
Expand Down Expand Up @@ -97,28 +96,11 @@ func (s *BabylonSDKTestSuite) Test1ContractDeployment() {
s.NoError(err)
s.Equal(adminRespFinality["admin"], s.ConsumerCli.GetSender().String())

// get contract addresses
babylonContractAddress := s.ConsumerContract.Babylon.String()
btcStakingContractAddress := s.ConsumerContract.BTCStaking.String()
btcFinalityContractAddress := s.ConsumerContract.BTCFinality.String()

// update the contract address in parameters
msgUpdateParams := &bbntypes.MsgUpdateParams{
Authority: s.ConsumerApp.BabylonKeeper.GetAuthority(),
Params: bbntypes.Params{
MaxGasBeginBlocker: 500_000,
BabylonContractAddress: babylonContractAddress,
BtcStakingContractAddress: btcStakingContractAddress,
BtcFinalityContractAddress: btcFinalityContractAddress,
},
}
s.ConsumerCli.MustExecGovProposal(msgUpdateParams)

// assert the contract addresses are updated
params := s.ConsumerApp.BabylonKeeper.GetParams(s.ConsumerChain.GetContext())
s.Equal(babylonContractAddress, params.BabylonContractAddress)
s.Equal(btcStakingContractAddress, params.BtcStakingContractAddress)
s.Equal(btcFinalityContractAddress, params.BtcFinalityContractAddress)
s.Equal(s.ConsumerContract.Babylon.String(), params.BabylonContractAddress)
s.Equal(s.ConsumerContract.BTCStaking.String(), params.BtcStakingContractAddress)
s.Equal(s.ConsumerContract.BTCFinality.String(), params.BtcFinalityContractAddress)
}

func (s *BabylonSDKTestSuite) Test2MockConsumerFpDelegation() {
Expand Down
49 changes: 17 additions & 32 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,25 @@ 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)
if err != nil {
return nil, err
}
btcFinalityInitMsg := map[string]interface{}{
"admin": p.GetSender().String(),
}
btcFinalityInitMsgBytes, err := json.Marshal(btcFinalityInitMsg)
// 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
}

// 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 @@ -94,7 +83,7 @@ func parseInstantiateArgs(args []string, sender string) (*types.MsgInstantiateBa

func NewInstantiateBabylonContractsCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "instantiate-babylon-contracts [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]",
Use: "instantiate-babylon-contracts [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]",
Short: "Instantiate Babylon contracts",
Long: "Instantiate Babylon contracts",
Aliases: []string{"i"},
Expand All @@ -104,7 +93,7 @@ func NewInstantiateBabylonContractsCmd() *cobra.Command {
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
20 changes: 12 additions & 8 deletions x/babylon/contract/query.go
Original file line number Diff line number Diff line change
@@ -1,16 +1,20 @@
package contract

// CustomQuery is a query request from a smart contract to the Babylon module
// TODO: implement
type CustomQuery struct {
Test *TestQuery `json:"test,omitempty"`
Params *ParamsQuery `json:"params,omitempty"`
}

type TestQuery struct {
Placeholder string `json:"placeholder,omitempty"`
}
// ParamsQuery requests the current module parameters
type ParamsQuery struct{}

type TestResponse struct {
// MaxCap is the max cap limit
Placeholder2 string `json:"placeholder2"`
// ParamsResponse contains the current module parameters
type ParamsResponse struct {
BabylonContractCodeId uint64 `json:"babylon_contract_code_id,omitempty"`
BtcStakingContractCodeId uint64 `json:"btc_staking_contract_code_id,omitempty"`
BtcFinalityContractCodeId uint64 `json:"btc_finality_contract_code_id,omitempty"`
BabylonContractAddress string `json:"babylon_contract_address,omitempty"`
BtcStakingContractAddress string `json:"btc_staking_contract_address,omitempty"`
BtcFinalityContractAddress string `json:"btc_finality_contract_address,omitempty"`
MaxGasBeginBlocker uint32 `json:"max_gas_begin_blocker,omitempty"`
}
Loading
Loading