-
Notifications
You must be signed in to change notification settings - Fork 55
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
Changes from 4 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
6c39cd5
migrate oracle certik prefix to shentu
kevin-yuhh f867ffc
update ConsensusVersion
kevin-yuhh 205e10c
update operator migration
kevin-yuhh 443b480
add oracle withdraw migration
kevin-yuhh ca4c07c
sort imports
kevin-yuhh f8cff4f
change v280 to v3
kevin-yuhh 7bde959
add v3
kevin-yuhh File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 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 | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Put together with
shentufoundation
importThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
fixed