79
vendor/github.com/status-im/status-go/services/subscriptions/README.md
generated
vendored
Normal file
79
vendor/github.com/status-im/status-go/services/subscriptions/README.md
generated
vendored
Normal file
@@ -0,0 +1,79 @@
|
||||
# Signal Subscriptions
|
||||
|
||||
This package implements subscriptions mechanics using [`signal`](../../signal) package.
|
||||
|
||||
It defines 3 new RPC methods in the `eth` namespace and 2 signals.
|
||||
|
||||
## Methods
|
||||
|
||||
###`eth_subscribeSignal`
|
||||
Creates a new filter and subscribes to its changes via signals.
|
||||
|
||||
Parameters: receives the method name and parameters for the filter that is created.
|
||||
|
||||
Example 1:
|
||||
```json
|
||||
{
|
||||
"jsonrpc": "2.0",
|
||||
"id": 1,
|
||||
"method": "eth_subscribeSignal",
|
||||
"params": ["eth_newPendingTransactionFilter", []]
|
||||
}
|
||||
```
|
||||
|
||||
Example 2:
|
||||
```json
|
||||
{
|
||||
"jsonrpc": "2.0",
|
||||
"id": 2,
|
||||
"method": "eth_subscribeSignal",
|
||||
"params": [
|
||||
"shh_newMessageFilter",
|
||||
[{ "symKeyID":"abcabcabcabc", "topics": ["0x12341234"] }]
|
||||
]
|
||||
}
|
||||
```
|
||||
|
||||
Supported filters: `shh_newMessageFilter`, `eth_newFilter`, `eth_newBlockFilter`, `eth_newPendingTransactionFilter`
|
||||
(see [Ethereum documentation](https://github.com/ethereum/wiki/wiki/JSON-RPC) for respective parameters).
|
||||
|
||||
Returns: error or `subscriptionID`.
|
||||
|
||||
|
||||
###`eth_unsubscribeSignal`
|
||||
Unsubscribes and removes one filter by its ID.
|
||||
NOTE: Unsubscribing from a filter removes it.
|
||||
|
||||
Parameters: `subscriptionID` obtained from `eth_subscribeSignal`
|
||||
Returns: error if something went wrong while unsubscribing.
|
||||
|
||||
|
||||
## Signals
|
||||
|
||||
1. Subscription data received
|
||||
|
||||
```json
|
||||
{
|
||||
"type": "subscriptions.data",
|
||||
"event": {
|
||||
"subscription_id": "shh_0x01",
|
||||
"data": {
|
||||
<whisper envelope 01>,
|
||||
<whisper envelope 02>,
|
||||
...
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
2. Subscription error received
|
||||
|
||||
```json
|
||||
{
|
||||
"type": "subscriptions.error",
|
||||
"event": {
|
||||
"subscription_id": "shh_0x01",
|
||||
"error_message": "can not find filter with id: 0x01"
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
47
vendor/github.com/status-im/status-go/services/subscriptions/api.go
generated
vendored
Normal file
47
vendor/github.com/status-im/status-go/services/subscriptions/api.go
generated
vendored
Normal file
@@ -0,0 +1,47 @@
|
||||
package subscriptions
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"time"
|
||||
|
||||
"github.com/status-im/status-go/rpc"
|
||||
)
|
||||
|
||||
type API struct {
|
||||
rpcPrivateClientFunc func() *rpc.Client
|
||||
activeSubscriptions *Subscriptions
|
||||
}
|
||||
|
||||
func NewPublicAPI(rpcPrivateClientFunc func() *rpc.Client) *API {
|
||||
return &API{
|
||||
rpcPrivateClientFunc: rpcPrivateClientFunc,
|
||||
activeSubscriptions: NewSubscriptions(100 * time.Millisecond),
|
||||
}
|
||||
}
|
||||
|
||||
func (api *API) SubscribeSignal(method string, args []interface{}) (SubscriptionID, error) {
|
||||
var (
|
||||
filter filter
|
||||
err error
|
||||
namespace = method[:3]
|
||||
)
|
||||
|
||||
switch namespace {
|
||||
case "shh":
|
||||
filter, err = installShhFilter(api.rpcPrivateClientFunc(), method, args)
|
||||
case "eth":
|
||||
filter, err = installEthFilter(api.rpcPrivateClientFunc(), method, args)
|
||||
default:
|
||||
err = fmt.Errorf("unexpected namespace: %s", namespace)
|
||||
}
|
||||
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("[SubscribeSignal] could not subscribe, failed to call %s: %v", method, err)
|
||||
}
|
||||
|
||||
return api.activeSubscriptions.Create(namespace, filter)
|
||||
}
|
||||
|
||||
func (api *API) UnsubscribeSignal(id string) error {
|
||||
return api.activeSubscriptions.Remove(SubscriptionID(id))
|
||||
}
|
||||
7
vendor/github.com/status-im/status-go/services/subscriptions/filters.go
generated
vendored
Normal file
7
vendor/github.com/status-im/status-go/services/subscriptions/filters.go
generated
vendored
Normal file
@@ -0,0 +1,7 @@
|
||||
package subscriptions
|
||||
|
||||
type filter interface {
|
||||
getID() string
|
||||
getChanges() ([]interface{}, error)
|
||||
uninstall() error
|
||||
}
|
||||
65
vendor/github.com/status-im/status-go/services/subscriptions/filters_eth.go
generated
vendored
Normal file
65
vendor/github.com/status-im/status-go/services/subscriptions/filters_eth.go
generated
vendored
Normal file
@@ -0,0 +1,65 @@
|
||||
package subscriptions
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"github.com/status-im/status-go/rpc"
|
||||
)
|
||||
|
||||
type ethFilter struct {
|
||||
id string
|
||||
rpcClient *rpc.Client
|
||||
}
|
||||
|
||||
func installEthFilter(rpcClient *rpc.Client, method string, args []interface{}) (*ethFilter, error) {
|
||||
|
||||
if err := validateEthMethod(method); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
var result string
|
||||
|
||||
err := rpcClient.Call(&result, rpcClient.UpstreamChainID, method, args...)
|
||||
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
filter := ðFilter{
|
||||
id: result,
|
||||
rpcClient: rpcClient,
|
||||
}
|
||||
|
||||
return filter, nil
|
||||
|
||||
}
|
||||
|
||||
func (ef *ethFilter) getID() string {
|
||||
return ef.id
|
||||
}
|
||||
|
||||
func (ef *ethFilter) getChanges() ([]interface{}, error) {
|
||||
var result []interface{}
|
||||
|
||||
err := ef.rpcClient.Call(&result, ef.rpcClient.UpstreamChainID, "eth_getFilterChanges", ef.getID())
|
||||
|
||||
return result, err
|
||||
}
|
||||
|
||||
func (ef *ethFilter) uninstall() error {
|
||||
return ef.rpcClient.Call(nil, ef.rpcClient.UpstreamChainID, "eth_uninstallFilter", ef.getID())
|
||||
}
|
||||
|
||||
func validateEthMethod(method string) error {
|
||||
for _, allowedMethod := range []string{
|
||||
"eth_newFilter",
|
||||
"eth_newBlockFilter",
|
||||
"eth_newPendingTransactionFilter",
|
||||
} {
|
||||
if method == allowedMethod {
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
||||
return fmt.Errorf("unexpected filter method: %s", method)
|
||||
}
|
||||
57
vendor/github.com/status-im/status-go/services/subscriptions/filters_shh.go
generated
vendored
Normal file
57
vendor/github.com/status-im/status-go/services/subscriptions/filters_shh.go
generated
vendored
Normal file
@@ -0,0 +1,57 @@
|
||||
package subscriptions
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"github.com/status-im/status-go/rpc"
|
||||
)
|
||||
|
||||
type whisperFilter struct {
|
||||
id string
|
||||
rpcClient *rpc.Client
|
||||
}
|
||||
|
||||
func installShhFilter(rpcClient *rpc.Client, method string, args []interface{}) (*whisperFilter, error) {
|
||||
|
||||
if err := validateShhMethod(method); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
var result string
|
||||
|
||||
err := rpcClient.Call(&result, rpcClient.UpstreamChainID, method, args...)
|
||||
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
filter := &whisperFilter{
|
||||
id: result,
|
||||
rpcClient: rpcClient,
|
||||
}
|
||||
|
||||
return filter, nil
|
||||
}
|
||||
|
||||
func (wf *whisperFilter) getChanges() ([]interface{}, error) {
|
||||
var result []interface{}
|
||||
|
||||
err := wf.rpcClient.Call(&result, wf.rpcClient.UpstreamChainID, "shh_getFilterMessages", wf.getID())
|
||||
|
||||
return result, err
|
||||
}
|
||||
|
||||
func (wf *whisperFilter) getID() string {
|
||||
return wf.id
|
||||
}
|
||||
|
||||
func (wf *whisperFilter) uninstall() error {
|
||||
return wf.rpcClient.Call(nil, wf.rpcClient.UpstreamChainID, "shh_deleteMessageFilter", wf.getID())
|
||||
}
|
||||
|
||||
func validateShhMethod(method string) error {
|
||||
if method != "shh_newMessageFilter" {
|
||||
return fmt.Errorf("unexpected filter method: %s", method)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
51
vendor/github.com/status-im/status-go/services/subscriptions/service.go
generated
vendored
Normal file
51
vendor/github.com/status-im/status-go/services/subscriptions/service.go
generated
vendored
Normal file
@@ -0,0 +1,51 @@
|
||||
package subscriptions
|
||||
|
||||
import (
|
||||
gethnode "github.com/ethereum/go-ethereum/node"
|
||||
"github.com/ethereum/go-ethereum/p2p"
|
||||
gethrpc "github.com/ethereum/go-ethereum/rpc"
|
||||
|
||||
"github.com/status-im/status-go/rpc"
|
||||
)
|
||||
|
||||
// Make sure that Service implements gethnode.Lifecycle interface.
|
||||
var _ gethnode.Lifecycle = (*Service)(nil)
|
||||
|
||||
// Service represents our own implementation of personal sign operations.
|
||||
type Service struct {
|
||||
api *API
|
||||
}
|
||||
|
||||
// New returns a new Service.
|
||||
func New(rpcPrivateClientFunc func() *rpc.Client) *Service {
|
||||
return &Service{
|
||||
api: NewPublicAPI(rpcPrivateClientFunc),
|
||||
}
|
||||
}
|
||||
|
||||
// Protocols returns a new protocols list. In this case, there are none.
|
||||
func (s *Service) Protocols() []p2p.Protocol {
|
||||
return []p2p.Protocol{}
|
||||
}
|
||||
|
||||
// APIs returns a list of new APIs.
|
||||
func (s *Service) APIs() []gethrpc.API {
|
||||
return []gethrpc.API{
|
||||
{
|
||||
Namespace: "eth",
|
||||
Version: "1.0",
|
||||
Service: s.api,
|
||||
Public: true,
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
// Start is run when a service is started.
|
||||
func (s *Service) Start() error {
|
||||
return nil
|
||||
}
|
||||
|
||||
// Stop is run when a service is stopped.
|
||||
func (s *Service) Stop() error {
|
||||
return s.api.activeSubscriptions.removeAll()
|
||||
}
|
||||
19
vendor/github.com/status-im/status-go/services/subscriptions/signals.go
generated
vendored
Normal file
19
vendor/github.com/status-im/status-go/services/subscriptions/signals.go
generated
vendored
Normal file
@@ -0,0 +1,19 @@
|
||||
package subscriptions
|
||||
|
||||
import "github.com/status-im/status-go/signal"
|
||||
|
||||
type filterSignal struct {
|
||||
filterID string
|
||||
}
|
||||
|
||||
func newFilterSignal(filterID string) *filterSignal {
|
||||
return &filterSignal{filterID}
|
||||
}
|
||||
|
||||
func (s *filterSignal) SendError(err error) {
|
||||
signal.SendSubscriptionErrorEvent(s.filterID, err)
|
||||
}
|
||||
|
||||
func (s *filterSignal) SendData(data []interface{}) {
|
||||
signal.SendSubscriptionDataEvent(s.filterID, data)
|
||||
}
|
||||
82
vendor/github.com/status-im/status-go/services/subscriptions/subscription.go
generated
vendored
Normal file
82
vendor/github.com/status-im/status-go/services/subscriptions/subscription.go
generated
vendored
Normal file
@@ -0,0 +1,82 @@
|
||||
package subscriptions
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"sync"
|
||||
"time"
|
||||
)
|
||||
|
||||
type SubscriptionID string
|
||||
|
||||
type Subscription struct {
|
||||
mu sync.RWMutex
|
||||
id SubscriptionID
|
||||
signal *filterSignal
|
||||
quit chan struct{}
|
||||
filter filter
|
||||
started bool
|
||||
}
|
||||
|
||||
func NewSubscription(namespace string, filter filter) *Subscription {
|
||||
subscriptionID := NewSubscriptionID(namespace, filter.getID())
|
||||
return &Subscription{
|
||||
id: subscriptionID,
|
||||
signal: newFilterSignal(string(subscriptionID)),
|
||||
filter: filter,
|
||||
}
|
||||
}
|
||||
|
||||
func (s *Subscription) Start(checkPeriod time.Duration) error {
|
||||
s.mu.Lock()
|
||||
if s.started {
|
||||
s.mu.Unlock()
|
||||
return errors.New("subscription already started or used")
|
||||
}
|
||||
s.started = true
|
||||
s.quit = make(chan struct{})
|
||||
quit := s.quit
|
||||
s.mu.Unlock()
|
||||
|
||||
ticker := time.NewTicker(checkPeriod)
|
||||
defer ticker.Stop()
|
||||
|
||||
for {
|
||||
select {
|
||||
case <-ticker.C:
|
||||
filterData, err := s.filter.getChanges()
|
||||
if err != nil {
|
||||
s.signal.SendError(err)
|
||||
} else if len(filterData) > 0 {
|
||||
s.signal.SendData(filterData)
|
||||
}
|
||||
case <-quit:
|
||||
return nil
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func (s *Subscription) Stop(uninstall bool) error {
|
||||
s.mu.Lock()
|
||||
defer s.mu.Unlock()
|
||||
if !s.started {
|
||||
return nil
|
||||
}
|
||||
select {
|
||||
case _, ok := <-s.quit:
|
||||
// handle a case of a closed channel
|
||||
if !ok {
|
||||
return nil
|
||||
}
|
||||
default:
|
||||
close(s.quit)
|
||||
}
|
||||
if !uninstall {
|
||||
return nil
|
||||
}
|
||||
return s.filter.uninstall()
|
||||
}
|
||||
|
||||
func NewSubscriptionID(namespace, filterID string) SubscriptionID {
|
||||
return SubscriptionID(fmt.Sprintf("%s-%s", namespace, filterID))
|
||||
}
|
||||
84
vendor/github.com/status-im/status-go/services/subscriptions/subscriptions.go
generated
vendored
Normal file
84
vendor/github.com/status-im/status-go/services/subscriptions/subscriptions.go
generated
vendored
Normal file
@@ -0,0 +1,84 @@
|
||||
package subscriptions
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"github.com/ethereum/go-ethereum/log"
|
||||
)
|
||||
|
||||
type Subscriptions struct {
|
||||
mu sync.Mutex
|
||||
subs map[SubscriptionID]*Subscription
|
||||
checkPeriod time.Duration
|
||||
log log.Logger
|
||||
}
|
||||
|
||||
func NewSubscriptions(period time.Duration) *Subscriptions {
|
||||
return &Subscriptions{
|
||||
subs: make(map[SubscriptionID]*Subscription),
|
||||
checkPeriod: period,
|
||||
log: log.New("package", "status-go/services/subsriptions.Subscriptions"),
|
||||
}
|
||||
}
|
||||
|
||||
func (s *Subscriptions) Create(namespace string, filter filter) (SubscriptionID, error) {
|
||||
s.mu.Lock()
|
||||
defer s.mu.Unlock()
|
||||
|
||||
newSub := NewSubscription(namespace, filter)
|
||||
|
||||
go func() {
|
||||
err := newSub.Start(s.checkPeriod)
|
||||
if err != nil {
|
||||
s.log.Error("error while starting subscription", "err", err)
|
||||
}
|
||||
}()
|
||||
|
||||
s.subs[newSub.id] = newSub
|
||||
|
||||
return newSub.id, nil
|
||||
}
|
||||
|
||||
func (s *Subscriptions) Remove(id SubscriptionID) error {
|
||||
s.mu.Lock()
|
||||
defer s.mu.Unlock()
|
||||
|
||||
found, err := s.stopSubscription(id, true)
|
||||
if found {
|
||||
delete(s.subs, id)
|
||||
}
|
||||
|
||||
return err
|
||||
}
|
||||
|
||||
func (s *Subscriptions) removeAll() error {
|
||||
s.mu.Lock()
|
||||
defer s.mu.Unlock()
|
||||
|
||||
unsubscribeErrors := make(map[SubscriptionID]error)
|
||||
|
||||
for id := range s.subs {
|
||||
_, err := s.stopSubscription(id, false)
|
||||
if err != nil {
|
||||
unsubscribeErrors[id] = err
|
||||
}
|
||||
}
|
||||
|
||||
s.subs = make(map[SubscriptionID]*Subscription)
|
||||
|
||||
if len(unsubscribeErrors) > 0 {
|
||||
return fmt.Errorf("errors while cleaning up subscriptions: %+v", unsubscribeErrors)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s *Subscriptions) stopSubscription(id SubscriptionID, uninstall bool) (bool, error) {
|
||||
sub, found := s.subs[id]
|
||||
if !found {
|
||||
return false, nil
|
||||
}
|
||||
return true, sub.Stop(uninstall)
|
||||
}
|
||||
Reference in New Issue
Block a user