2019-11-23 17:57:05 +00:00
|
|
|
package gethbridge
|
|
|
|
|
|
|
|
import (
|
2020-01-13 19:17:30 +00:00
|
|
|
"errors"
|
|
|
|
|
2020-01-02 09:10:19 +00:00
|
|
|
"go.uber.org/zap"
|
|
|
|
|
2020-01-15 11:36:49 +00:00
|
|
|
"github.com/status-im/status-go/waku"
|
|
|
|
|
2019-11-23 17:57:05 +00:00
|
|
|
"github.com/ethereum/go-ethereum/node"
|
2019-12-12 16:06:00 +00:00
|
|
|
"github.com/ethereum/go-ethereum/p2p/enode"
|
2020-01-02 09:10:19 +00:00
|
|
|
|
2019-11-23 17:57:05 +00:00
|
|
|
gethens "github.com/status-im/status-go/eth-node/bridge/geth/ens"
|
|
|
|
"github.com/status-im/status-go/eth-node/types"
|
|
|
|
enstypes "github.com/status-im/status-go/eth-node/types/ens"
|
|
|
|
)
|
|
|
|
|
|
|
|
type gethNodeWrapper struct {
|
|
|
|
stack *node.Node
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewNodeBridge(stack *node.Node) types.Node {
|
|
|
|
return &gethNodeWrapper{stack: stack}
|
|
|
|
}
|
|
|
|
|
2019-11-27 12:22:23 +00:00
|
|
|
func (w *gethNodeWrapper) Poll() {
|
|
|
|
// noop
|
|
|
|
}
|
|
|
|
|
2019-11-23 17:57:05 +00:00
|
|
|
func (w *gethNodeWrapper) NewENSVerifier(logger *zap.Logger) enstypes.ENSVerifier {
|
|
|
|
return gethens.NewVerifier(logger)
|
|
|
|
}
|
|
|
|
|
2020-01-13 19:17:30 +00:00
|
|
|
func (w *gethNodeWrapper) GetWaku(ctx interface{}) (types.Waku, error) {
|
|
|
|
var nativeWaku *waku.Waku
|
|
|
|
if ctx == nil || ctx == w {
|
|
|
|
err := w.stack.Service(&nativeWaku)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
switch serviceProvider := ctx.(type) {
|
|
|
|
case *node.ServiceContext:
|
|
|
|
err := serviceProvider.Service(&nativeWaku)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if nativeWaku == nil {
|
|
|
|
return nil, errors.New("waku service is not available")
|
|
|
|
}
|
|
|
|
|
|
|
|
return NewGethWakuWrapper(nativeWaku), nil
|
|
|
|
}
|
|
|
|
|
2019-12-12 16:06:00 +00:00
|
|
|
func (w *gethNodeWrapper) AddPeer(url string) error {
|
|
|
|
parsedNode, err := enode.ParseV4(url)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
w.stack.Server().AddPeer(parsedNode)
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (w *gethNodeWrapper) RemovePeer(url string) error {
|
|
|
|
parsedNode, err := enode.ParseV4(url)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
w.stack.Server().RemovePeer(parsedNode)
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
2020-08-27 12:38:59 +00:00
|
|
|
|
|
|
|
func (w *gethNodeWrapper) PeersCount() int {
|
2020-11-02 15:50:05 +00:00
|
|
|
if w.stack.Server() == nil {
|
|
|
|
return 0
|
|
|
|
}
|
2020-08-27 12:38:59 +00:00
|
|
|
return len(w.stack.Server().Peers())
|
|
|
|
}
|