From 9718602ce32d25b2d07e9cb9b3bcc7a5110e3ab4 Mon Sep 17 00:00:00 2001 From: Roman Khimov Date: Wed, 11 Jan 2023 11:21:58 +0300 Subject: [PATCH 1/2] rpcsrv: return empty set from findstates when there is no data Fix #2863. --- pkg/services/rpcsrv/server.go | 4 ++-- pkg/services/rpcsrv/server_test.go | 15 ++++++++++++++- 2 files changed, 16 insertions(+), 3 deletions(-) diff --git a/pkg/services/rpcsrv/server.go b/pkg/services/rpcsrv/server.go index bcd89542a..71ed92cf6 100644 --- a/pkg/services/rpcsrv/server.go +++ b/pkg/services/rpcsrv/server.go @@ -1541,8 +1541,8 @@ func (s *Server) findStates(ps params.Params) (interface{}, *neorpc.Error) { } pKey := makeStorageKey(cs.ID, prefix) kvs, err := s.chain.GetStateModule().FindStates(root, pKey, key, count+1) // +1 to define result truncation - if err != nil { - return nil, neorpc.NewInternalServerError(fmt.Sprintf("failed to find historical items: %s", err)) + if err != nil && !errors.Is(err, mpt.ErrNotFound) { + return nil, neorpc.NewInternalServerError(fmt.Sprintf("failed to find state items: %s", err)) } res := result.FindStates{} if len(kvs) == count+1 { diff --git a/pkg/services/rpcsrv/server_test.go b/pkg/services/rpcsrv/server_test.go index a6851a841..7841c4828 100644 --- a/pkg/services/rpcsrv/server_test.go +++ b/pkg/services/rpcsrv/server_test.go @@ -2123,7 +2123,9 @@ func testRPCProtocol(t *testing.T, doRPCCall func(string, string, *testing.T) [] require.NoError(t, json.Unmarshal(rawRes, vp)) require.Equal(t, value, vp.Value) } - checkProof(t, actual.FirstProof, actual.Results[0].Value) + if len(actual.Results) > 0 { + checkProof(t, actual.FirstProof, actual.Results[0].Value) + } if len(actual.Results) > 1 { checkProof(t, actual.LastProof, actual.Results[len(actual.Results)-1].Value) } @@ -2157,6 +2159,17 @@ func testRPCProtocol(t *testing.T, doRPCCall func(string, string, *testing.T) [] Truncated: false, }) }) + t.Run("good: empty prefix, no limit, no data", func(t *testing.T) { + // empty prefix should be considered as no prefix specified. + root, err := e.chain.GetStateModule().GetStateRoot(20) + require.NoError(t, err) + stdHash, _ := e.chain.GetNativeContractScriptHash(nativenames.StdLib) // It has no data. + params := fmt.Sprintf(`"%s", "%s", ""`, root.Root.StringLE(), stdHash.StringLE()) + testFindStates(t, params, root.Root, result.FindStates{ + Results: []result.KeyValue{}, + Truncated: false, + }) + }) t.Run("good: with prefix, no limit", func(t *testing.T) { // pairs for this test where put to the contract storage at block #16 root, err := e.chain.GetStateModule().GetStateRoot(16) From e780037cb4e361fcc9c477cd2a073b4bfb2d5797 Mon Sep 17 00:00:00 2001 From: Roman Khimov Date: Wed, 11 Jan 2023 11:23:47 +0300 Subject: [PATCH 2/2] rpcsrv: use valid stateroot hash in failing tests Just in case. --- pkg/services/rpcsrv/server_test.go | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/pkg/services/rpcsrv/server_test.go b/pkg/services/rpcsrv/server_test.go index 7841c4828..08d636d07 100644 --- a/pkg/services/rpcsrv/server_test.go +++ b/pkg/services/rpcsrv/server_test.go @@ -472,22 +472,22 @@ var rpcTestCases = map[string][]rpcTestCase{ }, { name: "invalid contract", - params: `["0000000000000000000000000000000000000000000000000000000000000000", "0xabcdef"]`, + params: `["` + block20StateRootLE + `", "0xabcdef"]`, fail: true, }, { name: "invalid prefix", - params: `["0000000000000000000000000000000000000000000000000000000000000000", "` + testContractHash + `", "notabase64%"]`, + params: `["` + block20StateRootLE + `", "` + testContractHash + `", "notabase64%"]`, fail: true, }, { name: "invalid key", - params: `["0000000000000000000000000000000000000000000000000000000000000000", "` + testContractHash + `", "QQ==", "notabase64%"]`, + params: `["` + block20StateRootLE + `", "` + testContractHash + `", "QQ==", "notabase64%"]`, fail: true, }, { name: "unknown contract/large count", - params: `["0000000000000000000000000000000000000000000000000000000000000000", "0000000000000000000000000000000000000000", "QQ==", "QQ==", 101]`, + params: `["` + block20StateRootLE + `", "0000000000000000000000000000000000000000", "QQ==", "QQ==", 101]`, fail: true, }, },