-
Notifications
You must be signed in to change notification settings - Fork 14
/
keeper.go
177 lines (150 loc) · 5.95 KB
/
keeper.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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
package keeper
import (
"fmt"
errorsmod "cosmossdk.io/errors"
sdkmath "cosmossdk.io/math"
"github.com/cosmos/cosmos-sdk/codec"
storetypes "github.com/cosmos/cosmos-sdk/store/types"
sdk "github.com/cosmos/cosmos-sdk/types"
stakingtypes "github.com/cosmos/cosmos-sdk/x/staking/types"
"github.com/tendermint/tendermint/libs/log"
"github.com/functionx/fx-core/v6/x/crosschain/types"
)
// Keeper maintains the link to storage and exposes getter/setter methods for the various parts of the state machine
type Keeper struct {
moduleName string
cdc codec.BinaryCodec // The wire codec for binary encoding/decoding.
storeKey storetypes.StoreKey // Unexposed key to access store from sdk.Context
stakingKeeper types.StakingKeeper
stakingMsgServer types.StakingMsgServer
distributionKeeper types.DistributionMsgServer
bankKeeper types.BankKeeper
ibcTransferKeeper types.IBCTransferKeeper
erc20Keeper types.Erc20Keeper
authority string
}
// NewKeeper returns a new instance of the gravity keeper
func NewKeeper(cdc codec.BinaryCodec, moduleName string, storeKey storetypes.StoreKey,
stakingKeeper types.StakingKeeper, stakingMsgServer types.StakingMsgServer, distributionKeeper types.DistributionMsgServer,
bankKeeper types.BankKeeper, ibcTransferKeeper types.IBCTransferKeeper, erc20Keeper types.Erc20Keeper, ak types.AccountKeeper,
authority string,
) Keeper {
if addr := ak.GetModuleAddress(moduleName); addr == nil {
panic(fmt.Sprintf("%s module account has not been set", moduleName))
}
return Keeper{
moduleName: moduleName,
cdc: cdc,
storeKey: storeKey,
stakingKeeper: stakingKeeper,
stakingMsgServer: stakingMsgServer,
distributionKeeper: distributionKeeper,
bankKeeper: bankKeeper,
ibcTransferKeeper: ibcTransferKeeper,
erc20Keeper: erc20Keeper,
authority: authority,
}
}
func (k Keeper) GetAuthority() string {
return k.authority
}
// Logger returns a module-specific logger.
func (k Keeper) Logger(ctx sdk.Context) log.Logger {
return ctx.Logger().With("module", "x/"+k.moduleName)
}
// SetLastOracleSlashBlockHeight sets the last proposal block height
func (k Keeper) SetLastOracleSlashBlockHeight(ctx sdk.Context, blockHeight uint64) {
store := ctx.KVStore(k.storeKey)
store.Set(types.LastOracleSlashBlockHeight, sdk.Uint64ToBigEndian(blockHeight))
}
// GetLastOracleSlashBlockHeight returns the last proposal block height
func (k Keeper) GetLastOracleSlashBlockHeight(ctx sdk.Context) uint64 {
store := ctx.KVStore(k.storeKey)
data := store.Get(types.LastOracleSlashBlockHeight)
if len(data) == 0 {
return 0
}
return sdk.BigEndianToUint64(data)
}
// SetLastEventBlockHeightByOracle set the latest event blockHeight for a give oracle
func (k Keeper) SetLastEventBlockHeightByOracle(ctx sdk.Context, oracleAddr sdk.AccAddress, blockHeight uint64) {
store := ctx.KVStore(k.storeKey)
store.Set(types.GetLastEventBlockHeightByOracleKey(oracleAddr), sdk.Uint64ToBigEndian(blockHeight))
}
// GetLastEventBlockHeightByOracle get the latest event blockHeight for a give oracle
func (k Keeper) GetLastEventBlockHeightByOracle(ctx sdk.Context, oracleAddr sdk.AccAddress) uint64 {
store := ctx.KVStore(k.storeKey)
key := types.GetLastEventBlockHeightByOracleKey(oracleAddr)
if !store.Has(key) {
return 0
}
data := store.Get(key)
return sdk.BigEndianToUint64(data)
}
func (k Keeper) UpdateChainOracles(ctx sdk.Context, Oracles []string) error {
if len(Oracles) > types.MaxOracleSize {
return errorsmod.Wrapf(types.ErrInvalid,
fmt.Sprintf("oracle length must be less than or equal: %d", types.MaxOracleSize))
}
newOracleMap := make(map[string]bool, len(Oracles))
for _, oracle := range Oracles {
newOracleMap[oracle] = true
}
var unbondedOracleList []types.Oracle
totalPower, deleteTotalPower := sdkmath.ZeroInt(), sdkmath.ZeroInt()
allOracles := k.GetAllOracles(ctx, false)
proposalOracle, _ := k.GetProposalOracle(ctx)
oldOracleMap := make(map[string]bool, len(Oracles))
for _, oracle := range proposalOracle.Oracles {
oldOracleMap[oracle] = true
}
for _, oracle := range allOracles {
if oracle.Online {
totalPower = totalPower.Add(oracle.GetPower())
}
// oracle in new proposal
if _, ok := newOracleMap[oracle.OracleAddress]; ok {
continue
}
// oracle not in new proposal and oracle in old proposal
if _, ok := oldOracleMap[oracle.OracleAddress]; ok {
unbondedOracleList = append(unbondedOracleList, oracle)
if oracle.Online {
deleteTotalPower = deleteTotalPower.Add(oracle.GetPower())
}
}
}
maxChangePowerThreshold := types.AttestationProposalOracleChangePowerThreshold.Mul(totalPower).Quo(sdkmath.NewInt(100))
k.Logger(ctx).Info("update chain oracles proposal",
"maxChangePowerThreshold", maxChangePowerThreshold.String(), "deleteTotalPower", deleteTotalPower.String())
if deleteTotalPower.GT(sdkmath.ZeroInt()) && deleteTotalPower.GTE(maxChangePowerThreshold) {
return errorsmod.Wrapf(types.ErrInvalid, "max change power, "+
"maxChangePowerThreshold: %s, deleteTotalPower: %s", maxChangePowerThreshold.String(), deleteTotalPower.String())
}
// update proposal oracle
k.SetProposalOracle(ctx, &types.ProposalOracle{Oracles: Oracles})
for _, unbondedOracle := range unbondedOracleList {
if err := k.UnbondedOracleFromProposal(ctx, unbondedOracle); err != nil {
return err
}
}
return nil
}
func (k Keeper) UnbondedOracleFromProposal(ctx sdk.Context, oracle types.Oracle) error {
delegateAddr := oracle.GetDelegateAddress(k.moduleName)
valAddr := oracle.GetValidator()
getOracleDelegateToken, err := k.GetOracleDelegateToken(ctx, delegateAddr, valAddr)
if err != nil {
return err
}
msgUndelegate := stakingtypes.NewMsgUndelegate(delegateAddr, valAddr, types.NewDelegateAmount(getOracleDelegateToken))
if _, err = k.stakingMsgServer.Undelegate(sdk.WrapSDKContext(ctx), msgUndelegate); err != nil {
return err
}
oracle.Online = false
k.SetOracle(ctx, oracle)
return nil
}
func (k Keeper) ModuleName() string {
return k.moduleName
}