Skip to content

Commit

Permalink
migrate certificate.content
Browse files Browse the repository at this point in the history
  • Loading branch information
kevin-yuhh committed Aug 3, 2023
1 parent fc5a2f7 commit e9d60fb
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 4 deletions.
37 changes: 37 additions & 0 deletions x/cert/legacy/v2/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ 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"

Expand All @@ -25,12 +26,48 @@ func migrateCertificate(store sdk.KVStore, cdc codec.BinaryCodec) error {
}

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())

Expand Down
9 changes: 5 additions & 4 deletions x/cert/legacy/v2/store_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,15 @@ import (

shentuapp "github.com/shentufoundation/shentu/v2/app"
"github.com/shentufoundation/shentu/v2/common"
v280 "github.com/shentufoundation/shentu/v2/x/cert/legacy/v2"
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, "test")
content := types.AssembleContent(certType, certifier)
msg, ok := content.(proto.Message)
if !ok {
panic(fmt.Errorf("%T does not implement proto.Message", content))
Expand Down Expand Up @@ -81,7 +81,7 @@ func TestMigrateStore(t *testing.T) {
ctx := app.BaseApp.NewContext(false, tmproto.Header{Time: time.Now().UTC()})
cdc := shentuapp.MakeEncodingConfig().Marshaler

oldCertificate := makeCertificate("AUDITING")
oldCertificate := makeCertificate("IDENTITY")

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

Expand All @@ -92,7 +92,7 @@ func TestMigrateStore(t *testing.T) {

oldLibrary := saveLibrary(cdc, store)

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

//check for Certificate
Expand All @@ -109,6 +109,7 @@ func TestMigrateStore(t *testing.T) {
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)
Expand Down

0 comments on commit e9d60fb

Please sign in to comment.