Skip to content

Commit

Permalink
msg formats
Browse files Browse the repository at this point in the history
  • Loading branch information
SebastianElvis committed Dec 3, 2024
1 parent 7a46385 commit 51dbbbb
Show file tree
Hide file tree
Showing 9 changed files with 1,355 additions and 74 deletions.
67 changes: 67 additions & 0 deletions docs/proto/proto-docs.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@
- [Query](#babylonlabs.babylon.v1beta1.Query)

- [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 All @@ -41,6 +45,9 @@ Params defines the parameters for the x/babylon module.

| Field | Type | Label | Description |
| ----- | ---- | ----- | ----------- |
| `babylon_contract_code_id` | [uint64](#uint64) | | babylon_contract_code_id is the code ID of the Babylon contract |
| `btc_staking_contract_code_id` | [uint64](#uint64) | | btc_staking_contract_code_id is the code ID of the BTC staking contract |
| `btc_finality_contract_code_id` | [uint64](#uint64) | | btc_finality_contract_code_id is the code ID of the BTC finality contract |
| `babylon_contract_address` | [string](#string) | | babylon_contract_address is the address of the Babylon contract |
| `btc_staking_contract_address` | [string](#string) | | btc_staking_contract_address is the address of the BTC staking contract |
| `btc_finality_contract_address` | [string](#string) | | btc_finality_contract_address is the address of the BTC finality contract |
Expand Down Expand Up @@ -151,6 +158,64 @@ Query provides defines the gRPC querier service



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

### MsgInstantiateBabylonContracts
MsgInstantiateBabylonContracts is the Msg/InstantiateBabylonContracts request
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. |






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

### MsgInstantiateBabylonContractsResponse
MsgInstantiateBabylonContractsResponse is the Msg/InstantiateBabylonContracts
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 @@ -193,6 +258,8 @@ 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. | |

<!-- end services -->
Expand Down
16 changes: 11 additions & 5 deletions proto/babylonlabs/babylon/v1beta1/babylon.proto
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,22 @@ option (gogoproto.equal_all) = false;
message Params {
option (gogoproto.equal) = true;

// babylon_contract_code_id is the code ID of the Babylon contract
uint64 babylon_contract_code_id = 1;
// btc_staking_contract_code_id is the code ID of the BTC staking contract
uint64 btc_staking_contract_code_id = 2;
// btc_finality_contract_code_id is the code ID of the BTC finality contract
uint64 btc_finality_contract_code_id = 3;
// babylon_contract_address is the address of the Babylon contract
string babylon_contract_address = 1
string babylon_contract_address = 4
[ (cosmos_proto.scalar) = "cosmos.AddressString" ];
// btc_staking_contract_address is the address of the BTC staking contract
string btc_staking_contract_address = 2
string btc_staking_contract_address = 5
[ (cosmos_proto.scalar) = "cosmos.AddressString" ];
// btc_finality_contract_address is the address of the BTC finality contract
string btc_finality_contract_address = 3
[ (cosmos_proto.scalar) = "cosmos.AddressString" ];
string btc_finality_contract_address = 6
[ (cosmos_proto.scalar) = "cosmos.AddressString" ];
// max_gas_begin_blocker defines the maximum gas that can be spent in a
// contract sudo callback
uint32 max_gas_begin_blocker = 4;
uint32 max_gas_begin_blocker = 7;
}
47 changes: 47 additions & 0 deletions proto/babylonlabs/babylon/v1beta1/tx.proto
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,58 @@ 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)
returns (MsgInstantiateBabylonContractsResponse);

// UpdateParams defines a (governance) operation for updating the x/auth
// module parameters. The authority defaults to the x/gov module account.
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 {
option (cosmos.msg.v1.signer) = "signer";

// 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;
}

// MsgInstantiateBabylonContractsResponse is the Msg/InstantiateBabylonContracts
// response type.
message MsgInstantiateBabylonContractsResponse {}

// MsgUpdateParams is the Msg/UpdateParams request type.
message MsgUpdateParams {
option (cosmos.msg.v1.signer) = "authority";
Expand Down
54 changes: 54 additions & 0 deletions x/babylon/keeper/msg_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,60 @@ 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)

params := ms.k.GetParams(ctx)

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

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

// update params
params.BabylonContractAddress = babylonContractAddr
params.BtcStakingContractAddress = btcStakingContractAddr
params.BtcFinalityContractAddress = btcFinalityContractAddr
if err := ms.k.SetParams(ctx, params); err != nil {
panic(err)
}

return &types.MsgInstantiateBabylonContractsResponse{}, nil
}

// UpdateParams updates the params.
func (ms msgServer) UpdateParams(goCtx context.Context, req *types.MsgUpdateParams) (*types.MsgUpdateParamsResponse, error) {
if ms.k.authority != req.Authority {
Expand Down
84 changes: 82 additions & 2 deletions x/babylon/keeper/wasm.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,92 @@ import (
"fmt"

errorsmod "cosmossdk.io/errors"
wasmkeeper "github.com/CosmWasm/wasmd/x/wasm/keeper"
wasmtypes "github.com/CosmWasm/wasmd/x/wasm/types"
"github.com/babylonlabs-io/babylon-sdk/x/babylon/contract"
types "github.com/babylonlabs-io/babylon-sdk/x/babylon/types"
sdk "github.com/cosmos/cosmos-sdk/types"
)

func (k Keeper) StoreBabylonContracts(ctx sdk.Context) error {
return nil
// StoreBabylonContractCodes stores the Babylon contract codes
func (k Keeper) StoreBabylonContractCodes(
ctx sdk.Context,
babylonContractCode []byte,
btcStakingContractCode []byte,
btcFinalityContractCode []byte,
) (uint64, uint64, uint64, error) {
contractKeeper := wasmkeeper.NewGovPermissionKeeper(k.wasm)
instantiateConfig := wasmtypes.AccessConfig{
Permission: wasmtypes.AccessTypeAnyOfAddresses,
Addresses: []string{k.authority},
}

// gov address
govAddr, err := sdk.AccAddressFromBech32(k.authority)
if err != nil {
panic(err)
}

// store Babylon contract
babylonContractCodeID, _, err := contractKeeper.Create(ctx, govAddr, babylonContractCode, &instantiateConfig)
if err != nil {
return 0, 0, 0, err
}
// store BTC staking contract
btcStakingContractCodeID, _, err := contractKeeper.Create(ctx, govAddr, btcStakingContractCode, &instantiateConfig)
if err != nil {
return 0, 0, 0, err
}
// store BTC finality contract
btcFinalityContractCodeID, _, err := contractKeeper.Create(ctx, govAddr, btcFinalityContractCode, &instantiateConfig)
if err != nil {
return 0, 0, 0, err
}

return babylonContractCodeID, btcStakingContractCodeID, btcFinalityContractCodeID, nil
}

func (k Keeper) InstantiateBabylonContracts(ctx sdk.Context, initMsg []byte) (string, string, string, error) {
contractKeeper := wasmkeeper.NewGovPermissionKeeper(k.wasm)

// get params
params := k.GetParams(ctx)

// check that the contract codes are stored
if !params.IsCodeStored() {
return "", "", "", types.ErrInvalid.Wrapf("Babylon contract code ID is not set")
}

// gov address
govAddr, err := sdk.AccAddressFromBech32(k.authority)
if err != nil {
panic(err)
}

// instantiate Babylon contract
babylonContractAddr, _, err := contractKeeper.Instantiate(ctx, params.BabylonContractCodeId, govAddr, govAddr, initMsg, "Babylon contract", nil)
if err != nil {
return "", "", "", err
}

// get contract addresses
res, err := k.wasm.QuerySmart(ctx, babylonContractAddr, []byte(`"config":{}`))
if err != nil {
return "", "", "", err
}
var config types.BabylonContractConfig
err = json.Unmarshal(res, &config)
if err != nil {
return "", "", "", err
}
if len(config.BTCStaking) == 0 {
return "", "", "", errorsmod.Wrap(types.ErrInvalid, "failed to instantiate BTC staking contract")
}
if len(config.BTCFinality) == 0 {
return "", "", "", errorsmod.Wrap(types.ErrInvalid, "failed to instantiate BTC finality contract")
}

return babylonContractAddr.String(), config.BTCStaking, config.BTCFinality, nil
}

func (k Keeper) getBTCStakingContractAddr(ctx sdk.Context) sdk.AccAddress {
Expand Down
55 changes: 55 additions & 0 deletions x/babylon/types/babylon.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package types

// Config represents the configuration for the Babylon contract
type BabylonContractConfig struct {
Network string `json:"network"`
BabylonTag []byte `json:"babylon_tag"`
BTCConfirmationDepth uint32 `json:"btc_confirmation_depth"`
CheckpointFinalizationTimeout uint32 `json:"checkpoint_finalization_timeout"`
// NotifyCosmosZone indicates whether to send Cosmos zone messages notifying BTC-finalised headers.
// NOTE: if set to true, then the Cosmos zone needs to integrate the corresponding message
// handler as well
NotifyCosmosZone bool `json:"notify_cosmos_zone"`
// BTCStaking stores a BTC staking contract used for BTC re-staking if set
BTCStaking string `json:"btc_staking,omitempty"`
// BTCFinality stores a BTC finality contract used for BTC finality on the Consumer if set
BTCFinality string `json:"btc_finality,omitempty"`
// ConsumerName represents the name of the Consumer
ConsumerName string `json:"consumer_name,omitempty"`
// ConsumerDescription represents the description of the Consumer
ConsumerDescription string `json:"consumer_description,omitempty"`
}

type BabylonContractInitMsg struct {
// Network represents the Bitcoin network (mainnet, testnet, etc.)
Network string `json:"network"`
// BabylonTag is a string encoding four bytes used for identification / tagging of the Babylon zone.
// NOTE: this is a hex string, not raw bytes
BabylonTag string `json:"babylon_tag"`
// BTCConfirmationDepth is the number of confirmations required for BTC headers
BTCConfirmationDepth uint32 `json:"btc_confirmation_depth"`
// CheckpointFinalizationTimeout is the timeout period for checkpoint finalization
CheckpointFinalizationTimeout uint32 `json:"checkpoint_finalization_timeout"`
// NotifyCosmosZone indicates whether to send Cosmos zone messages notifying BTC-finalised headers.
// NOTE: If set to true, then the Cosmos zone needs to integrate the corresponding message handler
// as well
NotifyCosmosZone bool `json:"notify_cosmos_zone"`
// BTCStakingCodeID is the code ID for the BTC staking contract, if set
BTCStakingCodeID *uint64 `json:"btc_staking_code_id,omitempty"`
// BTCStakingMsg is the instantiation message for the BTC staking contract.
// This message is opaque to the Babylon contract, and depends on the specific staking contract
// being instantiated
BTCStakingMsg []byte `json:"btc_staking_msg,omitempty"`
// BTCFinalityCodeID is the code ID for the BTC finality contract, if set
BTCFinalityCodeID *uint64 `json:"btc_finality_code_id,omitempty"`
// BTCFinalityMsg is the instantiation message for the BTC finality contract.
// This message is opaque to the Babylon contract, and depends on the specific finality contract
// being instantiated
BTCFinalityMsg []byte `json:"btc_finality_msg,omitempty"`
// Admin is the Wasm migration / upgrade admin of the BTC staking contract and the BTC finality contract
Admin string `json:"admin,omitempty"`
// ConsumerName represents the name of the Consumer
ConsumerName string `json:"consumer_name,omitempty"`
// ConsumerDescription represents the description of the Consumer
ConsumerDescription string `json:"consumer_description,omitempty"`
}
Loading

0 comments on commit 51dbbbb

Please sign in to comment.