From d02ebbd519c3824865d0265e8a4de9da86bfdbcf Mon Sep 17 00:00:00 2001 From: Akshay Aggarwal Date: Wed, 27 Sep 2023 19:05:54 +0100 Subject: [PATCH] Fix automation - mercury v0.3 response decoding (#10812) (#10818) * Fix automation - mercury v0.3 response decoding * update (cherry picked from commit f7d0b38b6379d5cf3399b9d84064752a9ad4cdee) --- .../ocr2keeper/evm21/streams_lookup.go | 15 ++++- .../ocr2keeper/evm21/streams_lookup_test.go | 60 ++++++++++++++----- 2 files changed, 57 insertions(+), 18 deletions(-) diff --git a/core/services/ocr2/plugins/ocr2keeper/evm21/streams_lookup.go b/core/services/ocr2/plugins/ocr2keeper/evm21/streams_lookup.go index 2155b383002..6c1789de9c4 100644 --- a/core/services/ocr2/plugins/ocr2keeper/evm21/streams_lookup.go +++ b/core/services/ocr2/plugins/ocr2keeper/evm21/streams_lookup.go @@ -66,10 +66,10 @@ type MercuryV03Response struct { } type MercuryV03Report struct { - FeedID []byte `json:"feedID"` // feed id in hex + FeedID string `json:"feedID"` // feed id in hex encoded ValidFromTimestamp uint32 `json:"validFromTimestamp"` ObservationsTimestamp uint32 `json:"observationsTimestamp"` - FullReport []byte `json:"fullReport"` // the actual mercury report of this feed, can be sent to verifier + FullReport string `json:"fullReport"` // the actual hex encoded mercury report of this feed, can be sent to verifier } type MercuryData struct { @@ -528,6 +528,7 @@ func (r *EvmRegistry) multiFeedsRequest(ctx context.Context, ch chan<- MercuryDa } else if resp.StatusCode == 420 { // in 0.3, this will happen when missing/malformed query args, missing or bad required headers, non-existent feeds, or no permissions for feeds retryable = false + state = encoding.InvalidMercuryRequest return fmt.Errorf("at timestamp %s upkeep %s received status code %d from mercury v0.3, most likely this is caused by missing/malformed query args, missing or bad required headers, non-existent feeds, or no permissions for feeds", sl.time.String(), sl.upkeepId.String(), resp.StatusCode) } else if resp.StatusCode != http.StatusOK { retryable = false @@ -549,13 +550,21 @@ func (r *EvmRegistry) multiFeedsRequest(ctx context.Context, ch chan<- MercuryDa // hence, retry in this case. retry will help when we send a very new timestamp and reports are not yet generated if len(response.Reports) != len(sl.feeds) { // TODO: AUTO-5044: calculate what reports are missing and log a warning + lggr.Warnf("at timestamp %s upkeep %s mercury v0.3 server retruned 200 status with %d reports while we requested %d feeds, treating as 404 (not found) and retrying", sl.time.String(), sl.upkeepId.String(), len(response.Reports), len(sl.feeds)) retryable = true state = encoding.MercuryFlakyFailure return fmt.Errorf("%d", http.StatusNotFound) } var reportBytes [][]byte for _, rsp := range response.Reports { - reportBytes = append(reportBytes, rsp.FullReport) + b, err := hexutil.Decode(rsp.FullReport) + if err != nil { + lggr.Warnf("at timestamp %s upkeep %s failed to decode reportBlob %s: %v", sl.time.String(), sl.upkeepId.String(), rsp.FullReport, err) + retryable = false + state = encoding.InvalidMercuryResponse + return err + } + reportBytes = append(reportBytes, b) } ch <- MercuryData{ Index: 0, diff --git a/core/services/ocr2/plugins/ocr2keeper/evm21/streams_lookup_test.go b/core/services/ocr2/plugins/ocr2keeper/evm21/streams_lookup_test.go index 42aeecb64f6..f59cec18c1e 100644 --- a/core/services/ocr2/plugins/ocr2keeper/evm21/streams_lookup_test.go +++ b/core/services/ocr2/plugins/ocr2keeper/evm21/streams_lookup_test.go @@ -731,16 +731,16 @@ func TestEvmRegistry_MultiFeedRequest(t *testing.T) { response: &MercuryV03Response{ Reports: []MercuryV03Report{ { - FeedID: hexutil.MustDecode("0x4554482d5553442d415242495452554d2d544553544e45540000000000000000"), + FeedID: "0x4554482d5553442d415242495452554d2d544553544e45540000000000000000", ValidFromTimestamp: 123456, ObservationsTimestamp: 123456, - FullReport: hexutil.MustDecode("0xab2123dc00000012"), + FullReport: "0xab2123dc00000012", }, { - FeedID: hexutil.MustDecode("0x4254432d5553442d415242495452554d2d544553544e45540000000000000000"), + FeedID: "0x4254432d5553442d415242495452554d2d544553544e45540000000000000000", ValidFromTimestamp: 123458, ObservationsTimestamp: 123458, - FullReport: hexutil.MustDecode("0xab2123dc00000016"), + FullReport: "0xab2123dc00000016", }, }, }, @@ -761,20 +761,49 @@ func TestEvmRegistry_MultiFeedRequest(t *testing.T) { response: &MercuryV03Response{ Reports: []MercuryV03Report{ { - FeedID: hexutil.MustDecode("0x4554482d5553442d415242495452554d2d544553544e45540000000000000000"), + FeedID: "0x4554482d5553442d415242495452554d2d544553544e45540000000000000000", ValidFromTimestamp: 123456, ObservationsTimestamp: 123456, - FullReport: hexutil.MustDecode("0xab2123dc00000012"), + FullReport: "0xab2123dc00000012", }, { - FeedID: hexutil.MustDecode("0x4254432d5553442d415242495452554d2d544553544e45540000000000000000"), + FeedID: "0x4254432d5553442d415242495452554d2d544553544e45540000000000000000", ValidFromTimestamp: 123458, ObservationsTimestamp: 123458, - FullReport: hexutil.MustDecode("0xab2123dc00000019"), + FullReport: "0xab2123dc00000019", }, }, }, }, + { + name: "failure - fail to decode reportBlob", + lookup: &StreamsLookup{ + feedParamKey: feedIDs, + feeds: []string{"0x4554482d5553442d415242495452554d2d544553544e45540000000000000000", "0x4254432d5553442d415242495452554d2d544553544e45540000000000000000"}, + timeParamKey: timestamp, + time: big.NewInt(123456), + upkeepId: upkeepId, + }, + response: &MercuryV03Response{ + Reports: []MercuryV03Report{ + { + FeedID: "0x4554482d5553442d415242495452554d2d544553544e45540000000000000000", + ValidFromTimestamp: 123456, + ObservationsTimestamp: 123456, + FullReport: "qerwiu", // invalid hex blob + }, + { + FeedID: "0x4254432d5553442d415242495452554d2d544553544e45540000000000000000", + ValidFromTimestamp: 123458, + ObservationsTimestamp: 123458, + FullReport: "0xab2123dc00000016", + }, + }, + }, + statusCode: http.StatusOK, + retryable: false, + errorMessage: "All attempts fail:\n#1: hex string without 0x prefix", + }, { name: "failure - returns retryable", lookup: &StreamsLookup{ @@ -839,26 +868,26 @@ func TestEvmRegistry_MultiFeedRequest(t *testing.T) { firstResponse: &MercuryV03Response{ Reports: []MercuryV03Report{ { - FeedID: hexutil.MustDecode("0x4554482d5553442d415242495452554d2d544553544e45540000000000000000"), + FeedID: "0x4554482d5553442d415242495452554d2d544553544e45540000000000000000", ValidFromTimestamp: 123456, ObservationsTimestamp: 123456, - FullReport: hexutil.MustDecode("0xab2123dc00000012"), + FullReport: "0xab2123dc00000012", }, }, }, response: &MercuryV03Response{ Reports: []MercuryV03Report{ { - FeedID: hexutil.MustDecode("0x4554482d5553442d415242495452554d2d544553544e45540000000000000000"), + FeedID: "0x4554482d5553442d415242495452554d2d544553544e45540000000000000000", ValidFromTimestamp: 123456, ObservationsTimestamp: 123456, - FullReport: hexutil.MustDecode("0xab2123dc00000012"), + FullReport: "0xab2123dc00000012", }, { - FeedID: hexutil.MustDecode("0x4254432d5553442d415242495452554d2d544553544e45540000000000000000"), + FeedID: "0x4254432d5553442d415242495452554d2d544553544e45540000000000000000", ValidFromTimestamp: 123458, ObservationsTimestamp: 123458, - FullReport: hexutil.MustDecode("0xab2123dc00000019"), + FullReport: "0xab2123dc00000019", }, }, }, @@ -930,7 +959,8 @@ func TestEvmRegistry_MultiFeedRequest(t *testing.T) { assert.Nil(t, m.Error) var reports [][]byte for _, rsp := range tt.response.Reports { - reports = append(reports, rsp.FullReport) + b, _ := hexutil.Decode(rsp.FullReport) + reports = append(reports, b) } assert.Equal(t, reports, m.Bytes) }