Skip to content

Commit

Permalink
chore: Upgrade CosmWasm Parameters (#132)
Browse files Browse the repository at this point in the history
- Update cosmwasm params during upgrade
- Mainnet only gov account can store new contracts
- Testnet everybody can store new contracts
  • Loading branch information
RafilxTenfen authored Oct 8, 2024
1 parent bbdfa4e commit 07e4437
Show file tree
Hide file tree
Showing 12 changed files with 166 additions and 10 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,11 @@ and add `verified` to delegation status parse `NewBTCDelegationStatusFromString`

### State Machine Breaking

* [#132](https://github.com/babylonlabs-io/babylon/pull/132) Add CosmWasm parameters
update during v1 upgrade handler.

### Misc Improvements

* [#130](https://github.com/babylonlabs-io/babylon/pull/130) Fix bugs in the
transaction fee refunding mechanism for covenant signatures and finality signatures
* [#125](https://github.com/babylonlabs-io/babylon/pull/125) Implement ADR-028 and
Expand Down
1 change: 1 addition & 0 deletions app/include_upgrade_mainnet.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ func init() {
Upgrades = []upgrades.Upgrade{v1.CreateUpgrade(v1.UpgradeDataString{
BtcStakingParamStr: mainnet.BtcStakingParamStr,
FinalityParamStr: mainnet.FinalityParamStr,
CosmWasmParamStr: mainnet.CosmWasmParamStr,
NewBtcHeadersStr: mainnet.NewBtcHeadersStr,
SignedFPsStr: mainnet.SignedFPsStr,
TokensDistributionStr: mainnet.TokensDistributionStr,
Expand Down
1 change: 1 addition & 0 deletions app/include_upgrade_testnet.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ func init() {
Upgrades = []upgrades.Upgrade{v1.CreateUpgrade(v1.UpgradeDataString{
BtcStakingParamStr: testnet.BtcStakingParamStr,
FinalityParamStr: testnet.FinalityParamStr,
CosmWasmParamStr: testnet.CosmWasmParamStr,
NewBtcHeadersStr: testnet.NewBtcHeadersStr,
SignedFPsStr: testnet.SignedFPsStr,
TokensDistributionStr: testnet.TokensDistributionStr,
Expand Down
7 changes: 6 additions & 1 deletion app/upgrades/v1/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,18 @@ to push the devenet data into the github repository.

## Upgrade data as string

This upgrade loads 5 JSONs from strings in different files.
The upgrade data was left as string to make it easier for `devnet` testing
where it is needed to replace the data files, and would be harder to create
the data files if it used actual go structures to generate the data.

This upgrade loads 6 JSONs from strings in different files.

- BTC Headers at `./data_btc_headers.go`
- Finality Providers signed messages at`./data_signed_fps.go`
- Tokens distribution at `./data_token_distribution.go`
- BTC Staking Parameters `./btcstaking_params.go`
- Finality Parameters `./finality_params.go`
- CosmWasm Parameters `./cosmwasm_params.go`

### BTC Headers

Expand Down
20 changes: 16 additions & 4 deletions app/upgrades/v1/data_params_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package v1_test
import (
"testing"

wasmtypes "github.com/CosmWasm/wasmd/x/wasm/types"
"github.com/stretchr/testify/require"

"github.com/babylonlabs-io/babylon/app"
Expand All @@ -12,17 +13,28 @@ import (
func TestHardCodedBtcStakingParamsAreValid(t *testing.T) {
bbnApp := app.NewTmpBabylonApp()
for _, upgradeData := range UpgradeV1Data {
loadedParamas, err := v1.LoadBtcStakingParamsFromData(bbnApp.AppCodec(), upgradeData.BtcStakingParamStr)
params, err := v1.LoadBtcStakingParamsFromData(bbnApp.AppCodec(), upgradeData.BtcStakingParamStr)
require.NoError(t, err)
require.NoError(t, loadedParamas.Validate())
require.NoError(t, params.Validate())
}
}

func TestHardCodedFinalityParamsAreValid(t *testing.T) {
bbnApp := app.NewTmpBabylonApp()
for _, upgradeData := range UpgradeV1Data {
loadedParamas, err := v1.LoadFinalityParamsFromData(bbnApp.AppCodec(), upgradeData.FinalityParamStr)
params, err := v1.LoadFinalityParamsFromData(bbnApp.AppCodec(), upgradeData.FinalityParamStr)
require.NoError(t, err)
require.NoError(t, loadedParamas.Validate())
require.NoError(t, params.Validate())
}
}

func TestHardCodedWasmParamsAreValid(t *testing.T) {
bbnApp := app.NewTmpBabylonApp()

for _, upgradeData := range UpgradeV1Data {
params, err := v1.LoadCosmWasmParamsFromData(bbnApp.AppCodec(), upgradeData.CosmWasmParamStr)
require.NoError(t, err)
require.NotNil(t, params)
require.Equal(t, params.InstantiateDefaultPermission, wasmtypes.AccessTypeEverybody)
}
}
12 changes: 12 additions & 0 deletions app/upgrades/v1/mainnet/cosmwasm_params.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package mainnet

// CosmWasm parameters for mainnet should allow only the
// governance module account authtypes.NewModuleAddress(govtypes.ModuleName)
// to upload and everybody to instantiate.
const CosmWasmParamStr = `{
"code_upload_access": {
"permission": "AnyOfAddresses",
"addresses": ["bbn10d07y265gmmuvt4z0w9aw880jnsr700jduz5f2"]
},
"instantiate_default_permission": "Everybody"
}`
Binary file added app/upgrades/v1/testdata/reflect_1_5.wasm
Binary file not shown.
10 changes: 10 additions & 0 deletions app/upgrades/v1/testnet/cosmwasm_params.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package testnet

// CosmWasm parameters for testnet should allow everybody to
// upload and instantiate.
const CosmWasmParamStr = `{
"code_upload_access": {
"permission": "Everybody"
},
"instantiate_default_permission": "Everybody"
}`
1 change: 1 addition & 0 deletions app/upgrades/v1/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package v1
type UpgradeDataString struct {
BtcStakingParamStr string
FinalityParamStr string
CosmWasmParamStr string
NewBtcHeadersStr string
SignedFPsStr string
TokensDistributionStr string
Expand Down
43 changes: 40 additions & 3 deletions app/upgrades/v1/upgrades.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ import (
sdkmath "cosmossdk.io/math"
store "cosmossdk.io/store/types"
upgradetypes "cosmossdk.io/x/upgrade/types"
wasmkeeper "github.com/CosmWasm/wasmd/x/wasm/keeper"
wasmtypes "github.com/CosmWasm/wasmd/x/wasm/types"
"github.com/btcsuite/btcd/chaincfg"
"github.com/cosmos/cosmos-sdk/codec"
sdk "github.com/cosmos/cosmos-sdk/types"
Expand Down Expand Up @@ -59,7 +61,12 @@ func CreateUpgradeHandler(upgradeDataStr UpgradeDataString) upgrades.UpgradeHand
return nil, err
}

if err := upgradeParameters(ctx, keepers.EncCfg.Codec, &keepers.BTCStakingKeeper, &keepers.FinalityKeeper, upgradeDataStr.BtcStakingParamStr, upgradeDataStr.FinalityParamStr); err != nil {
err = upgradeParameters(
ctx, keepers.EncCfg.Codec,
&keepers.BTCStakingKeeper, &keepers.FinalityKeeper, &keepers.WasmKeeper,
upgradeDataStr.BtcStakingParamStr, upgradeDataStr.FinalityParamStr, upgradeDataStr.CosmWasmParamStr,
)
if err != nil {
return nil, err
}

Expand All @@ -77,14 +84,32 @@ func upgradeParameters(
cdc codec.Codec,
btcK *btcstkkeeper.Keeper,
finK *finalitykeeper.Keeper,
btcStakingParam, finalityParam string,
wasmK *wasmkeeper.Keeper,
btcStakingParam, finalityParam, wasmParam string,
) error {
// Upgrade the staking parameters as first, as other upgrades depend on it.
if err := upgradeBtcStakingParameters(ctx, cdc, btcK, btcStakingParam); err != nil {
return err
}
if err := upgradeFinalityParameters(ctx, cdc, finK, finalityParam); err != nil {
return err
}

return upgradeCosmWasmParameters(ctx, cdc, wasmK, wasmParam)
}

func upgradeCosmWasmParameters(
ctx sdk.Context,
cdc codec.Codec,
k *wasmkeeper.Keeper,
wasmParam string,
) error {
params, err := LoadCosmWasmParamsFromData(cdc, wasmParam)
if err != nil {
return err
}

return upgradeFinalityParameters(ctx, cdc, finK, finalityParam)
return k.SetParams(ctx, params)
}

func upgradeBtcStakingParameters(
Expand Down Expand Up @@ -208,6 +233,18 @@ func LoadFinalityParamsFromData(cdc codec.Codec, data string) (finalitytypes.Par
return params, nil
}

func LoadCosmWasmParamsFromData(cdc codec.Codec, data string) (wasmtypes.Params, error) {
buff := bytes.NewBufferString(data)

var params wasmtypes.Params
err := cdc.UnmarshalJSON(buff.Bytes(), &params)
if err != nil {
return wasmtypes.Params{}, err
}

return params, nil
}

// LoadBTCHeadersFromData returns the BTC headers load from the json string with the headers inside of it.
func LoadBTCHeadersFromData(cdc codec.Codec, data string) ([]*btclighttypes.BTCHeaderInfo, error) {
buff := bytes.NewBufferString(data)
Expand Down
72 changes: 70 additions & 2 deletions app/upgrades/v1/upgrades_test.go
Original file line number Diff line number Diff line change
@@ -1,20 +1,30 @@
package v1_test

import (
_ "embed"
"fmt"
"testing"
"time"

"cosmossdk.io/core/appmodule"
"cosmossdk.io/core/header"
errorsmod "cosmossdk.io/errors"
sdkmath "cosmossdk.io/math"
"cosmossdk.io/x/upgrade"
upgradetypes "cosmossdk.io/x/upgrade/types"
wasmkeeper "github.com/CosmWasm/wasmd/x/wasm/keeper"
wasmtypes "github.com/CosmWasm/wasmd/x/wasm/types"
wasmvm "github.com/CosmWasm/wasmvm/v2"
wasmvmtypes "github.com/CosmWasm/wasmvm/v2/types"
appparams "github.com/babylonlabs-io/babylon/app/params"
"github.com/babylonlabs-io/babylon/app/upgrades"
"github.com/babylonlabs-io/babylon/test/e2e/util"
"github.com/babylonlabs-io/babylon/testutil/datagen"
tmproto "github.com/cometbft/cometbft/proto/tendermint/types"
sdk "github.com/cosmos/cosmos-sdk/types"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
authtypes "github.com/cosmos/cosmos-sdk/x/auth/types"
govtypes "github.com/cosmos/cosmos-sdk/x/gov/types"
minttypes "github.com/cosmos/cosmos-sdk/x/mint/types"
"github.com/stretchr/testify/suite"

Expand All @@ -32,16 +42,21 @@ const (
)

var (
//go:embed testdata/reflect_1_5.wasm
wasmContract []byte

UpgradeV1DataTestnet = v1.UpgradeDataString{
BtcStakingParamStr: testnetdata.BtcStakingParamStr,
FinalityParamStr: testnetdata.FinalityParamStr,
CosmWasmParamStr: testnetdata.CosmWasmParamStr,
NewBtcHeadersStr: testnetdata.NewBtcHeadersStr,
SignedFPsStr: testnetdata.SignedFPsStr,
TokensDistributionStr: testnetdata.TokensDistributionStr,
}
UpgradeV1DataMainnet = v1.UpgradeDataString{
BtcStakingParamStr: mainnetdata.BtcStakingParamStr,
FinalityParamStr: mainnetdata.FinalityParamStr,
CosmWasmParamStr: mainnetdata.CosmWasmParamStr,
NewBtcHeadersStr: mainnetdata.NewBtcHeadersStr,
SignedFPsStr: mainnetdata.SignedFPsStr,
TokensDistributionStr: mainnetdata.TokensDistributionStr,
Expand Down Expand Up @@ -71,6 +86,10 @@ func TestKeeperTestSuite(t *testing.T) {
}

func (s *UpgradeTestSuite) TestUpgrade() {

stakingWasmChecksum, err := wasmvm.CreateChecksum(wasmContract)
s.NoError(err)

testCases := []struct {
msg string
upgradeDataStr v1.UpgradeDataString
Expand All @@ -83,14 +102,58 @@ func (s *UpgradeTestSuite) TestUpgrade() {
UpgradeV1DataMainnet,
s.PreUpgrade,
s.Upgrade,
s.PostUpgrade,
func() {
s.PostUpgrade()

randAddr := datagen.GenRandomAddress().String()
// checks that not everybody can instantiate a contract
wasmMsgServer := wasmkeeper.NewMsgServerImpl(&s.app.WasmKeeper)
resp, err := wasmMsgServer.StoreCode(s.ctx, &wasmtypes.MsgStoreCode{
Sender: randAddr,
WASMByteCode: wasmContract,
})
s.Nil(resp)
s.EqualError(err, errorsmod.Wrap(sdkerrors.ErrUnauthorized, "can not create code").Error())

// onlu gov account can store new contracts
respFromGov, err := wasmMsgServer.StoreCode(s.ctx, &wasmtypes.MsgStoreCode{
Sender: authtypes.NewModuleAddress(govtypes.ModuleName).String(),
WASMByteCode: wasmContract,
})
s.NoError(err)
s.EqualValues(respFromGov.CodeID, 1)
s.Equal(stakingWasmChecksum[:], wasmvmtypes.Checksum(respFromGov.Checksum))

// anyone can instantiate if it was stored already
respInst, err := wasmMsgServer.InstantiateContract(s.ctx, &wasmtypes.MsgInstantiateContract{
Sender: randAddr,
CodeID: respFromGov.CodeID,
Label: "xxxx",
Msg: []byte(`{}`),
Funds: sdk.Coins{},
})
s.NoError(err)
s.NotNil(respInst.Address)
},
},
{
"Test launch software upgrade v1 testnet",
UpgradeV1DataTestnet,
s.PreUpgrade,
s.Upgrade,
s.PostUpgrade,
func() {
s.PostUpgrade()

// checks that anyone can instantiate a contract
wasmMsgServer := wasmkeeper.NewMsgServerImpl(&s.app.WasmKeeper)
resp, err := wasmMsgServer.StoreCode(s.ctx, &wasmtypes.MsgStoreCode{
Sender: datagen.GenRandomAddress().String(),
WASMByteCode: wasmContract,
})
s.NoError(err)
s.EqualValues(resp.CodeID, 1)
s.Equal(stakingWasmChecksum[:], wasmvmtypes.Checksum(resp.Checksum))
},
},
}

Expand Down Expand Up @@ -252,4 +315,9 @@ func (s *UpgradeTestSuite) PostUpgrade() {
balanceAfterUpgrade := s.app.BankKeeper.GetBalance(s.ctx, sdkAddr, appparams.DefaultBondDenom)
s.Equal(expectedBalance.String(), balanceAfterUpgrade.String())
}

chainWasmParams := s.app.WasmKeeper.GetParams(s.ctx)
upgradeWasmParams, err := v1.LoadCosmWasmParamsFromData(s.app.AppCodec(), s.upgradeDataStr.CosmWasmParamStr)
s.NoError(err)
s.EqualValues(chainWasmParams, upgradeWasmParams)
}
4 changes: 4 additions & 0 deletions testutil/datagen/val_set.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,10 @@ func GenRandomValidatorAddress() sdk.ValAddress {
return sdk.ValAddress(ed25519.GenPrivKey().PubKey().Address())
}

func GenRandomAddress() sdk.AccAddress {
return sdk.AccAddress(ed25519.GenPrivKey().PubKey().Address())
}

func GenRandomPubkeysAndSigs(n int, msg []byte) ([]bls12381.PublicKey, []bls12381.Signature) {
var blsPubkeys []bls12381.PublicKey
var blsSigs []bls12381.Signature
Expand Down

0 comments on commit 07e4437

Please sign in to comment.