whisper: interface changed to simplify the transition to v5

* whisper: mailserver test introduced, refactoring

* whisper: validation test updated

* whisper: max number of peers fixed

* whisper: verification bug fixed

* whisper: esthetic fix

* whisper: interface changed to simplify the transition to v5

* whisper: preparation for version switch
This commit is contained in:
gluk256 2017-02-14 15:44:47 +01:00 committed by Péter Szilágyi
parent 72dcd3c58b
commit 15a609d5d6
9 changed files with 96 additions and 90 deletions

View File

@ -198,10 +198,11 @@ func initialize() {
utils.Fatalf("Failed to read Mail Server password: %s", err) utils.Fatalf("Failed to read Mail Server password: %s", err)
} }
} }
shh = whisper.NewWhisper(&mailServer) shh = whisper.New()
shh.RegisterServer(&mailServer)
mailServer.Init(shh, *argDBPath, msPassword, *argServerPoW) mailServer.Init(shh, *argDBPath, msPassword, *argServerPoW)
} else { } else {
shh = whisper.NewWhisper(nil) shh = whisper.New()
} }
asymKey = shh.NewIdentity() asymKey = shh.NewIdentity()

View File

@ -32,7 +32,7 @@ import (
"github.com/ethereum/go-ethereum/node" "github.com/ethereum/go-ethereum/node"
"github.com/ethereum/go-ethereum/p2p/nat" "github.com/ethereum/go-ethereum/p2p/nat"
"github.com/ethereum/go-ethereum/params" "github.com/ethereum/go-ethereum/params"
"github.com/ethereum/go-ethereum/whisper/whisperv2" whisper "github.com/ethereum/go-ethereum/whisper/whisperv2"
) )
// NodeConfig represents the collection of configuration values to fine tune the Geth // NodeConfig represents the collection of configuration values to fine tune the Geth
@ -172,7 +172,7 @@ func NewNode(datadir string, config *NodeConfig) (stack *Node, _ error) {
} }
// Register the Whisper protocol if requested // Register the Whisper protocol if requested
if config.WhisperEnabled { if config.WhisperEnabled {
if err := rawStack.Register(func(*node.ServiceContext) (node.Service, error) { return whisperv2.New(), nil }); err != nil { if err := rawStack.Register(func(*node.ServiceContext) (node.Service, error) { return whisper.New(), nil }); err != nil {
return nil, fmt.Errorf("whisper init: %v", err) return nil, fmt.Errorf("whisper init: %v", err)
} }
} }

View File

@ -84,7 +84,9 @@ func TestMailServer(t *testing.T) {
} }
var server WMailServer var server WMailServer
shh = whisper.NewWhisper(&server) shh = whisper.New()
shh.RegisterServer(&server)
server.Init(shh, dbPath, password, powRequirement) server.Init(shh, dbPath, password, powRequirement)
defer server.Close() defer server.Close()

View File

