-
Notifications
You must be signed in to change notification settings - Fork 24
/
eth_pubsub.go
144 lines (119 loc) · 4.09 KB
/
eth_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
package rpc
import (
"github.com/Conflux-Chain/confura/node"
"github.com/Conflux-Chain/confura/util"
rpcutil "github.com/Conflux-Chain/confura/util/rpc"
"github.com/openweb3/go-rpc-provider"
"github.com/openweb3/web3go/types"
"github.com/sirupsen/logrus"
)
// ethDelegateClient eth client delegated for pubsub subscription
type ethDelegateClient struct {
*node.Web3goClient
delegateContexts util.ConcurrentMap // context name => *delegateContext
}
func getOrNewEthDelegateClient(eth *node.Web3goClient) *ethDelegateClient {
nodeName := rpcutil.Url2NodeName(eth.URL)
client, _ := delegateClients.LoadOrStore(nodeName, ðDelegateClient{Web3goClient: eth})
return client.(*ethDelegateClient)
}
func (client *ethDelegateClient) getDelegateCtx(ctxName string) *delegateContext {
dctx, _ := client.delegateContexts.LoadOrStore(ctxName, newDelegateContext())
return dctx.(*delegateContext)
}
func (client *ethDelegateClient) delegateSubscribeNewHeads(
subId rpc.ID, channel chan *types.Header) (*delegateSubscription, error) {
dctx := client.getDelegateCtx(nhCtxName)
if dctx.getStatus() == delegateStatusErr {
return nil, errDelegateNotReady
}
return dctx.registerDelegateSub(client.proxySubscribeNewHeads, subId, channel)
}
func (client *ethDelegateClient) proxySubscribeNewHeads(dctx *delegateContext) error {
nhCh := make(chan *types.Header, pubsubChannelBufferSize)
csub, err := client.Eth.SubscribeNewHead(nhCh)
if err != nil {
logrus.WithField("nodeURL", client.URL).
WithError(err).
Info("ETH Pub/Sub NewHead proxy subscription conn error")
return err
}
go func() { // run subscription loop
dctx.setStatus(delegateStatusOK)
defer dctx.setStatus(delegateStatusInit)
for dctx.getStatus() == delegateStatusOK {
select {
case err = <-csub.Err():
logrus.WithField("nodeURL", client.URL).
WithError(err).
Info("ETH Pub/Sub NewHeads proxy subscription delegate error")
dctx.setStatus(delegateStatusErr)
csub.Unsubscribe()
dctx.cancel(err)
case h := <-nhCh: // notify all delegated subscriptions
dctx.notify(h)
}
}
}()
logrus.WithField("nodeURL", client.URL).
Info("Eth Pub/Sub NewHeads proxy subscription run loop started")
return nil
}
func (client *ethDelegateClient) delegateSubscribeLogs(
subId rpc.ID, channel chan *types.Log, filter types.FilterQuery) (*delegateSubscription, error) {
dCtx := client.getDelegateCtx(logsCtxName)
if dCtx.getStatus() == delegateStatusErr {
return nil, errDelegateNotReady
}
return dCtx.registerDelegateSub(client.proxySubscribeLogs, subId, channel, func(item interface{}) bool {
log, ok := item.(*types.Log)
return !ok || !matchEthPubSubLogFilter(log, &filter)
})
}
func (client *ethDelegateClient) proxySubscribeLogs(dctx *delegateContext) error {
logsCh := make(chan types.Log, pubsubChannelBufferSize)
csub, err := client.Eth.SubscribeFilterLogs(types.FilterQuery{}, logsCh)
if err != nil {
logrus.WithField("nodeURL", client.URL).
WithError(err).
Info("ETH Pub/Sub Logs proxy subscription conn error")
return err
}
go func() { // run subscription loop
dctx.setStatus(delegateStatusOK)
defer dctx.setStatus(delegateStatusInit)
for dctx.getStatus() == delegateStatusOK {
select {
case err = <-csub.Err():
logrus.WithField("nodeURL", client.URL).
WithError(err).
Info("ETH Pub/Sub Logs delegate subscription delegate error")
dctx.setStatus(delegateStatusErr)
csub.Unsubscribe()
dctx.cancel(err)
case l := <-logsCh: // notify all delegated subscriptions
dctx.notify(&l)
}
}
}()
logrus.WithField("nodeURL", client.URL).
Info("Eth Pub/Sub Logs proxy subscription run loop started")
return nil
}
func matchEthPubSubLogFilter(log *types.Log, filter *types.FilterQuery) bool {
if len(filter.Addresses) == 0 && len(filter.Topics) == 0 {
return true
}
return util.IncludeEthLogAddrs(log, filter.Addresses) && util.MatchEthLogTopics(log, filter.Topics)
}
func isEmptyEthLogFilter(filter types.FilterQuery) bool {
if len(filter.Addresses) > 0 {
return false
}
for i := range filter.Topics {
if len(filter.Topics[i]) > 0 {
return false
}
}
return true
}