Skip to content
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

[Bugfix] sort migrated map array to get deterministic result #536

Merged
merged 2 commits into from
Aug 17, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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