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"
)
// 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
}
}

View file

@ -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
}
}

View file

@ -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{

View file

@ -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,

View file

@ -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)

View file

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