2022-07-21 13:21:44 +00:00
|
|
|
package rpcsrv
|
2020-05-10 22:00:19 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"github.com/gorilla/websocket"
|
2020-05-13 14:13:33 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/core/block"
|
2020-09-03 16:58:50 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/core/state"
|
2020-05-13 14:13:33 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/core/transaction"
|
2022-07-22 16:09:29 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/neorpc"
|
2022-07-22 18:26:29 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/neorpc/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),
|
2022-04-20 18:30:09 +00:00
|
|
|
// pointing to an EventID is an obvious overkill at the moment, but
|
2020-05-10 22:00:19 +00:00
|
|
|
// that's not for long.
|
2020-05-13 14:13:33 +00:00
|
|
|
feeds [maxFeeds]feed
|
|
|
|
}
|
|
|
|
feed struct {
|
2022-07-22 16:09:29 +00:00
|
|
|
event neorpc.EventID
|
2020-05-13 14:13:33 +00:00
|
|
|
filter interface{}
|
2020-05-10 22:00:19 +00:00
|
|
|
}
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
// Maximum number of subscriptions per one client.
|
|
|
|
maxFeeds = 16
|
|
|
|
|
2022-04-20 18:30:09 +00:00
|
|
|
// This sets notification messages buffer depth. It may seem to be quite
|
2020-05-10 22:00:19 +00:00
|
|
|
// 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
|
2022-04-20 18:30:09 +00:00
|
|
|
// a short time and they will put some pressure to this buffer (consider
|
2020-05-10 22:00:19 +00:00
|
|
|
// ~500 invocation txs in one block with some notifications). At the same
|
2022-04-20 18:30:09 +00:00
|
|
|
// time, this channel is about sending pointers, so it's doesn't cost
|
2020-05-10 22:00:19 +00:00
|
|
|
// a lot in terms of memory used.
|
|
|
|
notificationBufSize = 1024
|
|
|
|
)
|
2020-05-13 14:13:33 +00:00
|
|
|
|
2022-07-22 16:09:29 +00:00
|
|
|
func (f *feed) Matches(r *neorpc.Notification) bool {
|
2020-05-13 14:13:33 +00:00
|
|
|
if r.Event != f.event {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
if f.filter == nil {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
switch f.event {
|
2022-07-22 16:09:29 +00:00
|
|
|
case neorpc.BlockEventID:
|
|
|
|
filt := f.filter.(neorpc.BlockFilter)
|
2020-05-13 14:13:33 +00:00
|
|
|
b := r.Payload[0].(*block.Block)
|
2021-03-01 12:20:27 +00:00
|
|
|
return int(b.PrimaryIndex) == filt.Primary
|
2022-07-22 16:09:29 +00:00
|
|
|
case neorpc.TransactionEventID:
|
|
|
|
filt := f.filter.(neorpc.TxFilter)
|
2020-05-13 14:13:33 +00:00
|
|
|
tx := r.Payload[0].(*transaction.Transaction)
|
2020-07-29 16:57:38 +00:00
|
|
|
senderOK := filt.Sender == nil || tx.Sender().Equals(*filt.Sender)
|
|
|
|
signerOK := true
|
|
|
|
if filt.Signer != nil {
|
|
|
|
signerOK = false
|
|
|
|
for i := range tx.Signers {
|
|
|
|
if tx.Signers[i].Account.Equals(*filt.Signer) {
|
|
|
|
signerOK = true
|
2020-05-13 14:13:33 +00:00
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-07-29 16:57:38 +00:00
|
|
|
return senderOK && signerOK
|
2022-07-22 16:09:29 +00:00
|
|
|
case neorpc.NotificationEventID:
|
|
|
|
filt := f.filter.(neorpc.NotificationFilter)
|
2022-07-22 18:17:23 +00:00
|
|
|
notification := r.Payload[0].(*state.ContainedNotificationEvent)
|
2020-09-03 16:58:50 +00:00
|
|
|
hashOk := filt.Contract == nil || notification.ScriptHash.Equals(*filt.Contract)
|
2020-08-04 13:24:32 +00:00
|
|
|
nameOk := filt.Name == nil || notification.Name == *filt.Name
|
|
|
|
return hashOk && nameOk
|
2022-07-22 16:09:29 +00:00
|
|
|
case neorpc.ExecutionEventID:
|
|
|
|
filt := f.filter.(neorpc.ExecutionFilter)
|
2020-09-03 16:58:50 +00:00
|
|
|
applog := r.Payload[0].(*state.AppExecResult)
|
|
|
|
return applog.VMState.String() == filt.State
|
2022-07-22 16:09:29 +00:00
|
|
|
case neorpc.NotaryRequestEventID:
|
|
|
|
filt := f.filter.(neorpc.TxFilter)
|
2022-07-22 18:26:29 +00:00
|
|
|
req := r.Payload[0].(*result.NotaryRequestEvent)
|
2021-05-28 11:55:06 +00:00
|
|
|
senderOk := filt.Sender == nil || req.NotaryRequest.FallbackTransaction.Signers[1].Account == *filt.Sender
|
|
|
|
signerOK := true
|
|
|
|
if filt.Signer != nil {
|
|
|
|
signerOK = false
|
|
|
|
for _, signer := range req.NotaryRequest.MainTransaction.Signers {
|
|
|
|
if signer.Account.Equals(*filt.Signer) {
|
|
|
|
signerOK = true
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return senderOk && signerOK
|
2020-05-13 14:13:33 +00:00
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|