-
Notifications
You must be signed in to change notification settings - Fork 24
/
handler.go
221 lines (208 loc) · 6.42 KB
/
handler.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
package sse
import (
"fmt"
"net/http"
"strconv"
"strings"
"sync/atomic"
"time"
"github.com/tonkeeper/opentonapi/pkg/pusher/errors"
"github.com/tonkeeper/opentonapi/pkg/pusher/events"
"github.com/tonkeeper/opentonapi/pkg/pusher/sources"
"github.com/tonkeeper/tongo"
)
// Handler handles http methods for sse.
type Handler struct {
txSource sources.TransactionSource
blockSource sources.BlockSource
blockHeadersSource sources.BlockHeadersSource
traceSource sources.TraceSource
memPool sources.MemPoolSource
currentEventID int64
}
type handlerFunc func(session *session, request *http.Request) error
func NewHandler(blockSource sources.BlockSource, blockHeadersSource sources.BlockHeadersSource, txSource sources.TransactionSource, traceSource sources.TraceSource, memPool sources.MemPoolSource) *Handler {
h := Handler{
txSource: txSource,
blockSource: blockSource,
blockHeadersSource: blockHeadersSource,
traceSource: traceSource,
memPool: memPool,
currentEventID: time.Now().UnixNano(),
}
return &h
}
func parseQueryStrings(accountsStr string, operationsStr string) (*sources.SubscribeToTransactionsOptions, error) {
allAccounts := false
var accounts []tongo.AccountID
if strings.ToUpper(accountsStr) == "ALL" {
allAccounts = true
} else {
accountStrings := strings.Split(accountsStr, ",")
accounts = make([]tongo.AccountID, 0, len(accountStrings))
for _, account := range accountStrings {
accountID, err := tongo.ParseAddress(account)
if err != nil {
return nil, err
}
accounts = append(accounts, accountID.ID)
}
}
allOps := len(operationsStr) == 0
var operations []string
if len(operationsStr) > 0 {
operations = strings.Split(operationsStr, ",")
}
options := sources.SubscribeToTransactionsOptions{
Accounts: accounts,
AllAccounts: allAccounts,
Operations: operations,
AllOperations: allOps,
}
return &options, nil
}
func (h *Handler) SubscribeToTransactions(session *session, request *http.Request) error {
if h.txSource == nil {
return errors.BadRequest("trace source is not configured")
}
query := request.URL.Query()
options, err := parseQueryStrings(query.Get("accounts"), query.Get("operations"))
if err != nil {
return errors.BadRequest(fmt.Sprintf("failed to parse query parameters: %v", err))
}
cancelFn := h.txSource.SubscribeToTransactions(request.Context(), func(data []byte) {
event := Event{
Name: events.AccountTxEvent,
EventID: h.nextID(),
Data: data,
}
session.SendEvent(event)
}, *options)
session.SetCancelFn(cancelFn)
return nil
}
func (h *Handler) SubscribeToMessages(session *session, request *http.Request) error {
if h.memPool == nil {
return errors.BadRequest("mempool source is not configured")
}
accountsStr := request.URL.Query().Get("accounts")
var accounts []tongo.AccountID
if len(accountsStr) > 0 {
accountStrings := strings.Split(accountsStr, ",")
accounts = make([]tongo.AccountID, 0, len(accountStrings))
for _, account := range accountStrings {
accountID, err := tongo.ParseAddress(account)
if err != nil {
return err
}
accounts = append(accounts, accountID.ID)
}
}
cancelFn, err := h.memPool.SubscribeToMessages(request.Context(), func(data []byte) {
event := Event{
Name: events.MempoolEvent,
EventID: h.nextID(),
Data: data,
}
session.SendEvent(event)
}, sources.SubscribeToMempoolOptions{Accounts: accounts})
if err != nil {
return err
}
session.SetCancelFn(cancelFn)
return nil
}
func parseAccountsToTraceOptions(str string) (*sources.SubscribeToTraceOptions, error) {
if strings.ToUpper(str) == "ALL" {
return &sources.SubscribeToTraceOptions{AllAccounts: true}, nil
}
accountStrings := strings.Split(str, ",")
accounts := make([]tongo.AccountID, 0, len(accountStrings))
for _, account := range accountStrings {
accountID, err := tongo.ParseAddress(account)
if err != nil {
return nil, err
}
accounts = append(accounts, accountID.ID)
}
return &sources.SubscribeToTraceOptions{Accounts: accounts}, nil
}
func (h *Handler) SubscribeToTraces(session *session, request *http.Request) error {
if h.traceSource == nil {
return errors.BadRequest("trace source is not configured")
}
accounts := request.URL.Query().Get("accounts")
options, err := parseAccountsToTraceOptions(accounts)
if err != nil {
return errors.BadRequest("failed to parse 'accounts' parameter in query")
}
cancelFn := h.traceSource.SubscribeToTraces(request.Context(), func(data []byte) {
event := Event{
Name: events.TraceEvent,
EventID: h.nextID(),
Data: data,
}
session.SendEvent(event)
}, *options)
session.SetCancelFn(cancelFn)
return nil
}
func (h *Handler) SubscribeToBlockHeaders(session *session, request *http.Request) error {
if h.blockHeadersSource == nil {
return errors.BadRequest("block headers source is not configured")
}
workchain := request.URL.Query().Get("workchain")
opts := sources.SubscribeToBlockHeadersOptions{}
if len(workchain) > 0 {
value, err := strconv.Atoi(workchain)
if err != nil {
return errors.BadRequest("failed to parse 'workchain' parameter in query")
}
if value != -1 && value != 0 {
return errors.BadRequest("invalid 'workchain' parameter in query")
}
opts.Workchain = &value
}
cancelFn := h.blockHeadersSource.SubscribeToBlockHeaders(request.Context(), func(data []byte) {
event := Event{
Name: events.BlockEvent,
EventID: h.nextID(),
Data: data,
}
session.SendEvent(event)
}, opts)
session.SetCancelFn(cancelFn)
return nil
}
func (h *Handler) SubscribeToBlocks(session *session, request *http.Request) error {
if h.blockSource == nil {
return errors.BadRequest("block source is not configured")
}
opts := sources.SubscribeToBlocksOptions{}
if seqno := request.URL.Query().Get("masterchain_seqno"); len(seqno) > 0 {
value, err := strconv.ParseInt(seqno, 10, 32)
if err != nil {
return errors.BadRequest("failed to parse 'masterchain_seqno' parameter in query")
}
if value < 1 {
value = 0
}
opts.MasterchainSeqno = uint32(value)
}
cancelFn, err := h.blockSource.SubscribeToBlocks(request.Context(), func(data []byte) {
event := Event{
Name: events.BlockchainEvent,
EventID: h.nextID(),
Data: data,
}
session.SendEvent(event)
}, opts)
if err != nil {
return err
}
session.SetCancelFn(cancelFn)
return nil
}
func (h *Handler) nextID() int64 {
return atomic.AddInt64(&h.currentEventID, 1)
}