Skip to content

Commit

Permalink
migrate cert stored prefix to shentu (#739)
Browse files Browse the repository at this point in the history
* migrate cert stored prefix to shentu

* sort import files

* update ConsensusVersion

* add cert library migration

* change v280 to v2

* migrate certificate.content
  • Loading branch information
kevin-yuhh committed Aug 4, 2023
1 parent 21cb461 commit cae4814
Show file tree
Hide file tree
Showing 4 changed files with 334 additions and 1 deletion.
22 changes: 22 additions & 0 deletions x/cert/keeper/migrations.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package keeper

import (
sdk "github.com/cosmos/cosmos-sdk/types"

v2 "github.com/shentufoundation/shentu/v2/x/cert/legacy/v2"
)

// Migrator is a struct for handling in-place store migrations.
type Migrator struct {
keeper Keeper
}

// NewMigrator returns a new Migrator.
func NewMigrator(keeper Keeper) Migrator {
return Migrator{keeper: keeper}
}

// Migrate1to2 migrates from version 1 to 2.
func (m Migrator) Migrate1to2(ctx sdk.Context) error {
return v2.MigrateStore(ctx, m.keeper.storeKey, m.keeper.cdc)

Check warning on line 21 in x/cert/keeper/migrations.go

View check run for this annotation

Codecov / codecov/patch

x/cert/keeper/migrations.go#L20-L21

Added lines #L20 - L21 were not covered by tests
}
174 changes: 174 additions & 0 deletions x/cert/legacy/v2/store.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,174 @@
package v2

import (
"github.com/cosmos/cosmos-sdk/codec"
codectypes "github.com/cosmos/cosmos-sdk/codec/types"
"github.com/cosmos/cosmos-sdk/store/prefix"
sdk "github.com/cosmos/cosmos-sdk/types"

"github.com/shentufoundation/shentu/v2/common"
"github.com/shentufoundation/shentu/v2/x/cert/types"
)

func migrateCertificate(store sdk.KVStore, cdc codec.BinaryCodec) error {
oldStore := prefix.NewStore(store, types.CertificatesStoreKey())

iterator := oldStore.Iterator(nil, nil)
defer iterator.Close()

for ; iterator.Valid(); iterator.Next() {
var cert types.Certificate
cdc.MustUnmarshal(iterator.Value(), &cert)

shentuAddr, err := common.PrefixToShentu(cert.Certifier)
if err != nil {
return err
}

Check warning on line 26 in x/cert/legacy/v2/store.go

View check run for this annotation

Codecov / codecov/patch

x/cert/legacy/v2/store.go#L25-L26

Added lines #L25 - L26 were not covered by tests
cert.Certifier = shentuAddr

transistCertContent(&cert)

bz := cdc.MustMarshalLengthPrefixed(&cert)
oldStore.Set(iterator.Key(), bz)
}
return nil
}

func transistCertContent(certificate *types.Certificate) {
switch certificate.GetContent().(type) {
case *types.OracleOperator:
contentShentu, err := common.PrefixToShentu(certificate.GetContentString())
if err != nil {
return
}
content := types.OracleOperator{Content: contentShentu}
setContentAny(certificate, &content)
case *types.ShieldPoolCreator:
contentShentu, err := common.PrefixToShentu(certificate.GetContentString())
if err != nil {
return
}
content := types.ShieldPoolCreator{Content: contentShentu}
setContentAny(certificate, &content)

Check warning on line 52 in x/cert/legacy/v2/store.go

View check run for this annotation

Codecov / codecov/patch

x/cert/legacy/v2/store.go#L39-L52

Added lines #L39 - L52 were not covered by tests
case *types.Identity:
contentShentu, err := common.PrefixToShentu(certificate.GetContentString())
if err != nil {
return
}

Check warning on line 57 in x/cert/legacy/v2/store.go

View check run for this annotation

Codecov / codecov/patch

x/cert/legacy/v2/store.go#L56-L57

Added lines #L56 - L57 were not covered by tests
content := types.Identity{Content: contentShentu}
setContentAny(certificate, &content)
}
}

func setContentAny(certificate *types.Certificate, content types.Content) {
any, err := codectypes.NewAnyWithValue(content)
if err != nil {
panic(err)

Check warning on line 66 in x/cert/legacy/v2/store.go

View check run for this annotation

Codecov / codecov/patch

x/cert/legacy/v2/store.go#L66

Added line #L66 was not covered by tests
}
certificate.Content = any
}

func migrateCertifier(store sdk.KVStore, cdc codec.BinaryCodec) error {
oldStore := prefix.NewStore(store, types.CertifiersStoreKey())

iterator := oldStore.Iterator(nil, nil)
defer iterator.Close()

for ; iterator.Valid(); iterator.Next() {
var certifier types.Certifier
cdc.MustUnmarshalLengthPrefixed(iterator.Value(), &certifier)

certifierAddr, err := common.PrefixToShentu(certifier.Address)
if err != nil {
return err
}

Check warning on line 84 in x/cert/legacy/v2/store.go

View check run for this annotation

Codecov / codecov/patch

x/cert/legacy/v2/store.go#L83-L84

Added lines #L83 - L84 were not covered by tests
certifier.Address = certifierAddr

proposalAddr, err := common.PrefixToShentu(certifier.Proposer)
if err != nil {
return err
}

Check warning on line 90 in x/cert/legacy/v2/store.go

View check run for this annotation

Codecov / codecov/patch

x/cert/legacy/v2/store.go#L89-L90

Added lines #L89 - L90 were not covered by tests
certifier.Proposer = proposalAddr

bz := cdc.MustMarshalLengthPrefixed(&certifier)
oldStore.Set(iterator.Key(), bz)
}
return nil
}

func migrateCertifierAlias(store sdk.KVStore, cdc codec.BinaryCodec) error {
oldStore := prefix.NewStore(store, types.CertifierAliasesStoreKey())

iterator := oldStore.Iterator(nil, nil)
defer iterator.Close()

for ; iterator.Valid(); iterator.Next() {
var certifier types.Certifier
cdc.MustUnmarshalLengthPrefixed(iterator.Value(), &certifier)

certifierAddr, err := common.PrefixToShentu(certifier.Address)
if err != nil {
return err
}

Check warning on line 112 in x/cert/legacy/v2/store.go

View check run for this annotation

Codecov / codecov/patch

x/cert/legacy/v2/store.go#L111-L112

Added lines #L111 - L112 were not covered by tests
certifier.Address = certifierAddr

proposalAddr, err := common.PrefixToShentu(certifier.Proposer)
if err != nil {
return err
}

Check warning on line 118 in x/cert/legacy/v2/store.go

View check run for this annotation

Codecov / codecov/patch

x/cert/legacy/v2/store.go#L117-L118

Added lines #L117 - L118 were not covered by tests
certifier.Proposer = proposalAddr

bz := cdc.MustMarshalLengthPrefixed(&certifier)
oldStore.Set(iterator.Key(), bz)
}
return nil
}

func migrateLibrary(store sdk.KVStore, cdc codec.BinaryCodec) error {
oldStore := prefix.NewStore(store, types.LibrariesStoreKey())

iterator := oldStore.Iterator(nil, nil)
defer iterator.Close()

for ; iterator.Valid(); iterator.Next() {
var library types.Library
cdc.MustUnmarshalLengthPrefixed(iterator.Value(), &library)

libraryAddr, err := common.PrefixToShentu(library.Address)
if err != nil {
return err
}

Check warning on line 140 in x/cert/legacy/v2/store.go

View check run for this annotation

Codecov / codecov/patch

x/cert/legacy/v2/store.go#L139-L140

Added lines #L139 - L140 were not covered by tests
library.Address = libraryAddr

publisher, err := common.PrefixToShentu(library.Publisher)
if err != nil {
return err
}

Check warning on line 146 in x/cert/legacy/v2/store.go

View check run for this annotation

Codecov / codecov/patch

x/cert/legacy/v2/store.go#L145-L146

Added lines #L145 - L146 were not covered by tests
library.Publisher = publisher

bz := cdc.MustMarshalLengthPrefixed(&library)
oldStore.Set(iterator.Key(), bz)
}
return nil
}

func MigrateStore(ctx sdk.Context, storeKey sdk.StoreKey, cdc codec.BinaryCodec) error {
store := ctx.KVStore(storeKey)

if err := migrateCertificate(store, cdc); err != nil {
return err
}

Check warning on line 160 in x/cert/legacy/v2/store.go

View check run for this annotation

Codecov / codecov/patch

x/cert/legacy/v2/store.go#L159-L160

Added lines #L159 - L160 were not covered by tests

if err := migrateCertifier(store, cdc); err != nil {
return err
}

Check warning on line 164 in x/cert/legacy/v2/store.go

View check run for this annotation

Codecov / codecov/patch

x/cert/legacy/v2/store.go#L163-L164

Added lines #L163 - L164 were not covered by tests

if err := migrateCertifierAlias(store, cdc); err != nil {
return err
}

Check warning on line 168 in x/cert/legacy/v2/store.go

View check run for this annotation

Codecov / codecov/patch

x/cert/legacy/v2/store.go#L167-L168

Added lines #L167 - L168 were not covered by tests

if err := migrateLibrary(store, cdc); err != nil {
return err
}

Check warning on line 172 in x/cert/legacy/v2/store.go

View check run for this annotation

Codecov / codecov/patch

x/cert/legacy/v2/store.go#L171-L172

Added lines #L171 - L172 were not covered by tests
return nil
}
131 changes: 131 additions & 0 deletions x/cert/legacy/v2/store_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
package v2_test

import (
"fmt"
"github.com/cosmos/cosmos-sdk/codec"
"github.com/gogo/protobuf/proto"
"github.com/stretchr/testify/require"
"math/rand"
"testing"
"time"

tmproto "github.com/tendermint/tendermint/proto/tendermint/types"

codectypes "github.com/cosmos/cosmos-sdk/codec/types"
"github.com/cosmos/cosmos-sdk/testutil/testdata"
sdk "github.com/cosmos/cosmos-sdk/types"

shentuapp "github.com/shentufoundation/shentu/v2/app"
"github.com/shentufoundation/shentu/v2/common"
v2 "github.com/shentufoundation/shentu/v2/x/cert/legacy/v2"
"github.com/shentufoundation/shentu/v2/x/cert/types"
)

func makeCertificate(certType string) types.Certificate {
_, _, addr := testdata.KeyTestPubAddr()
certifier, _ := common.PrefixToCertik(addr.String())

content := types.AssembleContent(certType, certifier)
msg, ok := content.(proto.Message)
if !ok {
panic(fmt.Errorf("%T does not implement proto.Message", content))
}
any, err := codectypes.NewAnyWithValue(msg)
if err != nil {
panic(err)
}

return types.Certificate{
CertificateId: rand.Uint64(),
Content: any,
CompilationContent: &types.CompilationContent{Compiler: "", BytecodeHash: ""},
Description: "for test",
Certifier: certifier,
}
}

func makeCertifier(certifierAddr, alias string) types.Certifier {
_, _, addr := testdata.KeyTestPubAddr()
proposer, _ := common.PrefixToCertik(addr.String())

return types.Certifier{
Address: certifierAddr,
Alias: alias,
Proposer: proposer,
Description: "unit test",
}
}

func saveLibrary(cdc codec.BinaryCodec, storeKey sdk.KVStore) types.Library {
_, _, libraryAddr := testdata.KeyTestPubAddr()
libraryAddrStr, _ := common.PrefixToCertik(libraryAddr.String())

_, _, publisherAddr := testdata.KeyTestPubAddr()
publisherStr, _ := common.PrefixToCertik(publisherAddr.String())

library := types.Library{
Address: libraryAddrStr,
Publisher: publisherStr,
}

bz := cdc.MustMarshalLengthPrefixed(&library)
storeKey.Set(types.LibraryStoreKey(libraryAddr), bz)
return library
}

func TestMigrateStore(t *testing.T) {
cfg := sdk.GetConfig()
cfg.SetBech32PrefixForAccount(common.Bech32PrefixAccAddr, common.Bech32PrefixAccPub)

app := shentuapp.Setup(false)
ctx := app.BaseApp.NewContext(false, tmproto.Header{Time: time.Now().UTC()})
cdc := shentuapp.MakeEncodingConfig().Marshaler

oldCertificate := makeCertificate("IDENTITY")

oldCertifier := makeCertifier(oldCertificate.Certifier, "test_certifier")

app.CertKeeper.SetCertificate(ctx, oldCertificate)
app.CertKeeper.SetCertifier(ctx, oldCertifier)

store := ctx.KVStore(app.GetKey(types.StoreKey))

oldLibrary := saveLibrary(cdc, store)

err := v2.MigrateStore(ctx, app.GetKey(types.StoreKey), cdc)
require.NoError(t, err)

//check for Certificate
var cert types.Certificate
bz := store.Get(types.CertificateStoreKey(oldCertificate.CertificateId))
cdc.MustUnmarshalLengthPrefixed(bz, &cert)

newCertifierAddr, _ := common.PrefixToShentu(oldCertificate.Certifier)
require.Equal(t, newCertifierAddr, cert.Certifier)

var certifier types.Certifier
certifierAcc, err := sdk.AccAddressFromBech32(cert.Certifier)
require.NoError(t, err)
bz = store.Get(types.CertifierStoreKey(certifierAcc))
cdc.MustUnmarshalLengthPrefixed(bz, &certifier)
require.Equal(t, newCertifierAddr, certifier.Address)
require.Equal(t, newCertifierAddr, cert.GetContentString())

newCertifierProposer, _ := common.PrefixToShentu(oldCertifier.Proposer)
require.Equal(t, newCertifierProposer, certifier.Proposer)

bz = store.Get(types.CertifierAliasStoreKey(oldCertifier.Alias))
var certifierAlias types.Certifier
cdc.MustUnmarshalLengthPrefixed(bz, &certifierAlias)
require.Equal(t, newCertifierAddr, certifierAlias.Address)
require.Equal(t, newCertifierProposer, certifierAlias.Proposer)

libraryAddr, _ := sdk.AccAddressFromBech32(oldLibrary.Address)
bz = store.Get(types.LibraryStoreKey(libraryAddr))
var library types.Library
cdc.MustUnmarshalLengthPrefixed(bz, &library)
newLibraryAddr, _ := common.PrefixToShentu(oldLibrary.Address)
newPublisherAddr, _ := common.PrefixToShentu(oldLibrary.Publisher)
require.Equal(t, library.Address, newLibraryAddr)
require.Equal(t, library.Publisher, newPublisherAddr)
}
8 changes: 7 additions & 1 deletion x/cert/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,12 @@ func (am AppModule) LegacyQuerierHandler(legacyQuerierCdc *codec.LegacyAmino) sd
func (am AppModule) RegisterServices(cfg module.Configurator) {
types.RegisterMsgServer(cfg.MsgServer(), keeper.NewMsgServerImpl(am.moduleKeeper))
types.RegisterQueryServer(cfg.QueryServer(), keeper.Querier{Keeper: am.moduleKeeper})

m := keeper.NewMigrator(am.moduleKeeper)
err := cfg.RegisterMigration(types.ModuleName, 1, m.Migrate1to2)
if err != nil {
panic(err)

Check warning on line 140 in x/cert/module.go

View check run for this annotation

Codecov / codecov/patch

x/cert/module.go#L140

Added line #L140 was not covered by tests
}
}

// InitGenesis initializes the module genesis.
Expand All @@ -150,7 +156,7 @@ func (am AppModule) ExportGenesis(ctx sdk.Context, cdc codec.JSONCodec) json.Raw
}

// ConsensusVersion implements AppModule/ConsensusVersion.
func (AppModule) ConsensusVersion() uint64 { return 1 }
func (AppModule) ConsensusVersion() uint64 { return 2 }

//____________________________________________________________________________

Expand Down

0 comments on commit cae4814

Please sign in to comment.