Skip to content

Commit abc3bb4

Browse files
committed
fix from comments
1 parent 63c4947 commit abc3bb4

27 files changed

+361
-300
lines changed

x/feeds/README.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ This module is used in the BandChain.
2828
- [CurrentFeeds](#currentfeeds)
2929
- [ValidatorPriceList](#validatorpricelist)
3030
- [Price](#price-1)
31-
- [DelegatorSignal](#delegatorsignal)
31+
- [DelegatorSignals](#delegatorsignals)
3232
- [SignalTotalPower](#signaltotalpower)
3333
- [SignalTotalPowerByPowerIndex](#signaltotalpowerbypowerindex)
3434
- [Params](#params)
@@ -132,11 +132,11 @@ The Price is a space for holding the current price information of signals.
132132

133133
* Price: `0x02 -> ProtocolBuffer(Price)`
134134

135-
### DelegatorSignal
135+
### DelegatorSignals
136136

137-
The DelegatorSignal is a space for holding current Delegator Signals information of delegators.
137+
The DelegatorSignals is a space for holding current Delegator Signals information of delegators.
138138

139-
* DelegatorSignal: `0x03 -> ProtocolBuffer(DelegatorSignals)`
139+
* DelegatorSignals: `0x03 -> ProtocolBuffer(DelegatorSignals)`
140140

141141
### SignalTotalPower
142142

x/feeds/client/cli/query.go

Lines changed: 34 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,21 +25,23 @@ func GetQueryCmd() *cobra.Command {
2525
GetQueryCmdPrices(),
2626
GetQueryCmdPrice(),
2727
GetQueryCmdValidatorPrices(),
28+
GetQueryCmdValidValidator(),
2829
GetQueryCmdSignalTotalPowers(),
2930
GetQueryCmdParams(),
30-
GetQueryCmdDelegatorSignal(),
31+
GetQueryCmdReferenceSourceConfig(),
32+
GetQueryCmdDelegatorSignals(),
3133
GetQueryCmdCurrentFeeds(),
3234
GetQueryCmdIsFeeder(),
3335
)
3436

3537
return queryCmd
3638
}
3739

38-
// GetQueryCmdDelegatorSignal implements the query delegator signal command.
39-
func GetQueryCmdDelegatorSignal() *cobra.Command {
40+
// GetQueryCmdDelegatorSignals implements the query delegator signal command.
41+
func GetQueryCmdDelegatorSignals() *cobra.Command {
4042
cmd := &cobra.Command{
41-
Use: "delegator-signal [delegator-addr]",
42-
Short: "Shows delegator's currently active signal",
43+
Use: "delegator-signals [delegator-addr]",
44+
Short: "Shows delegator's currently active signals",
4345
Args: cobra.ExactArgs(1),
4446
RunE: func(cmd *cobra.Command, args []string) error {
4547
clientCtx := client.GetClientContextFromCmd(cmd)
@@ -175,6 +177,33 @@ func GetQueryCmdValidatorPrices() *cobra.Command {
175177
return cmd
176178
}
177179

180+
// GetQueryCmdValidValidator implements the query valid validator command.
181+
func GetQueryCmdValidValidator() *cobra.Command {
182+
cmd := &cobra.Command{
183+
Use: "valid-validator [validator-address]",
184+
Short: "Shows if the given address is a valid validator",
185+
Args: cobra.ExactArgs(1),
186+
RunE: func(cmd *cobra.Command, args []string) error {
187+
clientCtx := client.GetClientContextFromCmd(cmd)
188+
queryClient := types.NewQueryClient(clientCtx)
189+
190+
res, err := queryClient.ValidValidator(
191+
context.Background(),
192+
&types.QueryValidValidatorRequest{Validator: args[0]},
193+
)
194+
if err != nil {
195+
return err
196+
}
197+
198+
return clientCtx.PrintProto(res)
199+
},
200+
}
201+
202+
flags.AddQueryFlagsToCmd(cmd)
203+
204+
return cmd
205+
}
206+
178207
// GetQueryCmdSignalTotalPowers implements the query signal-total-powers command.
179208
func GetQueryCmdSignalTotalPowers() *cobra.Command {
180209
cmd := &cobra.Command{

x/feeds/keeper/calculate.go

Lines changed: 0 additions & 41 deletions
This file was deleted.

x/feeds/keeper/genesis.go

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,7 @@ func (k Keeper) InitGenesis(ctx sdk.Context, genState types.GenesisState) {
1414

1515
k.SetAllDelegatorSignals(ctx, genState.DelegatorSignals)
1616

17-
signalTotalPowers, err := k.CalculateNewSignalTotalPowers(ctx)
18-
if err != nil {
19-
panic(err)
20-
}
17+
signalTotalPowers := k.CalculateNewSignalTotalPowers(ctx)
2118
k.SetSignalTotalPowers(ctx, signalTotalPowers)
2219

2320
feeds := k.CalculateNewCurrentFeeds(ctx)

x/feeds/keeper/grpc_query.go

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,8 @@ func (q queryServer) ValidatorPrices(
137137
}
138138

139139
for _, valPrice := range valPricesList.ValidatorPrices {
140-
if _, exists := signalIDSet[valPrice.SignalID]; exists {
140+
if _, exists := signalIDSet[valPrice.SignalID]; exists &&
141+
valPrice.PriceStatus != types.PriceStatusUnspecified {
141142
filteredPrices = append(filteredPrices, valPrice)
142143
}
143144
}
@@ -161,13 +162,8 @@ func (q queryServer) ValidValidator(
161162
isValid := true
162163

163164
// check if it's bonded validators.
164-
isBonded := q.keeper.IsBondedValidator(ctx, val)
165-
if !isBonded {
166-
isValid = false
167-
}
168-
169-
validatorStatus := q.keeper.oracleKeeper.GetValidatorStatus(ctx, val)
170-
if !validatorStatus.IsActive {
165+
err = q.keeper.ValidateValidatorRequiredToSend(ctx, val)
166+
if err != nil {
171167
isValid = false
172168
}
173169

@@ -230,8 +226,14 @@ func (q queryServer) CurrentFeeds(
230226

231227
currentFeeds := q.keeper.GetCurrentFeeds(ctx)
232228
feedWithDeviations := make([]types.FeedWithDeviation, 0, len(currentFeeds.Feeds))
229+
params := q.keeper.GetParams(ctx)
233230
for _, feed := range currentFeeds.Feeds {
234-
deviation := CalculateDeviation(feed.Power, q.keeper.GetParams(ctx))
231+
deviation := types.CalculateDeviation(
232+
feed.Power,
233+
params.PowerStepThreshold,
234+
params.MinDeviationBasisPoint,
235+
params.MaxDeviationBasisPoint,
236+
)
235237
feedWithDeviations = append(
236238
feedWithDeviations,
237239
types.FeedWithDeviation{

x/feeds/keeper/grpc_query_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -203,7 +203,7 @@ func (suite *KeeperTestSuite) TestQueryValidatorPrices() {
203203
})
204204
suite.Require().NoError(err)
205205
suite.Require().Equal(&types.QueryValidatorPricesResponse{
206-
ValidatorPrices: []types.ValidatorPrice{valPrices[0]},
206+
ValidatorPrices: []types.ValidatorPrice(nil),
207207
}, res)
208208

209209
// query with invalid validator

x/feeds/keeper/keeper.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,9 @@ func NewKeeper(
3232
authzKeeper types.AuthzKeeper,
3333
authority string,
3434
) Keeper {
35+
if _, err := sdk.AccAddressFromBech32(authority); err != nil {
36+
panic(fmt.Errorf("invalid authority address: %w", err))
37+
}
3538
return Keeper{
3639
cdc: cdc,
3740
storeKey: storeKey,

x/feeds/keeper/keeper_feed.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,14 @@ func (k Keeper) SetCurrentFeeds(ctx sdk.Context, feeds []types.Feed) {
3434
func (k Keeper) CalculateNewCurrentFeeds(ctx sdk.Context) []types.Feed {
3535
signalTotalPowers := k.GetSignalTotalPowersByPower(ctx, k.GetParams(ctx).MaxCurrentFeeds)
3636
feeds := make([]types.Feed, 0, len(signalTotalPowers))
37+
params := k.GetParams(ctx)
3738
for _, signalTotalPower := range signalTotalPowers {
38-
interval := CalculateInterval(signalTotalPower.Power, k.GetParams(ctx))
39+
interval := types.CalculateInterval(
40+
signalTotalPower.Power,
41+
params.PowerStepThreshold,
42+
params.MinInterval,
43+
params.MaxInterval,
44+
)
3945
if interval > 0 {
4046
feed := types.Feed{
4147
SignalID: signalTotalPower.ID,

x/feeds/keeper/keeper_price.go

Lines changed: 54 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -54,23 +54,22 @@ func (k Keeper) SetPrice(ctx sdk.Context, price types.Price) {
5454
ctx.KVStore(k.storeKey).Set(types.PriceStoreKey(price.SignalID), k.cdc.MustMarshal(&price))
5555
}
5656

57-
// DeletePrice deletes a price by signal id.
58-
func (k Keeper) DeletePrice(ctx sdk.Context, signalID string) {
59-
ctx.KVStore(k.storeKey).Delete(types.PriceStoreKey(signalID))
60-
}
61-
6257
// CalculatePrices calculates final prices for all supported feeds.
6358
func (k Keeper) CalculatePrices(ctx sdk.Context) {
59+
// get the current feeds
6460
currentFeeds := k.GetCurrentFeeds(ctx)
6561

6662
var validatorsByPower []types.ValidatorInfo
63+
// iterate over bonded validators sorted by power
6764
k.stakingKeeper.IterateBondedValidatorsByPower(
6865
ctx,
6966
func(idx int64, val stakingtypes.ValidatorI) (stop bool) {
67+
// get the status of the validator
7068
status := k.oracleKeeper.GetValidatorStatus(ctx, val.GetOperator())
7169
if !status.IsActive {
7270
return false
7371
}
72+
// collect validator information
7473
validatorInfo := types.ValidatorInfo{
7574
Index: idx,
7675
Address: val.GetOperator(),
@@ -81,6 +80,7 @@ func (k Keeper) CalculatePrices(ctx sdk.Context) {
8180
return false
8281
})
8382

83+
// collect all validator prices
8484
allValidatorPrices := make(map[string]map[string]types.ValidatorPrice)
8585
for _, val := range validatorsByPower {
8686
valPricesList, err := k.GetValidatorPriceList(ctx, val.Address)
@@ -90,20 +90,22 @@ func (k Keeper) CalculatePrices(ctx sdk.Context) {
9090

9191
valPricesMap := make(map[string]types.ValidatorPrice)
9292
for _, valPrice := range valPricesList.ValidatorPrices {
93-
if valPrice.SignalID != "" {
93+
if valPrice.PriceStatus != types.PriceStatusUnspecified {
9494
valPricesMap[valPrice.SignalID] = valPrice
9595
}
9696
}
9797

9898
allValidatorPrices[val.Address.String()] = valPricesMap
9999
}
100100

101+
// calculate prices for each feed
101102
for _, feed := range currentFeeds.Feeds {
102103
var priceFeedInfos []types.PriceFeedInfo
103104
for _, valInfo := range validatorsByPower {
104105
valPrice := allValidatorPrices[valInfo.Address.String()][feed.SignalID]
105106

106-
missReport, havePrice := CheckMissReport(
107+
// check for miss report
108+
missReport := CheckMissReport(
107109
feed,
108110
currentFeeds.LastUpdateTimestamp,
109111
currentFeeds.LastUpdateBlock,
@@ -117,6 +119,8 @@ func (k Keeper) CalculatePrices(ctx sdk.Context) {
117119
k.oracleKeeper.MissReport(ctx, valInfo.Address, ctx.BlockTime())
118120
}
119121

122+
// check if the price is available
123+
havePrice := CheckHavePrice(feed, valPrice, ctx.BlockTime())
120124
if havePrice {
121125
priceFeedInfos = append(
122126
priceFeedInfos, types.PriceFeedInfo{
@@ -130,8 +134,10 @@ func (k Keeper) CalculatePrices(ctx sdk.Context) {
130134
}
131135
}
132136

137+
// calculate the final price for the feed
133138
price, err := k.CalculatePrice(ctx, feed, priceFeedInfos)
134139
if err != nil {
140+
// emit event for failed price calculation
135141
ctx.EventManager().EmitEvent(
136142
sdk.NewEvent(
137143
types.EventTypeCalculatePriceFailed,
@@ -142,8 +148,10 @@ func (k Keeper) CalculatePrices(ctx sdk.Context) {
142148
continue
143149
}
144150

151+
// set the calculated price in the store
145152
k.SetPrice(ctx, price)
146153

154+
// emit event for updated price
147155
ctx.EventManager().EmitEvent(
148156
sdk.NewEvent(
149157
types.EventTypeUpdatePrice,
@@ -213,35 +221,42 @@ func CheckMissReport(
213221
blockTime time.Time,
214222
blockHeight int64,
215223
gracePeriod int64,
216-
) (missReport bool, havePrice bool) {
217-
// During the grace period, if the block time exceeds MaximumGuaranteeBlockTime, it will be capped at MaximumGuaranteeBlockTime.
224+
) bool {
225+
// During the grace period, if the block time exceeds MaxGuaranteeBlockTime, it will be capped at MaxGuaranteeBlockTime.
218226
// This means that in cases of slow block time, the validator will not be deactivated
219-
// as long as the block height does not exceed the equivalent of assumed MaximumGuaranteeBlockTime of block time.
227+
// as long as the block height does not exceed the equivalent of assumed MaxGuaranteeBlockTime of block time.
220228
lastTime := lastUpdateTimestamp + gracePeriod
221-
lastBlock := lastUpdateBlock + gracePeriod/types.MaximumGuaranteeBlockTime
229+
lastBlock := lastUpdateBlock + gracePeriod/types.MaxGuaranteeBlockTime
222230

223231
if valInfo.Status.Since.Unix()+gracePeriod > lastTime {
224232
lastTime = valInfo.Status.Since.Unix() + gracePeriod
225233
}
226234

227-
if valPrice.SignalID != "" {
228-
// Append valid price feed info if within the acceptance period
229-
if valPrice.Timestamp >= blockTime.Unix()-feed.Interval {
230-
havePrice = true
231-
}
232-
235+
if valPrice.PriceStatus != types.PriceStatusUnspecified {
233236
if valPrice.Timestamp+feed.Interval > lastTime {
234237
lastTime = valPrice.Timestamp + feed.Interval
235238
}
236239

237-
if valPrice.BlockHeight+feed.Interval/types.MaximumGuaranteeBlockTime > lastBlock {
238-
lastBlock = valPrice.BlockHeight + feed.Interval/types.MaximumGuaranteeBlockTime
240+
if valPrice.BlockHeight+feed.Interval/types.MaxGuaranteeBlockTime > lastBlock {
241+
lastBlock = valPrice.BlockHeight + feed.Interval/types.MaxGuaranteeBlockTime
239242
}
240243
}
241244

242245
// Determine if the last action is too old, indicating a missed report
243-
missReport = lastTime < blockTime.Unix() && lastBlock < blockHeight
244-
return
246+
return lastTime < blockTime.Unix() && lastBlock < blockHeight
247+
}
248+
249+
// CheckHavePrice checks if a validator has a price feed within interval range.
250+
func CheckHavePrice(
251+
feed types.Feed,
252+
valPrice types.ValidatorPrice,
253+
blockTime time.Time,
254+
) bool {
255+
if valPrice.PriceStatus != types.PriceStatusUnspecified && valPrice.Timestamp >= blockTime.Unix()-feed.Interval {
256+
return true
257+
}
258+
259+
return false
245260
}
246261

247262
// GetValidatorPriceList gets a validator price by validator address.
@@ -275,3 +290,21 @@ func (k Keeper) SetValidatorPriceList(
275290

276291
return nil
277292
}
293+
294+
// ValidateValidatorRequiredToSend validates validator is required for price submission.
295+
func (k Keeper) ValidateValidatorRequiredToSend(
296+
ctx sdk.Context,
297+
val sdk.ValAddress,
298+
) error {
299+
isValid := k.IsBondedValidator(ctx, val)
300+
if !isValid {
301+
return types.ErrNotBondedValidator
302+
}
303+
304+
status := k.oracleKeeper.GetValidatorStatus(ctx, val)
305+
if !status.IsActive {
306+
return types.ErrOracleStatusNotActive.Wrapf("val: %s", val.String())
307+
}
308+
309+
return nil
310+
}

0 commit comments

Comments
 (0)