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

fix: add old supply offset functions #629

Merged
merged 4 commits into from
Oct 15, 2024
Merged
Show file tree
Hide file tree
Changes from 2 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
69 changes: 69 additions & 0 deletions x/bank/keeper/supply_offset_old.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
package keeper

import (
"context"
"fmt"

"cosmossdk.io/math"
"cosmossdk.io/store/prefix"

"github.com/cosmos/cosmos-sdk/runtime"
"github.com/cosmos/cosmos-sdk/x/bank/types"
)

// NOTE: All these functions should only be used in the v27 migration
// this file should be removed completely after the migration

// GetSupplyOffset retrieves the SupplyOffset from store for a specific denom using the pre v26 (old) key
// TODO: Remove after v27 migration
func (k BaseViewKeeper) GetSupplyOffsetOld(ctx context.Context, denom string) math.Int {
store := runtime.KVStoreAdapter(k.storeService.OpenKVStore(ctx))
supplyOffsetStore := prefix.NewStore(store, types.SupplyOffsetKeyOld)

bz := supplyOffsetStore.Get([]byte(denom))
if bz == nil {
return math.NewInt(0)
}

var amount math.Int
err := amount.Unmarshal(bz)
if err != nil {
panic(fmt.Errorf("unable to unmarshal supply offset value %v", err))
}

return amount
}

// RemoveOldSupplyOffset removes the old supply offset key
// TODO: Remove after v27 migration
func (k BaseViewKeeper) RemoveOldSupplyOffset(ctx context.Context, denom string) {
store := runtime.KVStoreAdapter(k.storeService.OpenKVStore(ctx))
supplyOffsetStore := prefix.NewStore(store, types.SupplyOffsetKeyOld)

supplyOffsetStore.Delete([]byte(denom))
}

// setSupplyOffsetOld sets the supply offset for the given denom using the pre v26 (old) key
// TODO: Remove after v27 migration
func (k BaseKeeper) setSupplyOffsetOld(ctx context.Context, denom string, offsetAmount math.Int) {
intBytes, err := offsetAmount.Marshal()
if err != nil {
panic(fmt.Errorf("unable to marshal amount value %v", err))
}

store := runtime.KVStoreAdapter(k.storeService.OpenKVStore(ctx))
supplyOffsetStore := prefix.NewStore(store, types.SupplyOffsetKeyOld)

// Bank invariants and IBC requires to remove zero coins.
if offsetAmount.IsZero() {
supplyOffsetStore.Delete([]byte(denom))
} else {
supplyOffsetStore.Set([]byte(denom), intBytes)
}
}

// AddSupplyOffset adjusts the current supply offset of a denom by the inputted offsetAmount using the pre v26 (old) key
czarcas7ic marked this conversation as resolved.
Show resolved Hide resolved
// TODO: Remove after v27 migration
func (k BaseKeeper) AddSupplyOffsetOld(ctx context.Context, denom string, offsetAmount math.Int) {
k.setSupplyOffsetOld(ctx, denom, k.GetSupplyOffsetOld(ctx, denom).Add(offsetAmount))
}
5 changes: 5 additions & 0 deletions x/bank/types/keys.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,12 @@ var (
// ParamsKey is the prefix for x/bank parameters
ParamsKey = collections.NewPrefix(5)

// SupplyOffKey is a Osmosis specific key that handles supply offsets
SupplyOffsetKey = collections.NewPrefix(88)

// SupplyOffKeyOld is a Osmosis specific key that handles supply offsets pre v0.50
// TODO: Remove in the v28 upgrade
SupplyOffsetKeyOld = []byte{0x88}
Comment on lines +38 to +41
Copy link
Member

Choose a reason for hiding this comment

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

I just want to verify that you checked NewPrefix 88 does not collide with []byte{0x88}

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Yeah I did, this is the bug

Copy link
Member

Choose a reason for hiding this comment

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

Ah, just understood, thanks

)

// BalanceValueCodec is a codec for encoding bank balances in a backwards compatible way.
Expand Down
Loading