Files
cosmos-sdk/x/slashing/migrations/v040/migrate.go
T
likhita-809 c91f3b2651 refactor: Rename x/{mod}/legacy to x/{mod}/migrations (#9628)
<!--
The default pull request template is for types feat, fix, or refactor.
For other templates, add one of the following parameters to the url:
- template=docs.md
- template=other.md
-->

## Description

Closes: #8700 

<!-- Add a description of the changes that this PR introduces and the files that
are the most critical to review. -->

---

### Author Checklist

*All items are required. Please add a note to the item if the item is not applicable and
please add links to any relevant follow up issues.*

I have...

- [x] included the correct [type prefix](https://github.com/commitizen/conventional-commit-types/blob/v3.0.0/index.json) in the PR title
- [x] added `!` to the type prefix if API or client breaking change
- [x] targeted the correct branch (see [PR Targeting](https://github.com/cosmos/cosmos-sdk/blob/master/CONTRIBUTING.md#pr-targeting))
- [x] provided a link to the relevant issue or specification
- [x] followed the guidelines for [building modules](https://github.com/cosmos/cosmos-sdk/blob/master/docs/building-modules)
- [x] included the necessary unit and integration [tests](https://github.com/cosmos/cosmos-sdk/blob/master/CONTRIBUTING.md#testing)
- [x] added a changelog entry to `CHANGELOG.md`
- [x] included comments for [documenting Go code](https://blog.golang.org/godoc)
- [x] updated the relevant documentation or specification
- [ ] reviewed "Files changed" and left comments if necessary
- [ ] confirmed all CI checks have passed

### Reviewers Checklist

*All items are required. Please add a note if the item is not applicable and please add
your handle next to the items reviewed if you only reviewed selected items.*

I have...

- [ ] confirmed the correct [type prefix](https://github.com/commitizen/conventional-commit-types/blob/v3.0.0/index.json) in the PR title
- [ ] confirmed `!` in the type prefix if API or client breaking change
- [ ] confirmed all author checklist items have been addressed 
- [ ] reviewed state machine logic
- [ ] reviewed API design and naming
- [ ] reviewed documentation is accurate
- [ ] reviewed tests and test coverage
- [ ] manually tested (if applicable)
2021-07-05 14:55:51 +00:00

65 lines
2.7 KiB
Go

package v040
import (
"sort"
v039slashing "github.com/cosmos/cosmos-sdk/x/slashing/migrations/v039"
v040slashing "github.com/cosmos/cosmos-sdk/x/slashing/types"
)
// Migrate accepts exported x/slashing genesis state from v0.39 and migrates it
// to v0.40 x/slashing genesis state. The migration includes:
//
// - Chaning SigningInfos and MissedBlocks from map to array.
// - Convert addresses from bytes to bech32 strings.
// - Re-encode in v0.40 GenesisState.
func Migrate(oldGenState v039slashing.GenesisState) *v040slashing.GenesisState {
// Note that the two following `for` loop over a map's keys, so are not
// deterministic.
var newSigningInfos = make([]v040slashing.SigningInfo, 0, len(oldGenState.SigningInfos))
for address, signingInfo := range oldGenState.SigningInfos {
newSigningInfos = append(newSigningInfos, v040slashing.SigningInfo{
Address: address,
ValidatorSigningInfo: v040slashing.ValidatorSigningInfo{
Address: signingInfo.Address.String(),
StartHeight: signingInfo.StartHeight,
IndexOffset: signingInfo.IndexOffset,
JailedUntil: signingInfo.JailedUntil,
Tombstoned: signingInfo.Tombstoned,
MissedBlocksCounter: signingInfo.MissedBlocksCounter,
},
})
}
var newValidatorMissedBlocks = make([]v040slashing.ValidatorMissedBlocks, 0, len(oldGenState.MissedBlocks))
for address, validatorMissedBlocks := range oldGenState.MissedBlocks {
var newMissedBlocks = make([]v040slashing.MissedBlock, len(validatorMissedBlocks))
for i, missedBlock := range validatorMissedBlocks {
newMissedBlocks[i] = v040slashing.MissedBlock{
Index: missedBlock.Index,
Missed: missedBlock.Missed,
}
}
newValidatorMissedBlocks = append(newValidatorMissedBlocks, v040slashing.ValidatorMissedBlocks{
Address: address,
MissedBlocks: newMissedBlocks,
})
}
// We sort these two arrays by address, so that we get determinstic states.
sort.Slice(newSigningInfos, func(i, j int) bool { return newSigningInfos[i].Address < newSigningInfos[j].Address })
sort.Slice(newValidatorMissedBlocks, func(i, j int) bool { return newValidatorMissedBlocks[i].Address < newValidatorMissedBlocks[j].Address })
return &v040slashing.GenesisState{
Params: v040slashing.Params{
SignedBlocksWindow: oldGenState.Params.SignedBlocksWindow,
MinSignedPerWindow: oldGenState.Params.MinSignedPerWindow,
DowntimeJailDuration: oldGenState.Params.DowntimeJailDuration,
SlashFractionDoubleSign: oldGenState.Params.SlashFractionDoubleSign,
SlashFractionDowntime: oldGenState.Params.SlashFractionDowntime,
},
SigningInfos: newSigningInfos,
MissedBlocks: newValidatorMissedBlocks,
}
}