Skip to content

Commit

Permalink
e2e
Browse files Browse the repository at this point in the history
  • Loading branch information
SebastianElvis committed Dec 3, 2024
1 parent c8d957a commit 7ba5c63
Show file tree
Hide file tree
Showing 12 changed files with 306 additions and 763 deletions.
37 changes: 4 additions & 33 deletions docs/proto/proto-docs.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,6 @@
- [babylonlabs/babylon/v1beta1/tx.proto](#babylonlabs/babylon/v1beta1/tx.proto)
- [MsgInstantiateBabylonContracts](#babylonlabs.babylon.v1beta1.MsgInstantiateBabylonContracts)
- [MsgInstantiateBabylonContractsResponse](#babylonlabs.babylon.v1beta1.MsgInstantiateBabylonContractsResponse)
- [MsgStoreBabylonContractCodes](#babylonlabs.babylon.v1beta1.MsgStoreBabylonContractCodes)
- [MsgStoreBabylonContractCodesResponse](#babylonlabs.babylon.v1beta1.MsgStoreBabylonContractCodesResponse)
- [MsgUpdateParams](#babylonlabs.babylon.v1beta1.MsgUpdateParams)
- [MsgUpdateParamsResponse](#babylonlabs.babylon.v1beta1.MsgUpdateParamsResponse)

Expand Down Expand Up @@ -168,6 +166,9 @@ type.
| Field | Type | Label | Description |
| ----- | ---- | ----- | ----------- |
| `signer` | [string](#string) | | signer is the address who submits the message. |
| `babylon_contract_code_id` | [uint64](#uint64) | | babylon_contract_code_id is the code ID for the Babylon contract. |
| `btc_staking_contract_code_id` | [uint64](#uint64) | | btc_staking_contract_code_id is the code ID for the BTC staking contract. |
| `btc_finality_contract_code_id` | [uint64](#uint64) | | btc_finality_contract_code_id is the code ID for the BTC finality 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 |
Expand All @@ -177,6 +178,7 @@ type.
| `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 All @@ -194,36 +196,6 @@ response type.



<a name="babylonlabs.babylon.v1beta1.MsgStoreBabylonContractCodes"></a>

### MsgStoreBabylonContractCodes
MsgStoreBabylonContractCodes is the Msg/StoreBabylonContractCodes request
type.


| Field | Type | Label | Description |
| ----- | ---- | ----- | ----------- |
| `signer` | [string](#string) | | signer is the address who submits the message. |
| `babylon_contract_code` | [bytes](#bytes) | | babylon_contract_code is the bytecode for the Babylon contract. WASMByteCode can be raw or gzip compressed |
| `btc_staking_contract_code` | [bytes](#bytes) | | btc_staking_contract_code is the bytecode for the BTC staking contract. WASMByteCode can be raw or gzip compressed |
| `btc_finality_contract_code` | [bytes](#bytes) | | btc_finality_contract_code is the bytecode for the BTC finality contract. WASMByteCode can be raw or gzip compressed |






<a name="babylonlabs.babylon.v1beta1.MsgStoreBabylonContractCodesResponse"></a>

### MsgStoreBabylonContractCodesResponse
MsgStoreBabylonContractCodesResponse is the Msg/StoreBabylonContractCodes
response type.






<a name="babylonlabs.babylon.v1beta1.MsgUpdateParams"></a>

### MsgUpdateParams
Expand Down Expand Up @@ -266,7 +238,6 @@ Msg defines the wasm Msg service.

| Method Name | Request Type | Response Type | Description | HTTP Verb | Endpoint |
| ----------- | ------------ | ------------- | ------------| ------- | -------- |
| `StoreBabylonContractCodes` | [MsgStoreBabylonContractCodes](#babylonlabs.babylon.v1beta1.MsgStoreBabylonContractCodes) | [MsgStoreBabylonContractCodesResponse](#babylonlabs.babylon.v1beta1.MsgStoreBabylonContractCodesResponse) | StoreBabylonContractCodes defines an operation for storing the Babylon contract codes. | |
| `InstantiateBabylonContracts` | [MsgInstantiateBabylonContracts](#babylonlabs.babylon.v1beta1.MsgInstantiateBabylonContracts) | [MsgInstantiateBabylonContractsResponse](#babylonlabs.babylon.v1beta1.MsgInstantiateBabylonContractsResponse) | InstantiateBabylonContracts defines an operation for instantiating the Babylon contracts. | |
| `UpdateParams` | [MsgUpdateParams](#babylonlabs.babylon.v1beta1.MsgUpdateParams) | [MsgUpdateParamsResponse](#babylonlabs.babylon.v1beta1.MsgUpdateParamsResponse) | UpdateParams defines a (governance) operation for updating the x/auth module parameters. The authority defaults to the x/gov module account. | |

Expand Down
54 changes: 18 additions & 36 deletions proto/babylonlabs/babylon/v1beta1/tx.proto
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,6 @@ option (gogoproto.goproto_getters_all) = false;
service Msg {
option (cosmos.msg.v1.service) = true;

// StoreBabylonContractCodes defines an operation for storing the Babylon
// contract codes.
rpc StoreBabylonContractCodes(MsgStoreBabylonContractCodes)
returns (MsgStoreBabylonContractCodesResponse);

// InstantiateBabylonContracts defines an operation for instantiating the
// Babylon contracts.
rpc InstantiateBabylonContracts(MsgInstantiateBabylonContracts)
Expand All @@ -28,28 +23,6 @@ service Msg {
rpc UpdateParams(MsgUpdateParams) returns (MsgUpdateParamsResponse);
}

// MsgStoreBabylonContractCodes is the Msg/StoreBabylonContractCodes request
// type.
message MsgStoreBabylonContractCodes {
option (cosmos.msg.v1.signer) = "signer";

// signer is the address who submits the message.
string signer = 1 [ (cosmos_proto.scalar) = "cosmos.AddressString" ];
// babylon_contract_code is the bytecode for the Babylon contract.
// WASMByteCode can be raw or gzip compressed
bytes babylon_contract_code = 2;
// btc_staking_contract_code is the bytecode for the BTC staking contract.
// WASMByteCode can be raw or gzip compressed
bytes btc_staking_contract_code = 3;
// btc_finality_contract_code is the bytecode for the BTC finality contract.
// WASMByteCode can be raw or gzip compressed
bytes btc_finality_contract_code = 4;
}

// MsgStoreBabylonContractCodesResponse is the Msg/StoreBabylonContractCodes
// response type.
message MsgStoreBabylonContractCodesResponse {}

// MsgInstantiateBabylonContracts is the Msg/InstantiateBabylonContracts request
// type.
message MsgInstantiateBabylonContracts {
Expand All @@ -58,28 +31,37 @@ message MsgInstantiateBabylonContracts {
// signer is the address who submits the message.
string signer = 1 [ (cosmos_proto.scalar) = "cosmos.AddressString" ];

// babylon_contract_code_id is the code ID for the Babylon contract.
uint64 babylon_contract_code_id = 2;
// btc_staking_contract_code_id is the code ID for the BTC staking contract.
uint64 btc_staking_contract_code_id = 3;
// btc_finality_contract_code_id is the code ID for the BTC finality contract.
uint64 btc_finality_contract_code_id = 4;

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

// MsgInstantiateBabylonContractsResponse is the Msg/InstantiateBabylonContracts
Expand Down
51 changes: 24 additions & 27 deletions tests/e2e/test_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"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/types"
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 @@ -120,43 +121,39 @@ func (p *TestConsumerClient) BootstrapContracts() (*ConsumerContract, error) {
if err != nil {
return nil, err
}
initMsg := map[string]interface{}{
"network": "regtest",
"babylon_tag": "01020304",
"btc_confirmation_depth": 1,
"checkpoint_finalization_timeout": 2,
"notify_cosmos_zone": false,
"btc_staking_code_id": btcStakingContractWasmId,
"btc_staking_msg": btcStakingInitMsgBytes,
"consumer_name": "test-consumer",
"consumer_description": "test-consumer-description",
"btc_finality_code_id": btcFinalityContractWasmId,
"btc_finality_msg": btcFinalityInitMsgBytes,
"admin": p.GetSender().String(),

// instantiate Babylon contract
msgInstantiate := types.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(),
}
initMsgBytes, err := json.Marshal(initMsg)
_, err = p.Chain.SendMsgs(&msgInstantiate)
if err != nil {
return nil, err
}

babylonContractAddr := InstantiateContract(p.t, p.Chain, babylonContractWasmId, initMsgBytes)
res, err := p.Query(babylonContractAddr, Query{"config": {}})
params := p.App.BabylonKeeper.GetParams(p.Chain.GetContext())
babylonAddr, btcStakingAddr, btcFinalityAddr, err := params.GetContractAddresses()
if err != nil {
return nil, err
}
btcStakingContractAddr, ok := res["btc_staking"]
if !ok {
return nil, fmt.Errorf("failed to instantiate BTC staking contract")
}
btcFinalityContractAddr, ok := res["btc_finality"]
if !ok {
return nil, fmt.Errorf("failed to instantiate BTC finality contract")
}

r := ConsumerContract{
Babylon: babylonContractAddr,
BTCStaking: sdk.MustAccAddressFromBech32(btcStakingContractAddr.(string)),
BTCFinality: sdk.MustAccAddressFromBech32(btcFinalityContractAddr.(string)),
Babylon: babylonAddr,
BTCStaking: btcStakingAddr,
BTCFinality: btcFinalityAddr,
}
p.Contracts = r
return &r, nil
Expand Down
28 changes: 3 additions & 25 deletions x/babylon/keeper/keeper_test.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package keeper_test

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

Expand All @@ -16,7 +14,6 @@ import (
"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 Down Expand Up @@ -335,35 +332,16 @@ const (
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)
babylonContractCode, err := types.GetGZippedContractCode(BabylonContractCodePath)
if err != nil {
panic(err)
}
btcStakingContractCode, err := GetGZippedContractCode(BtcStakingContractCodePath)
btcStakingContractCode, err := types.GetGZippedContractCode(BtcStakingContractCodePath)
if err != nil {
panic(err)
}
btcFinalityContractCode, err := GetGZippedContractCode(BtcFinalityContractCodePath)
btcFinalityContractCode, err := types.GetGZippedContractCode(BtcFinalityContractCodePath)
if err != nil {
panic(err)
}
Expand Down
37 changes: 7 additions & 30 deletions x/babylon/keeper/msg_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,33 +20,6 @@ func NewMsgServer(k *Keeper) *msgServer {
return &msgServer{k: k}
}

func (ms msgServer) StoreBabylonContractCodes(goCtx context.Context, req *types.MsgStoreBabylonContractCodes) (*types.MsgStoreBabylonContractCodesResponse, error) {
ctx := sdk.UnwrapSDKContext(goCtx)

params := ms.k.GetParams(ctx)

// only the authority can override the contract codes
if params.IsCodeStored() && req.Signer != ms.k.authority {
return nil, errorsmod.Wrapf(govtypes.ErrInvalidSigner, "only authority can override stored codes; expected %s, got %s", ms.k.authority, req.Signer)
}

// store the contract codes
babylonContractCodeID, btcStakingContractCodeID, btcFinalityContractCodeID, err := ms.k.StoreBabylonContractCodes(ctx, req.BabylonContractCode, req.BtcStakingContractCode, req.BtcFinalityContractCode)
if err != nil {
return nil, err
}

// update params
params.BabylonContractCodeId = babylonContractCodeID
params.BtcStakingContractCodeId = btcStakingContractCodeID
params.BtcFinalityContractCodeId = btcFinalityContractCodeID
if err := ms.k.SetParams(ctx, params); err != nil {
panic(err)
}

return &types.MsgStoreBabylonContractCodesResponse{}, nil
}

func (ms msgServer) InstantiateBabylonContracts(goCtx context.Context, req *types.MsgInstantiateBabylonContracts) (*types.MsgInstantiateBabylonContractsResponse, error) {
ctx := sdk.UnwrapSDKContext(goCtx)

Expand All @@ -59,29 +32,33 @@ func (ms msgServer) InstantiateBabylonContracts(goCtx context.Context, req *type

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

// instantiate the contracts
babylonContractAddr, btcStakingContractAddr, btcFinalityContractAddr, err := ms.k.InstantiateBabylonContracts(ctx, initMsg)
babylonContractAddr, btcStakingContractAddr, btcFinalityContractAddr, err := ms.k.InstantiateBabylonContracts(ctx, req.BabylonContractCodeId, initMsg)
if err != nil {
return nil, err
}

// update params
params.BabylonContractCodeId = req.BabylonContractCodeId
params.BtcStakingContractCodeId = req.BtcStakingContractCodeId
params.BtcFinalityContractCodeId = req.BtcFinalityContractCodeId
params.BabylonContractAddress = babylonContractAddr
params.BtcStakingContractAddress = btcStakingContractAddr
params.BtcFinalityContractAddress = btcFinalityContractAddr
Expand Down
Loading

0 comments on commit 7ba5c63

Please sign in to comment.