From 97a57de82d4b5d0bcbf8d91754982faeed80ecaf Mon Sep 17 00:00:00 2001 From: Anna Shaleva Date: Tue, 29 Aug 2023 12:32:00 +0300 Subject: [PATCH 1/5] rpcsrv: set MaxIteratorResultItems to default if not specified Do not use RPC configuration constructor for this, some external services may skip this part. Signed-off-by: Anna Shaleva --- pkg/config/config.go | 4 ++-- pkg/services/rpcsrv/server.go | 4 ++++ pkg/services/rpcsrv/server_test.go | 4 ++-- 3 files changed, 8 insertions(+), 4 deletions(-) diff --git a/pkg/config/config.go b/pkg/config/config.go index ede5b4102..ed701d8ac 100644 --- a/pkg/config/config.go +++ b/pkg/config/config.go @@ -17,7 +17,8 @@ const ( // UserAgentFormat is a formatted string used to generate user agent string. UserAgentFormat = UserAgentWrapper + UserAgentPrefix + "%s" + UserAgentWrapper // DefaultMaxIteratorResultItems is the default upper bound of traversed - // iterator items per JSON-RPC response. + // iterator items per JSON-RPC response. It covers both session-based and + // naive iterators. DefaultMaxIteratorResultItems = 100 // DefaultMaxFindStorageResultItems is the default maximum number of resulting // contract storage items that can be retrieved by `findstorge` JSON-RPC handler. @@ -74,7 +75,6 @@ func LoadFile(configPath string) (Config, error) { PingTimeout: 90 * time.Second, }, RPC: RPC{ - MaxIteratorResultItems: DefaultMaxIteratorResultItems, MaxFindResultItems: 100, MaxFindStorageResultItems: DefaultMaxFindStorageResultItems, MaxNEP11Tokens: 100, diff --git a/pkg/services/rpcsrv/server.go b/pkg/services/rpcsrv/server.go index a8bd13c45..9c588c3de 100644 --- a/pkg/services/rpcsrv/server.go +++ b/pkg/services/rpcsrv/server.go @@ -295,6 +295,10 @@ func New(chain Ledger, conf config.RPC, coreServer *network.Server, log.Info("SessionPoolSize is not set or wrong, setting default value", zap.Int("SessionPoolSize", defaultSessionPoolSize)) } } + if conf.MaxIteratorResultItems <= 0 { + conf.MaxIteratorResultItems = config.DefaultMaxIteratorResultItems + log.Info("MaxIteratorResultItems is not set or wrong, setting default value", zap.Int("MaxIteratorResultItems", config.DefaultMaxIteratorResultItems)) + } if conf.MaxWebSocketClients == 0 { conf.MaxWebSocketClients = defaultMaxWebSocketClients log.Info("MaxWebSocketClients is not set or wrong, setting default value", zap.Int("MaxWebSocketClients", defaultMaxWebSocketClients)) diff --git a/pkg/services/rpcsrv/server_test.go b/pkg/services/rpcsrv/server_test.go index b85640d25..81c542e5c 100644 --- a/pkg/services/rpcsrv/server_test.go +++ b/pkg/services/rpcsrv/server_test.go @@ -2973,9 +2973,9 @@ func testRPCProtocol(t *testing.T, doRPCCall func(string, string, *testing.T) [] }) t.Run("count is out of range", func(t *testing.T) { sID, iID := prepareIteratorSession(t) - rpc := fmt.Sprintf(`{"jsonrpc": "2.0", "id": 1, "method": "traverseiterator", "params": ["%s", "%s", %d]}"`, sID.String(), iID.String(), rpcSrv.config.MaxIteratorResultItems+1) + rpc := fmt.Sprintf(`{"jsonrpc": "2.0", "id": 1, "method": "traverseiterator", "params": ["%s", "%s", %d]}"`, sID.String(), iID.String(), config.DefaultMaxIteratorResultItems+1) body := doRPCCall(rpc, httpSrv.URL, t) - checkErrGetResult(t, body, true, neorpc.InvalidParamsCode, fmt.Sprintf("iterator items count is out of range (%d at max)", rpcSrv.config.MaxIteratorResultItems)) + checkErrGetResult(t, body, true, neorpc.InvalidParamsCode, fmt.Sprintf("iterator items count is out of range (%d at max)", config.DefaultMaxIteratorResultItems)) }) t.Run("unknown session", func(t *testing.T) { _, iID := prepareIteratorSession(t) From aeb7ee102197337742f516158898357908fa0fe9 Mon Sep 17 00:00:00 2001 From: Anna Shaleva Date: Tue, 29 Aug 2023 12:33:24 +0300 Subject: [PATCH 2/5] rpcsrv: improve error formatting Signed-off-by: Anna Shaleva --- pkg/services/rpcsrv/server.go | 2 +- pkg/services/rpcsrv/server_test.go | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/pkg/services/rpcsrv/server.go b/pkg/services/rpcsrv/server.go index 9c588c3de..153458cc7 100644 --- a/pkg/services/rpcsrv/server.go +++ b/pkg/services/rpcsrv/server.go @@ -2487,7 +2487,7 @@ func (s *Server) traverseIterator(reqParams params.Params) (any, *neorpc.Error) return nil, neorpc.NewInvalidParamsError("invalid iterator items count: not an int32") } if count > s.config.MaxIteratorResultItems { - return nil, neorpc.NewInvalidParamsError(fmt.Sprintf("iterator items count is out of range (%d at max)", s.config.MaxIteratorResultItems)) + return nil, neorpc.NewInvalidParamsError(fmt.Sprintf("iterator items count (%d) is out of range (%d at max)", count, s.config.MaxIteratorResultItems)) } s.sessionsLock.Lock() diff --git a/pkg/services/rpcsrv/server_test.go b/pkg/services/rpcsrv/server_test.go index 81c542e5c..6fb1222f4 100644 --- a/pkg/services/rpcsrv/server_test.go +++ b/pkg/services/rpcsrv/server_test.go @@ -2975,7 +2975,7 @@ func testRPCProtocol(t *testing.T, doRPCCall func(string, string, *testing.T) [] sID, iID := prepareIteratorSession(t) rpc := fmt.Sprintf(`{"jsonrpc": "2.0", "id": 1, "method": "traverseiterator", "params": ["%s", "%s", %d]}"`, sID.String(), iID.String(), config.DefaultMaxIteratorResultItems+1) body := doRPCCall(rpc, httpSrv.URL, t) - checkErrGetResult(t, body, true, neorpc.InvalidParamsCode, fmt.Sprintf("iterator items count is out of range (%d at max)", config.DefaultMaxIteratorResultItems)) + checkErrGetResult(t, body, true, neorpc.InvalidParamsCode, fmt.Sprintf("iterator items count (%d) is out of range (%d at max)", config.DefaultMaxIteratorResultItems+1, config.DefaultMaxIteratorResultItems)) }) t.Run("unknown session", func(t *testing.T) { _, iID := prepareIteratorSession(t) From dd7c762ff968b95896994d7fe86d38416f6ce705 Mon Sep 17 00:00:00 2001 From: Anna Shaleva Date: Tue, 29 Aug 2023 12:34:53 +0300 Subject: [PATCH 3/5] rpcsrv: set MaxFindResultItems to default if not specified Do not use RPC configuration constructor for this, some external services may skip this part. Signed-off-by: Anna Shaleva --- pkg/config/config.go | 4 +++- pkg/services/rpcsrv/server.go | 4 ++++ 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/pkg/config/config.go b/pkg/config/config.go index ed701d8ac..47318828c 100644 --- a/pkg/config/config.go +++ b/pkg/config/config.go @@ -20,6 +20,9 @@ const ( // iterator items per JSON-RPC response. It covers both session-based and // naive iterators. DefaultMaxIteratorResultItems = 100 + // DefaultMaxFindResultItems is the default maximum number of resulting + // contract states items that can be retrieved by `findstates` JSON-RPC handler. + DefaultMaxFindResultItems = 100 // DefaultMaxFindStorageResultItems is the default maximum number of resulting // contract storage items that can be retrieved by `findstorge` JSON-RPC handler. DefaultMaxFindStorageResultItems = 50 @@ -75,7 +78,6 @@ func LoadFile(configPath string) (Config, error) { PingTimeout: 90 * time.Second, }, RPC: RPC{ - MaxFindResultItems: 100, MaxFindStorageResultItems: DefaultMaxFindStorageResultItems, MaxNEP11Tokens: 100, }, diff --git a/pkg/services/rpcsrv/server.go b/pkg/services/rpcsrv/server.go index 153458cc7..d1f1a00c5 100644 --- a/pkg/services/rpcsrv/server.go +++ b/pkg/services/rpcsrv/server.go @@ -299,6 +299,10 @@ func New(chain Ledger, conf config.RPC, coreServer *network.Server, conf.MaxIteratorResultItems = config.DefaultMaxIteratorResultItems log.Info("MaxIteratorResultItems is not set or wrong, setting default value", zap.Int("MaxIteratorResultItems", config.DefaultMaxIteratorResultItems)) } + if conf.MaxFindResultItems <= 0 { + conf.MaxFindResultItems = config.DefaultMaxFindResultItems + log.Info("MaxFindResultItems is not set or wrong, setting default value", zap.Int("MaxFindResultItems", config.DefaultMaxFindResultItems)) + } if conf.MaxWebSocketClients == 0 { conf.MaxWebSocketClients = defaultMaxWebSocketClients log.Info("MaxWebSocketClients is not set or wrong, setting default value", zap.Int("MaxWebSocketClients", defaultMaxWebSocketClients)) From f5b0489d742fe37db963f0624235365d07c10a12 Mon Sep 17 00:00:00 2001 From: Anna Shaleva Date: Tue, 29 Aug 2023 12:59:41 +0300 Subject: [PATCH 4/5] rpcsrv: set MaxFindStorageResultItems to default if not specified Do not use RPC configuration constructor for this, some external services may skip this part. Signed-off-by: Anna Shaleva --- pkg/config/config.go | 3 +-- pkg/services/rpcsrv/server.go | 4 ++++ 2 files changed, 5 insertions(+), 2 deletions(-) diff --git a/pkg/config/config.go b/pkg/config/config.go index 47318828c..0accd2710 100644 --- a/pkg/config/config.go +++ b/pkg/config/config.go @@ -78,8 +78,7 @@ func LoadFile(configPath string) (Config, error) { PingTimeout: 90 * time.Second, }, RPC: RPC{ - MaxFindStorageResultItems: DefaultMaxFindStorageResultItems, - MaxNEP11Tokens: 100, + MaxNEP11Tokens: 100, }, }, } diff --git a/pkg/services/rpcsrv/server.go b/pkg/services/rpcsrv/server.go index d1f1a00c5..fa6a4af11 100644 --- a/pkg/services/rpcsrv/server.go +++ b/pkg/services/rpcsrv/server.go @@ -303,6 +303,10 @@ func New(chain Ledger, conf config.RPC, coreServer *network.Server, conf.MaxFindResultItems = config.DefaultMaxFindResultItems log.Info("MaxFindResultItems is not set or wrong, setting default value", zap.Int("MaxFindResultItems", config.DefaultMaxFindResultItems)) } + if conf.MaxFindStorageResultItems <= 0 { + conf.MaxFindStorageResultItems = config.DefaultMaxFindStorageResultItems + log.Info("MaxFindStorageResultItems is not set or wrong, setting default value", zap.Int("MaxFindStorageResultItems", config.DefaultMaxFindStorageResultItems)) + } if conf.MaxWebSocketClients == 0 { conf.MaxWebSocketClients = defaultMaxWebSocketClients log.Info("MaxWebSocketClients is not set or wrong, setting default value", zap.Int("MaxWebSocketClients", defaultMaxWebSocketClients)) From b89078c42a90465494a08bb8ce090a27ed3b6eb1 Mon Sep 17 00:00:00 2001 From: Anna Shaleva Date: Tue, 29 Aug 2023 13:03:52 +0300 Subject: [PATCH 5/5] rpcsrv: set MaxNEP11Tokens to default if not specified Do not use RPC configuration constructor for this, some external services may skip this part. Signed-off-by: Anna Shaleva --- pkg/config/config.go | 6 +++--- pkg/services/rpcsrv/server.go | 4 ++++ 2 files changed, 7 insertions(+), 3 deletions(-) diff --git a/pkg/config/config.go b/pkg/config/config.go index 0accd2710..086f23704 100644 --- a/pkg/config/config.go +++ b/pkg/config/config.go @@ -26,6 +26,9 @@ const ( // DefaultMaxFindStorageResultItems is the default maximum number of resulting // contract storage items that can be retrieved by `findstorge` JSON-RPC handler. DefaultMaxFindStorageResultItems = 50 + // DefaultMaxNEP11Tokens is the default maximum number of resulting NEP11 tokens + // that can be traversed by `getnep11balances` JSON-RPC handler. + DefaultMaxNEP11Tokens = 100 ) // Version is the version of the node, set at the build time. @@ -77,9 +80,6 @@ func LoadFile(configPath string) (Config, error) { PingInterval: 30 * time.Second, PingTimeout: 90 * time.Second, }, - RPC: RPC{ - MaxNEP11Tokens: 100, - }, }, } diff --git a/pkg/services/rpcsrv/server.go b/pkg/services/rpcsrv/server.go index fa6a4af11..4e843bc25 100644 --- a/pkg/services/rpcsrv/server.go +++ b/pkg/services/rpcsrv/server.go @@ -307,6 +307,10 @@ func New(chain Ledger, conf config.RPC, coreServer *network.Server, conf.MaxFindStorageResultItems = config.DefaultMaxFindStorageResultItems log.Info("MaxFindStorageResultItems is not set or wrong, setting default value", zap.Int("MaxFindStorageResultItems", config.DefaultMaxFindStorageResultItems)) } + if conf.MaxNEP11Tokens <= 0 { + conf.MaxNEP11Tokens = config.DefaultMaxNEP11Tokens + log.Info("MaxNEP11Tokens is not set or wrong, setting default value", zap.Int("MaxNEP11Tokens", config.DefaultMaxNEP11Tokens)) + } if conf.MaxWebSocketClients == 0 { conf.MaxWebSocketClients = defaultMaxWebSocketClients log.Info("MaxWebSocketClients is not set or wrong, setting default value", zap.Int("MaxWebSocketClients", defaultMaxWebSocketClients))