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

adr-37 parameters for btc light client ranges #310

Merged
Merged
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
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,11 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/)
- [#305](https://github.com/babylonlabs-io/babylon/pull/305) chore: add more error logs to `VerifyInclusionProofAndGetHeight`
- [#304](https://github.com/babylonlabs-io/babylon/pull/304) Add highest voted height to finality provider

### State Machine Breaking

- [#310](https://github.com/babylonlabs-io/babylon/pull/310) implement adr-37 -
making params valid for btc light client ranges

## v0.17.1

### Bug fixes
Expand Down
2 changes: 1 addition & 1 deletion app/include_upgrade_mainnet.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (
// init is used to include v1 upgrade for mainnet data
func init() {
Upgrades = []upgrades.Upgrade{v1.CreateUpgrade(v1.UpgradeDataString{
BtcStakingParamStr: mainnet.BtcStakingParamStr,
BtcStakingParamsStr: mainnet.BtcStakingParamsStr,
FinalityParamStr: mainnet.FinalityParamStr,
IncentiveParamStr: mainnet.IncentiveParamStr,
CosmWasmParamStr: mainnet.CosmWasmParamStr,
Expand Down
2 changes: 1 addition & 1 deletion app/include_upgrade_testnet.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import (
// it is also used for e2e testing
func init() {
Upgrades = []upgrades.Upgrade{v1.CreateUpgrade(v1.UpgradeDataString{
BtcStakingParamStr: testnet.BtcStakingParamStr,
BtcStakingParamsStr: testnet.BtcStakingParamsStr,
FinalityParamStr: testnet.FinalityParamStr,
IncentiveParamStr: testnet.IncentiveParamStr,
CosmWasmParamStr: testnet.CosmWasmParamStr,
Expand Down
19 changes: 16 additions & 3 deletions app/upgrades/v1/data_params_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,32 @@ package v1_test
import (
"testing"

"cosmossdk.io/log"
"cosmossdk.io/store"
storemetrics "cosmossdk.io/store/metrics"
wasmtypes "github.com/CosmWasm/wasmd/x/wasm/types"
dbm "github.com/cosmos/cosmos-db"
"github.com/stretchr/testify/require"

"github.com/babylonlabs-io/babylon/app"
v1 "github.com/babylonlabs-io/babylon/app/upgrades/v1"
testutilk "github.com/babylonlabs-io/babylon/testutil/keeper"
)

func TestHardCodedBtcStakingParamsAreValid(t *testing.T) {
bbnApp := app.NewTmpBabylonApp()
for _, upgradeData := range UpgradeV1Data {
params, err := v1.LoadBtcStakingParamsFromData(bbnApp.AppCodec(), upgradeData.BtcStakingParamStr)
db := dbm.NewMemDB()
stateStore := store.NewCommitMultiStore(db, log.NewTestLogger(t), storemetrics.NewNoOpMetrics())
k, ctx := testutilk.BTCStakingKeeperWithStore(t, db, stateStore, nil, nil, nil)

params, err := v1.LoadBtcStakingParamsFromData(upgradeData.BtcStakingParamsStr)
require.NoError(t, err)
require.NoError(t, params.Validate())

for _, p := range params {
// using set Params here makes sure the parameters in the upgrade string are consistent
err = k.SetParams(ctx, p)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this test checks that the params in the upgrade are consistent by passing through the AddNewPair
but during the upgrade it is overwritten the entire set of params
@KonradStaniec

require.NoError(t, err)
}
}
}

Expand Down
51 changes: 26 additions & 25 deletions app/upgrades/v1/mainnet/btcstaking_params.go
Original file line number Diff line number Diff line change
@@ -1,27 +1,28 @@
package mainnet

// TODO Some default parameters. Consider how to switch those depending on network:
// mainnet, testnet, devnet etc.
const BtcStakingParamStr = `
{
"covenant_pks": [
"43311589af63c2adda04fcd7792c038a05c12a4fe40351b3eb1612ff6b2e5a0e",
"d415b187c6e7ce9da46ac888d20df20737d6f16a41639e68ea055311e1535dd9",
"d27cd27dbff481bc6fc4aa39dd19405eb6010237784ecba13bab130a4a62df5d",
"a3e107fee8879f5cf901161dbf4ff61c252ba5fec6f6407fe81b9453d244c02c",
"c45753e856ad0abb06f68947604f11476c157d13b7efd54499eaa0f6918cf716"
],
"covenant_quorum": 3,
"min_staking_value_sat": "1000",
"max_staking_value_sat": "10000000000",
"min_staking_time_blocks": 10,
"max_staking_time_blocks": 65535,
"slashing_pk_script": "dqkUAQEBAQEBAQEBAQEBAQEBAQEBAQGIrA==",
"min_slashing_tx_fee_sat": "1000",
"slashing_rate": "0.100000000000000000",
"min_unbonding_time_blocks": 0,
"unbonding_fee_sat": "1000",
"min_commission_rate": "0.03",
"delegation_creation_base_gas_fee": 1000,
"allow_list_expiration_height": 0
}`
// TODO Some default parameters
const BtcStakingParamsStr = `[
{
"covenant_pks": [
"43311589af63c2adda04fcd7792c038a05c12a4fe40351b3eb1612ff6b2e5a0e",
"d415b187c6e7ce9da46ac888d20df20737d6f16a41639e68ea055311e1535dd9",
"d27cd27dbff481bc6fc4aa39dd19405eb6010237784ecba13bab130a4a62df5d",
"a3e107fee8879f5cf901161dbf4ff61c252ba5fec6f6407fe81b9453d244c02c",
"c45753e856ad0abb06f68947604f11476c157d13b7efd54499eaa0f6918cf716"
],
"covenant_quorum": 3,
"min_staking_value_sat": 1000,
"max_staking_value_sat": 10000000000,
"min_staking_time_blocks": 10,
"max_staking_time_blocks": 65535,
"slashing_pk_script": "dqkUAQEBAQEBAQEBAQEBAQEBAQEBAQGIrA==",
"min_slashing_tx_fee_sat": 1000,
"slashing_rate": "0.100000000000000000",
"min_unbonding_time_blocks": 0,
"unbonding_fee_sat": 1000,
"min_commission_rate": "0.03",
"delegation_creation_base_gas_fee": 1000,
"allow_list_expiration_height": 0,
"btc_activation_height": 100
}
]`
96 changes: 71 additions & 25 deletions app/upgrades/v1/testnet/btcstaking_params.go
Original file line number Diff line number Diff line change
@@ -1,27 +1,73 @@
package testnet

// TODO Some default parameters. Consider how to switch those depending on network:
// mainnet, testnet, devnet etc.
const BtcStakingParamStr = `
{
"covenant_pks": [
"43311589af63c2adda04fcd7792c038a05c12a4fe40351b3eb1612ff6b2e5a0e",
"d415b187c6e7ce9da46ac888d20df20737d6f16a41639e68ea055311e1535dd9",
"d27cd27dbff481bc6fc4aa39dd19405eb6010237784ecba13bab130a4a62df5d",
"a3e107fee8879f5cf901161dbf4ff61c252ba5fec6f6407fe81b9453d244c02c",
"c45753e856ad0abb06f68947604f11476c157d13b7efd54499eaa0f6918cf716"
],
"covenant_quorum": 3,
"min_staking_value_sat": "1000",
"max_staking_value_sat": "10000000000",
"min_staking_time_blocks": 10,
"max_staking_time_blocks": 65535,
"slashing_pk_script": "dqkUAQEBAQEBAQEBAQEBAQEBAQEBAQGIrA==",
"min_slashing_tx_fee_sat": "1000",
"slashing_rate": "0.100000000000000000",
"min_unbonding_time_blocks": 0,
"unbonding_fee_sat": "1000",
"min_commission_rate": "0.03",
"delegation_creation_base_gas_fee": 1000,
"allow_list_expiration_height": 0
}`
const BtcStakingParamsStr = `[
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

basically just copy&paste 3 times to check if all of those were inserted with a different btc_activation_height

{
"covenant_pks": [
"43311589af63c2adda04fcd7792c038a05c12a4fe40351b3eb1612ff6b2e5a0e",
"d415b187c6e7ce9da46ac888d20df20737d6f16a41639e68ea055311e1535dd9",
"d27cd27dbff481bc6fc4aa39dd19405eb6010237784ecba13bab130a4a62df5d",
"a3e107fee8879f5cf901161dbf4ff61c252ba5fec6f6407fe81b9453d244c02c",
"c45753e856ad0abb06f68947604f11476c157d13b7efd54499eaa0f6918cf716"
],
"covenant_quorum": 3,
"min_staking_value_sat": 1000,
"max_staking_value_sat": 10000000000,
"min_staking_time_blocks": 10,
"max_staking_time_blocks": 65535,
"slashing_pk_script": "dqkUAQEBAQEBAQEBAQEBAQEBAQEBAQGIrA==",
"min_slashing_tx_fee_sat": 1000,
"slashing_rate": "0.100000000000000000",
"min_unbonding_time_blocks": 0,
"unbonding_fee_sat": 1000,
"min_commission_rate": "0.03",
"delegation_creation_base_gas_fee": 1000,
"allow_list_expiration_height": 0,
"btc_activation_height": 100
},
{
"covenant_pks": [
"43311589af63c2adda04fcd7792c038a05c12a4fe40351b3eb1612ff6b2e5a0e",
"d415b187c6e7ce9da46ac888d20df20737d6f16a41639e68ea055311e1535dd9",
"d27cd27dbff481bc6fc4aa39dd19405eb6010237784ecba13bab130a4a62df5d",
"a3e107fee8879f5cf901161dbf4ff61c252ba5fec6f6407fe81b9453d244c02c",
"c45753e856ad0abb06f68947604f11476c157d13b7efd54499eaa0f6918cf716"
],
"covenant_quorum": 3,
"min_staking_value_sat": 1000,
"max_staking_value_sat": 10000000000,
"min_staking_time_blocks": 10,
"max_staking_time_blocks": 65535,
"slashing_pk_script": "dqkUAQEBAQEBAQEBAQEBAQEBAQEBAQGIrA==",
"min_slashing_tx_fee_sat": 1000,
"slashing_rate": "0.100000000000000000",
"min_unbonding_time_blocks": 0,
"unbonding_fee_sat": 1000,
"min_commission_rate": "0.03",
"delegation_creation_base_gas_fee": 1000,
"allow_list_expiration_height": 0,
"btc_activation_height": 150
},
{
"covenant_pks": [
"43311589af63c2adda04fcd7792c038a05c12a4fe40351b3eb1612ff6b2e5a0e",
"d415b187c6e7ce9da46ac888d20df20737d6f16a41639e68ea055311e1535dd9",
"d27cd27dbff481bc6fc4aa39dd19405eb6010237784ecba13bab130a4a62df5d",
"a3e107fee8879f5cf901161dbf4ff61c252ba5fec6f6407fe81b9453d244c02c",
"c45753e856ad0abb06f68947604f11476c157d13b7efd54499eaa0f6918cf716"
],
"covenant_quorum": 3,
"min_staking_value_sat": 1000,
"max_staking_value_sat": 10000000000,
"min_staking_time_blocks": 10,
"max_staking_time_blocks": 65535,
"slashing_pk_script": "dqkUAQEBAQEBAQEBAQEBAQEBAQEBAQGIrA==",
"min_slashing_tx_fee_sat": 1000,
"slashing_rate": "0.100000000000000000",
"min_unbonding_time_blocks": 0,
"unbonding_fee_sat": 1000,
"min_commission_rate": "0.03",
"delegation_creation_base_gas_fee": 1000,
"allow_list_expiration_height": 0,
"btc_activation_height": 128
}
]`
2 changes: 1 addition & 1 deletion app/upgrades/v1/types.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package v1

type UpgradeDataString struct {
BtcStakingParamStr string
BtcStakingParamsStr string
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@babylon-devops modified this to BtcStakingParamStr => BtcStakingParamsStr since now it is a slice of parameters

FinalityParamStr string
IncentiveParamStr string
CosmWasmParamStr string
Expand Down
32 changes: 21 additions & 11 deletions app/upgrades/v1/upgrades.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"encoding/json"
"errors"
"fmt"
"sort"

sdkmath "cosmossdk.io/math"
store "cosmossdk.io/store/types"
Expand Down Expand Up @@ -87,7 +88,7 @@ func CreateUpgradeHandler(upgradeDataStr UpgradeDataString) upgrades.UpgradeHand
&keepers.FinalityKeeper,
&keepers.IncentiveKeeper,
&keepers.WasmKeeper,
upgradeDataStr.BtcStakingParamStr,
upgradeDataStr.BtcStakingParamsStr,
upgradeDataStr.FinalityParamStr,
upgradeDataStr.IncentiveParamStr,
upgradeDataStr.CosmWasmParamStr,
Expand Down Expand Up @@ -142,7 +143,7 @@ func upgradeParameters(
btcStakingParam, finalityParam, incentiveParam, wasmParam string,
) error {
// Upgrade the staking parameters as first, as other upgrades depend on it.
if err := upgradeBtcStakingParameters(ctx, cdc, btcK, btcStakingParam); err != nil {
if err := upgradeBtcStakingParameters(ctx, btcK, btcStakingParam); err != nil {
return fmt.Errorf("failed to upgrade btc staking parameters: %w", err)
}
if err := upgradeFinalityParameters(ctx, cdc, finK, finalityParam); err != nil {
Expand Down Expand Up @@ -189,18 +190,22 @@ func upgradeCosmWasmParameters(

func upgradeBtcStakingParameters(
ctx sdk.Context,
cdc codec.Codec,
k *btcstkkeeper.Keeper,
btcStakingParam string,
) error {
params, err := LoadBtcStakingParamsFromData(cdc, btcStakingParam)
// params should be already sorted by their btc activation
// block height in ascending order
params, err := LoadBtcStakingParamsFromData(btcStakingParam)
if err != nil {
return err
}

// We are overwriting the params at version 0, as the upgrade is happening from
// TGE chain so there should be only one version of the params
return k.OverwriteParamsAtVersion(ctx, 0, params)
for version, p := range params {
if err := k.OverwriteParamsAtVersion(ctx, uint32(version), p); err != nil {
return err
}
}
return nil
}

func upgradeFinalityParameters(
Expand Down Expand Up @@ -295,15 +300,20 @@ func upgradeBTCHeaders(ctx sdk.Context, cdc codec.Codec, btcLigthK *btclightkeep
return insertBtcHeaders(ctx, btcLigthK, newHeaders)
}

func LoadBtcStakingParamsFromData(cdc codec.Codec, data string) (btcstktypes.Params, error) {
func LoadBtcStakingParamsFromData(data string) ([]btcstktypes.Params, error) {
buff := bytes.NewBufferString(data)

var params btcstktypes.Params
err := cdc.UnmarshalJSON(buff.Bytes(), &params)
var params []btcstktypes.Params
err := json.Unmarshal(buff.Bytes(), &params)
if err != nil {
return btcstktypes.Params{}, err
return []btcstktypes.Params{}, err
}

// sort params by the BTC activation height ascending order 100, 150, 200...
sort.Slice(params, func(i, j int) bool {
return params[i].BtcActivationHeight < params[j].BtcActivationHeight
})

return params, nil
}

Expand Down
20 changes: 15 additions & 5 deletions app/upgrades/v1/upgrades_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ var (
wasmContract []byte

UpgradeV1DataTestnet = v1.UpgradeDataString{
BtcStakingParamStr: testnetdata.BtcStakingParamStr,
BtcStakingParamsStr: testnetdata.BtcStakingParamsStr,
FinalityParamStr: testnetdata.FinalityParamStr,
IncentiveParamStr: testnetdata.IncentiveParamStr,
CosmWasmParamStr: testnetdata.CosmWasmParamStr,
Expand All @@ -55,7 +55,7 @@ var (
AllowedStakingTxHashesStr: testnetdata.AllowedStakingTxHashesStr,
}
UpgradeV1DataMainnet = v1.UpgradeDataString{
BtcStakingParamStr: mainnetdata.BtcStakingParamStr,
BtcStakingParamsStr: mainnetdata.BtcStakingParamsStr,
FinalityParamStr: mainnetdata.FinalityParamStr,
IncentiveParamStr: mainnetdata.IncentiveParamStr,
CosmWasmParamStr: mainnetdata.CosmWasmParamStr,
Expand Down Expand Up @@ -203,7 +203,7 @@ func (s *UpgradeTestSuite) PreUpgrade() {
s.btcHeadersLenPreUpgrade = len(allBtcHeaders)

// Before upgrade, the params should be different
bsParamsFromUpgrade, err := v1.LoadBtcStakingParamsFromData(s.app.AppCodec(), s.upgradeDataStr.BtcStakingParamStr)
bsParamsFromUpgrade, err := v1.LoadBtcStakingParamsFromData(s.upgradeDataStr.BtcStakingParamsStr)
s.NoError(err)
bsModuleParams := s.app.BTCStakingKeeper.GetParams(s.ctx)
s.NotEqualValues(bsModuleParams, bsParamsFromUpgrade)
Expand Down Expand Up @@ -277,10 +277,20 @@ func (s *UpgradeTestSuite) PostUpgrade() {
}

// After upgrade, the params should be the same
bsParamsFromUpgrade, err := v1.LoadBtcStakingParamsFromData(s.app.AppCodec(), s.upgradeDataStr.BtcStakingParamStr)
bsParamsFromUpgrade, err := v1.LoadBtcStakingParamsFromData(s.upgradeDataStr.BtcStakingParamsStr)
s.NoError(err)

bsModuleParams := s.app.BTCStakingKeeper.GetParams(s.ctx)
s.EqualValues(bsModuleParams, bsParamsFromUpgrade)
lastParamInUpgradeData := bsParamsFromUpgrade[len(bsParamsFromUpgrade)-1]
s.EqualValues(bsModuleParams, lastParamInUpgradeData)

for expVersion, paramsInUpgradeData := range bsParamsFromUpgrade {
bsParamsAtBtcHeight, version, err := s.app.BTCStakingKeeper.GetParamsForBtcHeight(s.ctx, uint64(paramsInUpgradeData.BtcActivationHeight))
s.NoError(err)
s.Equal(uint32(expVersion), version)
s.Equal(*bsParamsAtBtcHeight, paramsInUpgradeData)
}

fParamsFromUpgrade, err := v1.LoadFinalityParamsFromData(s.app.AppCodec(), s.upgradeDataStr.FinalityParamStr)
s.NoError(err)
fModuleParams := s.app.FinalityKeeper.GetParams(s.ctx)
Expand Down
16 changes: 16 additions & 0 deletions proto/babylon/btcstaking/v1/params.proto
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,22 @@ message Params {
// i.e all staking transactions are allowed to enter Babylon chain afterwards
// setting it to 0 means allow list is disabled
uint64 allow_list_expiration_height = 14;
// btc_activation_height is the btc height from which parameters are activated (inclusive)
uint32 btc_activation_height = 15;
}

// HeightVersionPair pairs a btc height with a version of the parameters
message HeightVersionPair {
// start_height is the height from which the parameters are activated (inclusive)
uint64 start_height = 1;
// version is the version of the parameters
uint32 version = 2;
}

// HeightToVersionMap maps a btc height to a version of the parameters
message HeightToVersionMap {
// Pairs must be sorted by `start_height` in ascending order, without duplicates
repeated HeightVersionPair pairs = 1;
}

// StoredParams attach information about the version of stored parameters
Expand Down
Loading
Loading