-
Notifications
You must be signed in to change notification settings - Fork 52
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
migrate cert stored prefix to shentu (#739)
* 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
1 parent
21cb461
commit cae4814
Showing
4 changed files
with
334 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
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) | ||
case *types.Identity: | ||
contentShentu, err := common.PrefixToShentu(certificate.GetContentString()) | ||
if err != nil { | ||
return | ||
} | ||
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) | ||
} | ||
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 | ||
} | ||
certifier.Address = certifierAddr | ||
|
||
proposalAddr, err := common.PrefixToShentu(certifier.Proposer) | ||
if err != nil { | ||
return err | ||
} | ||
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 | ||
} | ||
certifier.Address = certifierAddr | ||
|
||
proposalAddr, err := common.PrefixToShentu(certifier.Proposer) | ||
if err != nil { | ||
return err | ||
} | ||
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 | ||
} | ||
library.Address = libraryAddr | ||
|
||
publisher, err := common.PrefixToShentu(library.Publisher) | ||
if err != nil { | ||
return err | ||
} | ||
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 | ||
} | ||
|
||
if err := migrateCertifier(store, cdc); err != nil { | ||
return err | ||
} | ||
|
||
if err := migrateCertifierAlias(store, cdc); err != nil { | ||
return err | ||
} | ||
|
||
if err := migrateLibrary(store, cdc); err != nil { | ||
return err | ||
} | ||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters