-
Notifications
You must be signed in to change notification settings - Fork 49
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'release/v1.x' into backport/PR-519
- Loading branch information
Showing
87 changed files
with
5,815 additions
and
990 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
package testnet | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
|
||
sdkmath "cosmossdk.io/math" | ||
upgradetypes "cosmossdk.io/x/upgrade/types" | ||
sdk "github.com/cosmos/cosmos-sdk/types" | ||
"github.com/cosmos/cosmos-sdk/types/module" | ||
"github.com/cosmos/cosmos-sdk/types/query" | ||
|
||
"github.com/babylonlabs-io/babylon/app/keepers" | ||
"github.com/babylonlabs-io/babylon/app/upgrades" | ||
btcstakingkeeper "github.com/babylonlabs-io/babylon/x/btcstaking/keeper" | ||
btcstakingtypes "github.com/babylonlabs-io/babylon/x/btcstaking/types" | ||
) | ||
|
||
const ( | ||
UpgradeName = "v1rc7" | ||
) | ||
|
||
func CreateUpgrade() upgrades.Upgrade { | ||
return upgrades.Upgrade{ | ||
UpgradeName: UpgradeName, | ||
CreateUpgradeHandler: CreateUpgradeHandler(), | ||
} | ||
} | ||
|
||
// CreateUpgradeHandler upgrade handler for launch. | ||
func CreateUpgradeHandler() upgrades.UpgradeHandlerCreator { | ||
return func(mm *module.Manager, cfg module.Configurator, keepers *keepers.AppKeepers) upgradetypes.UpgradeHandler { | ||
return func(context context.Context, _plan upgradetypes.Plan, fromVM module.VersionMap) (module.VersionMap, error) { | ||
ctx := sdk.UnwrapSDKContext(context) | ||
|
||
logger := ctx.Logger().With("upgrade", UpgradeName) | ||
migrations, err := mm.RunMigrations(ctx, cfg, fromVM) | ||
if err != nil { | ||
return nil, fmt.Errorf("failed to run migrations: %w", err) | ||
} | ||
|
||
logger.Info("migrating finality providers...") | ||
if err := MigrateFinalityProviders(ctx, keepers.BTCStakingKeeper); err != nil { | ||
return nil, fmt.Errorf("failed migrate finality providers: %w", err) | ||
} | ||
logger.Info("finality providers migration done!") | ||
|
||
return migrations, nil | ||
} | ||
} | ||
} | ||
|
||
// MigrateFinalityProviders populates the new CommissionInfo field | ||
// with default values | ||
func MigrateFinalityProviders(goCtx context.Context, k btcstakingkeeper.Keeper) error { | ||
var ( | ||
defaultCommissionMaxRate = sdkmath.LegacyMustNewDecFromStr("0.2") | ||
defaultCommissionMaxChangeRate = sdkmath.LegacyMustNewDecFromStr("0.01") | ||
ctx = sdk.UnwrapSDKContext(goCtx) | ||
pagKey = []byte{} | ||
) | ||
for { | ||
// FinalityProviders query is paginated, so we'll need to use | ||
// the page key to make sure to migrate all of them | ||
res, err := k.FinalityProviders(goCtx, &btcstakingtypes.QueryFinalityProvidersRequest{ | ||
Pagination: &query.PageRequest{Key: pagKey}, | ||
}) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
for _, fp := range res.FinalityProviders { | ||
err := k.UpdateFinalityProvider(goCtx, &btcstakingtypes.FinalityProvider{ | ||
Addr: fp.Addr, | ||
Description: fp.Description, | ||
Commission: fp.Commission, | ||
BtcPk: fp.BtcPk, | ||
Pop: fp.Pop, | ||
SlashedBabylonHeight: fp.SlashedBabylonHeight, | ||
SlashedBtcHeight: fp.SlashedBtcHeight, | ||
Jailed: fp.Jailed, | ||
HighestVotedHeight: fp.HighestVotedHeight, | ||
CommissionInfo: btcstakingtypes.NewCommissionInfoWithTime(defaultCommissionMaxRate, defaultCommissionMaxChangeRate, ctx.BlockHeader().Time), | ||
}) | ||
if err != nil { | ||
return err | ||
} | ||
} | ||
|
||
// Break if there are no more pages | ||
if res.Pagination == nil || len(res.Pagination.NextKey) == 0 { | ||
break | ||
} | ||
|
||
// Set the next pagination key | ||
pagKey = res.Pagination.NextKey | ||
} | ||
|
||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
package testnet_test | ||
|
||
import ( | ||
"math/rand" | ||
"sort" | ||
"testing" | ||
|
||
sdkmath "cosmossdk.io/math" | ||
"cosmossdk.io/store/prefix" | ||
storetypes "cosmossdk.io/store/types" | ||
|
||
appparams "github.com/babylonlabs-io/babylon/app/params" | ||
"github.com/babylonlabs-io/babylon/app/upgrades/v1rc7/testnet" | ||
"github.com/babylonlabs-io/babylon/testutil/datagen" | ||
keepertest "github.com/babylonlabs-io/babylon/testutil/keeper" | ||
btcstakingtypes "github.com/babylonlabs-io/babylon/x/btcstaking/types" | ||
"github.com/cosmos/cosmos-sdk/runtime" | ||
|
||
"github.com/test-go/testify/require" | ||
) | ||
|
||
func FuzzMigrateFinalityProviders(f *testing.F) { | ||
datagen.AddRandomSeedsToFuzzer(f, 10) | ||
f.Fuzz(func(t *testing.T, seed int64) { | ||
var ( | ||
r = rand.New(rand.NewSource(seed)) | ||
storeKey = storetypes.NewKVStoreKey(btcstakingtypes.StoreKey) | ||
storeService = runtime.NewKVStoreService(storeKey) | ||
encConf = appparams.DefaultEncodingConfig() | ||
keeper, ctx = keepertest.BTCStakingKeeperWithStoreKey(t, storeKey, nil, nil, nil) | ||
) | ||
|
||
// seed the store with finality providers without commission info | ||
storeAdapter := runtime.KVStoreAdapter(storeService.OpenKVStore(ctx)) | ||
store := prefix.NewStore(storeAdapter, btcstakingtypes.FinalityProviderKey) | ||
fpCount := rand.Intn(300) | ||
// slice of the expected finality providers after the migration | ||
expFps := make([]btcstakingtypes.FinalityProvider, fpCount) | ||
for i := range expFps { | ||
fp, err := datagen.GenRandomFinalityProvider(r) | ||
require.NoError(t, err) | ||
// make sure commission info is nil when seeding the store | ||
fp.CommissionInfo = nil | ||
// use store directly to store the fps | ||
fpBytes := encConf.Codec.MustMarshal(fp) | ||
store.Set(fp.BtcPk.MustMarshal(), fpBytes) | ||
|
||
// Add the expected fp with the commission info defined | ||
expFps[i] = btcstakingtypes.FinalityProvider{ | ||
Addr: fp.Addr, | ||
Description: fp.Description, | ||
Commission: fp.Commission, | ||
BtcPk: fp.BtcPk, | ||
Pop: fp.Pop, | ||
SlashedBabylonHeight: fp.SlashedBabylonHeight, | ||
SlashedBtcHeight: fp.SlashedBtcHeight, | ||
Jailed: fp.Jailed, | ||
HighestVotedHeight: fp.HighestVotedHeight, | ||
CommissionInfo: btcstakingtypes.NewCommissionInfoWithTime( | ||
sdkmath.LegacyMustNewDecFromStr("0.2"), | ||
sdkmath.LegacyMustNewDecFromStr("0.01"), | ||
ctx.BlockHeader().Time, | ||
), | ||
} | ||
} | ||
|
||
// Run the migration logic | ||
require.NoError(t, testnet.MigrateFinalityProviders(ctx, *keeper)) | ||
|
||
// get all the stored finality providers | ||
migratedFps := []btcstakingtypes.FinalityProvider{} | ||
iter := store.Iterator(nil, nil) | ||
defer iter.Close() | ||
for ; iter.Valid(); iter.Next() { | ||
var fp btcstakingtypes.FinalityProvider | ||
encConf.Codec.MustUnmarshal(iter.Value(), &fp) | ||
migratedFps = append(migratedFps, fp) | ||
} | ||
|
||
// sort the expected and migrated slices | ||
sort.Slice(expFps, func(i, j int) bool { | ||
return expFps[i].Addr < expFps[j].Addr | ||
}) | ||
sort.Slice(migratedFps, func(i, j int) bool { | ||
return migratedFps[i].Addr < migratedFps[j].Addr | ||
}) | ||
require.Equal(t, expFps, migratedFps) | ||
}) | ||
} |
Oops, something went wrong.