-
Notifications
You must be signed in to change notification settings - Fork 24
/
cfx_api_pubsub.go
236 lines (185 loc) · 6.97 KB
/
cfx_api_pubsub.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
package rpc
import (
"context"
"github.com/Conflux-Chain/confura/node"
"github.com/Conflux-Chain/confura/util/metrics"
rpcutil "github.com/Conflux-Chain/confura/util/rpc"
sdk "github.com/Conflux-Chain/go-conflux-sdk"
"github.com/Conflux-Chain/go-conflux-sdk/types"
"github.com/openweb3/go-rpc-provider"
"github.com/pkg/errors"
"github.com/sirupsen/logrus"
)
// PubSub notification
// TODO: restrict total sessions and sessions per IP, otherwise it maybe susceptible
// to flooding attack.
// NewHeads send a notification each time a new header (block) is appended to the chain.
func (api *cfxAPI) NewHeads(ctx context.Context) (*rpc.Subscription, error) {
psCtx, supported, err := api.pubsubCtxFromContext(ctx)
if !supported {
logrus.WithError(err).Error("NewHeads pubsub notification unsupported")
return &rpc.Subscription{}, rpc.ErrNotificationsUnsupported
}
if err != nil {
logrus.WithError(err).Error("NewHeads pubsub context error")
return &rpc.Subscription{}, errSubscriptionProxyError
}
rpcSub := psCtx.notifier.CreateSubscription()
headersCh := make(chan *types.BlockHeader, pubsubChannelBufferSize)
dClient := getOrNewDelegateClient(psCtx.cfx)
dSub, err := dClient.delegateSubscribeNewHeads(rpcSub.ID, headersCh)
if err != nil {
logrus.WithError(err).Error("Failed to delegate pubsub NewHeads")
return &rpc.Subscription{}, errSubscriptionProxyError
}
logger := logrus.WithField("rpcSubID", rpcSub.ID)
nodeName := rpcutil.Url2NodeName(psCtx.cfx.GetNodeURL())
counter := metrics.Registry.PubSub.Sessions("cfx", "new_heads", nodeName)
counter.Inc(1)
go func() {
defer dSub.unsubscribe()
defer counter.Dec(1)
for {
select {
case blockHeader := <-headersCh:
logger.WithField("blockHeader", blockHeader).Debug("Received new block header from pubsub delegate")
psCtx.notifier.Notify(rpcSub.ID, blockHeader)
case err = <-dSub.err: // delegate subscription error
logger.WithError(err).Debug("Received error from newHeads pubsub delegate")
psCtx.rpcClient.Close()
return
case err = <-rpcSub.Err():
logger.WithError(err).Debug("NewHeads pubsub subscription error")
return
case <-psCtx.notifier.Closed():
logger.Debug("NewHeads pubsub connection closed")
return
}
}
}()
return rpcSub, nil
}
// Epochs send a notification each time a new epoch is appended to the chain.
func (api *cfxAPI) Epochs(ctx context.Context, subEpoch *types.Epoch) (*rpc.Subscription, error) {
if subEpoch == nil {
subEpoch = types.EpochLatestMined
}
if !subEpoch.Equals(types.EpochLatestMined) && !subEpoch.Equals(types.EpochLatestState) {
return &rpc.Subscription{}, rpc.ErrNotificationsUnsupported
}
psCtx, supported, err := api.pubsubCtxFromContext(ctx)
if !supported {
logrus.WithError(err).Errorf("Epochs pubsub notification unsupported (%v)", subEpoch)
return &rpc.Subscription{}, rpc.ErrNotificationsUnsupported
}
if err != nil {
logrus.WithError(err).Errorf("Epochs pubsub context error (%v)", subEpoch)
return &rpc.Subscription{}, errSubscriptionProxyError
}
rpcSub := psCtx.notifier.CreateSubscription()
epochsCh := make(chan *types.WebsocketEpochResponse, pubsubChannelBufferSize)
dClient := getOrNewDelegateClient(psCtx.cfx)
dSub, err := dClient.delegateSubscribeEpochs(rpcSub.ID, epochsCh, *subEpoch)
if err != nil {
logrus.WithError(err).Errorf("Failed to delegate pubsub epochs subscription (%v)", subEpoch)
return &rpc.Subscription{}, errSubscriptionProxyError
}
logger := logrus.WithField("rpcSubID", rpcSub.ID)
nodeName := rpcutil.Url2NodeName(psCtx.cfx.GetNodeURL())
counter := metrics.Registry.PubSub.Sessions("cfx", "epochs", nodeName)
counter.Inc(1)
go func() {
defer dSub.unsubscribe()
defer counter.Dec(1)
for {
select {
case epoch := <-epochsCh:
logger.WithField("epoch", epoch).Debugf("Received new epoch from pubsub delegate (%v)", subEpoch)
psCtx.notifier.Notify(rpcSub.ID, epoch)
case err = <-dSub.err: // delegate subscription error
logger.WithError(err).Debugf("Received error from epochs pubsub delegate (%v)", subEpoch)
psCtx.rpcClient.Close()
return
case err = <-rpcSub.Err():
logger.WithError(err).Debugf("Epochs pubsub subscription error (%v)", subEpoch)
return
case <-psCtx.notifier.Closed():
logger.Debugf("Epochs pubsub connection closed (%v)", subEpoch)
return
}
}
}()
return rpcSub, nil
}
// Logs creates a subscription that fires for all new log that match the given filter criteria.
func (api *cfxAPI) Logs(ctx context.Context, filter types.LogFilter) (*rpc.Subscription, error) {
metrics.Registry.PubSub.InputLogFilter("cfx").Mark(!isEmptyLogFilter(filter))
psCtx, supported, err := api.pubsubCtxFromContext(ctx)
if !supported {
logrus.WithError(err).Error("Logs pubsub notification unsupported")
return &rpc.Subscription{}, rpc.ErrNotificationsUnsupported
}
if err != nil {
logrus.WithError(err).Error("Logs pubsub context error")
return &rpc.Subscription{}, errSubscriptionProxyError
}
rpcSub := psCtx.notifier.CreateSubscription()
logsCh := make(chan *types.SubscriptionLog, pubsubChannelBufferSize)
dClient := getOrNewDelegateClient(psCtx.cfx)
dSub, err := dClient.delegateSubscribeLogs(rpcSub.ID, logsCh, filter)
if err != nil {
logrus.WithField("filter", filter).WithError(err).Error("Failed to delegate pubsub logs subscription")
return &rpc.Subscription{}, errSubscriptionProxyError
}
logger := logrus.WithField("rpcSubID", rpcSub.ID)
nodeName := rpcutil.Url2NodeName(psCtx.cfx.GetNodeURL())
counter := metrics.Registry.PubSub.Sessions("cfx", "logs", nodeName)
counter.Inc(1)
go func() {
defer dSub.unsubscribe()
defer counter.Dec(1)
for {
select {
case log := <-logsCh:
logger.WithField("log", log).Debug("Received new log from pubsub delegate")
psCtx.notifier.Notify(rpcSub.ID, log)
case err = <-dSub.err: // delegate subscription error
logger.WithError(err).Debug("Received error from logs pubsub delegate")
psCtx.rpcClient.Close()
return
case err = <-rpcSub.Err():
logger.WithError(err).Debugf("Logs pubsub subscription error")
return
case <-psCtx.notifier.Closed():
logger.Debugf("Logs pubsub connection closed")
return
}
}
}()
return rpcSub, nil
}
type pubsubContext struct {
notifier *rpc.Notifier
rpcClient *rpc.Client
cfx sdk.ClientOperator
}
// pubsubCtxFromContext returns the pubsub context with member variables stored in ctx, if any.
func (api *cfxAPI) pubsubCtxFromContext(ctx context.Context) (psCtx *pubsubContext, supported bool, err error) {
notifier, supported := rpc.NotifierFromContext(ctx)
if !supported {
err = errors.New("failed to get notifier from context")
return
}
rpcClient, supported := rpcClientFromContext(ctx)
if !supported {
err = errors.New("failed to get rpc client from context")
return
}
cfx, err := api.provider.GetClientByIP(ctx, node.GroupCfxWs)
if err != nil {
err = errors.WithMessage(err, "failed to get cfx wsclient by ip")
return
}
psCtx = &pubsubContext{notifier, rpcClient, cfx}
return
}