-
Notifications
You must be signed in to change notification settings - Fork 1
/
genesis.go
executable file
·112 lines (93 loc) · 3.33 KB
/
genesis.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
package oracle
import (
"fmt"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/Timwood0x10/sei-chain/x/oracle/keeper"
"github.com/Timwood0x10/sei-chain/x/oracle/types"
)
// InitGenesis initialize default parameters
// and the keeper's address to pubkey map
func InitGenesis(ctx sdk.Context, keeper keeper.Keeper, data *types.GenesisState) {
keeper.SetParams(ctx, data.Params)
for _, d := range data.FeederDelegations {
voter, err := sdk.ValAddressFromBech32(d.ValidatorAddress)
if err != nil {
panic(err)
}
feeder, err := sdk.AccAddressFromBech32(d.FeederAddress)
if err != nil {
panic(err)
}
keeper.SetFeederDelegation(ctx, voter, feeder)
}
for _, ex := range data.ExchangeRates {
keeper.SetBaseExchangeRate(ctx, ex.Denom, ex.ExchangeRate)
}
for _, pc := range data.PenaltyCounters {
operator, err := sdk.ValAddressFromBech32(pc.ValidatorAddress)
if err != nil {
panic(err)
}
keeper.SetVotePenaltyCounter(ctx, operator, pc.VotePenaltyCounter.MissCount, pc.VotePenaltyCounter.AbstainCount, pc.VotePenaltyCounter.SuccessCount)
}
for _, av := range data.AggregateExchangeRateVotes {
valAddr, err := sdk.ValAddressFromBech32(av.Voter)
if err != nil {
panic(err)
}
keeper.SetAggregateExchangeRateVote(ctx, valAddr, av)
}
for _, priceSnapshot := range data.PriceSnapshots {
keeper.AddPriceSnapshot(ctx, priceSnapshot)
}
// check if the module account exists
moduleAcc := keeper.GetOracleAccount(ctx)
if moduleAcc == nil {
panic(fmt.Sprintf("%s module account has not been set", types.ModuleName))
}
}
// ExportGenesis writes the current store values
// to a genesis file, which can be imported again
// with InitGenesis
func ExportGenesis(ctx sdk.Context, keeper keeper.Keeper) *types.GenesisState {
params := keeper.GetParams(ctx)
feederDelegations := []types.FeederDelegation{}
keeper.IterateFeederDelegations(ctx, func(valAddr sdk.ValAddress, feederAddr sdk.AccAddress) (stop bool) {
feederDelegations = append(feederDelegations, types.FeederDelegation{
FeederAddress: feederAddr.String(),
ValidatorAddress: valAddr.String(),
})
return false
})
exchangeRates := []types.ExchangeRateTuple{}
keeper.IterateBaseExchangeRates(ctx, func(denom string, rate types.OracleExchangeRate) (stop bool) {
exchangeRates = append(exchangeRates, types.ExchangeRateTuple{Denom: denom, ExchangeRate: rate.ExchangeRate})
return false
})
penaltyCounters := []types.PenaltyCounter{}
keeper.IterateVotePenaltyCounters(ctx, func(operator sdk.ValAddress, votePenaltyCounter types.VotePenaltyCounter) (stop bool) {
penaltyCounters = append(penaltyCounters, types.PenaltyCounter{
ValidatorAddress: operator.String(),
VotePenaltyCounter: &votePenaltyCounter,
})
return false
})
aggregateExchangeRateVotes := []types.AggregateExchangeRateVote{}
keeper.IterateAggregateExchangeRateVotes(ctx, func(_ sdk.ValAddress, aggregateVote types.AggregateExchangeRateVote) bool {
aggregateExchangeRateVotes = append(aggregateExchangeRateVotes, aggregateVote)
return false
})
priceSnapshots := types.PriceSnapshots{}
keeper.IteratePriceSnapshots(ctx, func(snapshot types.PriceSnapshot) bool {
priceSnapshots = append(priceSnapshots, snapshot)
return false
})
return types.NewGenesisState(
params,
exchangeRates,
feederDelegations,
penaltyCounters,
aggregateExchangeRateVotes,
priceSnapshots,
)
}