feat: Waku v2 bridge

Issue #12610
This commit is contained in:
Michal Iskierko
2023-11-12 13:29:38 +01:00
parent 56e7bd01ca
commit 6d31343205
6716 changed files with 1982502 additions and 5891 deletions

View File

@@ -0,0 +1,48 @@
package peer
import (
"context"
"errors"
)
var (
// ErrInvalidTopic error returned when the requested topic is not valid.
ErrInvalidTopic = errors.New("topic not valid")
// ErrInvalidRange error returned when max-min range is not valid.
ErrInvalidRange = errors.New("invalid range, Min should be lower or equal to Max")
// ErrDiscovererNotProvided error when discoverer is not being provided.
ErrDiscovererNotProvided = errors.New("discoverer not provided")
)
// PublicAPI represents a set of APIs from the `web3.peer` namespace.
type PublicAPI struct {
s *Service
}
// NewAPI creates an instance of the peer API.
func NewAPI(s *Service) *PublicAPI {
return &PublicAPI{s: s}
}
// DiscoverRequest json request for peer_discover.
type DiscoverRequest struct {
Topic string `json:"topic"`
Max int `json:"max"`
Min int `json:"min"`
}
// Discover is an implementation of `peer_discover` or `web3.peer.discover` API.
func (api *PublicAPI) Discover(context context.Context, req DiscoverRequest) (err error) {
if api.s.d == nil {
return ErrDiscovererNotProvided
}
if len(req.Topic) == 0 {
return ErrInvalidTopic
}
if req.Max < req.Min {
return ErrInvalidRange
}
return api.s.d.Discover(req.Topic, req.Max, req.Min)
}

View File

@@ -0,0 +1,48 @@
// Code generated by MockGen. DO NOT EDIT.
// Source: services/peer/service.go
// Package peer is a generated GoMock package.
package peer
import (
reflect "reflect"
gomock "github.com/golang/mock/gomock"
)
// MockDiscoverer is a mock of Discoverer interface
type MockDiscoverer struct {
ctrl *gomock.Controller
recorder *MockDiscovererMockRecorder
}
// MockDiscovererMockRecorder is the mock recorder for MockDiscoverer
type MockDiscovererMockRecorder struct {
mock *MockDiscoverer
}
// NewMockDiscoverer creates a new mock instance
func NewMockDiscoverer(ctrl *gomock.Controller) *MockDiscoverer {
mock := &MockDiscoverer{ctrl: ctrl}
mock.recorder = &MockDiscovererMockRecorder{mock}
return mock
}
// EXPECT returns an object that allows the caller to indicate expected use
func (m *MockDiscoverer) EXPECT() *MockDiscovererMockRecorder {
return m.recorder
}
// Discover mocks base method
func (m *MockDiscoverer) Discover(topic string, max, min int) error {
m.ctrl.T.Helper()
ret := m.ctrl.Call(m, "Discover", topic, max, min)
ret0, _ := ret[0].(error)
return ret0
}
// Discover indicates an expected call of Discover
func (mr *MockDiscovererMockRecorder) Discover(topic, max, min interface{}) *gomock.Call {
mr.mock.ctrl.T.Helper()
return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "Discover", reflect.TypeOf((*MockDiscoverer)(nil).Discover), topic, max, min)
}

View File

@@ -0,0 +1,59 @@
package peer
import (
"github.com/ethereum/go-ethereum/node"
"github.com/ethereum/go-ethereum/p2p"
"github.com/ethereum/go-ethereum/rpc"
)
// Make sure that Service implements node.Lifecycle interface.
var _ node.Lifecycle = (*Service)(nil)
// Discoverer manages peer discovery.
type Discoverer interface {
Discover(topic string, max, min int) error
}
// Service it manages all endpoints for peer operations.
type Service struct {
d Discoverer
}
// New returns a new Service.
func New() *Service {
return &Service{}
}
// 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() []rpc.API {
return []rpc.API{
{
Namespace: "peer",
Version: "1.0",
Service: NewAPI(s),
Public: false,
},
}
}
// SetDiscoverer sets discoverer for the API calls.
func (s *Service) SetDiscoverer(d Discoverer) {
s.d = d
}
// Start is run when a service is started.
// It does nothing in this case but is required by `node.Service` interface.
func (s *Service) Start() error {
return nil
}
// Stop is run when a service is stopped.
// It does nothing in this case but is required by `node.Service` interface.
func (s *Service) Stop() error {
return nil
}