From d2ff8f7253c78642d812e97ab0811203d316bf5b Mon Sep 17 00:00:00 2001 From: ghost Date: Wed, 2 Oct 2024 17:18:40 +0100 Subject: [PATCH 1/3] fix: add old supply offset functions --- x/bank/keeper/supply_offset_old.go | 69 ++++++++++++++++++++++++++++++ x/bank/types/keys.go | 5 +++ 2 files changed, 74 insertions(+) create mode 100644 x/bank/keeper/supply_offset_old.go diff --git a/x/bank/keeper/supply_offset_old.go b/x/bank/keeper/supply_offset_old.go new file mode 100644 index 000000000000..e5fc90439ebd --- /dev/null +++ b/x/bank/keeper/supply_offset_old.go @@ -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 +// 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)) +} diff --git a/x/bank/types/keys.go b/x/bank/types/keys.go index 68eb56a0538e..5134a0c581ce 100644 --- a/x/bank/types/keys.go +++ b/x/bank/types/keys.go @@ -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} ) // BalanceValueCodec is a codec for encoding bank balances in a backwards compatible way. From 7f5bf20c168f078fcd6991f586b2597aac0c11d7 Mon Sep 17 00:00:00 2001 From: Adam Tucker Date: Sat, 5 Oct 2024 10:32:43 -0500 Subject: [PATCH 2/3] Update x/bank/keeper/supply_offset_old.go Co-authored-by: Matt, Park <45252226+mattverse@users.noreply.github.com> --- x/bank/keeper/supply_offset_old.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/x/bank/keeper/supply_offset_old.go b/x/bank/keeper/supply_offset_old.go index e5fc90439ebd..50e289deb31f 100644 --- a/x/bank/keeper/supply_offset_old.go +++ b/x/bank/keeper/supply_offset_old.go @@ -62,7 +62,7 @@ func (k BaseKeeper) setSupplyOffsetOld(ctx context.Context, denom string, offset } } -// AddSupplyOffset adjusts the current supply offset of a denom by the inputted offsetAmount using the pre v26 (old) key +// AddSupplyOffsetOld adjusts the current supply offset of a denom by the inputted offsetAmount using the pre v26 (old) key // 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)) From 02cc5d2e01fd406178fd5c22e382b3fc86d91098 Mon Sep 17 00:00:00 2001 From: ghost Date: Tue, 15 Oct 2024 11:39:13 +0100 Subject: [PATCH 3/3] chore: update CHANGELOG --- CHANGELOG.md | 3 +++ 1 file changed, 3 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 23b3afc94d0f..91af76819411 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -64,6 +64,9 @@ This v0.50.x branch was created at the [bd886bb4b03fdfc52bdc1fa2f98e960316604f89 ### Osmosis Specific PRs (should not upstream) +* Fix Supply Offsets + * [#629](https://github.com/osmosis-labs/cosmos-sdk/pull/629) fix: add old supply offset functions + * Slashing * [0609929](https://github.com/osmosis-labs/cosmos-sdk/commit/0609929d2ddfd845aebfe53622176660f8392fcb) Osmosis specific key migration slashing changes, due to implementing it early in v0.47.x