Skip to content

Commit

Permalink
Phase-1 stake ingesting - RFC15 (#265)
Browse files Browse the repository at this point in the history
Implement:
- allow list that will expire at certain Babylon block
- upgrade to insert allowed transactions hashes
- modification in message handler to use allow list under certain
Babylon height

i.e approach 4, from
[RFC-15](https://github.com/babylonlabs-io/pm/blob/main/rfc/rfc-015-moderate-intake-of-phase-1-stake.md#4-fixed-list-of-allowed-transactions-with-parameter-how-long-to-keep-the-allow-list)

Rationale for putting allowlist expiration height in params:
- if we discover some problems during initial blocks of phase-2, we may
extend the periond when allow list is used
- we should be able to disable allow list for testnet (setting
expiration height to `0` disable allow list)
  • Loading branch information
KonradStaniec authored Nov 14, 2024
1 parent dd4009d commit 50e9a23
Show file tree
Hide file tree
Showing 25 changed files with 446 additions and 103 deletions.
3 changes: 3 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -191,3 +191,6 @@ jobs:
- name: Run e2e TestSoftwareUpgradeV1TestnetTestSuite
run: |
sudo make test-e2e-cache-upgrade-v1
- name: Dump docker logs on failure
if: failure()
uses: jwalton/gh-docker-logs@v2
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ in checkpointing module
implementation
* [#254](https://github.com/babylonlabs-io/babylon/pull/254) Avoid constant
bech-32 decoding in power table
* [#265](https://github.com/babylonlabs-io/babylon/pull/265) Add allow list for staking txs

## v0.15.0

Expand Down
13 changes: 7 additions & 6 deletions app/include_upgrade_mainnet.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,12 @@ import (
// init is used to include v1 upgrade for mainnet data
func init() {
Upgrades = []upgrades.Upgrade{v1.CreateUpgrade(v1.UpgradeDataString{
BtcStakingParamStr: mainnet.BtcStakingParamStr,
FinalityParamStr: mainnet.FinalityParamStr,
IncentiveParamStr: mainnet.IncentiveParamStr,
CosmWasmParamStr: mainnet.CosmWasmParamStr,
NewBtcHeadersStr: mainnet.NewBtcHeadersStr,
TokensDistributionStr: mainnet.TokensDistributionStr,
BtcStakingParamStr: mainnet.BtcStakingParamStr,
FinalityParamStr: mainnet.FinalityParamStr,
IncentiveParamStr: mainnet.IncentiveParamStr,
CosmWasmParamStr: mainnet.CosmWasmParamStr,
NewBtcHeadersStr: mainnet.NewBtcHeadersStr,
TokensDistributionStr: mainnet.TokensDistributionStr,
AllowedStakingTxHashesStr: mainnet.AllowedStakingTxHashesStr,
})}
}
13 changes: 7 additions & 6 deletions app/include_upgrade_testnet.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,12 @@ import (
// it is also used for e2e testing
func init() {
Upgrades = []upgrades.Upgrade{v1.CreateUpgrade(v1.UpgradeDataString{
BtcStakingParamStr: testnet.BtcStakingParamStr,
FinalityParamStr: testnet.FinalityParamStr,
IncentiveParamStr: testnet.IncentiveParamStr,
CosmWasmParamStr: testnet.CosmWasmParamStr,
NewBtcHeadersStr: testnet.NewBtcHeadersStr,
TokensDistributionStr: testnet.TokensDistributionStr,
BtcStakingParamStr: testnet.BtcStakingParamStr,
FinalityParamStr: testnet.FinalityParamStr,
IncentiveParamStr: testnet.IncentiveParamStr,
CosmWasmParamStr: testnet.CosmWasmParamStr,
NewBtcHeadersStr: testnet.NewBtcHeadersStr,
TokensDistributionStr: testnet.TokensDistributionStr,
AllowedStakingTxHashesStr: testnet.AllowedStakingTxHashesStr,
})}
}
2 changes: 2 additions & 0 deletions app/upgrades/v1/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,10 @@ This upgrade loads 5 JSONs from strings in different files.
- BTC Headers at `./data_btc_headers.go`
- Tokens distribution at `./data_token_distribution.go`
- BTC Staking Parameters `./btcstaking_params.go`
- Incentive Parameters `./incentive.go`
- Finality Parameters `./finality_params.go`
- CosmWasm Parameters `./cosmwasm_params.go`
- Allowed Staking Tx Hashes `./allowed_staking_tx_hashes.go`

### BTC Headers

Expand Down
23 changes: 23 additions & 0 deletions app/upgrades/v1/data_allowed_staking_tx_hashes_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package v1_test

import (
"testing"

v1 "github.com/babylonlabs-io/babylon/app/upgrades/v1"
"github.com/btcsuite/btcd/chaincfg/chainhash"
"github.com/stretchr/testify/require"
)

func TestLoadAllowedStakingTxHashesFromData(t *testing.T) {
for _, upgradeData := range UpgradeV1Data {
d, err := v1.LoadAllowedStakingTransactionHashesFromData(upgradeData.AllowedStakingTxHashesStr)
require.NoError(t, err)
require.NotNil(t, d)
require.Greater(t, len(d.TxHashes), 0)

for _, txHash := range d.TxHashes {
_, err := chainhash.NewHashFromStr(txHash)
require.NoError(t, err)
}
}
}
9 changes: 9 additions & 0 deletions app/upgrades/v1/mainnet/allowed_staking_tx_hashes.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package mainnet

// TODO: Specify allowed staking tx hashes for v1 upgrade for mainnet
const AllowedStakingTxHashesStr = `{
"tx_hashes": [
"90463a1e0b19137f07c304f2c1c79351fa6f97f1195f1f90dc0b3b57893a350d",
"1db6de324beefd6870ec62a7385cac7144a43eba8e61fca202a8b41d963680ef"
]
}`
4 changes: 3 additions & 1 deletion app/upgrades/v1/mainnet/btcstaking_params.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,5 +21,7 @@ const BtcStakingParamStr = `
"slashing_rate": "0.100000000000000000",
"min_unbonding_time_blocks": 0,
"unbonding_fee_sat": "1000",
"min_commission_rate": "0.03"
"min_commission_rate": "0.03",
"delegation_creation_base_gas_fee": 1000,
"allow_list_expiration_height": 0
}`
9 changes: 9 additions & 0 deletions app/upgrades/v1/testnet/allowed_staking_tx_hashes.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package testnet

// TODO: Specify allowed staking tx hashes for v1 upgrade for testnet
const AllowedStakingTxHashesStr = `{
"tx_hashes": [
"a3572b22571b46b7688fbfe1e8aeee82bb772d52c5ddb069cda361dc33fb4191",
"276a2b384614976c9a6a737f347e981b91f4dc269ee510a6f71e5eec9ecb903a"
]
}`
3 changes: 2 additions & 1 deletion app/upgrades/v1/testnet/btcstaking_params.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,5 +22,6 @@ const BtcStakingParamStr = `
"min_unbonding_time_blocks": 0,
"unbonding_fee_sat": "1000",
"min_commission_rate": "0.03",
"delegation_creation_base_gas_fee": 1000
"delegation_creation_base_gas_fee": 1000,
"allow_list_expiration_height": 0
}`
17 changes: 11 additions & 6 deletions app/upgrades/v1/types.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
package v1

type UpgradeDataString struct {
BtcStakingParamStr string
FinalityParamStr string
IncentiveParamStr string
CosmWasmParamStr string
NewBtcHeadersStr string
TokensDistributionStr string
BtcStakingParamStr string
FinalityParamStr string
IncentiveParamStr string
CosmWasmParamStr string
NewBtcHeadersStr string
TokensDistributionStr string
AllowedStakingTxHashesStr string
}

type DataTokenDistribution struct {
Expand All @@ -16,3 +17,7 @@ type DataTokenDistribution struct {
Amount int64 `json:"amount"`
} `json:"token_distribution"`
}

type AllowedStakingTransactionHashes struct {
TxHashes []string `json:"tx_hashes"`
}
65 changes: 55 additions & 10 deletions app/upgrades/v1/upgrades.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import (
wasmkeeper "github.com/CosmWasm/wasmd/x/wasm/keeper"
wasmtypes "github.com/CosmWasm/wasmd/x/wasm/types"
"github.com/btcsuite/btcd/chaincfg"
"github.com/btcsuite/btcd/chaincfg/chainhash"
"github.com/cosmos/cosmos-sdk/codec"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/types/module"
Expand Down Expand Up @@ -63,7 +64,7 @@ func CreateUpgradeHandler(upgradeDataStr UpgradeDataString) upgrades.UpgradeHand

migrations, err := mm.RunMigrations(ctx, cfg, fromVM)
if err != nil {
return nil, err
return nil, fmt.Errorf("failed to run migrations: %w", err)
}

// Re-initialise the mint module as we have replaced Cosmos SDK's
Expand All @@ -76,7 +77,7 @@ func CreateUpgradeHandler(upgradeDataStr UpgradeDataString) upgrades.UpgradeHand
keepers.StakingKeeper,
)
if err != nil {
return nil, err
return nil, fmt.Errorf("failed to upgrade mint module: %w", err)
}

err = upgradeParameters(
Expand All @@ -92,19 +93,21 @@ func CreateUpgradeHandler(upgradeDataStr UpgradeDataString) upgrades.UpgradeHand
upgradeDataStr.CosmWasmParamStr,
)
if err != nil {
return nil, err
return nil, fmt.Errorf("failed to upgrade parameters: %w", err)
}

err = upgradeLaunch(
ctx,
keepers.EncCfg,
&keepers.BTCLightClientKeeper,
&keepers.BTCStakingKeeper,
keepers.BankKeeper,
upgradeDataStr.NewBtcHeadersStr,
upgradeDataStr.TokensDistributionStr,
upgradeDataStr.AllowedStakingTxHashesStr,
)
if err != nil {
return nil, err
return nil, fmt.Errorf("failed to upgrade inserting additional data: %w", err)
}

return migrations, nil
Expand Down Expand Up @@ -140,16 +143,20 @@ func upgradeParameters(
) error {
// Upgrade the staking parameters as first, as other upgrades depend on it.
if err := upgradeBtcStakingParameters(ctx, cdc, btcK, btcStakingParam); err != nil {
return err
return fmt.Errorf("failed to upgrade btc staking parameters: %w", err)
}
if err := upgradeFinalityParameters(ctx, cdc, finK, finalityParam); err != nil {
return err
return fmt.Errorf("failed to upgrade finality parameters: %w", err)
}
if err := upgradeIncentiveParameters(ctx, cdc, iK, incentiveParam); err != nil {
return err
}

return upgradeCosmWasmParameters(ctx, cdc, wasmK, wasmParam)
if err := upgradeCosmWasmParameters(ctx, cdc, wasmK, wasmParam); err != nil {
return fmt.Errorf("failed to upgrade cosmwasm parameters: %w", err)
}

return nil
}

func upgradeIncentiveParameters(
Expand Down Expand Up @@ -217,14 +224,23 @@ func upgradeLaunch(
ctx sdk.Context,
encCfg *appparams.EncodingConfig,
btcLigthK *btclightkeeper.Keeper,
btcK *btcstkkeeper.Keeper,
bankK bankkeeper.SendKeeper,
btcHeaders, tokensDistribution string,
btcHeaders, tokensDistribution, allowedStakingTxHashes string,
) error {
if err := upgradeTokensDistribution(ctx, bankK, tokensDistribution); err != nil {
return err
return fmt.Errorf("failed to upgrade tokens distribution: %w", err)
}

return upgradeBTCHeaders(ctx, encCfg.Codec, btcLigthK, btcHeaders)
if err := upgradeAllowedStakingTransactions(ctx, encCfg.Codec, btcK, allowedStakingTxHashes); err != nil {
return fmt.Errorf("failed to upgrade allowed staking transactions: %w", err)
}

if err := upgradeBTCHeaders(ctx, encCfg.Codec, btcLigthK, btcHeaders); err != nil {
return fmt.Errorf("failed to upgrade btc headers: %w", err)
}

return nil
}

func upgradeTokensDistribution(ctx sdk.Context, bankK bankkeeper.SendKeeper, tokensDistribution string) error {
Expand Down Expand Up @@ -253,6 +269,23 @@ func upgradeTokensDistribution(ctx sdk.Context, bankK bankkeeper.SendKeeper, tok
return nil
}

func upgradeAllowedStakingTransactions(ctx sdk.Context, cdc codec.Codec, btcStakingK *btcstkkeeper.Keeper, allowedStakingTxHashes string) error {
data, err := LoadAllowedStakingTransactionHashesFromData(allowedStakingTxHashes)
if err != nil {
return fmt.Errorf("failed to load allowed staking transaction hashes from string %s: %w", allowedStakingTxHashes, err)
}

for _, txHash := range data.TxHashes {
hash, err := chainhash.NewHashFromStr(txHash)
if err != nil {
return fmt.Errorf("failed to parse tx hash: %w", err)
}
btcStakingK.IndexAllowedStakingTransaction(ctx, hash)
}

return nil
}

func upgradeBTCHeaders(ctx sdk.Context, cdc codec.Codec, btcLigthK *btclightkeeper.Keeper, btcHeaders string) error {
newHeaders, err := LoadBTCHeadersFromData(cdc, btcHeaders)
if err != nil {
Expand Down Expand Up @@ -336,6 +369,18 @@ func LoadTokenDistributionFromData(data string) (DataTokenDistribution, error) {
return d, nil
}

func LoadAllowedStakingTransactionHashesFromData(data string) (*AllowedStakingTransactionHashes, error) {
buff := bytes.NewBufferString(data)

var d AllowedStakingTransactionHashes
err := json.Unmarshal(buff.Bytes(), &d)
if err != nil {
return nil, err
}

return &d, nil
}

func insertBtcHeaders(
ctx sdk.Context,
k *btclightkeeper.Keeper,
Expand Down
42 changes: 30 additions & 12 deletions app/upgrades/v1/upgrades_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"github.com/babylonlabs-io/babylon/test/e2e/util"
"github.com/babylonlabs-io/babylon/testutil/datagen"
minttypes "github.com/babylonlabs-io/babylon/x/mint/types"
"github.com/btcsuite/btcd/chaincfg/chainhash"
tmproto "github.com/cometbft/cometbft/proto/tendermint/types"
sdk "github.com/cosmos/cosmos-sdk/types"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
Expand All @@ -45,20 +46,22 @@ var (
wasmContract []byte

UpgradeV1DataTestnet = v1.UpgradeDataString{
BtcStakingParamStr: testnetdata.BtcStakingParamStr,
FinalityParamStr: testnetdata.FinalityParamStr,
IncentiveParamStr: testnetdata.IncentiveParamStr,
CosmWasmParamStr: testnetdata.CosmWasmParamStr,
NewBtcHeadersStr: testnetdata.NewBtcHeadersStr,
TokensDistributionStr: testnetdata.TokensDistributionStr,
BtcStakingParamStr: testnetdata.BtcStakingParamStr,
FinalityParamStr: testnetdata.FinalityParamStr,
IncentiveParamStr: testnetdata.IncentiveParamStr,
CosmWasmParamStr: testnetdata.CosmWasmParamStr,
NewBtcHeadersStr: testnetdata.NewBtcHeadersStr,
TokensDistributionStr: testnetdata.TokensDistributionStr,
AllowedStakingTxHashesStr: testnetdata.AllowedStakingTxHashesStr,
}
UpgradeV1DataMainnet = v1.UpgradeDataString{
BtcStakingParamStr: mainnetdata.BtcStakingParamStr,
FinalityParamStr: mainnetdata.FinalityParamStr,
IncentiveParamStr: mainnetdata.IncentiveParamStr,
CosmWasmParamStr: mainnetdata.CosmWasmParamStr,
NewBtcHeadersStr: mainnetdata.NewBtcHeadersStr,
TokensDistributionStr: mainnetdata.TokensDistributionStr,
BtcStakingParamStr: mainnetdata.BtcStakingParamStr,
FinalityParamStr: mainnetdata.FinalityParamStr,
IncentiveParamStr: mainnetdata.IncentiveParamStr,
CosmWasmParamStr: mainnetdata.CosmWasmParamStr,
NewBtcHeadersStr: mainnetdata.NewBtcHeadersStr,
TokensDistributionStr: mainnetdata.TokensDistributionStr,
AllowedStakingTxHashesStr: mainnetdata.AllowedStakingTxHashesStr,
}
UpgradeV1Data = []v1.UpgradeDataString{UpgradeV1DataTestnet, UpgradeV1DataMainnet}
)
Expand Down Expand Up @@ -300,4 +303,19 @@ func (s *UpgradeTestSuite) PostUpgrade() {
upgradeWasmParams, err := v1.LoadCosmWasmParamsFromData(s.app.AppCodec(), s.upgradeDataStr.CosmWasmParamStr)
s.NoError(err)
s.EqualValues(chainWasmParams, upgradeWasmParams)

allowedStakingTxHashes, err := v1.LoadAllowedStakingTransactionHashesFromData(s.upgradeDataStr.AllowedStakingTxHashesStr)
s.NoError(err)
s.NotNil(allowedStakingTxHashes)
s.Greater(len(allowedStakingTxHashes.TxHashes), 0)

for _, txHash := range allowedStakingTxHashes.TxHashes {
hash, err := chainhash.NewHashFromStr(txHash)
s.NoError(err)

s.True(s.app.BTCStakingKeeper.IsStakingTransactionAllowed(s.ctx, hash))
}

nonExistentTxHash := chainhash.Hash{}
s.False(s.app.BTCStakingKeeper.IsStakingTransactionAllowed(s.ctx, &nonExistentTxHash))
}
4 changes: 4 additions & 0 deletions proto/babylon/btcstaking/v1/params.proto
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,10 @@ message Params {
];
// base gas fee for delegation creation
uint64 delegation_creation_base_gas_fee = 13;
// allow_list_expiration_height is the height at which the allow list expires
// 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;
}

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

0 comments on commit 50e9a23

Please sign in to comment.