2020-05-10 22:00:19 +00:00
|
|
|
package server
|
|
|
|
|
|
|
|
import (
|
|
|
|
"github.com/gorilla/websocket"
|
2020-05-13 14:13:33 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/core/transaction"
|
|
|
|
"github.com/nspcc-dev/neo-go/pkg/rpc/request"
|
2020-05-10 22:00:19 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/rpc/response"
|
2020-05-13 14:13:33 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/rpc/response/result"
|
2020-05-12 19:38:29 +00:00
|
|
|
"go.uber.org/atomic"
|
2020-05-10 22:00:19 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type (
|
|
|
|
// subscriber is an event subscriber.
|
|
|
|
subscriber struct {
|
2020-05-12 19:38:29 +00:00
|
|
|
writer chan<- *websocket.PreparedMessage
|
|
|
|
ws *websocket.Conn
|
|
|
|
overflown atomic.Bool
|
2020-05-10 22:00:19 +00:00
|
|
|
// These work like slots as there is not a lot of them (it's
|
|
|
|
// cheaper doing it this way rather than creating a map),
|
|
|
|
// pointing to EventID is an obvious overkill at the moment, but
|
|
|
|
// that's not for long.
|
2020-05-13 14:13:33 +00:00
|
|
|
feeds [maxFeeds]feed
|
|
|
|
}
|
|
|
|
feed struct {
|
|
|
|
event response.EventID
|
|
|
|
filter interface{}
|
2020-05-10 22:00:19 +00:00
|
|
|
}
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
// Maximum number of subscriptions per one client.
|
|
|
|
maxFeeds = 16
|
|
|
|
|
|
|
|
// This sets notification messages buffer depth, it may seem to be quite
|
|
|
|
// big, but there is a big gap in speed between internal event processing
|
|
|
|
// and networking communication that is combined with spiky nature of our
|
|
|
|
// event generation process, which leads to lots of events generated in
|
|
|
|
// short time and they will put some pressure to this buffer (consider
|
|
|
|
// ~500 invocation txs in one block with some notifications). At the same
|
|
|
|
// time this channel is about sending pointers, so it's doesn't cost
|
|
|
|
// a lot in terms of memory used.
|
|
|
|
notificationBufSize = 1024
|
|
|
|
)
|
2020-05-13 14:13:33 +00:00
|
|
|
|
|
|
|
func (f *feed) Matches(r *response.Notification) bool {
|
|
|
|
if r.Event != f.event {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
if f.filter == nil {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
switch f.event {
|
|
|
|
case response.TransactionEventID:
|
|
|
|
filt := f.filter.(request.TxFilter)
|
|
|
|
tx := r.Payload[0].(*transaction.Transaction)
|
|
|
|
return tx.Type == filt.Type
|
|
|
|
case response.NotificationEventID:
|
|
|
|
filt := f.filter.(request.NotificationFilter)
|
|
|
|
notification := r.Payload[0].(result.NotificationEvent)
|
|
|
|
return notification.Contract.Equals(filt.Contract)
|
|
|
|
case response.ExecutionEventID:
|
|
|
|
filt := f.filter.(request.ExecutionFilter)
|
|
|
|
applog := r.Payload[0].(result.ApplicationLog)
|
|
|
|
return len(applog.Executions) != 0 && applog.Executions[0].VMState == filt.State
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|