Skip to content

Commit

Permalink
some test cases
Browse files Browse the repository at this point in the history
  • Loading branch information
SebastianElvis committed Dec 3, 2024
1 parent 51dbbbb commit c8d957a
Show file tree
Hide file tree
Showing 8 changed files with 657 additions and 107 deletions.
10 changes: 9 additions & 1 deletion docs/proto/proto-docs.md
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,15 @@ type.
| Field | Type | Label | Description |
| ----- | ---- | ----- | ----------- |
| `signer` | [string](#string) | | signer is the address who submits the message. |
| `init_msg` | [bytes](#bytes) | | init_msg is the instantiation message for the Babylon contract. |
| `network` | [string](#string) | | network is the Bitcoin network to connect to (e.g. "regtest", "testnet", "mainnet") |
| `babylon_tag` | [string](#string) | | babylon_tag is a unique identifier for this Babylon instance |
| `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 |



Expand Down
25 changes: 23 additions & 2 deletions proto/babylonlabs/babylon/v1beta1/tx.proto
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,29 @@ message MsgInstantiateBabylonContracts {

// signer is the address who submits the message.
string signer = 1 [ (cosmos_proto.scalar) = "cosmos.AddressString" ];
// init_msg is the instantiation message for the Babylon contract.
bytes init_msg = 2;

// network is the Bitcoin network to connect to (e.g. "regtest", "testnet",
// "mainnet")
string network = 2;
// babylon_tag is a unique identifier for this Babylon instance
string babylon_tag = 3;
// btc_confirmation_depth is the number of confirmations required for Bitcoin
// transactions
uint32 btc_confirmation_depth = 4;
// checkpoint_finalization_timeout is the timeout in blocks for checkpoint
// finalization
uint32 checkpoint_finalization_timeout = 5;
// notify_cosmos_zone indicates whether to notify the Cosmos zone of events
bool notify_cosmos_zone = 6;
// btc_staking_msg is the initialization message for the BTC staking contract
bytes btc_staking_msg = 7;
// btc_finality_msg is the initialization message for the BTC finality
// contract
bytes btc_finality_msg = 8;
// consumer_name is the name of this consumer chain
string consumer_name = 9;
// consumer_description is a description of this consumer chain
string consumer_description = 10;
}

// MsgInstantiateBabylonContractsResponse is the Msg/InstantiateBabylonContracts
Expand Down
116 changes: 93 additions & 23 deletions x/babylon/keeper/keeper_test.go
Original file line number Diff line number Diff line change
@@ -1,17 +1,22 @@
package keeper_test

import (
"fmt"
"os"
"testing"
"time"

"cosmossdk.io/log"
"cosmossdk.io/store"
storemetrics "cosmossdk.io/store/metrics"
storetypes "cosmossdk.io/store/types"
"cosmossdk.io/x/evidence"
evidencetypes "cosmossdk.io/x/evidence/types"
"cosmossdk.io/x/feegrant"
"cosmossdk.io/x/upgrade"
upgradekeeper "cosmossdk.io/x/upgrade/keeper"
upgradetypes "cosmossdk.io/x/upgrade/types"
"github.com/CosmWasm/wasmd/x/wasm/ioutils"
wasmkeeper "github.com/CosmWasm/wasmd/x/wasm/keeper"
"github.com/CosmWasm/wasmd/x/wasm/keeper/wasmtesting"
wasmtypes "github.com/CosmWasm/wasmd/x/wasm/types"
Expand All @@ -30,28 +35,35 @@ import (
authkeeper "github.com/cosmos/cosmos-sdk/x/auth/keeper"
"github.com/cosmos/cosmos-sdk/x/auth/tx"
authtypes "github.com/cosmos/cosmos-sdk/x/auth/types"
"github.com/cosmos/cosmos-sdk/x/auth/vesting"
authzkeeper "github.com/cosmos/cosmos-sdk/x/authz/keeper"
"github.com/cosmos/cosmos-sdk/x/bank"
bankkeeper "github.com/cosmos/cosmos-sdk/x/bank/keeper"
banktypes "github.com/cosmos/cosmos-sdk/x/bank/types"
"github.com/cosmos/cosmos-sdk/x/crisis"
"github.com/cosmos/cosmos-sdk/x/distribution"
distributionkeeper "github.com/cosmos/cosmos-sdk/x/distribution/keeper"
distributiontypes "github.com/cosmos/cosmos-sdk/x/distribution/types"
"github.com/cosmos/cosmos-sdk/x/gov"
govclient "github.com/cosmos/cosmos-sdk/x/gov/client"
govtypes "github.com/cosmos/cosmos-sdk/x/gov/types"
"github.com/cosmos/cosmos-sdk/x/mint"
minttypes "github.com/cosmos/cosmos-sdk/x/mint/types"
"github.com/cosmos/cosmos-sdk/x/params"
paramsclient "github.com/cosmos/cosmos-sdk/x/params/client"
paramskeeper "github.com/cosmos/cosmos-sdk/x/params/keeper"
paramstypes "github.com/cosmos/cosmos-sdk/x/params/types"
"github.com/cosmos/cosmos-sdk/x/slashing"
slashingkeeper "github.com/cosmos/cosmos-sdk/x/slashing/keeper"
slashingtypes "github.com/cosmos/cosmos-sdk/x/slashing/types"
"github.com/cosmos/cosmos-sdk/x/staking"
stakingkeeper "github.com/cosmos/cosmos-sdk/x/staking/keeper"
stakingtypes "github.com/cosmos/cosmos-sdk/x/staking/types"
"github.com/cosmos/ibc-go/modules/capability"
capabilitykeeper "github.com/cosmos/ibc-go/modules/capability/keeper"
capabilitytypes "github.com/cosmos/ibc-go/modules/capability/types"
ibctransfertypes "github.com/cosmos/ibc-go/v8/modules/apps/transfer/types"
ibc "github.com/cosmos/ibc-go/v8/modules/core"
ibcexported "github.com/cosmos/ibc-go/v8/modules/core/exported"
ibckeeper "github.com/cosmos/ibc-go/v8/modules/core/keeper"
"github.com/stretchr/testify/require"
Expand All @@ -70,11 +82,20 @@ type encodingConfig struct {
var moduleBasics = module.NewBasicManager(
auth.AppModuleBasic{},
bank.AppModuleBasic{},
capability.AppModuleBasic{},
staking.AppModuleBasic{},
mint.AppModuleBasic{},
distribution.AppModuleBasic{},
gov.NewAppModuleBasic([]govclient.ProposalHandler{
paramsclient.ProposalHandler,
}),
params.AppModuleBasic{},
crisis.AppModuleBasic{},
slashing.AppModuleBasic{},
ibc.AppModuleBasic{},
upgrade.AppModuleBasic{},
evidence.AppModuleBasic{},
vesting.AppModuleBasic{},
)

func makeEncodingConfig(_ testing.TB) encodingConfig {
Expand All @@ -86,6 +107,9 @@ func makeEncodingConfig(_ testing.TB) encodingConfig {

moduleBasics.RegisterLegacyAminoCodec(amino)
moduleBasics.RegisterInterfaces(interfaceRegistry)
// add wasm types
wasmtypes.RegisterInterfaces(interfaceRegistry)
wasmtypes.RegisterLegacyAminoCodec(amino)
// add babylon types
types.RegisterInterfaces(interfaceRegistry)
types.RegisterLegacyAminoCodec(amino)
Expand All @@ -99,17 +123,18 @@ func makeEncodingConfig(_ testing.TB) encodingConfig {
}

type TestKeepers struct {
Ctx sdk.Context
StakingKeeper *stakingkeeper.Keeper
SlashingKeeper slashingkeeper.Keeper
BankKeeper bankkeeper.Keeper
StoreKey *storetypes.KVStoreKey
EncodingConfig encodingConfig
BabylonKeeper *keeper.Keeper
AccountKeeper authkeeper.AccountKeeper
WasmKeeper *wasmkeeper.Keeper
WasmMsgServer wasmtypes.MsgServer
Faucet *wasmkeeper.TestFaucet
Ctx sdk.Context
StakingKeeper *stakingkeeper.Keeper
SlashingKeeper slashingkeeper.Keeper
BankKeeper bankkeeper.Keeper
StoreKey *storetypes.KVStoreKey
EncodingConfig encodingConfig
BabylonKeeper *keeper.Keeper
BabylonMsgServer types.MsgServer
AccountKeeper authkeeper.AccountKeeper
WasmKeeper *wasmkeeper.Keeper
WasmMsgServer wasmtypes.MsgServer
Faucet *wasmkeeper.TestFaucet
}

func NewTestKeepers(t testing.TB, opts ...keeper.Option) TestKeepers {
Expand Down Expand Up @@ -267,7 +292,7 @@ func NewTestKeepers(t testing.TB, opts ...keeper.Option) TestKeepers {
querier,
t.TempDir(),
wasmtypes.DefaultWasmConfig(),
[]string{"iterator", "staking", "stargate", "cosmwasm_1_1", "cosmwasm_1_2", "cosmwasm_1_3", "virtual_staking"},
[]string{"iterator", "staking", "stargate", "cosmwasm_1_1", "cosmwasm_1_2", "cosmwasm_1_3", "cosmwasm_1_4", "cosmwasm_2_0", "virtual_staking"},
authtypes.NewModuleAddress(govtypes.ModuleName).String(),
)
require.NoError(t, wasmKeeper.SetParams(ctx, wasmtypes.DefaultParams()))
Expand All @@ -284,19 +309,64 @@ func NewTestKeepers(t testing.TB, opts ...keeper.Option) TestKeepers {
opts...,
)
require.NoError(t, babylonKeeper.SetParams(ctx, types.DefaultParams(sdk.DefaultBondDenom)))
babylonMsgServer := keeper.NewMsgServer(babylonKeeper)

faucet := wasmkeeper.NewTestFaucet(t, ctx, bankKeeper, minttypes.ModuleName, sdk.NewInt64Coin(sdk.DefaultBondDenom, 1_000_000_000_000))
return TestKeepers{
Ctx: ctx,
AccountKeeper: accountKeeper,
StakingKeeper: stakingKeeper,
SlashingKeeper: slashingKeeper,
BankKeeper: bankKeeper,
StoreKey: keys[types.StoreKey],
EncodingConfig: encConfig,
BabylonKeeper: babylonKeeper,
WasmKeeper: &wasmKeeper,
WasmMsgServer: wasmMsgServer,
Faucet: faucet,
Ctx: ctx,
AccountKeeper: accountKeeper,
StakingKeeper: stakingKeeper,
SlashingKeeper: slashingKeeper,
BankKeeper: bankKeeper,
StoreKey: keys[types.StoreKey],
EncodingConfig: encConfig,
BabylonKeeper: babylonKeeper,
BabylonMsgServer: babylonMsgServer,
WasmKeeper: &wasmKeeper,
WasmMsgServer: wasmMsgServer,
Faucet: faucet,
}
}

const (
TestDataPath = "../../../tests/testdata"
BabylonContractCodePath = TestDataPath + "/babylon_contract.wasm"
BtcStakingContractCodePath = TestDataPath + "/btc_staking.wasm"
BtcFinalityContractCodePath = TestDataPath + "/btc_finality.wasm"
)

func GetGZippedContractCode(path string) ([]byte, error) {
wasm, err := os.ReadFile(path)
if err != nil {
return nil, err
}
// gzip the wasm file
if ioutils.IsWasm(wasm) {
wasm, err = ioutils.GzipIt(wasm)

if err != nil {
return nil, err
}
} else if !ioutils.IsGzip(wasm) {
return nil, fmt.Errorf("invalid input file. Use wasm binary or gzip")
}

return wasm, nil
}

func GetGZippedContractCodes() ([]byte, []byte, []byte) {
babylonContractCode, err := GetGZippedContractCode(BabylonContractCodePath)
if err != nil {
panic(err)
}
btcStakingContractCode, err := GetGZippedContractCode(BtcStakingContractCodePath)
if err != nil {
panic(err)
}
btcFinalityContractCode, err := GetGZippedContractCode(BtcFinalityContractCodePath)
if err != nil {
panic(err)
}

return babylonContractCode, btcStakingContractCode, btcFinalityContractCode
}
20 changes: 19 additions & 1 deletion x/babylon/keeper/msg_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,26 @@ 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)
}

// construct the init message
initMsg, err := types.NewInitMsg(
ms.k.authority,
&params,
req.Network,
req.BabylonTag,
req.BtcConfirmationDepth,
req.CheckpointFinalizationTimeout,
req.NotifyCosmosZone,
req.BtcStakingMsg,
req.BtcFinalityMsg,
req.ConsumerName,
req.ConsumerDescription,
)
if err != nil {
return nil, err
}

// instantiate the contracts
babylonContractAddr, btcStakingContractAddr, btcFinalityContractAddr, err := ms.k.InstantiateBabylonContracts(ctx, req.InitMsg)
babylonContractAddr, btcStakingContractAddr, btcFinalityContractAddr, err := ms.k.InstantiateBabylonContracts(ctx, initMsg)
if err != nil {
return nil, err
}
Expand Down
90 changes: 89 additions & 1 deletion x/babylon/keeper/msg_server_test.go
Original file line number Diff line number Diff line change
@@ -1 +1,89 @@
package keeper
package keeper_test

import (
"encoding/json"
"testing"

"github.com/babylonlabs-io/babylon-sdk/x/babylon/types"
"github.com/stretchr/testify/require"
)

func TestStoreBabylonContractCodes(t *testing.T) {
keepers := NewTestKeepers(t)
msgServer := keepers.BabylonMsgServer

babylonContractCode, btcStakingContractCode, btcFinalityContractCode := GetGZippedContractCodes()

// store Babylon contract codes
_, err := msgServer.StoreBabylonContractCodes(keepers.Ctx, &types.MsgStoreBabylonContractCodes{
BabylonContractCode: babylonContractCode,
BtcStakingContractCode: btcStakingContractCode,
BtcFinalityContractCode: btcFinalityContractCode,
})
require.NoError(t, err)

// ensure params are set
params := keepers.BabylonKeeper.GetParams(keepers.Ctx)
require.Positive(t, params.BabylonContractCodeId)
require.Positive(t, params.BtcStakingContractCodeId)
require.Positive(t, params.BtcFinalityContractCodeId)

// ensure non-gov account cannot override
_, err = msgServer.StoreBabylonContractCodes(keepers.Ctx, &types.MsgStoreBabylonContractCodes{
BabylonContractCode: babylonContractCode,
BtcStakingContractCode: btcStakingContractCode,
BtcFinalityContractCode: btcFinalityContractCode,
})
require.Error(t, err)

// gov can override
_, err = msgServer.StoreBabylonContractCodes(keepers.Ctx, &types.MsgStoreBabylonContractCodes{
Signer: keepers.BabylonKeeper.GetAuthority(),
BabylonContractCode: babylonContractCode,
BtcStakingContractCode: btcStakingContractCode,
BtcFinalityContractCode: btcFinalityContractCode,
})
require.NoError(t, err)
}

// TODO: fix this test
func TestInstantiateBabylonContracts(t *testing.T) {
keepers := NewTestKeepers(t)
msgServer := keepers.BabylonMsgServer

// store Babylon contract codes
babylonContractCode, btcStakingContractCode, btcFinalityContractCode := GetGZippedContractCodes()
_, err := msgServer.StoreBabylonContractCodes(keepers.Ctx, &types.MsgStoreBabylonContractCodes{
BabylonContractCode: babylonContractCode,
BtcStakingContractCode: btcStakingContractCode,
BtcFinalityContractCode: btcFinalityContractCode,
})
require.NoError(t, err)

// BTC staking init message
btcStakingInitMsg := map[string]interface{}{
"admin": keepers.BabylonKeeper.GetAuthority(),
}
btcStakingInitMsgBytes, err := json.Marshal(btcStakingInitMsg)
require.NoError(t, err)
// BTC finality init message
btcFinalityInitMsg := map[string]interface{}{
"admin": keepers.BabylonKeeper.GetAuthority(),
}
btcFinalityInitMsgBytes, err := json.Marshal(btcFinalityInitMsg)
require.NoError(t, err)

// instantiate Babylon contract
_, err = msgServer.InstantiateBabylonContracts(keepers.Ctx, &types.MsgInstantiateBabylonContracts{
Network: "regtest",
BabylonTag: "01020304",
BtcConfirmationDepth: 1,
CheckpointFinalizationTimeout: 2,
NotifyCosmosZone: false,
BtcStakingMsg: btcStakingInitMsgBytes,
BtcFinalityMsg: btcFinalityInitMsgBytes,
ConsumerName: "test-consumer",
ConsumerDescription: "test-consumer-description",
})
require.NoError(t, err)
}
Loading

0 comments on commit c8d957a

Please sign in to comment.