Skip to content

Commit

Permalink
AUTO-6622-Enable-Mercury-v0.3-StreamsLookup-to-also-use-blockNumber (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
cmalec authored Oct 20, 2023
1 parent f8a36e7 commit 75b7554
Show file tree
Hide file tree
Showing 2 changed files with 202 additions and 43 deletions.
45 changes: 25 additions & 20 deletions core/services/ocr2/plugins/ocr2keeper/evm21/streams_lookup.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,20 +28,21 @@ import (
)

const (
applicationJson = "application/json"
blockNumber = "blockNumber" // valid for v0.2
feedIDs = "feedIDs" // valid for v0.3
feedIdHex = "feedIdHex" // valid for v0.2
headerAuthorization = "Authorization"
headerContentType = "Content-Type"
headerTimestamp = "X-Authorization-Timestamp"
headerSignature = "X-Authorization-Signature-SHA256"
headerUpkeepId = "X-Authorization-Upkeep-Id"
mercuryPathV02 = "/client?" // only used to access mercury v0.2 server
mercuryBatchPathV03 = "/api/v1/reports/bulk?" // only used to access mercury v0.3 server
retryDelay = 500 * time.Millisecond
timestamp = "timestamp" // valid for v0.3
totalAttempt = 3
applicationJson = "application/json"
blockNumber = "blockNumber" // valid for v0.2
feedIDs = "feedIDs" // valid for v0.3
feedIdHex = "feedIdHex" // valid for v0.2
headerAuthorization = "Authorization"
headerContentType = "Content-Type"
headerTimestamp = "X-Authorization-Timestamp"
headerSignature = "X-Authorization-Signature-SHA256"
headerUpkeepId = "X-Authorization-Upkeep-Id"
mercuryPathV02 = "/client?" // only used to access mercury v0.2 server
mercuryBatchPathV03 = "/api/v1/reports/bulk?" // only used to access mercury v0.3 server
mercuryBatchPathV03BlockNumber = "/api/v1gmx/reports/bulk?" // only used to access mercury v0.3 server with blockNumber
retryDelay = 500 * time.Millisecond
timestamp = "timestamp" // valid for v0.3
totalAttempt = 3
)

type StreamsLookup struct {
Expand Down Expand Up @@ -132,7 +133,7 @@ func (r *EvmRegistry) streamsLookup(ctx context.Context, checkResults []ocr2keep
checkResults[i].IneligibilityReason = uint8(encoding.UpkeepFailureReasonMercuryAccessNotAllowed)
continue
}
} else if l.FeedParamKey != feedIDs || l.TimeParamKey != timestamp {
} else if l.FeedParamKey != feedIDs {
// if mercury version cannot be determined, set failure reason
lggr.Debugf("at block %d upkeep %s NOT allowed to query Mercury server", block, upkeepId)
checkResults[i].IneligibilityReason = uint8(encoding.UpkeepFailureReasonInvalidRevertDataInput)
Expand Down Expand Up @@ -289,7 +290,7 @@ func (r *EvmRegistry) doMercuryRequest(ctx context.Context, sl *StreamsLookup, l
for i := range sl.Feeds {
go r.singleFeedRequest(ctx, ch, i, sl, lggr)
}
} else if sl.FeedParamKey == feedIDs && sl.TimeParamKey == timestamp {
} else if sl.FeedParamKey == feedIDs {
// only mercury v0.3
resultLen = 1
isMercuryV03 = true
Expand Down Expand Up @@ -441,8 +442,12 @@ func (r *EvmRegistry) multiFeedsRequest(ctx context.Context, ch chan<- MercuryDa
// feedIDs: {strings.Join(sl.Feeds, ",")},
// timestamp: {sl.Time.String()},
//}
params := fmt.Sprintf("%s=%s&%s=%s", feedIDs, strings.Join(sl.Feeds, ","), timestamp, sl.Time.String())
reqUrl := fmt.Sprintf("%s%s%s", r.mercury.cred.URL, mercuryBatchPathV03, params)
params := fmt.Sprintf("%s=%s&%s=%s", feedIDs, strings.Join(sl.Feeds, ","), sl.TimeParamKey, sl.Time.String())
batchPathV03 := mercuryBatchPathV03
if sl.TimeParamKey == blockNumber {
batchPathV03 = mercuryBatchPathV03BlockNumber
}
reqUrl := fmt.Sprintf("%s%s%s", r.mercury.cred.URL, batchPathV03, params)
lggr.Debugf("request URL for upkeep %s userId %s: %s", sl.upkeepId.String(), r.mercury.cred.Username, reqUrl)

req, err := http.NewRequestWithContext(ctx, http.MethodGet, reqUrl, nil)
Expand All @@ -452,7 +457,7 @@ func (r *EvmRegistry) multiFeedsRequest(ctx context.Context, ch chan<- MercuryDa
}

ts := time.Now().UTC().UnixMilli()
signature := r.generateHMAC(http.MethodGet, mercuryBatchPathV03+params, []byte{}, r.mercury.cred.Username, r.mercury.cred.Password, ts)
signature := r.generateHMAC(http.MethodGet, batchPathV03+params, []byte{}, r.mercury.cred.Username, r.mercury.cred.Password, ts)
req.Header.Set(headerContentType, applicationJson)
// username here is often referred to as user id
req.Header.Set(headerAuthorization, r.mercury.cred.Username)
Expand Down Expand Up @@ -498,7 +503,7 @@ func (r *EvmRegistry) multiFeedsRequest(ctx context.Context, ch chan<- MercuryDa
} else if resp.StatusCode == http.StatusBadRequest {
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 invalid format of timestamp", sl.Time.String(), sl.upkeepId.String(), resp.StatusCode)
return fmt.Errorf("at timestamp %s upkeep %s received status code %d from mercury v0.3 with message: %s", sl.Time.String(), sl.upkeepId.String(), resp.StatusCode, string(body))
} else if resp.StatusCode == http.StatusInternalServerError {
retryable = true
state = encoding.MercuryFlakyFailure
Expand Down
Loading

0 comments on commit 75b7554

Please sign in to comment.