From 314fb318867160ef1619f869141e424bec169407 Mon Sep 17 00:00:00 2001 From: Ian Davis Date: Wed, 16 Nov 2022 12:16:19 +0000 Subject: [PATCH] Fix signature of EthSubscribe --- api/api_full.go | 2 +- api/mocks/mock_full.go | 2 +- api/proxy_gen.go | 6 +-- documentation/en/api-v1-unstable-methods.md | 4 +- node/impl/full/eth.go | 57 +++++++++------------ 5 files changed, 30 insertions(+), 41 deletions(-) diff --git a/api/api_full.go b/api/api_full.go index 211cb3b59..1f17c627c 100644 --- a/api/api_full.go +++ b/api/api_full.go @@ -824,7 +824,7 @@ type FullNode interface { // - logs: notify new event logs that match a criteria // params contains additional parameters used with the log event type // The client will receive a stream of EthSubscriptionResponse values until EthUnsubscribe is called. - EthSubscribe(ctx context.Context, eventTypes []string, params EthSubscriptionParams) (<-chan EthSubscriptionResponse, error) //perm:write + EthSubscribe(ctx context.Context, eventType string, params *EthSubscriptionParams) (<-chan EthSubscriptionResponse, error) //perm:write // Unsubscribe from a websocket subscription EthUnsubscribe(ctx context.Context, id EthSubscriptionID) (bool, error) //perm:write diff --git a/api/mocks/mock_full.go b/api/mocks/mock_full.go index 7801497f4..2e915d6d6 100644 --- a/api/mocks/mock_full.go +++ b/api/mocks/mock_full.go @@ -1342,7 +1342,7 @@ func (mr *MockFullNodeMockRecorder) EthSendRawTransaction(arg0, arg1 interface{} } // EthSubscribe mocks base method. -func (m *MockFullNode) EthSubscribe(arg0 context.Context, arg1 []string, arg2 api.EthSubscriptionParams) (<-chan api.EthSubscriptionResponse, error) { +func (m *MockFullNode) EthSubscribe(arg0 context.Context, arg1 string, arg2 *api.EthSubscriptionParams) (<-chan api.EthSubscriptionResponse, error) { m.ctrl.T.Helper() ret := m.ctrl.Call(m, "EthSubscribe", arg0, arg1, arg2) ret0, _ := ret[0].(<-chan api.EthSubscriptionResponse) diff --git a/api/proxy_gen.go b/api/proxy_gen.go index 2677c48bb..a7eebe436 100644 --- a/api/proxy_gen.go +++ b/api/proxy_gen.go @@ -275,7 +275,7 @@ type FullNodeStruct struct { EthSendRawTransaction func(p0 context.Context, p1 EthBytes) (EthHash, error) `perm:"read"` - EthSubscribe func(p0 context.Context, p1 []string, p2 EthSubscriptionParams) (<-chan EthSubscriptionResponse, error) `perm:"write"` + EthSubscribe func(p0 context.Context, p1 string, p2 *EthSubscriptionParams) (<-chan EthSubscriptionResponse, error) `perm:"write"` EthUninstallFilter func(p0 context.Context, p1 EthFilterID) (bool, error) `perm:"write"` @@ -2166,14 +2166,14 @@ func (s *FullNodeStub) EthSendRawTransaction(p0 context.Context, p1 EthBytes) (E return *new(EthHash), ErrNotSupported } -func (s *FullNodeStruct) EthSubscribe(p0 context.Context, p1 []string, p2 EthSubscriptionParams) (<-chan EthSubscriptionResponse, error) { +func (s *FullNodeStruct) EthSubscribe(p0 context.Context, p1 string, p2 *EthSubscriptionParams) (<-chan EthSubscriptionResponse, error) { if s.Internal.EthSubscribe == nil { return nil, ErrNotSupported } return s.Internal.EthSubscribe(p0, p1, p2) } -func (s *FullNodeStub) EthSubscribe(p0 context.Context, p1 []string, p2 EthSubscriptionParams) (<-chan EthSubscriptionResponse, error) { +func (s *FullNodeStub) EthSubscribe(p0 context.Context, p1 string, p2 *EthSubscriptionParams) (<-chan EthSubscriptionResponse, error) { return nil, ErrNotSupported } diff --git a/documentation/en/api-v1-unstable-methods.md b/documentation/en/api-v1-unstable-methods.md index fefc502e1..e58106849 100644 --- a/documentation/en/api-v1-unstable-methods.md +++ b/documentation/en/api-v1-unstable-methods.md @@ -2796,9 +2796,7 @@ Perms: write Inputs: ```json [ - [ - "string value" - ], + "string value", { "topics": [ [ diff --git a/node/impl/full/eth.go b/node/impl/full/eth.go index 370b1f745..41fd3e5c3 100644 --- a/node/impl/full/eth.go +++ b/node/impl/full/eth.go @@ -70,7 +70,7 @@ type EthEventAPI interface { EthNewBlockFilter(ctx context.Context) (api.EthFilterID, error) EthNewPendingTransactionFilter(ctx context.Context) (api.EthFilterID, error) EthUninstallFilter(ctx context.Context, id api.EthFilterID) (bool, error) - EthSubscribe(ctx context.Context, eventTypes []string, params api.EthSubscriptionParams) (<-chan api.EthSubscriptionResponse, error) + EthSubscribe(ctx context.Context, eventType string, params *api.EthSubscriptionParams) (<-chan api.EthSubscriptionResponse, error) EthUnsubscribe(ctx context.Context, id api.EthSubscriptionID) (bool, error) } @@ -1135,39 +1135,28 @@ const ( EthSubscribeEventTypeLogs = "logs" ) -func (e *EthEvent) EthSubscribe(ctx context.Context, eventTypes []string, params api.EthSubscriptionParams) (<-chan api.EthSubscriptionResponse, error) { +func (e *EthEvent) EthSubscribe(ctx context.Context, eventType string, params *api.EthSubscriptionParams) (<-chan api.EthSubscriptionResponse, error) { // Note that go-jsonrpc will set the method field of the response to "xrpc.ch.val" but the ethereum api expects the name of the // method to be "eth_subscription". This probably doesn't matter in practice. - // Validate event types and parameters first - for _, et := range eventTypes { - switch et { - case EthSubscribeEventTypeHeads: - case EthSubscribeEventTypeLogs: - default: - return nil, xerrors.Errorf("unsupported event type: %s", et) - - } - } - sub, err := e.SubManager.StartSubscription(ctx) if err != nil { return nil, err } - for _, et := range eventTypes { - switch et { - case EthSubscribeEventTypeHeads: - f, err := e.TipSetFilterManager.Install(ctx) - if err != nil { - // clean up any previous filters added and stop the sub - _, _ = e.EthUnsubscribe(ctx, api.EthSubscriptionID(sub.id)) - return nil, err - } - sub.addFilter(ctx, f) + switch eventType { + case EthSubscribeEventTypeHeads: + f, err := e.TipSetFilterManager.Install(ctx) + if err != nil { + // clean up any previous filters added and stop the sub + _, _ = e.EthUnsubscribe(ctx, api.EthSubscriptionID(sub.id)) + return nil, err + } + sub.addFilter(ctx, f) - case EthSubscribeEventTypeLogs: - keys := map[string][][]byte{} + case EthSubscribeEventTypeLogs: + keys := map[string][][]byte{} + if params != nil { for idx, vals := range params.Topics { // Ethereum topics are emitted using `LOG{0..4}` opcodes resulting in topics1..4 key := fmt.Sprintf("topic%d", idx+1) @@ -1177,15 +1166,17 @@ func (e *EthEvent) EthSubscribe(ctx context.Context, eventTypes []string, params } keys[key] = keyvals } - - f, err := e.EventFilterManager.Install(ctx, -1, -1, cid.Undef, []address.Address{}, keys) - if err != nil { - // clean up any previous filters added and stop the sub - _, _ = e.EthUnsubscribe(ctx, api.EthSubscriptionID(sub.id)) - return nil, err - } - sub.addFilter(ctx, f) } + + f, err := e.EventFilterManager.Install(ctx, -1, -1, cid.Undef, []address.Address{}, keys) + if err != nil { + // clean up any previous filters added and stop the sub + _, _ = e.EthUnsubscribe(ctx, api.EthSubscriptionID(sub.id)) + return nil, err + } + sub.addFilter(ctx, f) + default: + return nil, xerrors.Errorf("unsupported event type: %s", eventType) } return sub.out, nil