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 large numbers when unmarshalling to an operation #1581

Merged
merged 3 commits into from
Sep 17, 2024

Conversation

matthew1001
Copy link
Contributor

@matthew1001 matthew1001 commented Sep 17, 2024

Relates to #1568

When unmarshalling contract invoke payloads to a Firefly operation, unmarshal numbers to json.Number instead of float64. This avoids unexpected (and silent) precision loss if a large number is included in the input payload.

I've tested this locally in a FF stack:

  1. Submit a curl request to invoke a contract's setter function with a very large number:
curl -X 'POST' \
  'http://127.0.0.1:5000/api/v1/namespaces/default/apis/contract1/invoke/store?confirm=true' \
  -H 'accept: application/json' \
  -H 'Request-Timeout: 2m0s' \
  -H 'Content-Type: application/json' \
  -d '{
  "input": {
    "newMin": 1000000000000000000004
  }
}'
{"id":
  1. The DB insert of the FF operation has the correct value:
[2024-09-17T10:51:51.702Z] TRACE SQL-> insert query: INSERT INTO operations (id,namespace,tx_id,optype,opstatus,plugin,created,updated,error,input,output,retry_id) VALUES ($1,$2,$3,$4,$5,$6,$7,$8,$9,$10,$11,$12) (args: [f1aa8675-5018-446c-959e-06d4a7844e0a default 32c056ae-aa63-4042-8bea-9adfdd8982c3 blockchain_invoke Initialized ethereum 2024-09-17T10:51:51.697166508Z 2024-09-17T10:51:51.697166508Z  {"input":{"newMin":1000000000000000000004},"interface":"1875b0b2-e554-4155-9d4b-4bffcde877ad"
  1. The input of the FF response matches the request:
{..."type":"blockchain_invoke","status":"Succeeded","plugin":"ethereum","input":{"input":{"newMin":1000000000000000000004},"interface":"1875b0b2-e554-4155-9d4b-4bffcde877ad","key"...}
  1. The event from the blockchain comes back correctly:
[2024-09-17T10:51:53.037Z] TRACE WS ws://evmconnect_0:5008/ws read (mt=1): {"batchNumber":9,"events":[{"address":"0xcca8847223ac774e88686f84167060e501c78d0d","blockHash":"0x1ac1955c279c4f94daffcacba235cdeeb05d34ff6d6d958f83c519c5bd538bdd","blockNumber":"47","data":{"newValue":"1000000000000000000004"},"listenerId":"0191eb17-1651-4eb5-1e0b-0072d0621670"

Signed-off-by: Matthew Whitehead <matthew1001@gmail.com>
@matthew1001 matthew1001 requested a review from a team as a code owner September 17, 2024 10:23
Signed-off-by: Matthew Whitehead <matthew1001@gmail.com>
Signed-off-by: Matthew Whitehead <matthew1001@gmail.com>
Copy link

codecov bot commented Sep 17, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 99.75%. Comparing base (6fe5adf) to head (3396044).
Report is 13 commits behind head on main.

Additional details and impacted files
@@           Coverage Diff           @@
##             main    #1581   +/-   ##
=======================================
  Coverage   99.75%   99.75%           
=======================================
  Files         338      338           
  Lines       24223    24227    +4     
=======================================
+ Hits        24164    24168    +4     
  Misses         58       58           
  Partials        1        1           

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

Copy link
Contributor

@EnriqueL8 EnriqueL8 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good to me

@@ -39,7 +40,9 @@ type BlockchainInvokeData struct {
func RetrieveBlockchainInvokeInputs(ctx context.Context, op *core.Operation) (*core.ContractCallRequest, error) {
var req core.ContractCallRequest
s := op.Input.String()
if err := json.Unmarshal([]byte(s), &req); err != nil {
d := json.NewDecoder(bytes.NewReader([]byte(s)))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Chatted that in the future we could have an ffjson in FireFly Common that handles this and we can use it in all place where we unmarshall/decode some payload

@EnriqueL8 EnriqueL8 merged commit 7ee8782 into hyperledger:main Sep 17, 2024
17 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants