Skip to content

Commit

Permalink
[Bugfix] sort migrated map array to get deterministic result (#536)
Browse files Browse the repository at this point in the history
* sort migrated map items to get deterministic result

* add changelog
  • Loading branch information
yys authored Aug 17, 2021
1 parent d6037b9 commit 7ed34ed
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 10 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
## 0.5.1

### Bug Fixes
- [\#536](https://github.com/terra-money/core/pull/536) Sort migrated map array with key to generate deterministic result

## 0.5.0

### Improvements
Expand Down
32 changes: 22 additions & 10 deletions x/oracle/legacy/v05/migrate.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package v05

import (
"sort"

v04oracle "github.com/terra-money/core/x/oracle/legacy/v04"
v05oracle "github.com/terra-money/core/x/oracle/types"
)
Expand Down Expand Up @@ -45,6 +47,8 @@ func Migrate(
}
}

// Note that the four following `for` loop over a map's keys, so are not
// deterministic.
i := 0
missCounters := make([]v05oracle.MissCounter, len(oracleGenState.MissCounters))
for validatorAddress, missCounter := range oracleGenState.MissCounters {
Expand All @@ -57,22 +61,22 @@ func Migrate(
}

i = 0
exchangeRates := make([]v05oracle.ExchangeRateTuple, len(oracleGenState.ExchangeRates))
for denom, exchangeRate := range oracleGenState.ExchangeRates {
exchangeRates[i] = v05oracle.ExchangeRateTuple{
Denom: denom,
ExchangeRate: exchangeRate,
feederDelegations := make([]v05oracle.FeederDelegation, len(oracleGenState.FeederDelegations))
for validatorAddress, feederAddress := range oracleGenState.FeederDelegations {
feederDelegations[i] = v05oracle.FeederDelegation{
ValidatorAddress: validatorAddress,
FeederAddress: feederAddress.String(),
}

i++
}

i = 0
feederDelegations := make([]v05oracle.FeederDelegation, len(oracleGenState.FeederDelegations))
for validatorAddress, feederAddress := range oracleGenState.FeederDelegations {
feederDelegations[i] = v05oracle.FeederDelegation{
ValidatorAddress: validatorAddress,
FeederAddress: feederAddress.String(),
exchangeRates := make([]v05oracle.ExchangeRateTuple, len(oracleGenState.ExchangeRates))
for denom, exchangeRate := range oracleGenState.ExchangeRates {
exchangeRates[i] = v05oracle.ExchangeRateTuple{
Denom: denom,
ExchangeRate: exchangeRate,
}

i++
Expand All @@ -89,6 +93,14 @@ func Migrate(
i++
}

// We sort these four arrays by validator address and denom, so that we get determinstic states.
sort.Slice(missCounters, func(i, j int) bool { return missCounters[i].ValidatorAddress < missCounters[j].ValidatorAddress })
sort.Slice(feederDelegations, func(i, j int) bool {
return feederDelegations[i].ValidatorAddress < feederDelegations[j].ValidatorAddress
})
sort.Slice(exchangeRates, func(i, j int) bool { return exchangeRates[i].Denom < exchangeRates[j].Denom })
sort.Slice(tobinTaxes, func(i, j int) bool { return tobinTaxes[i].Denom < tobinTaxes[j].Denom })

return &v05oracle.GenesisState{
AggregateExchangeRatePrevotes: aggregateExchangeRatePrevote,
AggregateExchangeRateVotes: aggregateExchangeRateVote,
Expand Down
7 changes: 7 additions & 0 deletions x/treasury/legacy/v05/migrate.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package v05

import (
"sort"

v04treasury "github.com/terra-money/core/x/treasury/legacy/v04"
v05treasury "github.com/terra-money/core/x/treasury/types"

Expand All @@ -17,6 +19,8 @@ import (
func Migrate(
treasuryGenState v04treasury.GenesisState,
) *v05treasury.GenesisState {
// Note that the following `for` loop over a map's keys, so are not
// deterministic.
i := 0
taxCaps := make([]v05treasury.TaxCap, len(treasuryGenState.TaxCaps))
for denom, cap := range treasuryGenState.TaxCaps {
Expand All @@ -28,6 +32,9 @@ func Migrate(
i++
}

// We sort this array by denom, so that we get determinstic states.
sort.Slice(taxCaps, func(i, j int) bool { return taxCaps[i].Denom < taxCaps[j].Denom })

// Remove cumulative height dependencies
cumulativeEpochs := int(treasuryGenState.CumulativeHeight / int64(v04treasury.BlocksPerWeek))
epochStates := make([]v05treasury.EpochState, len(treasuryGenState.TRs)-cumulativeEpochs)
Expand Down

0 comments on commit 7ed34ed

Please sign in to comment.