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

migrate oracle certik prefix to shentu #740

Merged
merged 7 commits into from
Aug 4, 2023
Merged
Show file tree
Hide file tree
Changes from 4 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
6 changes: 6 additions & 0 deletions x/oracle/keeper/migrations.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package keeper

import (
sdk "github.com/cosmos/cosmos-sdk/types"
v280 "github.com/shentufoundation/shentu/v2/x/oracle/legacy/v280"
Copy link
Contributor

@skyargos skyargos Jul 31, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Put together with shentufoundation import

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed


v2 "github.com/shentufoundation/shentu/v2/x/oracle/legacy/v2"
)
Expand All @@ -21,3 +22,8 @@ func (m Migrator) Migrate1to2(ctx sdk.Context) error {
v2.UpdateParams(ctx, m.keeper.paramSpace)
return v2.MigrateTaskStore(ctx, m.keeper.storeKey, m.keeper.cdc)
}

// Migrate2to3 migrates from version 2 to 3.
func (m Migrator) Migrate2to3(ctx sdk.Context) error {
kevin-yuhh marked this conversation as resolved.
Show resolved Hide resolved
return v280.MigrateStore(ctx, m.keeper.storeKey, m.keeper.cdc)
}
2 changes: 1 addition & 1 deletion x/oracle/keeper/task.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ func (k Keeper) GetTask(ctx sdk.Context, taskID []byte) (task types.TaskI, err e
return
}

//remove ID of the task from closingBlockStore because it has been handled in shortcut
// DeleteFromClosingTaskIDs remove ID of the task from closingBlockStore because it has been handled in shortcut
func (k Keeper) DeleteFromClosingTaskIDs(ctx sdk.Context, task types.TaskI) {
taskIDs := k.GetClosingTaskIDs(ctx, task)
for i := range taskIDs {
Expand Down
192 changes: 192 additions & 0 deletions x/oracle/legacy/v280/store.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,192 @@
package v280

import (
"fmt"

"github.com/cosmos/cosmos-sdk/codec"
"github.com/cosmos/cosmos-sdk/store"
sdk "github.com/cosmos/cosmos-sdk/types"

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

func MigrateAllTaskStore(ctx sdk.Context, storeKey sdk.StoreKey, cdc codec.BinaryCodec) error {
store := ctx.KVStore(storeKey)
iterator := sdk.KVStorePrefixIterator(store, types.TaskStoreKeyPrefix)

defer iterator.Close()
for ; iterator.Valid(); iterator.Next() {
var oldTask types.TaskI

err := cdc.UnmarshalInterface(iterator.Value(), &oldTask)
if err != nil {
return err
}

switch task := oldTask.(type) {
case *types.Task:
if err = MigrateTaskStore(task, store, iterator.Key(), cdc); err != nil {
return err
}
case *types.TxTask:
if err = MigrateTxTaskStore(task, store, iterator.Key(), cdc); err != nil {
return err
}
default:
return fmt.Errorf("err kvstore")
}

}
return nil
}

func MigrateTaskStore(task *types.Task, store store.KVStore, key []byte, cdc codec.BinaryCodec) error {
shentuAddr, err := common.PrefixToShentu(task.Creator)
if err != nil {
return err
}

newTask := types.Task{
Contract: task.Contract,
Function: task.Function,
BeginBlock: task.BeginBlock,
Bounty: task.Bounty,
Description: task.Description,
Expiration: task.Expiration,
Creator: shentuAddr,
Responses: nil,
Result: task.Result,
ExpireHeight: task.ExpireHeight,
WaitingBlocks: task.WaitingBlocks,
Status: task.Status,
}

for _, response := range task.Responses {
operator, err := common.PrefixToShentu(response.Operator)
if err != nil {
return err
}
newResponse := types.Response{
Operator: operator,
Score: response.Score,
Weight: response.Weight,
Reward: response.Reward,
}
newTask.Responses = append(newTask.Responses, newResponse)
}
// delete old task
store.Delete(key)
// set task
bz, err := cdc.MarshalInterface(&newTask)
if err != nil {
return err
}
store.Set(types.TaskStoreKey(newTask.GetID()), bz)
return nil
}

func MigrateTxTaskStore(task *types.TxTask, store store.KVStore, key []byte, cdc codec.BinaryCodec) error {
shentuAddr, err := common.PrefixToShentu(task.Creator)
if err != nil {
return err
}

newTask := types.TxTask{
AtxHash: task.AtxHash,
Creator: shentuAddr,
Bounty: task.Bounty,
ValidTime: task.ValidTime,
Expiration: task.Expiration,
Responses: nil,
Score: task.Score,
Status: task.Status,
}

for _, response := range task.Responses {
operator, err := common.PrefixToShentu(response.Operator)
if err != nil {
return err
}
newResponse := types.Response{
Operator: operator,
Score: response.Score,
Weight: response.Weight,
Reward: response.Reward,
}
newTask.Responses = append(newTask.Responses, newResponse)
}
// delete old task
store.Delete(key)
// set task
bz, err := cdc.MarshalInterface(&newTask)
if err != nil {
return err
}
store.Set(types.TaskStoreKey(newTask.GetID()), bz)
return nil
}

func MigrateOperatorStore(ctx sdk.Context, storeKey sdk.StoreKey, cdc codec.BinaryCodec) error {
store := ctx.KVStore(storeKey)
iterator := sdk.KVStorePrefixIterator(store, types.OperatorStoreKeyPrefix)

defer iterator.Close()
for ; iterator.Valid(); iterator.Next() {
var operator types.Operator
cdc.MustUnmarshalLengthPrefixed(iterator.Value(), &operator)

shentuOperatorAddress, err := common.PrefixToShentu(operator.Address)
if err != nil {
return err
}
shentuProposal, err := common.PrefixToShentu(operator.Proposer)
if err != nil {
return err
}

operator.Address = shentuOperatorAddress
operator.Proposer = shentuProposal

bz := cdc.MustMarshalLengthPrefixed(&operator)
addr := sdk.MustAccAddressFromBech32(shentuOperatorAddress)
store.Set(types.OperatorStoreKey(addr), bz)
}
return nil
}

func MigrateWithdrawStore(ctx sdk.Context, storeKey sdk.StoreKey, cdc codec.BinaryCodec) error {
store := ctx.KVStore(storeKey)
iterator := sdk.KVStorePrefixIterator(store, types.WithdrawStoreKeyPrefix)

defer iterator.Close()
for ; iterator.Valid(); iterator.Next() {
var withdraw types.Withdraw
cdc.MustUnmarshalLengthPrefixed(iterator.Value(), &withdraw)

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

withdraw.Address = shentuAddr

bz := cdc.MustMarshalLengthPrefixed(&withdraw)
withdrawAddr := sdk.MustAccAddressFromBech32(shentuAddr)
store.Set(types.WithdrawStoreKey(withdrawAddr, withdraw.DueBlock), bz)
}
return nil
}

func MigrateStore(ctx sdk.Context, storeKey sdk.StoreKey, cdc codec.BinaryCodec) error {
if err := MigrateAllTaskStore(ctx, storeKey, cdc); err != nil {
return err
}
if err := MigrateOperatorStore(ctx, storeKey, cdc); err != nil {
return err
}
if err := MigrateWithdrawStore(ctx, storeKey, cdc); err != nil {
return err
}
return nil
}
Loading