github.com/status-im/status-go@v1.1.0/eth-node/bridge/geth/node.go (about)

     1  package gethbridge
     2  
     3  import (
     4  	"errors"
     5  
     6  	"go.uber.org/zap"
     7  
     8  	"github.com/status-im/status-go/waku"
     9  	"github.com/status-im/status-go/wakuv2"
    10  
    11  	"github.com/ethereum/go-ethereum/node"
    12  	"github.com/ethereum/go-ethereum/p2p/enode"
    13  
    14  	gethens "github.com/status-im/status-go/eth-node/bridge/geth/ens"
    15  	"github.com/status-im/status-go/eth-node/types"
    16  	enstypes "github.com/status-im/status-go/eth-node/types/ens"
    17  )
    18  
    19  type gethNodeWrapper struct {
    20  	stack *node.Node
    21  	waku1 *waku.Waku
    22  	waku2 *wakuv2.Waku
    23  }
    24  
    25  func NewNodeBridge(stack *node.Node, waku1 *waku.Waku, waku2 *wakuv2.Waku) types.Node {
    26  	return &gethNodeWrapper{stack: stack, waku1: waku1, waku2: waku2}
    27  }
    28  
    29  func (w *gethNodeWrapper) Poll() {
    30  	// noop
    31  }
    32  
    33  func (w *gethNodeWrapper) NewENSVerifier(logger *zap.Logger) enstypes.ENSVerifier {
    34  	return gethens.NewVerifier(logger)
    35  }
    36  
    37  func (w *gethNodeWrapper) SetWaku1(waku *waku.Waku) {
    38  	w.waku1 = waku
    39  }
    40  
    41  func (w *gethNodeWrapper) SetWaku2(waku *wakuv2.Waku) {
    42  	w.waku2 = waku
    43  }
    44  
    45  func (w *gethNodeWrapper) GetWaku(ctx interface{}) (types.Waku, error) {
    46  	if w.waku1 == nil {
    47  		return nil, errors.New("waku service is not available")
    48  	}
    49  
    50  	return NewGethWakuWrapper(w.waku1), nil
    51  }
    52  
    53  func (w *gethNodeWrapper) GetWakuV2(ctx interface{}) (types.Waku, error) {
    54  	if w.waku2 == nil {
    55  		return nil, errors.New("waku service is not available")
    56  	}
    57  
    58  	return NewGethWakuV2Wrapper(w.waku2), nil
    59  }
    60  
    61  func (w *gethNodeWrapper) AddPeer(url string) error {
    62  	parsedNode, err := enode.ParseV4(url)
    63  	if err != nil {
    64  		return err
    65  	}
    66  
    67  	w.stack.Server().AddPeer(parsedNode)
    68  
    69  	return nil
    70  }
    71  
    72  func (w *gethNodeWrapper) RemovePeer(url string) error {
    73  	parsedNode, err := enode.ParseV4(url)
    74  	if err != nil {
    75  		return err
    76  	}
    77  
    78  	w.stack.Server().RemovePeer(parsedNode)
    79  
    80  	return nil
    81  }
    82  
    83  func (w *gethNodeWrapper) PeersCount() int {
    84  	if w.stack.Server() == nil {
    85  		return 0
    86  	}
    87  	return len(w.stack.Server().Peers())
    88  }