fix(utils): accept json.Number in ToDecimal#22460
Open
ozpool wants to merge 1 commit into
Open
Conversation
The pipeline parses HTTP JSON payloads with Decoder.UseNumber(), which
returns numeric values as json.Number rather than float64. ToDecimal's
type switch did not include a json.Number case, so any downstream task
that fed a HTTP-derived number into it (ethabiencode with a uint256[]
argument, fluxmonitor, OCR data_source, the OCR telemetry path)
failed with:
type json.Number cannot be converted to decimal.Decimal
Add a case that converts via decimal.NewFromString(string(v)) so both
integer and floating point JSON numbers round-trip cleanly. Bad input
still surfaces as a parse error rather than the previous misleading
type-mismatch message.
Adds regression coverage in core/utils/decimal_test.go for the integer,
floating point, and malformed json.Number forms. The integer/float
cases fail on develop without this patch.
Fixes smartcontractkit#8504
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
utils.ToDecimalis used wherever the node has to coerce a pipeline value into adecimal.Decimal:pipeline/task_params.go,pipeline/common_eth.go,ocrcommon/data_source.go,ocrcommon/telemetry.go,fluxmonitorv2/flux_monitor.go, etc.The pipeline JSON decoder uses
Decoder.UseNumber()(seecore/services/pipeline/getters.go:132andtask.jsonparse.go:64), so any value coming from an HTTP JSON payload arrives downstream asjson.Number, notfloat64.ToDecimal's type switch handledstring, allint*/uint*,float32/float64,big.Int,decimal.Decimal- but notjson.Number. Result: every task that pulled a numeric value out of an HTTP response and tried to turn it into a decimal blew up with:The user-visible case in #8504 is
ethabiencodewith auint256[]argument, butfluxmonitor, OCRdata_source, and OCRtelemetryall sit on the same code path.Fix
Add a
json.Numbercase at the top of the type switch inToDecimal:json.Numberis astringalias under the hood, sodecimal.NewFromStringparses integer and floating point forms identically and surfaces bad input as a regular parse error instead of the previous misleading type-mismatch message.Test
core/utils/decimal_test.gois extended with three cases:json.Number("123")succeedsjson.Number("-1.1")succeedsjson.Number("not-a-number")returns an errorBoth successful cases fail on
developwithout this patch.Fixes #8504