Skip to content

Commit

Permalink
whisper: interface changed to simplify the transition to v5
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
gluk256 authored and karalabe committed Feb 14, 2017
1 parent 72dcd3c commit 15a609d
Show file tree
Hide file tree
Showing 9 changed files with 96 additions and 90 deletions.
5 changes: 3 additions & 2 deletions cmd/wnode/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -198,10 +198,11 @@ func initialize() {
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)
} else {
shh = whisper.NewWhisper(nil)
shh = whisper.New()
}

asymKey = shh.NewIdentity()
Expand Down
4 changes: 2 additions & 2 deletions mobile/geth.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ import (
"github.com/ethereum/go-ethereum/node"
"github.com/ethereum/go-ethereum/p2p/nat"
"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
Expand Down Expand Up @@ -172,7 +172,7 @@ func NewNode(datadir string, config *NodeConfig) (stack *Node, _ error) {
}
// Register the Whisper protocol if requested
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)
}
}
Expand Down
4 changes: 3 additions & 1 deletion whisper/mailserver/server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,9 @@ func TestMailServer(t *testing.T) {
}

var server WMailServer
shh = whisper.NewWhisper(&server)
shh = whisper.New()
shh.RegisterServer(&server)

server.Init(shh, dbPath, password, powRequirement)
defer server.Close()

Expand Down
77 changes: 31 additions & 46 deletions whisper/shhapi/api.go → whisper/whisperv5/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
// 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/>.

package shhapi
package whisperv5

