Skip to content

Commit

Permalink
migrate oracle certik prefix to shentu (#740)
Browse files Browse the repository at this point in the history
* migrate oracle certik prefix to shentu

* update ConsensusVersion

* update operator migration

* add oracle withdraw migration

* sort imports

* change v280 to v3

* add v3
  • Loading branch information
kevin-yuhh authored Aug 4, 2023
1 parent 3b14d35 commit 21cb461
Show file tree
Hide file tree
Showing 5 changed files with 400 additions and 3 deletions.
6 changes: 6 additions & 0 deletions x/oracle/keeper/migrations.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
sdk "github.com/cosmos/cosmos-sdk/types"

v2 "github.com/shentufoundation/shentu/v2/x/oracle/legacy/v2"
v3 "github.com/shentufoundation/shentu/v2/x/oracle/legacy/v3"
)

// Migrator is a struct for handling in-place store migrations.
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 {
return v3.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/v3/store.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,192 @@
package v3

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

0 comments on commit 21cb461

Please sign in to comment.