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

Denom migration from peggy1 to peggy2 #3387

Open
wants to merge 1 commit into
base: future/peggy2
Choose a base branch
from
Open
Show file tree
Hide file tree
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
32 changes: 32 additions & 0 deletions x/tokenregistry/keeper/denom_mapping.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package keeper

import (
"github.com/Sifchain/sifnode/x/tokenregistry/types"
"github.com/cosmos/cosmos-sdk/store/prefix"
sdk "github.com/cosmos/cosmos-sdk/types"
)

func (k keeper) getPeggy1DenomMappingPrefix(ctx sdk.Context, peggy1_denom string) []byte {
return append(types.Peggy1DenomMappingPrefix, []byte(peggy1_denom)...)
}

func (k keeper) GetPeggy2Denom(ctx sdk.Context, peggy1_denom string) (string, bool) {
denom_prefix := k.getPeggy1DenomMappingPrefix(ctx, peggy1_denom)

store := prefix.NewStore(ctx.KVStore(k.storeKey), types.Peggy1DenomMappingPrefix)

b := store.Get(denom_prefix)

if b == nil {
return "", false
}
return string(b), true
}

func (k keeper) SetPeggy2Denom(ctx sdk.Context, peggy1_denom string, peggy2_denom string) {
denom_prefix := k.getPeggy1DenomMappingPrefix(ctx, peggy1_denom)

store := prefix.NewStore(ctx.KVStore(k.storeKey), types.Peggy1DenomMappingPrefix)

store.Set(denom_prefix, []byte(peggy2_denom))
}
23 changes: 23 additions & 0 deletions x/tokenregistry/keeper/denom_mapping_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package keeper_test

import (
"testing"

"github.com/Sifchain/sifnode/x/tokenregistry/test"
"github.com/stretchr/testify/require"
)

func TestGetPeggy2Denom(t *testing.T) {
app, ctx, _ := test.CreateTestApp(false)
peggy1Denom := "peggy1"
_, found := app.TokenRegistryKeeper.GetPeggy2Denom(ctx, peggy1Denom)
// not found before set
require.Equal(t, found, false)

peggy2Denom := "peggy2"
app.TokenRegistryKeeper.SetPeggy2Denom(ctx, peggy1Denom, peggy2Denom)
peggy2DenomInKeeper, found := app.TokenRegistryKeeper.GetPeggy2Denom(ctx, peggy1Denom)
// found after set
require.Equal(t, found, true)
require.Equal(t, peggy2Denom, peggy2DenomInKeeper)
}
1 change: 1 addition & 0 deletions x/tokenregistry/keeper/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package keeper

import (
"fmt"

adminkeeper "github.com/Sifchain/sifnode/x/admin/keeper"
"github.com/cosmos/cosmos-sdk/codec"
sdk "github.com/cosmos/cosmos-sdk/types"
Expand Down
30 changes: 30 additions & 0 deletions x/tokenregistry/keeper/migrations.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
package keeper

import (
"encoding/json"
"io/ioutil"
"path/filepath"

tkrtypes "github.com/Sifchain/sifnode/x/tokenregistry/types"
sdk "github.com/cosmos/cosmos-sdk/types"
)
Expand All @@ -26,5 +30,31 @@ func (m Migrator) MigrateToVer4(ctx sdk.Context) error {
store.Delete(iterator.Key())
}

denomMigrationMap := getDenomMigrationMap()
for peggy1denom, peggy2denom := range denomMigrationMap {
m.Keeper.SetPeggy2Denom(ctx, peggy1denom, peggy2denom)
}

return nil
}

func getDenomMigrationMap() map[string]string {
migrationMap := map[string]string{}
input, err := ioutil.ReadFile(denomMigrationFilePath())
if err != nil {
panic(err)
}
err = json.Unmarshal(input, &migrationMap)
if err != nil {
panic(err)
}
return migrationMap
}

func denomMigrationFilePath() string {
fp, err := filepath.Abs("../../../smart-contracts/data/denom_mapping_peggy1_to_peggy2.json")
if err != nil {
panic(err)
}
return fp
}
2 changes: 2 additions & 0 deletions x/tokenregistry/types/expected_keepers.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ type Keeper interface {
SetFirstDoublePeg(ctx sdk.Context, denom string, networkDescriptor oracletypes.NetworkDescriptor)
AddMultipleTokens(ctx sdk.Context, entries []*RegistryEntry)
RemoveMultipleTokens(ctx sdk.Context, denoms []string)
GetPeggy2Denom(ctx sdk.Context, peggy1_denom string) (string, bool)
SetPeggy2Denom(ctx sdk.Context, peggy1_denom string, peggy2_denom string)
}

type AccountKeeper interface {
Expand Down
1 change: 1 addition & 0 deletions x/tokenregistry/types/keys.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@ package types
var WhitelistStorePrefix = []byte{0x01}
var AdminAccountStorePrefix = []byte{0x02} // no longer used
var TokenDenomPrefix = []byte{0x03}
var Peggy1DenomMappingPrefix = []byte{0x04}