whipser: remove some warnings and fix formatting

This commit is contained in:
Guillaume Ballet 2018-03-02 23:16:34 +01:00
parent b92195e2b6
commit b226d5dd41
6 changed files with 26 additions and 12 deletions

View file

@ -20,22 +20,27 @@ import (
"github.com/ethereum/go-ethereum/p2p" "github.com/ethereum/go-ethereum/p2p"
) )
// DevP2PWhisperServer implements WhisperServer with a DevP2P backend
type DevP2PWhisperServer struct { type DevP2PWhisperServer struct {
Server *p2p.Server Server *p2p.Server
} }
// Start starts the server
func (server *DevP2PWhisperServer) Start() error { func (server *DevP2PWhisperServer) Start() error {
return server.Server.Start() return server.Server.Start()
} }
// Stop stops the server
func (server *DevP2PWhisperServer) Stop() { func (server *DevP2PWhisperServer) Stop() {
server.Server.Stop() server.Server.Stop()
} }
// PeerCount returns the peer count for the node
func (server *DevP2PWhisperServer) PeerCount() int { func (server *DevP2PWhisperServer) PeerCount() int {
return server.Server.PeerCount() return server.Server.PeerCount()
} }
// Enode returns the enode address of the node
func (server *DevP2PWhisperServer) Enode() string { func (server *DevP2PWhisperServer) Enode() string {
return server.Server.NodeInfo().Enode return server.Server.NodeInfo().Enode
} }

View file

@ -17,17 +17,18 @@
package whisperv6 package whisperv6
import ( import (
"context"
"bytes" "bytes"
"context"
"encoding/binary" "encoding/binary"
"fmt" "fmt"
"math" "math"
"github.com/ethereum/go-ethereum/p2p" "github.com/ethereum/go-ethereum/p2p"
libp2p "github.com/libp2p/go-libp2p" libp2p "github.com/libp2p/go-libp2p"
crypto "github.com/libp2p/go-libp2p-crypto"
host "github.com/libp2p/go-libp2p-host" host "github.com/libp2p/go-libp2p-host"
inet "github.com/libp2p/go-libp2p-net" 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 // LibP2PStream is a wrapper used to implement the MsgReadWriter
@ -112,20 +113,24 @@ func (stream *LibP2PStream) WriteMsg(msg p2p.Msg) error {
return nil return nil
} }
// LibP2PPeer implements Peer for libp2p
type LibP2PPeer struct { type LibP2PPeer struct {
PeerBase PeerBase
id peer.ID id peer.ID
} }
// ID returns the id of the peer
func (p *LibP2PPeer) ID() string { func (p *LibP2PPeer) ID() string {
return p.id.String() return p.id.String()
} }
// LibP2PWhisperServer implements WhisperServer for libp2p.
type LibP2PWhisperServer struct { type LibP2PWhisperServer struct {
Host host.Host Host host.Host
} }
// Start starts the server
func (server *LibP2PWhisperServer) Start() error { func (server *LibP2PWhisperServer) Start() error {
return nil return nil
} }
@ -145,12 +150,14 @@ func (server *LibP2PWhisperServer) Enode() string {
return server.Host.Addrs()[0].String() return server.Host.Addrs()[0].String()
} }
// NewLibP2PWhisperServer creates a new WhisperServer with
// a libp2p backend.
func NewLibP2PWhisperServer() (WhisperServer, error) { func NewLibP2PWhisperServer() (WhisperServer, error) {
priv, _, err := crypto.GenerateKeyPair(crypto.Ed25519, 384) priv, _, err := crypto.GenerateKeyPair(crypto.Ed25519, 384)
opts := []libp2p.Option{ opts := []libp2p.Option{
libp2p.ListenAddrStrings(fmt.Sprintf("/ip4/0.0.0.0/tcp/%d", WhisperPort)), libp2p.ListenAddrStrings(fmt.Sprintf("/ip4/0.0.0.0/tcp/%d", WhisperPort)),
libp2p.Identity(priv), libp2p.Identity(priv),
} }
h, err := libp2p.New(context.Background(), opts...) h, err := libp2p.New(context.Background(), opts...)
if err != nil { if err != nil {

View file

@ -17,10 +17,10 @@
package whisperv6 package whisperv6
import ( import (
"io/ioutil"
"bytes" "bytes"
"context" "context"
"encoding/binary" "encoding/binary"
"io/ioutil"
"math" "math"
"math/rand" "math/rand"
"testing" "testing"
@ -282,7 +282,7 @@ func TestMaxReadSize(t *testing.T) {
ctx := context.Background() ctx := context.Background()
hosts := createTestNetwork(ctx, t, 2) hosts := createTestNetwork(ctx, t, 2)
hosts[0].SetStreamHandler(testProtocolID, func (s inet.Stream) { hosts[0].SetStreamHandler(testProtocolID, func(s inet.Stream) {
defer s.Close() defer s.Close()
lps := LibP2PStream{ lps := LibP2PStream{

View file

@ -79,7 +79,7 @@ type DevP2PPeer struct {
// newPeer creates a new whisper peer object, but does not run the handshake itself. // 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 { func newPeer(host *Whisper, remote *p2p.Peer, rw p2p.MsgReadWriter) Peer {
return &DevP2PPeer{ return &DevP2PPeer{
&PeerBase { &PeerBase{
host: host, host: host,
ws: rw, ws: rw,
trusted: false, trusted: false,

View file

@ -56,6 +56,8 @@ const (
bloomFilterToleranceIdx // Bloom filter tolerated by the whisper node for a limited time 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 { type WhisperServer interface {
Start() error Start() error
Stop() Stop()
@ -77,8 +79,8 @@ type Whisper struct {
envelopes map[common.Hash]*Envelope // Pool of envelopes currently tracked by this node envelopes map[common.Hash]*Envelope // Pool of envelopes currently tracked by this node
expirations map[uint32]*set.SetNonTS // Message expiration pool expirations map[uint32]*set.SetNonTS // Message expiration pool
peerMu sync.RWMutex // Mutex to sync the active peer set peerMu sync.RWMutex // Mutex to sync the active peer set
peers map[Peer]struct{} // Set of currently active peers peers map[Peer]struct{} // Set of currently active peers
messageQueue chan *Envelope // Message queue for normal whisper messages 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) p2pMsgQueue chan *Envelope // Message queue for peer-to-peer messages (not to be forwarded any further)

View file

@ -17,10 +17,10 @@
package whisperv6 package whisperv6
import ( import (
"fmt"
"bytes" "bytes"
"crypto/ecdsa" "crypto/ecdsa"
"crypto/sha256" "crypto/sha256"
"fmt"
mrand "math/rand" mrand "math/rand"
"testing" "testing"
"time" "time"