Skip to content
Merged
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
38 changes: 38 additions & 0 deletions app/upgrades/v4/upgrades.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,10 @@ func migrateGovState(ctx context.Context, cdc codec.Codec, govKeeper *govkeeper.
errs = errors.Join(errs, fmt.Errorf("failed to migrate gov validator shares by governor: %w", err))
}

if err := migrateParticipationEMAs(ctx, govKeeper, sb); err != nil {
errs = errors.Join(errs, fmt.Errorf("failed to migrate gov participation EMAs: %w", err))
}

return errs
}

Expand Down Expand Up @@ -613,3 +617,37 @@ func migrateValidatorsCommission(ctx context.Context, stakingKeeper *stakingkeep
}
return nil
}

// migrateParticipationEMAs re-encodes the three participation EMA values from the old
// atomone decimal-string format ("0.58...") to the scaled-integer bytes expected by
// legacyDecValueCodec.
func migrateParticipationEMAs(ctx context.Context, govKeeper *govkeeper.Keeper, sb *collections.SchemaBuilder) error {
// big.Int.UnmarshalText rejects decimal points, so the primary codec fails on the old
// "0.581818..." bytes. The fallback handles them via LegacyNewDecFromStr.
altCodec := collcodec.NewAltValueCodec(sdk.LegacyDecValue, func(b []byte) (math.LegacyDec, error) {
return math.LegacyNewDecFromStr(string(b))
})

migrate := func(prefix collections.Prefix, name string, dest collections.Item[math.LegacyDec]) error {
ema, err := collections.NewItem(sb, prefix, name, altCodec).Get(ctx)
switch {
case errors.Is(err, collections.ErrNotFound):
ema = math.LegacyNewDecWithPrec(12, 2) // matches SDK v5.MigrateStore initial value
case err != nil:
return fmt.Errorf("get %s: %w", name, err)
}
return dest.Set(ctx, ema)
}

if err := migrate(collections.NewPrefix(80), "participation_ema_legacy", govKeeper.ParticipationEMA); err != nil {
return fmt.Errorf("migrate participation EMA: %w", err)
}
if err := migrate(collections.NewPrefix(96), "constitution_amendment_participation_ema_legacy", govKeeper.ConstitutionAmendmentParticipationEMA); err != nil {
return fmt.Errorf("migrate constitution amendment participation EMA: %w", err)
}
if err := migrate(collections.NewPrefix(112), "law_participation_ema_legacy", govKeeper.LawParticipationEMA); err != nil {
return fmt.Errorf("migrate law participation EMA: %w", err)
}

return nil
}
Loading