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
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
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ RUN apk add make=4.4.1-r2 \
gcc=13.2.1_git20231014-r0 \
build-base=0.5-r3 \
curl=8.9.1-r0 \
git=2.43.4-r0
git=2.43.5-r0
WORKDIR /firefly
RUN chgrp -R 0 /firefly \
&& chmod -R g+rwX /firefly \
Expand Down
7 changes: 5 additions & 2 deletions internal/contracts/operations.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright © 2023 Kaleido, Inc.
// Copyright © 2024 Kaleido, Inc.
//
// SPDX-License-Identifier: Apache-2.0
//
Expand All @@ -17,6 +17,7 @@
package contracts

import (
"bytes"
"context"
"encoding/json"

Expand All @@ -38,7 +39,9 @@ type blockchainContractDeployData struct {
func addBlockchainReqInputs(op *core.Operation, req interface{}) (err error) {
var reqJSON []byte
if reqJSON, err = json.Marshal(req); err == nil {
err = json.Unmarshal(reqJSON, &op.Input)
d := json.NewDecoder(bytes.NewReader(reqJSON))
d.UseNumber()
err = d.Decode(&op.Input)
}
return err
}
Expand Down
60 changes: 59 additions & 1 deletion internal/contracts/operations_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright © 2023 Kaleido, Inc.
// Copyright © 2024 Kaleido, Inc.
//
// SPDX-License-Identifier: Apache-2.0
//
Expand All @@ -16,7 +16,9 @@
package contracts

import (
"bytes"
"context"
"encoding/json"
"fmt"
"testing"

Expand All @@ -35,6 +37,21 @@ import (
"github.com/stretchr/testify/mock"
)

const sampleRequestLargeNumberInput = `{
"location": {
"address": "0x1111"
},
"key": "0x123",
"method": {
"name": "set",
"params": null,
"returns": null
},
"input": {
"value": 10000000000000000000000000001
}
}`

func reqWithMessage(msgType core.MessageType) *core.ContractCallRequest {
return &core.ContractCallRequest{
Key: "0x123",
Expand All @@ -60,6 +77,7 @@ func reqWithMessage(msgType core.MessageType) *core.ContractCallRequest {
}

func TestPrepareAndRunBlockchainInvoke(t *testing.T) {

cm := newTestContractManager()

op := &core.Operation{
Expand Down Expand Up @@ -101,6 +119,46 @@ func TestPrepareAndRunBlockchainInvoke(t *testing.T) {
mbi.AssertExpectations(t)
}

func TestPrepareAndRunBlockchainInvokeLargeNumberInput(t *testing.T) {

cm := newTestContractManager()

var req core.ContractCallRequest
d := json.NewDecoder(bytes.NewReader([]byte(sampleRequestLargeNumberInput)))
d.UseNumber()
err := d.Decode(&req)
assert.NoError(t, err)

op := &core.Operation{
Type: core.OpTypeBlockchainInvoke,
ID: fftypes.NewUUID(),
Namespace: "ns1",
}

err = addBlockchainReqInputs(op, req)
assert.NoError(t, err)

mbi := cm.blockchain.(*blockchainmocks.Plugin)
opaqueData := "anything"
mbi.On("ParseInterface", context.Background(), mock.MatchedBy(func(method *fftypes.FFIMethod) bool {
return method.Name == req.Method.Name
}), req.Errors).Return(opaqueData, nil)
mbi.On("InvokeContract", context.Background(), "ns1:"+op.ID.String(), "0x123", mock.MatchedBy(func(loc *fftypes.JSONAny) bool {
return loc.String() == req.Location.String()
}), opaqueData, req.Input, req.Options, (*blockchain.BatchPin)(nil)).Return(false, nil)

po, err := cm.PrepareOperation(context.Background(), op)
assert.NoError(t, err)
assert.Equal(t, &req, po.Data.(txcommon.BlockchainInvokeData).Request)

_, phase, err := cm.RunOperation(context.Background(), po)

assert.Equal(t, core.OpPhasePending, phase)
assert.NoError(t, err)

mbi.AssertExpectations(t)
}

func TestPrepareAndRunBlockchainInvokeRejected(t *testing.T) {
cm := newTestContractManager()

Expand Down
7 changes: 5 additions & 2 deletions internal/txcommon/contract_inputs.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright © 2023 Kaleido, Inc.
// Copyright © 2024 Kaleido, Inc.
//
// SPDX-License-Identifier: Apache-2.0
//
Expand All @@ -17,6 +17,7 @@
package txcommon

import (
"bytes"
"context"
"encoding/json"

Expand All @@ -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)))
d.UseNumber()
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

if err := d.Decode(&req); err != nil {
return nil, i18n.WrapError(ctx, err, i18n.MsgJSONObjectParseFailed, s)
}
return &req, nil
Expand Down
Loading