@ -14,7 +14,7 @@
// You should have received a copy of the GNU Lesser General Public License // You should have received a copy of the GNU Lesser General Public License
// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>. // along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
package shhapi package whisperv5
import ( import (
"encoding/json" "encoding/json"
@ -27,35 +27,20 @@ import (
"github.com/ethereum/go-ethereum/crypto" "github.com/ethereum/go-ethereum/crypto"
"github.com/ethereum/go-ethereum/logger" "github.com/ethereum/go-ethereum/logger"
"github.com/ethereum/go-ethereum/logger/glog" "github.com/ethereum/go-ethereum/logger/glog"
"github.com/ethereum/go-ethereum/rpc"
"github.com/ethereum/go-ethereum/whisper/whisperv5"
) )
var whisperOffLineErr = errors.New("whisper is offline") var whisperOffLineErr = errors.New("whisper is offline")
// PublicWhisperAPI provides the whisper RPC service. // PublicWhisperAPI provides the whisper RPC service.
type PublicWhisperAPI struct { type PublicWhisperAPI struct {
whisper *whisperv5.Whisper whisper *Whisper
} }
// NewPublicWhisperAPI create a new RPC whisper service. // NewPublicWhisperAPI create a new RPC whisper service.
func NewPublicWhisperAPI() *PublicWhisperAPI { func NewPublicWhisperAPI(w *Whisper) *PublicWhisperAPI {
w := whisperv5.NewWhisper(nil)
return &PublicWhisperAPI{whisper: w} return &PublicWhisperAPI{whisper: w}
} }
// APIs returns the RPC descriptors the Whisper implementation offers
func APIs() []rpc.API {
return []rpc.API{
{
Namespace: whisperv5.ProtocolName,
Version: whisperv5.ProtocolVersionStr,
Service: NewPublicWhisperAPI(),
Public: true,
},
}
}
// Start starts the Whisper worker threads. // Start starts the Whisper worker threads.
func (api *PublicWhisperAPI) Start() error { func (api *PublicWhisperAPI) Start() error {
if api.whisper == nil { if api.whisper == nil {
@ -171,11 +156,11 @@ func (api *PublicWhisperAPI) NewFilter(args WhisperFilterArgs) (uint32, error) {
return 0, whisperOffLineErr return 0, whisperOffLineErr
} }
filter := whisperv5.Filter{ filter := Filter{
Src: crypto.ToECDSAPub(common.FromHex(args.From)), Src: crypto.ToECDSAPub(common.FromHex(args.From)),
KeySym: api.whisper.GetSymKey(args.KeyName), KeySym: api.whisper.GetSymKey(args.KeyName),
PoW: args.PoW, PoW: args.PoW,
Messages: make(map[common.Hash]*whisperv5.ReceivedMessage), Messages: make(map[common.Hash]*ReceivedMessage),
AcceptP2P: args.AcceptP2P, AcceptP2P: args.AcceptP2P,
} }
if len(filter.KeySym) > 0 { if len(filter.KeySym) > 0 {
@ -209,7 +194,7 @@ func (api *PublicWhisperAPI) NewFilter(args WhisperFilterArgs) (uint32, error) {
if len(args.To) > 0 { if len(args.To) > 0 {
dst := crypto.ToECDSAPub(common.FromHex(args.To)) dst := crypto.ToECDSAPub(common.FromHex(args.To))
if !whisperv5.ValidatePublicKey(dst) { if !ValidatePublicKey(dst) {
info := "NewFilter: Invalid 'To' address" info := "NewFilter: Invalid 'To' address"
glog.V(logger.Error).Infof(info) glog.V(logger.Error).Infof(info)
return 0, errors.New(info) return 0, errors.New(info)
@ -223,7 +208,7 @@ func (api *PublicWhisperAPI) NewFilter(args WhisperFilterArgs) (uint32, error) {
} }
if len(args.From) > 0 { if len(args.From) > 0 {
if !whisperv5.ValidatePublicKey(filter.Src) { if !ValidatePublicKey(filter.Src) {
info := "NewFilter: Invalid 'From' address" info := "NewFilter: Invalid 'From' address"
glog.V(logger.Error).Infof(info) glog.V(logger.Error).Infof(info)
return 0, errors.New(info) return 0, errors.New(info)
@ -256,7 +241,7 @@ func (api *PublicWhisperAPI) GetMessages(filterId uint32) []WhisperMessage {
} }
// toWhisperMessages converts a Whisper message to a RPC whisper message. // toWhisperMessages converts a Whisper message to a RPC whisper message.
func toWhisperMessages(messages []*whisperv5.ReceivedMessage) []WhisperMessage { func toWhisperMessages(messages []*ReceivedMessage) []WhisperMessage {
msgs := make([]WhisperMessage, len(messages)) msgs := make([]WhisperMessage, len(messages))
for i, msg := range messages { for i, msg := range messages {
msgs[i] = NewWhisperMessage(msg) msgs[i] = NewWhisperMessage(msg)
@ -270,7 +255,7 @@ func (api *PublicWhisperAPI) Post(args PostArgs) error {
return whisperOffLineErr return whisperOffLineErr
} }
params := whisperv5.MessageParams{ params := MessageParams{
TTL: args.TTL, TTL: args.TTL,
Dst: crypto.ToECDSAPub(common.FromHex(args.To)), Dst: crypto.ToECDSAPub(common.FromHex(args.To)),
KeySym: api.whisper.GetSymKey(args.KeyName), KeySym: api.whisper.GetSymKey(args.KeyName),
@ -283,7 +268,7 @@ func (api *PublicWhisperAPI) Post(args PostArgs) error {
if len(args.From) > 0 { if len(args.From) > 0 {
pub := crypto.ToECDSAPub(common.FromHex(args.From)) pub := crypto.ToECDSAPub(common.FromHex(args.From))
if !whisperv5.ValidatePublicKey(pub) { if !ValidatePublicKey(pub) {
info := "Post: Invalid 'From' address" info := "Post: Invalid 'From' address"
glog.V(logger.Error).Infof(info) glog.V(logger.Error).Infof(info)
return errors.New(info) return errors.New(info)
@ -311,7 +296,7 @@ func (api *PublicWhisperAPI) Post(args PostArgs) error {
if params.Src == nil && filter.Src != nil { if params.Src == nil && filter.Src != nil {
params.Src = filter.KeyAsym params.Src = filter.KeyAsym
} }
if (params.Topic == whisperv5.TopicType{}) { if (params.Topic == TopicType{}) {
sz := len(filter.Topics) sz := len(filter.Topics)
if sz < 1 { if sz < 1 {
info := fmt.Sprintf("Post: no topics in filter # %d", args.FilterID) info := fmt.Sprintf("Post: no topics in filter # %d", args.FilterID)
@ -347,7 +332,7 @@ func (api *PublicWhisperAPI) Post(args PostArgs) error {
} }
if len(args.To) > 0 { if len(args.To) > 0 {
if !whisperv5.ValidatePublicKey(params.Dst) { if !ValidatePublicKey(params.Dst) {
info := "Post: Invalid 'To' address" info := "Post: Invalid 'To' address"
glog.V(logger.Error).Infof(info) glog.V(logger.Error).Infof(info)
return errors.New(info) return errors.New(info)
@ -355,18 +340,18 @@ func (api *PublicWhisperAPI) Post(args PostArgs) error {
} }
// encrypt and send // encrypt and send
message := whisperv5.NewSentMessage(&params) message := NewSentMessage(&params)
envelope, err := message.Wrap(&params) envelope, err := message.Wrap(&params)
if err != nil { if err != nil {
glog.V(logger.Error).Infof(err.Error()) glog.V(logger.Error).Infof(err.Error())
return err return err
} }
if len(envelope.Data) > whisperv5.MaxMessageLength { if len(envelope.Data) > MaxMessageLength {
info := "Post: message is too big" info := "Post: message is too big"
glog.V(logger.Error).Infof(info) glog.V(logger.Error).Infof(info)
return errors.New(info) return errors.New(info)
} }
if (envelope.Topic == whisperv5.TopicType{} && envelope.IsSymmetric()) { if (envelope.Topic == TopicType{} && envelope.IsSymmetric()) {
info := "Post: topic is missing for symmetric encryption" info := "Post: topic is missing for symmetric encryption"
glog.V(logger.Error).Infof(info) glog.V(logger.Error).Infof(info)
return errors.New(info) return errors.New(info)
@ -384,7 +369,7 @@ type PostArgs struct {
From string `json:"from"` From string `json:"from"`
To string `json:"to"` To string `json:"to"`
KeyName string `json:"keyname"` KeyName string `json:"keyname"`
Topic whisperv5.TopicType `json:"topic"` Topic TopicType `json:"topic"`
Padding hexutil.Bytes `json:"padding"` Padding hexutil.Bytes `json:"padding"`
Payload hexutil.Bytes `json:"payload"` Payload hexutil.Bytes `json:"payload"`
WorkTime uint32 `json:"worktime"` WorkTime uint32 `json:"worktime"`
@ -398,7 +383,7 @@ type WhisperFilterArgs struct {
From string From string
KeyName string KeyName string
PoW float64 PoW float64
Topics []whisperv5.TopicType Topics []TopicType
AcceptP2P bool AcceptP2P bool
} }
@ -437,13 +422,13 @@ func (args *WhisperFilterArgs) UnmarshalJSON(b []byte) (err error) {
return fmt.Errorf("topic[%d] is not a string", i) return fmt.Errorf("topic[%d] is not a string", i)
} }
} }
topicsDecoded := make([]whisperv5.TopicType, len(topics)) topicsDecoded := make([]TopicType, len(topics))
for j, s := range topics { for j, s := range topics {
x := common.FromHex(s) x := common.FromHex(s)
if x == nil || len(x) != whisperv5.TopicLength { if x == nil || len(x) != TopicLength {
return fmt.Errorf("topic[%d] is invalid", j) return fmt.Errorf("topic[%d] is invalid", j)
} }
topicsDecoded[j] = whisperv5.BytesToTopic(x) topicsDecoded[j] = BytesToTopic(x)
} }
args.Topics = topicsDecoded args.Topics = topicsDecoded
} }
@ -464,7 +449,7 @@ type WhisperMessage struct {
} }
// NewWhisperMessage converts an internal message into an API version. // NewWhisperMessage converts an internal message into an API version.
func NewWhisperMessage(message *whisperv5.ReceivedMessage) WhisperMessage { func NewWhisperMessage(message *ReceivedMessage) WhisperMessage {
return WhisperMessage{ return WhisperMessage{
Payload: common.ToHex(message.Payload), Payload: common.ToHex(message.Payload),
Padding: common.ToHex(message.Padding), Padding: common.ToHex(message.Padding),

View File

@ -14,22 +14,21 @@
// You should have received a copy of the GNU Lesser General Public License // You should have received a copy of the GNU Lesser General Public License
// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>. // along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
package shhapi package whisperv5
import ( import (
"bytes" "bytes"
"encoding/json"
"testing" "testing"
"time" "time"
"encoding/json"
"github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/whisper/whisperv5"
) )
func TestBasic(t *testing.T) { func TestBasic(t *testing.T) {
var id string = "test" var id string = "test"
api := NewPublicWhisperAPI() w := New()
api := NewPublicWhisperAPI(w)
if api == nil { if api == nil {
t.Fatalf("failed to create API.") t.Fatalf("failed to create API.")
} }
@ -39,7 +38,7 @@ func TestBasic(t *testing.T) {
t.Fatalf("failed generateFilter: %s.", err) t.Fatalf("failed generateFilter: %s.", err)
} }
if uint64(ver) != whisperv5.ProtocolVersion { if uint64(ver) != ProtocolVersion {
t.Fatalf("wrong version: %d.", ver) t.Fatalf("wrong version: %d.", ver)
} }
@ -182,22 +181,22 @@ func TestUnmarshalFilterArgs(t *testing.T) {
} }
i := 0 i := 0
if f.Topics[i] != (whisperv5.TopicType{0x00, 0x00, 0x00, 0x00}) { if f.Topics[i] != (TopicType{0x00, 0x00, 0x00, 0x00}) {
t.Fatalf("wrong topic[%d]: %x.", i, f.Topics[i]) t.Fatalf("wrong topic[%d]: %x.", i, f.Topics[i])
} }
i++ i++
if f.Topics[i] != (whisperv5.TopicType{0x00, 0x7f, 0x80, 0xff}) { if f.Topics[i] != (TopicType{0x00, 0x7f, 0x80, 0xff}) {
t.Fatalf("wrong topic[%d]: %x.", i, f.Topics[i]) t.Fatalf("wrong topic[%d]: %x.", i, f.Topics[i])
} }
i++ i++
if f.Topics[i] != (whisperv5.TopicType{0xff, 0x80, 0x7f, 0x00}) { if f.Topics[i] != (TopicType{0xff, 0x80, 0x7f, 0x00}) {
t.Fatalf("wrong topic[%d]: %x.", i, f.Topics[i]) t.Fatalf("wrong topic[%d]: %x.", i, f.Topics[i])
} }
i++ i++
if f.Topics[i] != (whisperv5.TopicType{0xf2, 0x6e, 0x77, 0x79}) { if f.Topics[i] != (TopicType{0xf2, 0x6e, 0x77, 0x79}) {
t.Fatalf("wrong topic[%d]: %x.", i, f.Topics[i]) t.Fatalf("wrong topic[%d]: %x.", i, f.Topics[i])
} }
} }
@ -235,7 +234,7 @@ func TestUnmarshalPostArgs(t *testing.T) {
if a.KeyName != "shh_test" { if a.KeyName != "shh_test" {
t.Fatalf("wrong KeyName: %s.", a.KeyName) t.Fatalf("wrong KeyName: %s.", a.KeyName)
} }
if a.Topic != (whisperv5.TopicType{0xf2, 0x6e, 0x77, 0x79}) { if a.Topic != (TopicType{0xf2, 0x6e, 0x77, 0x79}) {
t.Fatalf("wrong topic: %x.", a.Topic) t.Fatalf("wrong topic: %x.", a.Topic)
} }
if string(a.Padding) != "this is my test string" { if string(a.Padding) != "this is my test string" {
@ -272,7 +271,8 @@ func waitForMessage(api *PublicWhisperAPI, id uint32, target int) bool {
} }
func TestIntegrationAsym(t *testing.T) { func TestIntegrationAsym(t *testing.T) {
api := NewPublicWhisperAPI() w := New()
api := NewPublicWhisperAPI(w)
if api == nil { if api == nil {
t.Fatalf("failed to create API.") t.Fatalf("failed to create API.")
} }
@ -304,14 +304,14 @@ func TestIntegrationAsym(t *testing.T) {
t.Fatalf("wrong key") t.Fatalf("wrong key")
} }
var topics [2]whisperv5.TopicType var topics [2]TopicType
topics[0] = whisperv5.TopicType{0x00, 0x64, 0x00, 0xff} topics[0] = TopicType{0x00, 0x64, 0x00, 0xff}
topics[1] = whisperv5.TopicType{0xf2, 0x6e, 0x77, 0x79} topics[1] = TopicType{0xf2, 0x6e, 0x77, 0x79}
var f WhisperFilterArgs var f WhisperFilterArgs
f.To = key f.To = key
f.From = sig f.From = sig
f.Topics = topics[:] f.Topics = topics[:]
f.PoW = whisperv5.MinimumPoW / 2 f.PoW = MinimumPoW / 2
f.AcceptP2P = true f.AcceptP2P = true
id, err := api.NewFilter(f) id, err := api.NewFilter(f)
@ -325,8 +325,8 @@ func TestIntegrationAsym(t *testing.T) {
p.To = f.To p.To = f.To
p.Padding = []byte("test string") p.Padding = []byte("test string")
p.Payload = []byte("extended test string") p.Payload = []byte("extended test string")
p.PoW = whisperv5.MinimumPoW p.PoW = MinimumPoW
p.Topic = whisperv5.TopicType{0xf2, 0x6e, 0x77, 0x79} p.Topic = TopicType{0xf2, 0x6e, 0x77, 0x79}
p.WorkTime = 2 p.WorkTime = 2
err = api.Post(p) err = api.Post(p)
@ -373,7 +373,8 @@ func TestIntegrationAsym(t *testing.T) {
} }
func TestIntegrationSym(t *testing.T) { func TestIntegrationSym(t *testing.T) {
api := NewPublicWhisperAPI() w := New()
api := NewPublicWhisperAPI(w)
if api == nil { if api == nil {
t.Fatalf("failed to create API.") t.Fatalf("failed to create API.")
} }
@ -403,9 +404,9 @@ func TestIntegrationSym(t *testing.T) {
t.Fatalf("failed HasIdentity: false negative.") t.Fatalf("failed HasIdentity: false negative.")
} }
var topics [2]whisperv5.TopicType var topics [2]TopicType
topics[0] = whisperv5.TopicType{0x00, 0x7f, 0x80, 0xff} topics[0] = TopicType{0x00, 0x7f, 0x80, 0xff}
topics[1] = whisperv5.TopicType{0xf2, 0x6e, 0x77, 0x79} topics[1] = TopicType{0xf2, 0x6e, 0x77, 0x79}
var f WhisperFilterArgs var f WhisperFilterArgs
f.KeyName = keyname f.KeyName = keyname
f.Topics = topics[:] f.Topics = topics[:]
@ -424,8 +425,8 @@ func TestIntegrationSym(t *testing.T) {
p.From = f.From p.From = f.From
p.Padding = []byte("test string") p.Padding = []byte("test string")
p.Payload = []byte("extended test string") p.Payload = []byte("extended test string")
p.PoW = whisperv5.MinimumPoW p.PoW = MinimumPoW
p.Topic = whisperv5.TopicType{0xf2, 0x6e, 0x77, 0x79} p.Topic = TopicType{0xf2, 0x6e, 0x77, 0x79}
p.WorkTime = 2 p.WorkTime = 2
err = api.Post(p) err = api.Post(p)
@ -472,7 +473,8 @@ func TestIntegrationSym(t *testing.T) {
} }
func TestIntegrationSymWithFilter(t *testing.T) { func TestIntegrationSymWithFilter(t *testing.T) {
api := NewPublicWhisperAPI() w := New()
api := NewPublicWhisperAPI(w)
if api == nil { if api == nil {
t.Fatalf("failed to create API.") t.Fatalf("failed to create API.")
} }
@ -502,9 +504,9 @@ func TestIntegrationSymWithFilter(t *testing.T) {
t.Fatalf("failed HasIdentity: does not exist.") t.Fatalf("failed HasIdentity: does not exist.")
} }
var topics [2]whisperv5.TopicType var topics [2]TopicType
topics[0] = whisperv5.TopicType{0x00, 0x7f, 0x80, 0xff} topics[0] = TopicType{0x00, 0x7f, 0x80, 0xff}
topics[1] = whisperv5.TopicType{0xf2, 0x6e, 0x77, 0x79} topics[1] = TopicType{0xf2, 0x6e, 0x77, 0x79}
var f WhisperFilterArgs var f WhisperFilterArgs
f.KeyName = keyname f.KeyName = keyname
f.Topics = topics[:] f.Topics = topics[:]
@ -523,8 +525,8 @@ func TestIntegrationSymWithFilter(t *testing.T) {
p.From = sig p.From = sig
p.Padding = []byte("test string") p.Padding = []byte("test string")
p.Payload = []byte("extended test string") p.Payload = []byte("extended test string")
p.PoW = whisperv5.MinimumPoW p.PoW = MinimumPoW
p.Topic = whisperv5.TopicType{0xf2, 0x6e, 0x77, 0x79} p.Topic = TopicType{0xf2, 0x6e, 0x77, 0x79}
p.WorkTime = 2 p.WorkTime = 2
err = api.Post(p) err = api.Post(p)

View File

@ -96,7 +96,7 @@ func TestInstallFilters(t *testing.T) {
InitSingleTest() InitSingleTest()
const SizeTestFilters = 256 const SizeTestFilters = 256
w := NewWhisper(nil) w := New()
filters := NewFilters(w) filters := NewFilters(w)
tst := generateTestCases(t, SizeTestFilters) tst := generateTestCases(t, SizeTestFilters)
@ -520,7 +520,7 @@ func TestWatchers(t *testing.T) {
var j uint32 var j uint32
var e *Envelope var e *Envelope
w := NewWhisper(nil) w := New()
filters := NewFilters(w) filters := NewFilters(w)
tst := generateTestCases(t, NumFilters) tst := generateTestCases(t, NumFilters)
for i = 0; i < NumFilters; i++ { for i = 0; i < NumFilters; i++ {

View File

@ -116,7 +116,7 @@ func initialize(t *testing.T) {
for i := 0; i < NumNodes; i++ { for i := 0; i < NumNodes; i++ {
var node TestNode var node TestNode
node.shh = NewWhisper(nil) node.shh = New()
node.shh.test = true node.shh.test = true
node.shh.Start(nil) node.shh.Start(nil)
topics := make([]TopicType, 0) topics := make([]TopicType, 0)

View File

@ -31,6 +31,7 @@ import (
"github.com/ethereum/go-ethereum/logger" "github.com/ethereum/go-ethereum/logger"
"github.com/ethereum/go-ethereum/logger/glog" "github.com/ethereum/go-ethereum/logger/glog"
"github.com/ethereum/go-ethereum/p2p" "github.com/ethereum/go-ethereum/p2p"
"github.com/ethereum/go-ethereum/rpc"
"golang.org/x/crypto/pbkdf2" "golang.org/x/crypto/pbkdf2"
set "gopkg.in/fatih/set.v0" set "gopkg.in/fatih/set.v0"
) )
@ -65,7 +66,7 @@ type Whisper struct {
// New creates a Whisper client ready to communicate through the Ethereum P2P network. // New creates a Whisper client ready to communicate through the Ethereum P2P network.
// Param s should be passed if you want to implement mail server, otherwise nil. // Param s should be passed if you want to implement mail server, otherwise nil.
func NewWhisper(server MailServer) *Whisper { func New() *Whisper {
whisper := &Whisper{ whisper := &Whisper{
privateKeys: make(map[string]*ecdsa.PrivateKey), privateKeys: make(map[string]*ecdsa.PrivateKey),
symKeys: make(map[string][]byte), symKeys: make(map[string][]byte),
@ -73,7 +74,6 @@ func NewWhisper(server MailServer) *Whisper {
messages: make(map[common.Hash]*ReceivedMessage), messages: make(map[common.Hash]*ReceivedMessage),
expirations: make(map[uint32]*set.SetNonTS), expirations: make(map[uint32]*set.SetNonTS),
peers: make(map[*Peer]struct{}), peers: make(map[*Peer]struct{}),
mailServer: server,
messageQueue: make(chan *Envelope, messageQueueLimit), messageQueue: make(chan *Envelope, messageQueueLimit),
p2pMsgQueue: make(chan *Envelope, messageQueueLimit), p2pMsgQueue: make(chan *Envelope, messageQueueLimit),
quit: make(chan struct{}), quit: make(chan struct{}),
@ -91,6 +91,22 @@ func NewWhisper(server MailServer) *Whisper {
return whisper return whisper
} }
// APIs returns the RPC descriptors the Whisper implementation offers
func (w *Whisper) APIs() []rpc.API {
return []rpc.API{
{
Namespace: ProtocolName,
Version: ProtocolVersionStr,
Service: NewPublicWhisperAPI(w),
Public: true,
},
}
}
func (w *Whisper) RegisterServer(server MailServer) {
w.mailServer = server
}
// Protocols returns the whisper sub-protocols ran by this particular client. // Protocols returns the whisper sub-protocols ran by this particular client.
func (w *Whisper) Protocols() []p2p.Protocol { func (w *Whisper) Protocols() []p2p.Protocol {
return []p2p.Protocol{w.protocol} return []p2p.Protocol{w.protocol}

View File

@ -26,7 +26,7 @@ import (
) )
func TestWhisperBasic(t *testing.T) { func TestWhisperBasic(t *testing.T) {
w := NewWhisper(nil) w := New()
p := w.Protocols() p := w.Protocols()
shh := p[0] shh := p[0]
if shh.Name != ProtocolName { if shh.Name != ProtocolName {
@ -110,7 +110,7 @@ func TestWhisperBasic(t *testing.T) {
} }
func TestWhisperIdentityManagement(t *testing.T) { func TestWhisperIdentityManagement(t *testing.T) {
w := NewWhisper(nil) w := New()
id1 := w.NewIdentity() id1 := w.NewIdentity()
id2 := w.NewIdentity() id2 := w.NewIdentity()
pub1 := common.ToHex(crypto.FromECDSAPub(&id1.PublicKey)) pub1 := common.ToHex(crypto.FromECDSAPub(&id1.PublicKey))
@ -186,7 +186,7 @@ func TestWhisperSymKeyManagement(t *testing.T) {
InitSingleTest() InitSingleTest()
var k1, k2 []byte var k1, k2 []byte
w := NewWhisper(nil) w := New()
id1 := string("arbitrary-string-1") id1 := string("arbitrary-string-1")
id2 := string("arbitrary-string-2") id2 := string("arbitrary-string-2")
@ -304,7 +304,7 @@ func TestWhisperSymKeyManagement(t *testing.T) {
func TestExpiry(t *testing.T) { func TestExpiry(t *testing.T) {
InitSingleTest() InitSingleTest()
w := NewWhisper(nil) w := New()
w.test = true w.test = true
w.Start(nil) w.Start(nil)
defer w.Stop() defer w.Stop()