-
Notifications
You must be signed in to change notification settings - Fork 55
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
migrate oracle certik prefix to shentu (#740)
* 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
1 parent
3b14d35
commit 21cb461
Showing
5 changed files
with
400 additions
and
3 deletions.
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
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
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,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 | ||
} |
Oops, something went wrong.