diff --git a/whisper/whisperv6/devp2p_glue.go b/whisper/whisperv6/devp2p_glue.go index 6d36011151..8625942ffa 100644 --- a/whisper/whisperv6/devp2p_glue.go +++ b/whisper/whisperv6/devp2p_glue.go @@ -20,22 +20,27 @@ import ( "github.com/ethereum/go-ethereum/p2p" ) +// DevP2PWhisperServer implements WhisperServer with a DevP2P backend type DevP2PWhisperServer struct { Server *p2p.Server } +// Start starts the server func (server *DevP2PWhisperServer) Start() error { return server.Server.Start() } +// Stop stops the server func (server *DevP2PWhisperServer) Stop() { server.Server.Stop() } +// PeerCount returns the peer count for the node func (server *DevP2PWhisperServer) PeerCount() int { return server.Server.PeerCount() } +// Enode returns the enode address of the node func (server *DevP2PWhisperServer) Enode() string { return server.Server.NodeInfo().Enode -} \ No newline at end of file +} diff --git a/whisper/whisperv6/libp2p_glue.go b/whisper/whisperv6/libp2p_glue.go index ea4e12600e..cb5a2cbccc 100644 --- a/whisper/whisperv6/libp2p_glue.go +++ b/whisper/whisperv6/libp2p_glue.go @@ -17,17 +17,18 @@ package whisperv6 import ( - "context" "bytes" + "context" "encoding/binary" "fmt" "math" "github.com/ethereum/go-ethereum/p2p" libp2p "github.com/libp2p/go-libp2p" + crypto "github.com/libp2p/go-libp2p-crypto" host "github.com/libp2p/go-libp2p-host" inet "github.com/libp2p/go-libp2p-net" - crypto "github.com/libp2p/go-libp2p-crypto" + peer "github.com/libp2p/go-libp2p-peer" ) // LibP2PStream is a wrapper used to implement the MsgReadWriter @@ -112,20 +113,24 @@ func (stream *LibP2PStream) WriteMsg(msg p2p.Msg) error { return nil } +// LibP2PPeer implements Peer for libp2p type LibP2PPeer struct { PeerBase id peer.ID } +// ID returns the id of the peer func (p *LibP2PPeer) ID() string { return p.id.String() } +// LibP2PWhisperServer implements WhisperServer for libp2p. type LibP2PWhisperServer struct { - Host host.Host + Host host.Host } +// Start starts the server func (server *LibP2PWhisperServer) Start() error { return nil } @@ -145,16 +150,18 @@ func (server *LibP2PWhisperServer) Enode() string { return server.Host.Addrs()[0].String() } +// NewLibP2PWhisperServer creates a new WhisperServer with +// a libp2p backend. func NewLibP2PWhisperServer() (WhisperServer, error) { priv, _, err := crypto.GenerateKeyPair(crypto.Ed25519, 384) opts := []libp2p.Option{ libp2p.ListenAddrStrings(fmt.Sprintf("/ip4/0.0.0.0/tcp/%d", WhisperPort)), libp2p.Identity(priv), -} + } h, err := libp2p.New(context.Background(), opts...) if err != nil { return nil, fmt.Errorf("Error setting up the libp2p network: %s", err) } return &LibP2PWhisperServer{h}, nil -} \ No newline at end of file +} diff --git a/whisper/whisperv6/libp2p_glue_test.go b/whisper/whisperv6/libp2p_glue_test.go index e7f8f3eb89..ec46f3ed26 100644 --- a/whisper/whisperv6/libp2p_glue_test.go +++ b/whisper/whisperv6/libp2p_glue_test.go @@ -17,10 +17,10 @@ package whisperv6 import ( - "io/ioutil" "bytes" "context" "encoding/binary" + "io/ioutil" "math" "math/rand" "testing" @@ -282,7 +282,7 @@ func TestMaxReadSize(t *testing.T) { ctx := context.Background() hosts := createTestNetwork(ctx, t, 2) - hosts[0].SetStreamHandler(testProtocolID, func (s inet.Stream) { + hosts[0].SetStreamHandler(testProtocolID, func(s inet.Stream) { defer s.Close() lps := LibP2PStream{ diff --git a/whisper/whisperv6/peer.go b/whisper/whisperv6/peer.go index 81982f80b3..76d04976eb 100644 --- a/whisper/whisperv6/peer.go +++ b/whisper/whisperv6/peer.go @@ -79,7 +79,7 @@ type DevP2PPeer struct { // newPeer creates a new whisper peer object, but does not run the handshake itself. func newPeer(host *Whisper, remote *p2p.Peer, rw p2p.MsgReadWriter) Peer { return &DevP2PPeer{ - &PeerBase { + &PeerBase{ host: host, ws: rw, trusted: false, diff --git a/whisper/whisperv6/whisper.go b/whisper/whisperv6/whisper.go index eb0dc6ec47..2bfd32b96a 100644 --- a/whisper/whisperv6/whisper.go +++ b/whisper/whisperv6/whisper.go @@ -56,6 +56,8 @@ const ( bloomFilterToleranceIdx // Bloom filter tolerated by the whisper node for a limited time ) +// WhisperServer abstracts a server, which could be either DevP2p-based +// or libp2p-based. type WhisperServer interface { Start() error Stop() @@ -77,8 +79,8 @@ type Whisper struct { envelopes map[common.Hash]*Envelope // Pool of envelopes currently tracked by this node expirations map[uint32]*set.SetNonTS // Message expiration pool - peerMu sync.RWMutex // Mutex to sync the active peer set - peers map[Peer]struct{} // Set of currently active peers + peerMu sync.RWMutex // Mutex to sync the active peer set + peers map[Peer]struct{} // Set of currently active peers messageQueue chan *Envelope // Message queue for normal whisper messages p2pMsgQueue chan *Envelope // Message queue for peer-to-peer messages (not to be forwarded any further) diff --git a/whisper/whisperv6/whisper_test.go b/whisper/whisperv6/whisper_test.go index e46ac92196..4aa0ed982e 100644 --- a/whisper/whisperv6/whisper_test.go +++ b/whisper/whisperv6/whisper_test.go @@ -17,10 +17,10 @@ package whisperv6 import ( - "fmt" "bytes" "crypto/ecdsa" "crypto/sha256" + "fmt" mrand "math/rand" "testing" "time"