import (
"encoding/json"
Expand All @@ -27,35 +27,20 @@ import (
"github.com/ethereum/go-ethereum/crypto"
"github.com/ethereum/go-ethereum/logger"
"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")

// PublicWhisperAPI provides the whisper RPC service.
type PublicWhisperAPI struct {
whisper *whisperv5.Whisper
whisper *Whisper
}

// NewPublicWhisperAPI create a new RPC whisper service.
func NewPublicWhisperAPI() *PublicWhisperAPI {
w := whisperv5.NewWhisper(nil)
func NewPublicWhisperAPI(w *Whisper) *PublicWhisperAPI {
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.
func (api *PublicWhisperAPI) Start() error {
if api.whisper == nil {
Expand Down Expand Up @@ -171,11 +156,11 @@ func (api *PublicWhisperAPI) NewFilter(args WhisperFilterArgs) (uint32, error) {
return 0, whisperOffLineErr
}

filter := whisperv5.Filter{
filter := Filter{
Src: crypto.ToECDSAPub(common.FromHex(args.From)),
KeySym: api.whisper.GetSymKey(args.KeyName),
PoW: args.PoW,
Messages: make(map[common.Hash]*whisperv5.ReceivedMessage),
Messages: make(map[common.Hash]*ReceivedMessage),
AcceptP2P: args.AcceptP2P,
}
if len(filter.KeySym) > 0 {
Expand Down Expand Up @@ -209,7 +194,7 @@ func (api *PublicWhisperAPI) NewFilter(args WhisperFilterArgs) (uint32, error) {

if len(args.To) > 0 {
dst := crypto.ToECDSAPub(common.FromHex(args.To))
if !whisperv5.ValidatePublicKey(dst) {
if !ValidatePublicKey(dst) {
info := "NewFilter: Invalid 'To' address"
glog.V(logger.Error).Infof(info)
return 0, errors.New(info)
Expand All @@ -223,7 +208,7 @@ func (api *PublicWhisperAPI) NewFilter(args WhisperFilterArgs) (uint32, error) {
}

if len(args.From) > 0 {
if !whisperv5.ValidatePublicKey(filter.Src) {
if !ValidatePublicKey(filter.Src) {
info := "NewFilter: Invalid 'From' address"
glog.V(logger.Error).Infof(info)
return 0, errors.New(info)
Expand Down Expand Up @@ -256,7 +241,7 @@ func (api *PublicWhisperAPI) GetMessages(filterId uint32) []WhisperMessage {
}

// 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))
for i, msg := range messages {
msgs[i] = NewWhisperMessage(msg)
Expand All @@ -270,7 +255,7 @@ func (api *PublicWhisperAPI) Post(args PostArgs) error {
return whisperOffLineErr
}

params := whisperv5.MessageParams{
params := MessageParams{
TTL: args.TTL,
Dst: crypto.ToECDSAPub(common.FromHex(args.To)),
KeySym: api.whisper.GetSymKey(args.KeyName),
Expand All @@ -283,7 +268,7 @@ func (api *PublicWhisperAPI) Post(args PostArgs) error {

if len(args.From) > 0 {
pub := crypto.ToECDSAPub(common.FromHex(args.From))
if !whisperv5.ValidatePublicKey(pub) {
if !ValidatePublicKey(pub) {
info := "Post: Invalid 'From' address"
glog.V(logger.Error).Infof(info)
return errors.New(info)
Expand Down Expand Up @@ -311,7 +296,7 @@ func (api *PublicWhisperAPI) Post(args PostArgs) error {
if params.Src == nil && filter.Src != nil {
params.Src = filter.KeyAsym
}
if (params.Topic == whisperv5.TopicType{}) {
if (params.Topic == TopicType{}) {
sz := len(filter.Topics)
if sz < 1 {
info := fmt.Sprintf("Post: no topics in filter # %d", args.FilterID)
Expand Down Expand Up @@ -347,26 +332,26 @@ func (api *PublicWhisperAPI) Post(args PostArgs) error {
}

if len(args.To) > 0 {
if !whisperv5.ValidatePublicKey(params.Dst) {
if !ValidatePublicKey(params.Dst) {
info := "Post: Invalid 'To' address"
glog.V(logger.Error).Infof(info)
return errors.New(info)
}
}

// encrypt and send
message := whisperv5.NewSentMessage(&params)
message := NewSentMessage(&params)
envelope, err := message.Wrap(&params)
if err != nil {
glog.V(logger.Error).Infof(err.Error())
return err
}
if len(envelope.Data) > whisperv5.MaxMessageLength {
if len(envelope.Data) > MaxMessageLength {
info := "Post: message is too big"
glog.V(logger.Error).Infof(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"
glog.V(logger.Error).Infof(info)
return errors.New(info)
Expand All @@ -380,25 +365,25 @@ func (api *PublicWhisperAPI) Post(args PostArgs) error {
}

type PostArgs struct {
TTL uint32 `json:"ttl"`
From string `json:"from"`
To string `json:"to"`
KeyName string `json:"keyname"`
Topic whisperv5.TopicType `json:"topic"`
Padding hexutil.Bytes `json:"padding"`
Payload hexutil.Bytes `json:"payload"`
WorkTime uint32 `json:"worktime"`
PoW float64 `json:"pow"`
FilterID uint32 `json:"filterID"`
PeerID hexutil.Bytes `json:"peerID"`
TTL uint32 `json:"ttl"`
From string `json:"from"`
To string `json:"to"`
KeyName string `json:"keyname"`
Topic TopicType `json:"topic"`
Padding hexutil.Bytes `json:"padding"`
Payload hexutil.Bytes `json:"payload"`
WorkTime uint32 `json:"worktime"`
PoW float64 `json:"pow"`
FilterID uint32 `json:"filterID"`
PeerID hexutil.Bytes `json:"peerID"`
}

type WhisperFilterArgs struct {
To string
From string
KeyName string
PoW float64
Topics []whisperv5.TopicType
Topics []TopicType
AcceptP2P bool
}

Expand Down Expand Up @@ -437,13 +422,13 @@ func (args *WhisperFilterArgs) UnmarshalJSON(b []byte) (err error) {
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 {
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)
}
topicsDecoded[j] = whisperv5.BytesToTopic(x)
topicsDecoded[j] = BytesToTopic(x)
}
args.Topics = topicsDecoded
}
Expand All @@ -464,7 +449,7 @@ type WhisperMessage struct {
}

// NewWhisperMessage converts an internal message into an API version.
func NewWhisperMessage(message *whisperv5.ReceivedMessage) WhisperMessage {
func NewWhisperMessage(message *ReceivedMessage) WhisperMessage {
return WhisperMessage{
Payload: common.ToHex(message.Payload),
Padding: common.ToHex(message.Padding),
Expand Down
Loading

0 comments on commit 15a609d

Please sign in to comment.