mirror of
https://github.com/ethereum/go-ethereum.git
synced 2026-08-18 18:02:24 +00:00
whisper: Initial wnode support for libp2p
This doesn't currently handle streams, peers nor the api.
This commit is contained in:
parent
c2499c7dad
commit
842b6b85ad
5 changed files with 82 additions and 25 deletions
|
|
@ -53,7 +53,7 @@ const entropySize = 32
|
||||||
|
|
||||||
// singletons
|
// singletons
|
||||||
var (
|
var (
|
||||||
server WhisperServer
|
server whisper.WhisperServer
|
||||||
shh *whisper.Whisper
|
shh *whisper.Whisper
|
||||||
done chan struct{}
|
done chan struct{}
|
||||||
mailServer mailserver.WMailServer
|
mailServer mailserver.WMailServer
|
||||||
|
|
@ -277,21 +277,26 @@ func initialize() {
|
||||||
}
|
}
|
||||||
|
|
||||||
if *useLibP2P {
|
if *useLibP2P {
|
||||||
server = NewLibP2PWhisperServer()
|
server, err = whisper.NewLibP2PWhisperServer()
|
||||||
|
if err != nil {
|
||||||
|
utils.Fatalf("Error starting the libp2p client: %v", err)
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
server = &p2p.Server{
|
server = &whisper.DevP2PWhisperServer{
|
||||||
Config: p2p.Config{
|
&p2p.Server{
|
||||||
PrivateKey: nodeid,
|
Config: p2p.Config{
|
||||||
MaxPeers: maxPeers,
|
PrivateKey: nodeid,
|
||||||
Name: common.MakeName("wnode", "6.1"),
|
MaxPeers: maxPeers,
|
||||||
Protocols: shh.Protocols(),
|
Name: common.MakeName("wnode", "6.1"),
|
||||||
ListenAddr: *argIP,
|
Protocols: shh.Protocols(),
|
||||||
NAT: nat.Any(),
|
ListenAddr: *argIP,
|
||||||
BootstrapNodes: peers,
|
NAT: nat.Any(),
|
||||||
StaticNodes: peers,
|
BootstrapNodes: peers,
|
||||||
TrustedNodes: peers,
|
StaticNodes: peers,
|
||||||
|
TrustedNodes: peers,
|
||||||
|
},
|
||||||
},
|
},
|
||||||
},
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -303,7 +308,7 @@ func startServer() error {
|
||||||
}
|
}
|
||||||
|
|
||||||
fmt.Printf("my public key: %s \n", common.ToHex(crypto.FromECDSAPub(&asymKey.PublicKey)))
|
fmt.Printf("my public key: %s \n", common.ToHex(crypto.FromECDSAPub(&asymKey.PublicKey)))
|
||||||
fmt.Println(server.NodeInfo().Enode)
|
fmt.Println(server.Enode())
|
||||||
|
|
||||||
if *bootstrapMode {
|
if *bootstrapMode {
|
||||||
configureNode()
|
configureNode()
|
||||||
|
|
@ -686,7 +691,8 @@ func writeMessageToFile(dir string, msg *whisper.ReceivedMessage, show bool) {
|
||||||
}
|
}
|
||||||
|
|
||||||
func requestExpiredMessagesLoop() {
|
func requestExpiredMessagesLoop() {
|
||||||
var key, peerID, bloom []byte
|
var key, bloom []byte
|
||||||
|
var peerID string
|
||||||
var timeLow, timeUpp uint32
|
var timeLow, timeUpp uint32
|
||||||
var t string
|
var t string
|
||||||
var xt whisper.TopicType
|
var xt whisper.TopicType
|
||||||
|
|
@ -756,12 +762,15 @@ func requestExpiredMessagesLoop() {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func extractIDFromEnode(s string) []byte {
|
func extractIDFromEnode(s string) string {
|
||||||
n, err := discover.ParseNode(s)
|
if !*useLibP2P {
|
||||||
if err != nil {
|
n, err := discover.ParseNode(s)
|
||||||
utils.Fatalf("Failed to parse enode: %s", err)
|
if err != nil {
|
||||||
|
utils.Fatalf("Failed to parse enode: %s", err)
|
||||||
|
}
|
||||||
|
return n.ID.String()
|
||||||
}
|
}
|
||||||
return n.ID[:]
|
return ""
|
||||||
}
|
}
|
||||||
|
|
||||||
// obfuscateBloom adds 16 random bits to the the bloom
|
// obfuscateBloom adds 16 random bits to the the bloom
|
||||||
|
|
|
||||||
|
|
@ -21,9 +21,21 @@ import (
|
||||||
)
|
)
|
||||||
|
|
||||||
type DevP2PWhisperServer struct {
|
type DevP2PWhisperServer struct {
|
||||||
server *p2p.Server
|
Server *p2p.Server
|
||||||
}
|
}
|
||||||
|
|
||||||
func (server *DevP2PWhisperServer) Start() error {
|
func (server *DevP2PWhisperServer) Start() error {
|
||||||
return server.Start()
|
return server.Server.Start()
|
||||||
|
}
|
||||||
|
|
||||||
|
func (server *DevP2PWhisperServer) Stop() {
|
||||||
|
server.Server.Stop()
|
||||||
|
}
|
||||||
|
|
||||||
|
func (server *DevP2PWhisperServer) PeerCount() int {
|
||||||
|
return server.Server.PeerCount()
|
||||||
|
}
|
||||||
|
|
||||||
|
func (server *DevP2PWhisperServer) Enode() string {
|
||||||
|
return server.Server.NodeInfo().Enode
|
||||||
}
|
}
|
||||||
|
|
@ -77,6 +77,9 @@ const (
|
||||||
|
|
||||||
DefaultTTL = 50 // seconds
|
DefaultTTL = 50 // seconds
|
||||||
DefaultSyncAllowance = 10 // seconds
|
DefaultSyncAllowance = 10 // seconds
|
||||||
|
|
||||||
|
WhisperPort = 5348
|
||||||
|
WhisperProtocolString = "/whisper/6.1"
|
||||||
)
|
)
|
||||||
|
|
||||||
type unknownVersionError uint64
|
type unknownVersionError uint64
|
||||||
|
|
|
||||||
|
|
@ -17,13 +17,17 @@
|
||||||
package whisperv6
|
package whisperv6
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"context"
|
||||||
"bytes"
|
"bytes"
|
||||||
"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"
|
||||||
|
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"
|
||||||
)
|
)
|
||||||
|
|
||||||
// LibP2PStream is a wrapper used to implement the MsgReadWriter
|
// LibP2PStream is a wrapper used to implement the MsgReadWriter
|
||||||
|
|
@ -109,12 +113,38 @@ func (stream *LibP2PStream) WriteMsg(msg p2p.Msg) error {
|
||||||
}
|
}
|
||||||
|
|
||||||
type LibP2PWhisperServer struct {
|
type LibP2PWhisperServer struct {
|
||||||
|
Host host.Host
|
||||||
}
|
}
|
||||||
|
|
||||||
func (server *LibP2PWhisperServer) Start() error {
|
func (server *LibP2PWhisperServer) Start() error {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewLibP2PWhisperServer() WhisperServer {
|
// Stop stops the server
|
||||||
return &LibP2PWhisperServer{}
|
func (server *LibP2PWhisperServer) Stop() {
|
||||||
|
server.Host.Close()
|
||||||
|
}
|
||||||
|
|
||||||
|
// PeerCount returns the peer count for the node
|
||||||
|
func (server *LibP2PWhisperServer) PeerCount() int {
|
||||||
|
return 0
|
||||||
|
}
|
||||||
|
|
||||||
|
// Enode returns the enode address of the node
|
||||||
|
func (server *LibP2PWhisperServer) Enode() string {
|
||||||
|
return server.Host.Addrs()[0].String()
|
||||||
|
}
|
||||||
|
|
||||||
|
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
|
||||||
}
|
}
|
||||||
|
|
@ -58,6 +58,9 @@ const (
|
||||||
|
|
||||||
type WhisperServer interface {
|
type WhisperServer interface {
|
||||||
Start() error
|
Start() error
|
||||||
|
Stop()
|
||||||
|
PeerCount() int
|
||||||
|
Enode() string
|
||||||
}
|
}
|
||||||
|
|
||||||
// Whisper represents a dark communication interface through the Ethereum
|
// Whisper represents a dark communication interface through the Ethereum
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue