Skip to content

Commit

Permalink
register RPC methods automatically in tests
Browse files Browse the repository at this point in the history
  • Loading branch information
weiihann committed Oct 25, 2024
1 parent e3b5e9b commit c84636c
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 40 deletions.
38 changes: 4 additions & 34 deletions rpc/events_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -262,12 +262,6 @@ func TestSubscribeNewHeads(t *testing.T) {

handler, syncer, server := setupSubscriptionTest(t, ctx)

require.NoError(t, server.RegisterMethods(jsonrpc.Method{
Name: "starknet_subscribeNewHeads",
Params: []jsonrpc.Parameter{{Name: "block", Optional: true}},
Handler: handler.SubscribeNewHeads,
}))

ws := jsonrpc.NewWebsocket(server, utils.NewNopZapLogger())
httpSrv := httptest.NewServer(ws)

Expand Down Expand Up @@ -299,16 +293,6 @@ func TestMultipleSubscribeNewHeadsAndUnsubscribe(t *testing.T) {

handler, syncer, server := setupSubscriptionTest(t, ctx)

require.NoError(t, server.RegisterMethods(jsonrpc.Method{
Name: "starknet_subscribeNewHeads",
Params: []jsonrpc.Parameter{{Name: "block", Optional: true}},
Handler: handler.SubscribeNewHeads,
}, jsonrpc.Method{
Name: "juno_unsubscribe",
Params: []jsonrpc.Parameter{{Name: "id"}},
Handler: handler.Unsubscribe,
}))

ws := jsonrpc.NewWebsocket(server, utils.NewNopZapLogger())
httpSrv := httptest.NewServer(ws)

Expand Down Expand Up @@ -382,12 +366,8 @@ func TestSubscribeNewHeadsHistorical(t *testing.T) {
time.Sleep(50 * time.Millisecond)

server := jsonrpc.NewServer(1, log)

require.NoError(t, server.RegisterMethods(jsonrpc.Method{
Name: "starknet_subscribeNewHeads",
Params: []jsonrpc.Parameter{{Name: "block", Optional: true}},
Handler: handler.SubscribeNewHeads,
}))
methods, _ := handler.Methods()
require.NoError(t, server.RegisterMethods(methods...))

ws := jsonrpc.NewWebsocket(server, log)
httpSrv := httptest.NewServer(ws)
Expand Down Expand Up @@ -429,12 +409,6 @@ func TestSubscriptionReorg(t *testing.T) {

handler, syncer, server := setupSubscriptionTest(t, ctx)

require.NoError(t, server.RegisterMethods(jsonrpc.Method{
Name: "starknet_subscribeNewHeads",
Params: []jsonrpc.Parameter{{Name: "block", Optional: true}},
Handler: handler.SubscribeNewHeads,
}))

ws := jsonrpc.NewWebsocket(server, utils.NewNopZapLogger())
httpSrv := httptest.NewServer(ws)

Expand Down Expand Up @@ -472,12 +446,6 @@ func TestSubscribePendingTxs(t *testing.T) {

handler, syncer, server := setupSubscriptionTest(t, ctx)

require.NoError(t, server.RegisterMethods(jsonrpc.Method{
Name: "starknet_subscribePendingTransactions",
Params: []jsonrpc.Parameter{{Name: "transaction_details", Optional: true}, {Name: "sender_address", Optional: true}},
Handler: handler.SubscribePendingTxs,
}))

ws := jsonrpc.NewWebsocket(server, utils.NewNopZapLogger())
httpSrv := httptest.NewServer(ws)

Expand Down Expand Up @@ -635,6 +603,8 @@ func setupSubscriptionTest(t *testing.T, ctx context.Context) (*rpc.Handler, *fa
time.Sleep(50 * time.Millisecond)

server := jsonrpc.NewServer(1, log)
methods, _ := handler.Methods()
require.NoError(t, server.RegisterMethods(methods...))

return handler, syncer, server
}
Expand Down
11 changes: 5 additions & 6 deletions rpc/handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -330,6 +330,11 @@ func (h *Handler) Methods() ([]jsonrpc.Method, string) { //nolint: funlen
Params: []jsonrpc.Parameter{{Name: "block", Optional: true}},
Handler: h.SubscribeNewHeads,
},
{
Name: "starknet_subscribePendingTransactions",
Params: []jsonrpc.Parameter{{Name: "transaction_details", Optional: true}, {Name: "sender_address", Optional: true}},
Handler: h.SubscribePendingTxs,
},
{
Name: "juno_unsubscribe",
Params: []jsonrpc.Parameter{{Name: "id"}},
Expand Down Expand Up @@ -484,17 +489,11 @@ func (h *Handler) MethodsV0_7() ([]jsonrpc.Method, string) { //nolint: funlen
Name: "starknet_specVersion",
Handler: h.SpecVersionV0_7,
},

{
Name: "starknet_subscribeNewHeads",
Params: []jsonrpc.Parameter{{Name: "block", Optional: true}},
Handler: h.SubscribeNewHeads,
},
{
Name: "starknet_subscribePendingTransactions",
Params: []jsonrpc.Parameter{{Name: "transaction_details", Optional: true}, {Name: "sender_address", Optional: true}},
Handler: h.SubscribePendingTxs,
},
{
Name: "juno_unsubscribe",
Params: []jsonrpc.Parameter{{Name: "id"}},
Expand Down

0 comments on commit c84636c

Please sign in to comment.