-
Notifications
You must be signed in to change notification settings - Fork 12
/
keeper.go
159 lines (135 loc) · 5.4 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
package keeper
import (
"fmt"
"github.com/cosmos/cosmos-sdk/codec"
sdk "github.com/cosmos/cosmos-sdk/types"
paramtypes "github.com/cosmos/cosmos-sdk/x/params/types"
"github.com/tendermint/tendermint/libs/log"
"github.com/functionx/fx-core/v3/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 sdk.StoreKey // Unexposed key to access store from sdk.Context
paramSpace paramtypes.Subspace
stakingKeeper types.StakingKeeper
stakingMsgServer types.StakingMsgServer
distributionKeeper types.DistributionKeeper
bankKeeper types.BankKeeper
ibcTransferKeeper types.IBCTransferKeeper
erc20Keeper types.Erc20Keeper
}
// NewKeeper returns a new instance of the gravity keeper
func NewKeeper(cdc codec.BinaryCodec, moduleName string, storeKey sdk.StoreKey, paramSpace paramtypes.Subspace,
stakingKeeper types.StakingKeeper, stakingMsgServer types.StakingMsgServer, distributionKeeper types.DistributionKeeper,
bankKeeper types.BankKeeper, ibcTransferKeeper types.IBCTransferKeeper, erc20Keeper types.Erc20Keeper, ak types.AccountKeeper,
) Keeper {
// set KeyTable if it has not already been set
if !paramSpace.HasKeyTable() {
paramSpace = paramSpace.WithKeyTable(types.ParamKeyTable())
}
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,
paramSpace: paramSpace,
stakingKeeper: stakingKeeper,
stakingMsgServer: stakingMsgServer,
distributionKeeper: distributionKeeper,
bankKeeper: bankKeeper,
ibcTransferKeeper: ibcTransferKeeper,
erc20Keeper: erc20Keeper,
}
}
// Logger returns a module-specific logger.
func (k Keeper) Logger(ctx sdk.Context) log.Logger {
return ctx.Logger().With("module", "x/"+k.moduleName)
}
// --- PARAMETERS --- //
// GetParams returns the parameters from the store
func (k Keeper) GetParams(ctx sdk.Context) (params types.Params) {
k.paramSpace.GetParamSet(ctx, ¶ms)
return
}
// SetParams sets the parameters in the store
func (k Keeper) SetParams(ctx sdk.Context, ps *types.Params) {
k.paramSpace.SetParamSet(ctx, ps)
}
// GetGravityID returns the GravityID is essentially a salt value
// for bridge signatures, provided each chain running Gravity has a unique ID
// it won't be possible to play back signatures from one bridge onto another
// even if they share a oracle set.
//
// The lifecycle of the GravityID is that it is set in the Genesis file
// read from the live chain for the contract deployment, once a Gravity contract
// is deployed the GravityID CAN NOT BE CHANGED. Meaning that it can't just be the
// same as the chain id since the chain id may be changed many times with each
// successive chain in charge of the same bridge
func (k Keeper) GetGravityID(ctx sdk.Context) string {
var gravityId string
k.paramSpace.Get(ctx, types.ParamsStoreKeyGravityID, &gravityId)
return gravityId
}
func (k Keeper) GetOracleDelegateThreshold(ctx sdk.Context) sdk.Coin {
var threshold sdk.Coin
k.paramSpace.Get(ctx, types.ParamOracleDelegateThreshold, &threshold)
return threshold
}
func (k Keeper) GetOracleDelegateMultiple(ctx sdk.Context) int64 {
var multiple int64
k.paramSpace.Get(ctx, types.ParamOracleDelegateMultiple, &multiple)
return multiple
}
func (k Keeper) GetSlashFraction(ctx sdk.Context) sdk.Dec {
var dec sdk.Dec
k.paramSpace.Get(ctx, types.ParamsStoreSlashFraction, &dec)
return dec
}
func (k Keeper) GetSignedWindow(ctx sdk.Context) uint64 {
var i uint64
k.paramSpace.Get(ctx, types.ParamsStoreKeySignedWindow, &i)
return i
}
func (k Keeper) GetIbcTransferTimeoutHeight(ctx sdk.Context) uint64 {
var i uint64
k.paramSpace.Get(ctx, types.ParamStoreIbcTransferTimeoutHeight, &i)
return i
}
func (k Keeper) GetOracleSetUpdatePowerChangePercent(ctx sdk.Context) sdk.Dec {
var dec sdk.Dec
k.paramSpace.Get(ctx, types.ParamStoreOracleSetUpdatePowerChangePercent, &dec)
return dec
}
// 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)
}