Skip to content

Commit

Permalink
Add string equivalent for observation price, bid, ask (#11077)
Browse files Browse the repository at this point in the history
* Add string equivalent for observation price, bid, ask

* Update logging
  • Loading branch information
george-dorin authored Oct 27, 2023
1 parent d841463 commit e6118da
Show file tree
Hide file tree
Showing 4 changed files with 227 additions and 177 deletions.
64 changes: 35 additions & 29 deletions core/services/ocrcommon/telemetry.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"encoding/json"
"fmt"
"math/big"

"github.com/ethereum/go-ethereum/common"

Expand Down Expand Up @@ -230,7 +231,7 @@ func (e *EnhancedTelemetryService[T]) collectAndSend(trrs *pipeline.TaskRunResul
}
eaTelem, err := parseEATelemetry([]byte(bridgeRawResponse))
if err != nil {
e.lggr.Warnf("cannot parse EA telemetry, job %d, id %s", e.job.ID, trr.Task.DotID())
e.lggr.Warnw(fmt.Sprintf("cannot parse EA telemetry, job %d, id %s", e.job.ID, trr.Task.DotID()), "err", err)
}
value := e.getParsedValue(trrs, trr)

Expand Down Expand Up @@ -278,39 +279,42 @@ func (e *EnhancedTelemetryService[T]) collectMercuryEnhancedTelemetry(obs relaym

bridgeRawResponse, ok := trr.Result.Value.(string)
if !ok {
e.lggr.Warnf("cannot get bridge response from bridge task, job %d, id %s", e.job.ID, trr.Task.DotID())
e.lggr.Warnf("cannot get bridge response from bridge task, job %d, id %s, expected string got %T", e.job.ID, trr.Task.DotID(), trr.Result.Value)
continue
}
eaTelem, err := parseEATelemetry([]byte(bridgeRawResponse))
if err != nil {
e.lggr.Warnf("cannot parse EA telemetry, job %d, id %s", e.job.ID, trr.Task.DotID())
e.lggr.Warnw(fmt.Sprintf("cannot parse EA telemetry, job %d, id %s", e.job.ID, trr.Task.DotID()), "err", err)
}

assetSymbol := e.getAssetSymbolFromRequestData(bridgeTask.RequestData)
benchmarkPrice, bidPrice, askPrice := e.getPricesFromResults(trr, &trrs)

t := &telem.EnhancedEAMercury{
DataSource: eaTelem.DataSource,
DpBenchmarkPrice: benchmarkPrice,
DpBid: bidPrice,
DpAsk: askPrice,
CurrentBlockNumber: obsBlockNum,
CurrentBlockHash: common.BytesToHash(obsBlockHash).String(),
CurrentBlockTimestamp: obsBlockTimestamp,
BridgeTaskRunStartedTimestamp: trr.CreatedAt.UnixMilli(),
BridgeTaskRunEndedTimestamp: trr.FinishedAt.Time.UnixMilli(),
ProviderRequestedTimestamp: eaTelem.ProviderRequestedTimestamp,
ProviderReceivedTimestamp: eaTelem.ProviderReceivedTimestamp,
ProviderDataStreamEstablished: eaTelem.ProviderDataStreamEstablished,
ProviderIndicatedTime: eaTelem.ProviderIndicatedTime,
Feed: e.job.OCR2OracleSpec.FeedID.Hex(),
ObservationBenchmarkPrice: obsBenchmarkPrice,
ObservationBid: obsBid,
ObservationAsk: obsAsk,
ConfigDigest: repts.ConfigDigest.Hex(),
Round: int64(repts.Round),
Epoch: int64(repts.Epoch),
AssetSymbol: assetSymbol,
DataSource: eaTelem.DataSource,
DpBenchmarkPrice: benchmarkPrice,
DpBid: bidPrice,
DpAsk: askPrice,
CurrentBlockNumber: obsBlockNum,
CurrentBlockHash: common.BytesToHash(obsBlockHash).String(),
CurrentBlockTimestamp: obsBlockTimestamp,
BridgeTaskRunStartedTimestamp: trr.CreatedAt.UnixMilli(),
BridgeTaskRunEndedTimestamp: trr.FinishedAt.Time.UnixMilli(),
ProviderRequestedTimestamp: eaTelem.ProviderRequestedTimestamp,
ProviderReceivedTimestamp: eaTelem.ProviderReceivedTimestamp,
ProviderDataStreamEstablished: eaTelem.ProviderDataStreamEstablished,
ProviderIndicatedTime: eaTelem.ProviderIndicatedTime,
Feed: e.job.OCR2OracleSpec.FeedID.Hex(),
ObservationBenchmarkPrice: obsBenchmarkPrice.Int64(), //Deprecated: observation value will not fit in int64, we will use the string equivalent field ObservationBenchmarkPriceString
ObservationBid: obsBid.Int64(), //Deprecated: observation value will not fit in int64, we will use the string equivalent field ObservationBidString
ObservationAsk: obsAsk.Int64(), //Deprecated: observation value will not fit in int64, we will use the string equivalent field ObservationAskString
ConfigDigest: repts.ConfigDigest.Hex(),
Round: int64(repts.Round),
Epoch: int64(repts.Epoch),
AssetSymbol: assetSymbol,
ObservationBenchmarkPriceString: obsBenchmarkPrice.String(),
ObservationBidString: obsBid.String(),
ObservationAskString: obsAsk.String(),
}

bytes, err := proto.Marshal(t)
Expand Down Expand Up @@ -408,17 +412,19 @@ func (e *EnhancedTelemetryService[T]) getPricesFromResults(startTask pipeline.Ta
}

// getFinalValues runs a parse on the pipeline.TaskRunResults and returns the values
func (e *EnhancedTelemetryService[T]) getFinalValues(obs relaymercuryv1.Observation) (int64, int64, int64, int64, []byte, uint64) {
var benchmarkPrice, bid, ask int64
func (e *EnhancedTelemetryService[T]) getFinalValues(obs relaymercuryv1.Observation) (*big.Int, *big.Int, *big.Int, int64, []byte, uint64) {
benchmarkPrice := big.NewInt(0)
bid := big.NewInt(0)
ask := big.NewInt(0)

if obs.BenchmarkPrice.Val != nil {
benchmarkPrice = obs.BenchmarkPrice.Val.Int64()
benchmarkPrice = obs.BenchmarkPrice.Val
}
if obs.Bid.Val != nil {
bid = obs.Bid.Val.Int64()
bid = obs.Bid.Val
}
if obs.Ask.Val != nil {
ask = obs.Ask.Val.Int64()
ask = obs.Ask.Val
}

return benchmarkPrice, bid, ask, obs.CurrentBlockNum.Val, obs.CurrentBlockHash.Val, obs.CurrentBlockTimestamp.Val
Expand Down
57 changes: 30 additions & 27 deletions core/services/ocrcommon/telemetry_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -433,17 +433,17 @@ func TestGetFinalValues(t *testing.T) {
}

benchmarkPrice, bid, ask, blockNr, blockHash, blockTimestamp := e.getFinalValues(o)
require.Equal(t, benchmarkPrice, int64(111111))
require.Equal(t, bid, int64(222222))
require.Equal(t, ask, int64(333333))
require.Equal(t, benchmarkPrice, big.NewInt(111111))
require.Equal(t, bid, big.NewInt(222222))
require.Equal(t, ask, big.NewInt(333333))
require.Equal(t, blockNr, int64(123456789))
require.Equal(t, blockHash, common.HexToHash("0x123321").Bytes())
require.Equal(t, blockTimestamp, uint64(987654321))

benchmarkPrice, bid, ask, blockNr, blockHash, blockTimestamp = e.getFinalValues(mercuryv1.Observation{})
require.Equal(t, benchmarkPrice, int64(0))
require.Equal(t, bid, int64(0))
require.Equal(t, ask, int64(0))
require.Equal(t, benchmarkPrice, big.NewInt(0))
require.Equal(t, bid, big.NewInt(0))
require.Equal(t, ask, big.NewInt(0))
require.Equal(t, blockNr, int64(0))
require.Nil(t, blockHash)
require.Equal(t, blockTimestamp, uint64(0))
Expand Down Expand Up @@ -598,27 +598,30 @@ func TestCollectMercuryEnhancedTelemetry(t *testing.T) {
}

expectedTelemetry := telem.EnhancedEAMercury{
DataSource: "data-source-name",
DpBenchmarkPrice: 123456.123456,
DpBid: 1234567.1234567,
DpAsk: 321123,
CurrentBlockNumber: 123456789,
CurrentBlockHash: common.HexToHash("0x123321").String(),
CurrentBlockTimestamp: 987654321,
BridgeTaskRunStartedTimestamp: trrsMercury[0].CreatedAt.UnixMilli(),
BridgeTaskRunEndedTimestamp: trrsMercury[0].FinishedAt.Time.UnixMilli(),
ProviderRequestedTimestamp: 92233720368547760,
ProviderReceivedTimestamp: -92233720368547760,
ProviderDataStreamEstablished: 1,
ProviderIndicatedTime: -123456789,
Feed: common.HexToHash("0x111").String(),
ObservationBenchmarkPrice: 111111,
ObservationBid: 222222,
ObservationAsk: 333333,
ConfigDigest: "0200000000000000000000000000000000000000000000000000000000000000",
Round: 22,
Epoch: 11,
AssetSymbol: "USD/LINK",
DataSource: "data-source-name",
DpBenchmarkPrice: 123456.123456,
DpBid: 1234567.1234567,
DpAsk: 321123,
CurrentBlockNumber: 123456789,
CurrentBlockHash: common.HexToHash("0x123321").String(),
CurrentBlockTimestamp: 987654321,
BridgeTaskRunStartedTimestamp: trrsMercury[0].CreatedAt.UnixMilli(),
BridgeTaskRunEndedTimestamp: trrsMercury[0].FinishedAt.Time.UnixMilli(),
ProviderRequestedTimestamp: 92233720368547760,
ProviderReceivedTimestamp: -92233720368547760,
ProviderDataStreamEstablished: 1,
ProviderIndicatedTime: -123456789,
Feed: common.HexToHash("0x111").String(),
ObservationBenchmarkPrice: 111111,
ObservationBid: 222222,
ObservationAsk: 333333,
ConfigDigest: "0200000000000000000000000000000000000000000000000000000000000000",
Round: 22,
Epoch: 11,
AssetSymbol: "USD/LINK",
ObservationBenchmarkPriceString: "111111",
ObservationBidString: "222222",
ObservationAskString: "333333",
}

expectedMessage, _ := proto.Marshal(&expectedTelemetry)
Expand Down
Loading

0 comments on commit e6118da

Please sign in to comment.