From 1a3a494459f62a30df112155c4f56ff8fa9dda46 Mon Sep 17 00:00:00 2001 From: Evgenii Stratonikov Date: Thu, 14 Mar 2024 21:31:32 +0300 Subject: [PATCH] rpcclient: return FaultException in checkResOk if any FaultException can be non-empty even in Halt state when there were problems with stack marshaling to JSON. Signed-off-by: Evgenii Stratonikov --- pkg/rpcclient/unwrap/unwrap.go | 3 +++ pkg/rpcclient/unwrap/unwrap_test.go | 23 +++++++++++++++++++++++ 2 files changed, 26 insertions(+) diff --git a/pkg/rpcclient/unwrap/unwrap.go b/pkg/rpcclient/unwrap/unwrap.go index 7276adc39..1e2d7286f 100644 --- a/pkg/rpcclient/unwrap/unwrap.go +++ b/pkg/rpcclient/unwrap/unwrap.go @@ -401,6 +401,9 @@ func checkResOK(r *result.Invoke, err error) error { if r.State != vmstate.Halt.String() { return fmt.Errorf("invocation failed: %s", r.FaultException) } + if r.FaultException != "" { + return fmt.Errorf("inconsistent result, HALTed with exception: %s", r.FaultException) + } return nil } diff --git a/pkg/rpcclient/unwrap/unwrap_test.go b/pkg/rpcclient/unwrap/unwrap_test.go index 2014d4db1..51d1f451a 100644 --- a/pkg/rpcclient/unwrap/unwrap_test.go +++ b/pkg/rpcclient/unwrap/unwrap_test.go @@ -1,12 +1,14 @@ package unwrap import ( + "encoding/json" "errors" "math" "math/big" "testing" "github.com/google/uuid" + "github.com/nspcc-dev/neo-go/pkg/core/state" "github.com/nspcc-dev/neo-go/pkg/crypto/keys" "github.com/nspcc-dev/neo-go/pkg/neorpc/result" "github.com/nspcc-dev/neo-go/pkg/util" @@ -190,6 +192,27 @@ func TestBytes(t *testing.T) { require.Equal(t, []byte{1, 2, 3}, b) } +func TestItemJSONError(t *testing.T) { + bigValidSlice := stackitem.NewByteArray(make([]byte, stackitem.MaxSize-1)) + res := &result.Invoke{ + State: "HALT", + GasConsumed: 237626000, + Script: []byte{10}, + Stack: []stackitem.Item{bigValidSlice, bigValidSlice}, + FaultException: "", + Notifications: []state.NotificationEvent{}, + } + data, err := json.Marshal(res) + require.NoError(t, err) + + var received result.Invoke + require.NoError(t, json.Unmarshal(data, &received)) + + _, err = Item(&received, nil) + require.True(t, len(received.FaultException) != 0) + require.Contains(t, err.Error(), received.FaultException) +} + func TestUTF8String(t *testing.T) { _, err := UTF8String(&result.Invoke{State: "HALT", Stack: []stackitem.Item{stackitem.Make([]stackitem.Item{})}}, nil) require.Error(t, err)