Skip to content

Commit 8d0b574

Browse files
colmaziaRogerKSI
andauthored
[Feeds] Fix feeds issues (#149)
* add max signal id characters * deactivate using block height * calculate supported feeds every n blocks * change default block update param * using type Signal for signal-total-power * add check err in init genesis * fix test * handle error in test * delete power from feed, fix key, fix GetAllSignalTotalPowers bug * not delete signalTotalPower when power is 0, remove signalTotalPower from genesis * fix test * fix test * fix test * fix command * implement emit event for signal-total-power and supported-feeds --------- Co-authored-by: Kitipong Sirirueangsakul <[email protected]>
1 parent 9f7a6f5 commit 8d0b574

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

41 files changed

+1324
-989
lines changed

grogu/feed/check.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ func fetchData(
9797
if err != nil {
9898
return types.Params{}, nil, nil, nil, err
9999
}
100-
feeds = feedsResponse.Feeds
100+
feeds = feedsResponse.SupportedFeeds.Feeds
101101

102102
// Fetch validator prices
103103
validatorPricesResponse, err := c.QueryClient.ValidatorPrices(

proto/feeds/v1beta1/feeds.proto

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ message Signal {
1919

2020
// DelegatorSignals is the data structure that contains array of signals of a delegator.
2121
message DelegatorSignals {
22+
option (gogoproto.equal) = true;
23+
2224
// Delegator is the address of the delegator of this signals.
2325
string delegator = 1 [(cosmos_proto.scalar) = "cosmos.AddressString"];
2426

@@ -33,17 +35,25 @@ message Feed {
3335
// SignalID is the unique string that identifies the unit of feed.
3436
string signal_id = 1 [(gogoproto.customname) = "SignalID"];
3537

36-
// Power is the power of the corresponding feed.
37-
int64 power = 2;
38-
3938
// Interval is the interval of the price feed.
40-
int64 interval = 3;
41-
42-
// LastIntervalUpdateTimestamp is the timestamp of the last time interval is updated.
43-
int64 last_interval_update_timestamp = 4;
39+
int64 interval = 2;
4440

4541
// DeviationInThousandth is the maximum deviation value the feed can tolerate, expressed in thousandths.
46-
int64 deviation_in_thousandth = 5;
42+
int64 deviation_in_thousandth = 3;
43+
}
44+
45+
// SupportedFeeds is a structure that holds a list of currently supported feeds, and its last update time and block.
46+
message SupportedFeeds {
47+
option (gogoproto.equal) = true;
48+
49+
// Feed is a list of currently suppored feeds.
50+
repeated Feed feeds = 1 [(gogoproto.nullable) = false];
51+
52+
// LastUpdateTimestamp is the timestamp of the last time supported feeds list is updated.
53+
int64 last_update_timestamp = 2;
54+
55+
// LastUpdateBlock is the number of blocks of the last time supported feeds list is updated.
56+
int64 last_update_block = 3;
4757
}
4858

4959
// PriceStatus is a structure that defines the price status of a price.
@@ -112,6 +122,9 @@ message ValidatorPrice {
112122

113123
// Timestamp is the timestamp at which the price was submitted.
114124
int64 timestamp = 5;
125+
126+
// BlockHeight is the block height at which the price was submitted.
127+
int64 block_height = 6;
115128
}
116129

117130
// PriceService is a structure that defines the information of price service.

proto/feeds/v1beta1/genesis.proto

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,9 @@ message GenesisState {
1212
// Params is all parameters of the module.
1313
Params params = 1 [(gogoproto.nullable) = false];
1414

15-
// Feeds is a list of feeds that requires validators to submit the price.
16-
repeated Feed feeds = 2 [(gogoproto.nullable) = false];
15+
// DelegatorSignals is a list of delegator signals submitted by delegators.
16+
repeated DelegatorSignals delegator_signals = 2 [(gogoproto.nullable) = false];
1717

1818
// PriceService is the information about price service.
1919
PriceService price_service = 3 [(gogoproto.nullable) = false];
20-
21-
// DelegatorSignals is a list of delegator signals submitted by delegators.
22-
repeated DelegatorSignals delegator_signals = 4 [(gogoproto.nullable) = false];
2320
}

proto/feeds/v1beta1/params.proto

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,4 +43,10 @@ message Params {
4343

4444
// MaxDeviationInThousandth is the maximum limit of every feeds' deviation (in thousandth).
4545
int64 max_deviation_in_thousandth = 10;
46+
47+
// MaxSignalIDCharacters is the maximum limit of characters of a signal id.
48+
uint64 max_signal_id_characters = 11 [(gogoproto.customname) = "MaxSignalIDCharacters"];
49+
50+
// BlocksPerFeedsUpdate is the number of blocks after which the feed interval and deviation will be recalculated
51+
uint64 blocks_per_feeds_update = 12;
4652
}

proto/feeds/v1beta1/query.proto

Lines changed: 11 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -37,9 +37,9 @@ service Query {
3737
option (google.api.http).get = "/feeds/v1beta1/validators/{validator}";
3838
}
3939

40-
// Feeds is a RPC method that returns all feeds.
41-
rpc Feeds(QueryFeedsRequest) returns (QueryFeedsResponse) {
42-
option (google.api.http).get = "/feeds/v1beta1/feeds";
40+
// Signals is a RPC method that returns all signal-total-powers or specified signal-total-power by signal id.
41+
rpc SignalTotalPowers(QuerySignalTotalPowersRequest) returns (QuerySignalTotalPowersResponse) {
42+
option (google.api.http).get = "/feeds/v1beta1/signal-total-powers";
4343
}
4444

4545
// Params is a RPC method that returns all parameters of the module.
@@ -91,9 +91,6 @@ message QueryPriceRequest {
9191
message QueryPriceResponse {
9292
// Price is the aggregated price of the signal id.
9393
Price price = 1 [(gogoproto.nullable) = false];
94-
95-
// ValidatorPrices is a list of prices from each validator.
96-
repeated ValidatorPrice validator_prices = 2 [(gogoproto.nullable) = false];
9794
}
9895

9996
// QueryValidatorPricesRequest is the request type for the Query/ValidatorPrices RPC method.
@@ -135,19 +132,19 @@ message QueryValidValidatorResponse {
135132
bool valid = 1;
136133
}
137134

138-
// QueryFeedsRequest is the request type for the Query/Feeds RPC method.
139-
message QueryFeedsRequest {
135+
// QuerySignalTotalPowersRequest is the request type for the Query/SignalTotalPowers RPC method.
136+
message QuerySignalTotalPowersRequest {
140137
// SignalIDs is a list of signal ids to query.
141138
repeated string signal_ids = 1;
142139

143140
// Pagination is the pagination for the request.
144141
cosmos.base.query.v1beta1.PageRequest pagination = 2;
145142
}
146143

147-
// QueryFeedsResponse is the response type for the Query/Feeds RPC method.
148-
message QueryFeedsResponse {
149-
// Feeds is a list of feeds.
150-
repeated Feed feeds = 1;
144+
// QuerySignalTotalPowersResponse is the response type for the Query/SignalTotalPowers RPC method.
145+
message QuerySignalTotalPowersResponse {
146+
// SingalTotalPowers is a list of signal-total-powers.
147+
repeated Signal signal_total_powers = 1;
151148

152149
// Pagination is the pagination information in the response.
153150
cosmos.base.query.v1beta1.PageResponse pagination = 2;
@@ -187,6 +184,6 @@ message QuerySupportedFeedsRequest {}
187184

188185
// QuerySupportedFeedsResponse is the response type for the Query/SupportedFeeds RPC method
189186
message QuerySupportedFeedsResponse {
190-
// Feeds is a list of feeds.
191-
repeated Feed feeds = 1 [(gogoproto.nullable) = false];
187+
// SupportedFeeds is a list of currently supported feeds, and its last update time and block.
188+
SupportedFeeds supported_feeds = 1 [(gogoproto.nullable) = false];
192189
}

x/feeds/abci.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,8 @@ import (
99
// HandleEndBlock is a handler function for the EndBlock ABCI request.
1010
func HandleEndBlock(ctx sdk.Context, k keeper.Keeper) {
1111
k.CalculatePrices(ctx)
12+
if ctx.BlockHeight()%int64(k.GetParams(ctx).BlocksPerFeedsUpdate) == 0 {
13+
feeds := k.CalculateNewSupportedFeeds(ctx)
14+
k.SetSupportedFeeds(ctx, feeds)
15+
}
1216
}

x/feeds/client/cli/query.go

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ func GetQueryCmd() *cobra.Command {
2626
GetQueryCmdPrice(),
2727
GetQueryCmdValidatorPrices(),
2828
GetQueryCmdValidatorPrice(),
29-
GetQueryCmdFeeds(),
29+
GetQueryCmdSignalTotalPowers(),
3030
GetQueryCmdParams(),
3131
GetQueryCmdDelegatorSignal(),
3232
GetQueryCmdSupportedFeeds(),
@@ -195,11 +195,11 @@ func GetQueryCmdValidatorPrice() *cobra.Command {
195195
return cmd
196196
}
197197

198-
// GetQueryCmdFeeds implements the query feeds command.
199-
func GetQueryCmdFeeds() *cobra.Command {
198+
// GetQueryCmdSignalTotalPowers implements the query signal-total-powers command.
199+
func GetQueryCmdSignalTotalPowers() *cobra.Command {
200200
cmd := &cobra.Command{
201-
Use: "feeds",
202-
Short: "Shows all information of all feeds",
201+
Use: "signal-total-powers",
202+
Short: "Shows all information of all signals and its total power",
203203
Args: cobra.NoArgs,
204204
RunE: func(cmd *cobra.Command, args []string) error {
205205
clientCtx := client.GetClientContextFromCmd(cmd)
@@ -210,7 +210,10 @@ func GetQueryCmdFeeds() *cobra.Command {
210210
return err
211211
}
212212

213-
res, err := queryClient.Feeds(context.Background(), &types.QueryFeedsRequest{Pagination: pageReq})
213+
res, err := queryClient.SignalTotalPowers(
214+
context.Background(),
215+
&types.QuerySignalTotalPowersRequest{Pagination: pageReq},
216+
)
214217
if err != nil {
215218
return err
216219
}
@@ -219,7 +222,7 @@ func GetQueryCmdFeeds() *cobra.Command {
219222
},
220223
}
221224

222-
flags.AddPaginationFlagsToCmd(cmd, "feeds")
225+
flags.AddPaginationFlagsToCmd(cmd, "signal-total-powers")
223226
flags.AddQueryFlagsToCmd(cmd)
224227

225228
return cmd

x/feeds/keeper/calculate.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import (
55
)
66

77
// calculateIntervalAndDeviation calculates feed interval and deviation from power
8-
func calculateIntervalAndDeviation(power int64, param types.Params) (interval int64, deviation int64) {
8+
func CalculateIntervalAndDeviation(power int64, param types.Params) (interval int64, deviation int64) {
99
if power < param.PowerThreshold {
1010
return 0, 0
1111
}

x/feeds/keeper/calculate_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import (
99
)
1010

1111
func TestCalculateIntervalAndDeviation(t *testing.T) {
12-
params := types.NewParams("[NOT_SET]", 30, 30, 60, 3600, 1000_000_000, 100, 30, 5, 300)
12+
params := types.NewParams("[NOT_SET]", 30, 30, 60, 3600, 1000_000_000, 100, 30, 5, 300, 256, 28800)
1313

1414
testCases := []struct {
1515
name string
@@ -46,7 +46,7 @@ func TestCalculateIntervalAndDeviation(t *testing.T) {
4646
for _, tc := range testCases {
4747
tc := tc
4848
t.Run(tc.name, func(tt *testing.T) {
49-
interval, deviation := calculateIntervalAndDeviation(tc.power, params)
49+
interval, deviation := CalculateIntervalAndDeviation(tc.power, params)
5050
require.Equal(tt, tc.expInterval, interval)
5151
require.Equal(tt, tc.expDeviation, deviation)
5252
})

x/feeds/keeper/emit.go

Lines changed: 9 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -8,41 +8,25 @@ import (
88
"github.com/bandprotocol/chain/v2/x/feeds/types"
99
)
1010

11-
func emitEventUpdateFeed(ctx sdk.Context, feed types.Feed) {
11+
func emitEventUpdateSignalTotalPower(ctx sdk.Context, signal types.Signal) {
1212
ctx.EventManager().EmitEvent(
1313
sdk.NewEvent(
14-
types.EventTypeUpdateFeed,
15-
sdk.NewAttribute(types.AttributeKeySignalID, feed.SignalID),
16-
sdk.NewAttribute(types.AttributeKeyPower, fmt.Sprintf("%d", feed.Power)),
17-
sdk.NewAttribute(types.AttributeKeyInterval, fmt.Sprintf("%d", feed.Interval)),
18-
sdk.NewAttribute(
19-
types.AttributeKeyLastIntervalUpdateTimestamp,
20-
fmt.Sprintf("%d", feed.LastIntervalUpdateTimestamp),
21-
),
22-
sdk.NewAttribute(
23-
types.AttributeKeyDeviationInThousandth,
24-
fmt.Sprintf("%d", feed.DeviationInThousandth),
25-
),
14+
types.EventTypeUpdateSignalTotalPower,
15+
sdk.NewAttribute(types.AttributeKeySignalID, signal.ID),
16+
sdk.NewAttribute(types.AttributeKeyPower, fmt.Sprintf("%d", signal.Power)),
2617
),
2718
)
2819
}
2920

30-
func emitEventDeleteFeed(ctx sdk.Context, feed types.Feed) {
21+
func emitEventUpdateSupportedFeeds(ctx sdk.Context, supportedFeeds types.SupportedFeeds) {
3122
ctx.EventManager().EmitEvent(
3223
sdk.NewEvent(
33-
types.EventTypeDeleteFeed,
34-
sdk.NewAttribute(types.AttributeKeySignalID, feed.SignalID),
35-
sdk.NewAttribute(types.AttributeKeyPower, fmt.Sprintf("%d", feed.Power)),
36-
sdk.NewAttribute(types.AttributeKeyInterval, fmt.Sprintf("%d", feed.Interval)),
24+
types.EventTypeUpdateSupportedFeeds,
3725
sdk.NewAttribute(
38-
types.AttributeKeyLastIntervalUpdateTimestamp,
39-
fmt.Sprintf("%d", feed.LastIntervalUpdateTimestamp),
26+
types.AttributeKeyLastUpdateTimestamp,
27+
fmt.Sprintf("%d", supportedFeeds.LastUpdateTimestamp),
4028
),
41-
sdk.NewAttribute(
42-
types.AttributeKeyDeviationInThousandth,
43-
fmt.Sprintf("%d", feed.DeviationInThousandth),
44-
),
45-
),
29+
sdk.NewAttribute(types.AttributeKeyLastUpdateBlock, fmt.Sprintf("%d", supportedFeeds.LastUpdateBlock))),
4630
)
4731
}
4832

0 commit comments

Comments
 (0)