Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handle Hex String in EA Telemetry #14827

Merged
merged 24 commits into from
Oct 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/lemon-ads-fix.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"chainlink": patch
---

#added Handle Hex String in EA Telemetry
28 changes: 26 additions & 2 deletions core/services/ocrcommon/telemetry.go
cawthorne marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,10 @@ import (
"encoding/json"
"fmt"
"math/big"
"strings"

"github.com/ethereum/go-ethereum/common"
"github.com/shopspring/decimal"
"github.com/smartcontractkit/libocr/commontypes"
ocrtypes "github.com/smartcontractkit/libocr/offchainreporting2plus/types"
"google.golang.org/protobuf/proto"
Expand Down Expand Up @@ -220,20 +222,42 @@ func parseEATelemetry(b []byte) (EATelemetry, error) {
}

// getJsonParsedValue checks if the next logical task is of type pipeline.TaskTypeJSONParse and trys to return
// the response as a *big.Int
// the response as a *big.Int.
// Currently utils.ToDecimal cannot handle hex strings, so this function also has a special case,
// to check and handle if the result is a hex string, if the call to utils.ToDecimal fails.
// Draft PR to add hex string handling to utils.ToDecimal: https://github.com/smartcontractkit/chainlink/pull/14841
func getJsonParsedValue(trr pipeline.TaskRunResult, trrs *pipeline.TaskRunResults) *float64 {
nextTask := trrs.GetNextTaskOf(trr)
if nextTask != nil && nextTask.Task.Type() == pipeline.TaskTypeJSONParse {
asDecimal, err := utils.ToDecimal(nextTask.Result.Value)
if err != nil {
return nil
v, ok := nextTask.Result.Value.(string)
if !ok {
return nil
}
hexAnswer, success := hexStringToDecimal(v)
if !success {
return nil
}
asDecimal = hexAnswer
}
toFloat, _ := asDecimal.Float64()
return &toFloat
}
return nil
}

// hexStringToDecimal takes in a hex string, and returns (decimal.Decimal, bool), bool indicates success status
func hexStringToDecimal(hexString string) (decimal.Decimal, bool) {
hexString = strings.TrimPrefix(hexString, "0x")
n := new(big.Int)
_, success := n.SetString(hexString, 16)
if !success {
return decimal.Decimal{}, false
}
return decimal.NewFromBigInt(n, 0), true
}

// getObservation checks pipeline.FinalResult and extracts the observation
func (e *EnhancedTelemetryService[T]) getObservation(finalResult *pipeline.FinalResult) int64 {
singularResult, err := finalResult.SingularResult()
Expand Down
94 changes: 94 additions & 0 deletions core/services/ocrcommon/telemetry_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,100 @@ func TestGetJsonParsedValue(t *testing.T) {
assert.Nil(t, resp)
}

func TestGetJsonParsedValueHexValues(t *testing.T) {
trrsHexData := pipeline.TaskRunResults{
pipeline.TaskRunResult{
Task: &pipeline.BridgeTask{
Name: "test-bridge-1",
BaseTask: pipeline.NewBaseTask(0, "ds1", nil, nil, 0),
},
Result: pipeline.Result{
Value: bridgeResponse,
},
},
pipeline.TaskRunResult{
Task: &pipeline.JSONParseTask{
BaseTask: pipeline.NewBaseTask(1, "ds1_parse", nil, nil, 1),
},
Result: pipeline.Result{
Value: "0x1abcf",
},
},
}

resp := getJsonParsedValue(trrsHexData[0], &trrsHexData)
assert.InDelta(t, 109519.0, *resp, 0)

trrsHexData = pipeline.TaskRunResults{
pipeline.TaskRunResult{
Task: &pipeline.BridgeTask{
Name: "test-bridge-2",
BaseTask: pipeline.NewBaseTask(0, "ds2", nil, nil, 0),
},
Result: pipeline.Result{
Value: bridgeResponse,
},
},
pipeline.TaskRunResult{
Task: &pipeline.JSONParseTask{
BaseTask: pipeline.NewBaseTask(1, "ds2_parse", nil, nil, 1),
},
Result: pipeline.Result{
Value: "1abcf",
},
},
}

resp = getJsonParsedValue(trrsHexData[0], &trrsHexData)
assert.InDelta(t, 109519.0, *resp, 0)

trrsHexData = pipeline.TaskRunResults{
pipeline.TaskRunResult{
Task: &pipeline.BridgeTask{
Name: "test-bridge-3",
BaseTask: pipeline.NewBaseTask(0, "ds3", nil, nil, 0),
},
Result: pipeline.Result{
Value: bridgeResponse,
},
},
pipeline.TaskRunResult{
Task: &pipeline.JSONParseTask{
BaseTask: pipeline.NewBaseTask(1, "ds3_parse", nil, nil, 1),
},
Result: pipeline.Result{
Value: "0x1akbcf",
},
},
}

resp = getJsonParsedValue(trrsHexData[0], &trrsHexData)
assert.Nil(t, resp)

trrsHexData = pipeline.TaskRunResults{
pipeline.TaskRunResult{
Task: &pipeline.BridgeTask{
Name: "test-bridge-4",
BaseTask: pipeline.NewBaseTask(0, "ds4", nil, nil, 0),
},
Result: pipeline.Result{
Value: bridgeResponse,
},
},
pipeline.TaskRunResult{
Task: &pipeline.JSONParseTask{
BaseTask: pipeline.NewBaseTask(1, "ds4_parse", nil, nil, 1),
},
Result: pipeline.Result{
Value: "1akbcf",
},
},
}

resp = getJsonParsedValue(trrsHexData[0], &trrsHexData)
assert.Nil(t, resp)
}

func TestSendEATelemetry(t *testing.T) {
wg := sync.WaitGroup{}
ingressClient := mocks.NewTelemetryService(t)
Expand Down
3 changes: 3 additions & 0 deletions core/utils/decimal.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ import (
)

// ToDecimal converts an input to a decimal
// TODO: Currently this function does not handle hex string as the input.
// Draft PR to add hex string handling to utils.ToDecimal (review if this breaks other services):
// https://github.com/smartcontractkit/chainlink/pull/14841
func ToDecimal(input interface{}) (decimal.Decimal, error) {
switch v := input.(type) {
case string:
Expand Down
Loading