diff options
author | gluk256 <gluk256@users.noreply.github.com> | 2017-02-14 22:44:47 +0800 |
---|---|---|
committer | Péter Szilágyi <peterke@gmail.com> | 2017-02-14 22:44:47 +0800 |
commit | 15a609d5d6613e37e819975ceba01cb5ba735242 (patch) | |
tree | 1de23ad8c8d5885700164185f0d0d92f7ae95199 /whisper | |
parent | 72dcd3c58bec0a281280d5d42ed53b6e429ce4af (diff) | |
download | dexon-15a609d5d6613e37e819975ceba01cb5ba735242.tar.gz dexon-15a609d5d6613e37e819975ceba01cb5ba735242.tar.zst dexon-15a609d5d6613e37e819975ceba01cb5ba735242.zip |
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
Diffstat (limited to 'whisper')
-rw-r--r-- | whisper/mailserver/server_test.go | 4 | ||||
-rw-r--r-- | whisper/whisperv5/api.go (renamed from whisper/shhapi/api.go) | 77 | ||||
-rw-r--r-- | whisper/whisperv5/api_test.go (renamed from whisper/shhapi/api_test.go) | 62 | ||||
-rw-r--r-- | whisper/whisperv5/filter_test.go | 4 | ||||
-rw-r--r-- | whisper/whisperv5/peer_test.go | 2 | ||||
-rw-r--r-- | whisper/whisperv5/whisper.go | 20 | ||||
-rw-r--r-- | whisper/whisperv5/whisper_test.go | 8 |
7 files changed, 91 insertions, 86 deletions
diff --git a/whisper/mailserver/server_test.go b/whisper/mailserver/server_test.go index 24abf6c1a..02b30ff5e 100644 --- a/whisper/mailserver/server_test.go +++ b/whisper/mailserver/server_test.go @@ -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() diff --git a/whisper/shhapi/api.go b/whisper/whisperv5/api.go index b273053ec..ce41624df 100644 --- a/whisper/shhapi/api.go +++ b/whisper/whisperv5/api.go @@ -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" @@ -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 { @@ -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 { @@ -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) @@ -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) @@ -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) @@ -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), @@ -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) @@ -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) @@ -347,7 +332,7 @@ 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) @@ -355,18 +340,18 @@ func (api *PublicWhisperAPI) Post(args PostArgs) error { } // encrypt and send - message := whisperv5.NewSentMessage(¶ms) + message := NewSentMessage(¶ms) envelope, err := message.Wrap(¶ms) 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) @@ -380,17 +365,17 @@ 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 { @@ -398,7 +383,7 @@ type WhisperFilterArgs struct { From string KeyName string PoW float64 - Topics []whisperv5.TopicType + Topics []TopicType AcceptP2P bool } @@ -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 } @@ -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), diff --git a/whisper/shhapi/api_test.go b/whisper/whisperv5/api_test.go index 60b6fbd04..f7deab39c 100644 --- a/whisper/shhapi/api_test.go +++ b/whisper/whisperv5/api_test.go @@ -14,22 +14,21 @@ // 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 ( "bytes" + "encoding/json" "testing" "time" - "encoding/json" - "github.com/ethereum/go-ethereum/common" - "github.com/ethereum/go-ethereum/whisper/whisperv5" ) func TestBasic(t *testing.T) { var id string = "test" - api := NewPublicWhisperAPI() + w := New() + api := NewPublicWhisperAPI(w) if api == nil { t.Fatalf("failed to create API.") } @@ -39,7 +38,7 @@ func TestBasic(t *testing.T) { t.Fatalf("failed generateFilter: %s.", err) } - if uint64(ver) != whisperv5.ProtocolVersion { + if uint64(ver) != ProtocolVersion { t.Fatalf("wrong version: %d.", ver) } @@ -182,22 +181,22 @@ func TestUnmarshalFilterArgs(t *testing.T) { } 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]) } 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]) } 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]) } 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]) } } @@ -235,7 +234,7 @@ func TestUnmarshalPostArgs(t *testing.T) { if a.KeyName != "shh_test" { 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) } 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) { - api := NewPublicWhisperAPI() + w := New() + api := NewPublicWhisperAPI(w) if api == nil { t.Fatalf("failed to create API.") } @@ -304,14 +304,14 @@ func TestIntegrationAsym(t *testing.T) { t.Fatalf("wrong key") } - var topics [2]whisperv5.TopicType - topics[0] = whisperv5.TopicType{0x00, 0x64, 0x00, 0xff} - topics[1] = whisperv5.TopicType{0xf2, 0x6e, 0x77, 0x79} + var topics [2]TopicType + topics[0] = TopicType{0x00, 0x64, 0x00, 0xff} + topics[1] = TopicType{0xf2, 0x6e, 0x77, 0x79} var f WhisperFilterArgs f.To = key f.From = sig f.Topics = topics[:] - f.PoW = whisperv5.MinimumPoW / 2 + f.PoW = MinimumPoW / 2 f.AcceptP2P = true id, err := api.NewFilter(f) @@ -325,8 +325,8 @@ func TestIntegrationAsym(t *testing.T) { p.To = f.To p.Padding = []byte("test string") p.Payload = []byte("extended test string") - p.PoW = whisperv5.MinimumPoW - p.Topic = whisperv5.TopicType{0xf2, 0x6e, 0x77, 0x79} + p.PoW = MinimumPoW + p.Topic = TopicType{0xf2, 0x6e, 0x77, 0x79} p.WorkTime = 2 err = api.Post(p) @@ -373,7 +373,8 @@ func TestIntegrationAsym(t *testing.T) { } func TestIntegrationSym(t *testing.T) { - api := NewPublicWhisperAPI() + w := New() + api := NewPublicWhisperAPI(w) if api == nil { t.Fatalf("failed to create API.") } @@ -403,9 +404,9 @@ func TestIntegrationSym(t *testing.T) { t.Fatalf("failed HasIdentity: false negative.") } - var topics [2]whisperv5.TopicType - topics[0] = whisperv5.TopicType{0x00, 0x7f, 0x80, 0xff} - topics[1] = whisperv5.TopicType{0xf2, 0x6e, 0x77, 0x79} + var topics [2]TopicType + topics[0] = TopicType{0x00, 0x7f, 0x80, 0xff} + topics[1] = TopicType{0xf2, 0x6e, 0x77, 0x79} var f WhisperFilterArgs f.KeyName = keyname f.Topics = topics[:] @@ -424,8 +425,8 @@ func TestIntegrationSym(t *testing.T) { p.From = f.From p.Padding = []byte("test string") p.Payload = []byte("extended test string") - p.PoW = whisperv5.MinimumPoW - p.Topic = whisperv5.TopicType{0xf2, 0x6e, 0x77, 0x79} + p.PoW = MinimumPoW + p.Topic = TopicType{0xf2, 0x6e, 0x77, 0x79} p.WorkTime = 2 err = api.Post(p) @@ -472,7 +473,8 @@ func TestIntegrationSym(t *testing.T) { } func TestIntegrationSymWithFilter(t *testing.T) { - api := NewPublicWhisperAPI() + w := New() + api := NewPublicWhisperAPI(w) if api == nil { t.Fatalf("failed to create API.") } @@ -502,9 +504,9 @@ func TestIntegrationSymWithFilter(t *testing.T) { t.Fatalf("failed HasIdentity: does not exist.") } - var topics [2]whisperv5.TopicType - topics[0] = whisperv5.TopicType{0x00, 0x7f, 0x80, 0xff} - topics[1] = whisperv5.TopicType{0xf2, 0x6e, 0x77, 0x79} + var topics [2]TopicType + topics[0] = TopicType{0x00, 0x7f, 0x80, 0xff} + topics[1] = TopicType{0xf2, 0x6e, 0x77, 0x79} var f WhisperFilterArgs f.KeyName = keyname f.Topics = topics[:] @@ -523,8 +525,8 @@ func TestIntegrationSymWithFilter(t *testing.T) { p.From = sig p.Padding = []byte("test string") p.Payload = []byte("extended test string") - p.PoW = whisperv5.MinimumPoW - p.Topic = whisperv5.TopicType{0xf2, 0x6e, 0x77, 0x79} + p.PoW = MinimumPoW + p.Topic = TopicType{0xf2, 0x6e, 0x77, 0x79} p.WorkTime = 2 err = api.Post(p) diff --git a/whisper/whisperv5/filter_test.go b/whisper/whisperv5/filter_test.go index 9f2a58f28..5bf607ccc 100644 --- a/whisper/whisperv5/filter_test.go +++ b/whisper/whisperv5/filter_test.go @@ -96,7 +96,7 @@ func TestInstallFilters(t *testing.T) { InitSingleTest() const SizeTestFilters = 256 - w := NewWhisper(nil) + w := New() filters := NewFilters(w) tst := generateTestCases(t, SizeTestFilters) @@ -520,7 +520,7 @@ func TestWatchers(t *testing.T) { var j uint32 var e *Envelope - w := NewWhisper(nil) + w := New() filters := NewFilters(w) tst := generateTestCases(t, NumFilters) for i = 0; i < NumFilters; i++ { diff --git a/whisper/whisperv5/peer_test.go b/whisper/whisperv5/peer_test.go index 34e2ec255..cc98ba2d6 100644 --- a/whisper/whisperv5/peer_test.go +++ b/whisper/whisperv5/peer_test.go @@ -116,7 +116,7 @@ func initialize(t *testing.T) { for i := 0; i < NumNodes; i++ { var node TestNode - node.shh = NewWhisper(nil) + node.shh = New() node.shh.test = true node.shh.Start(nil) topics := make([]TopicType, 0) diff --git a/whisper/whisperv5/whisper.go b/whisper/whisperv5/whisper.go index ff31aab2d..a027fd84b 100644 --- a/whisper/whisperv5/whisper.go +++ b/whisper/whisperv5/whisper.go @@ -31,6 +31,7 @@ import ( "github.com/ethereum/go-ethereum/logger" "github.com/ethereum/go-ethereum/logger/glog" "github.com/ethereum/go-ethereum/p2p" + "github.com/ethereum/go-ethereum/rpc" "golang.org/x/crypto/pbkdf2" 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. // Param s should be passed if you want to implement mail server, otherwise nil. -func NewWhisper(server MailServer) *Whisper { +func New() *Whisper { whisper := &Whisper{ privateKeys: make(map[string]*ecdsa.PrivateKey), symKeys: make(map[string][]byte), @@ -73,7 +74,6 @@ func NewWhisper(server MailServer) *Whisper { messages: make(map[common.Hash]*ReceivedMessage), expirations: make(map[uint32]*set.SetNonTS), peers: make(map[*Peer]struct{}), - mailServer: server, messageQueue: make(chan *Envelope, messageQueueLimit), p2pMsgQueue: make(chan *Envelope, messageQueueLimit), quit: make(chan struct{}), @@ -91,6 +91,22 @@ func NewWhisper(server MailServer) *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. func (w *Whisper) Protocols() []p2p.Protocol { return []p2p.Protocol{w.protocol} diff --git a/whisper/whisperv5/whisper_test.go b/whisper/whisperv5/whisper_test.go index a3ded7b37..c2ae35a3e 100644 --- a/whisper/whisperv5/whisper_test.go +++ b/whisper/whisperv5/whisper_test.go @@ -26,7 +26,7 @@ import ( ) func TestWhisperBasic(t *testing.T) { - w := NewWhisper(nil) + w := New() p := w.Protocols() shh := p[0] if shh.Name != ProtocolName { @@ -110,7 +110,7 @@ func TestWhisperBasic(t *testing.T) { } func TestWhisperIdentityManagement(t *testing.T) { - w := NewWhisper(nil) + w := New() id1 := w.NewIdentity() id2 := w.NewIdentity() pub1 := common.ToHex(crypto.FromECDSAPub(&id1.PublicKey)) @@ -186,7 +186,7 @@ func TestWhisperSymKeyManagement(t *testing.T) { InitSingleTest() var k1, k2 []byte - w := NewWhisper(nil) + w := New() id1 := string("arbitrary-string-1") id2 := string("arbitrary-string-2") @@ -304,7 +304,7 @@ func TestWhisperSymKeyManagement(t *testing.T) { func TestExpiry(t *testing.T) { InitSingleTest() - w := NewWhisper(nil) + w := New() w.test = true w.Start(nil) defer w.Stop() |