From e80dda605130479086bed363e395a102a89a574a Mon Sep 17 00:00:00 2001 From: Felix Lange Date: Thu, 19 Mar 2015 15:18:31 +0100 Subject: eth, whisper: adapt for RLP encoder switch in package p2p I have rewritten the protocol test to use p2p.MsgPipe because p2p.NewMsg is gone. --- whisper/envelope.go | 2 +- whisper/peer.go | 9 ++------- 2 files changed, 3 insertions(+), 8 deletions(-) (limited to 'whisper') diff --git a/whisper/envelope.go b/whisper/envelope.go index 577638046..9ec9fc318 100644 --- a/whisper/envelope.go +++ b/whisper/envelope.go @@ -6,9 +6,9 @@ import ( "fmt" "time" + "github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/crypto" "github.com/ethereum/go-ethereum/crypto/ecies" - "github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/rlp" ) diff --git a/whisper/peer.go b/whisper/peer.go index 4bd9428f5..ee5bffd0b 100644 --- a/whisper/peer.go +++ b/whisper/peer.go @@ -77,12 +77,11 @@ func (self *peer) broadcast(envelopes []*Envelope) error { } if i > 0 { - if err := p2p.EncodeMsg(self.ws, envelopesMsg, envs[:i]...); err != nil { + if err := p2p.Send(self.ws, envelopesMsg, envs[:i]); err != nil { return err } self.peer.DebugDetailln("broadcasted", i, "message(s)") } - return nil } @@ -92,7 +91,7 @@ func (self *peer) addKnown(envelope *Envelope) { func (self *peer) handleStatus() error { ws := self.ws - if err := ws.WriteMsg(self.statusMsg()); err != nil { + if err := p2p.SendItems(ws, statusMsg, protocolVersion); err != nil { return err } msg, err := ws.ReadMsg() @@ -115,7 +114,3 @@ func (self *peer) handleStatus() error { } return msg.Discard() // ignore anything after protocol version } - -func (self *peer) statusMsg() p2p.Msg { - return p2p.NewMsg(statusMsg, protocolVersion) -} -- cgit From 483d43a15af11a9c456e4d9df0520441b947cd35 Mon Sep 17 00:00:00 2001 From: Felix Lange Date: Sat, 21 Mar 2015 00:49:58 +0100 Subject: whisper: use package rlp --- whisper/envelope.go | 40 +++++++++++++++------------------------- whisper/peer.go | 21 +++++++++------------ 2 files changed, 24 insertions(+), 37 deletions(-) (limited to 'whisper') diff --git a/whisper/envelope.go b/whisper/envelope.go index 9ec9fc318..15a974a2c 100644 --- a/whisper/envelope.go +++ b/whisper/envelope.go @@ -28,9 +28,9 @@ type Envelope struct { func (self *Envelope) Hash() Hash { if self.hash == EmptyHash { - self.hash = H(crypto.Sha3(common.Encode(self))) + enc, _ := rlp.EncodeToBytes(self) + self.hash = H(crypto.Sha3(enc)) } - return self.hash } @@ -76,7 +76,8 @@ func (self *Envelope) Open(prv *ecdsa.PrivateKey) (msg *Message, err error) { func (self *Envelope) proveWork(dura time.Duration) { var bestBit int d := make([]byte, 64) - copy(d[:32], common.Encode(self.withoutNonce())) + enc, _ := rlp.EncodeToBytes(self.withoutNonce()) + copy(d[:32], enc) then := time.Now().Add(dura).UnixNano() for n := uint32(0); time.Now().UnixNano() < then; { @@ -96,39 +97,28 @@ func (self *Envelope) proveWork(dura time.Duration) { func (self *Envelope) valid() bool { d := make([]byte, 64) - copy(d[:32], common.Encode(self.withoutNonce())) + enc, _ := rlp.EncodeToBytes(self.withoutNonce()) + copy(d[:32], enc) binary.BigEndian.PutUint32(d[60:], self.Nonce) return common.FirstBitSet(common.BigD(crypto.Sha3(d))) > 0 } func (self *Envelope) withoutNonce() interface{} { - return []interface{}{self.Expiry, self.Ttl, common.ByteSliceToInterface(self.Topics), self.Data} + return []interface{}{self.Expiry, self.Ttl, self.Topics, self.Data} } -func (self *Envelope) RlpData() interface{} { - return []interface{}{self.Expiry, self.Ttl, common.ByteSliceToInterface(self.Topics), self.Data, self.Nonce} -} +// rlpenv is an Envelope but is not an rlp.Decoder. +// It is used for decoding because we need to +type rlpenv Envelope func (self *Envelope) DecodeRLP(s *rlp.Stream) error { - var extenv struct { - Expiry uint32 - Ttl uint32 - Topics [][]byte - Data []byte - Nonce uint32 + raw, err := s.Raw() + if err != nil { + return err } - if err := s.Decode(&extenv); err != nil { + if err := rlp.DecodeBytes(raw, (*rlpenv)(self)); err != nil { return err } - - self.Expiry = extenv.Expiry - self.Ttl = extenv.Ttl - self.Topics = extenv.Topics - self.Data = extenv.Data - self.Nonce = extenv.Nonce - - // TODO We should use the stream directly here. - self.hash = H(crypto.Sha3(common.Encode(self))) - + self.hash = H(crypto.Sha3(raw)) return nil } diff --git a/whisper/peer.go b/whisper/peer.go index ee5bffd0b..338166c25 100644 --- a/whisper/peer.go +++ b/whisper/peer.go @@ -10,7 +10,7 @@ import ( ) const ( - protocolVersion = 0x02 + protocolVersion uint64 = 0x02 ) type peer struct { @@ -66,21 +66,18 @@ out: } func (self *peer) broadcast(envelopes []*Envelope) error { - envs := make([]interface{}, len(envelopes)) - i := 0 - for _, envelope := range envelopes { - if !self.known.Has(envelope.Hash()) { - envs[i] = envelope - self.known.Add(envelope.Hash()) - i++ + envs := make([]*Envelope, 0, len(envelopes)) + for _, env := range envelopes { + if !self.known.Has(env.Hash()) { + envs = append(envs, env) + self.known.Add(env.Hash()) } } - - if i > 0 { - if err := p2p.Send(self.ws, envelopesMsg, envs[:i]); err != nil { + if len(envs) > 0 { + if err := p2p.Send(self.ws, envelopesMsg, envs); err != nil { return err } - self.peer.DebugDetailln("broadcasted", i, "message(s)") + self.peer.DebugDetailln("broadcasted", len(envs), "message(s)") } return nil } -- cgit From 069c87b960c48864dc4f1b9086adf582e1dc88a9 Mon Sep 17 00:00:00 2001 From: Felix Lange Date: Sat, 21 Mar 2015 00:57:18 +0100 Subject: whisper: use common.Hash --- whisper/envelope.go | 23 ++++++++++++++--------- whisper/sort.go | 8 ++++++-- whisper/sort_test.go | 16 ++++++++++------ whisper/whisper.go | 28 ++++------------------------ 4 files changed, 34 insertions(+), 41 deletions(-) (limited to 'whisper') diff --git a/whisper/envelope.go b/whisper/envelope.go index 15a974a2c..20e3e6d39 100644 --- a/whisper/envelope.go +++ b/whisper/envelope.go @@ -18,26 +18,31 @@ const ( type Envelope struct { Expiry uint32 // Whisper protocol specifies int32, really should be int64 - Ttl uint32 // ^^^^^^ + TTL uint32 // ^^^^^^ Topics [][]byte Data []byte Nonce uint32 - hash Hash + hash common.Hash } -func (self *Envelope) Hash() Hash { - if self.hash == EmptyHash { +func (self *Envelope) Hash() common.Hash { + if (self.hash == common.Hash{}) { enc, _ := rlp.EncodeToBytes(self) - self.hash = H(crypto.Sha3(enc)) + self.hash = crypto.Sha3Hash(enc) } return self.hash } func NewEnvelope(ttl time.Duration, topics [][]byte, data *Message) *Envelope { exp := time.Now().Add(ttl) - - return &Envelope{uint32(exp.Unix()), uint32(ttl.Seconds()), topics, data.Bytes(), 0, Hash{}} + return &Envelope{ + Expiry: uint32(exp.Unix()), + TTL: uint32(ttl.Seconds()), + Topics: topics, + Data: data.Bytes(), + Nonce: 0, + } } func (self *Envelope) Seal(pow time.Duration) { @@ -104,7 +109,7 @@ func (self *Envelope) valid() bool { } func (self *Envelope) withoutNonce() interface{} { - return []interface{}{self.Expiry, self.Ttl, self.Topics, self.Data} + return []interface{}{self.Expiry, self.TTL, self.Topics, self.Data} } // rlpenv is an Envelope but is not an rlp.Decoder. @@ -119,6 +124,6 @@ func (self *Envelope) DecodeRLP(s *rlp.Stream) error { if err := rlp.DecodeBytes(raw, (*rlpenv)(self)); err != nil { return err } - self.hash = H(crypto.Sha3(raw)) + self.hash = crypto.Sha3Hash(raw) return nil } diff --git a/whisper/sort.go b/whisper/sort.go index 8c5b46e9e..313ba5ac0 100644 --- a/whisper/sort.go +++ b/whisper/sort.go @@ -1,6 +1,10 @@ package whisper -import "sort" +import ( + "sort" + + "github.com/ethereum/go-ethereum/common" +) type sortedKeys struct { k []int32 @@ -10,7 +14,7 @@ func (self *sortedKeys) Len() int { return len(self.k) } func (self *sortedKeys) Less(i, j int) bool { return self.k[i] < self.k[j] } func (self *sortedKeys) Swap(i, j int) { self.k[i], self.k[j] = self.k[j], self.k[i] } -func sortKeys(m map[int32]Hash) []int32 { +func sortKeys(m map[int32]common.Hash) []int32 { sorted := new(sortedKeys) sorted.k = make([]int32, len(m)) i := 0 diff --git a/whisper/sort_test.go b/whisper/sort_test.go index 5d8177d41..a61fde4c2 100644 --- a/whisper/sort_test.go +++ b/whisper/sort_test.go @@ -1,13 +1,17 @@ package whisper -import "testing" +import ( + "testing" + + "github.com/ethereum/go-ethereum/common" +) func TestSorting(t *testing.T) { - m := map[int32]Hash{ - 1: HS("1"), - 3: HS("3"), - 2: HS("2"), - 5: HS("5"), + m := map[int32]common.Hash{ + 1: {1}, + 3: {3}, + 2: {2}, + 5: {5}, } exp := []int32{1, 2, 3, 5} res := sortKeys(m) diff --git a/whisper/whisper.go b/whisper/whisper.go index 908df973c..dbd4fc85f 100644 --- a/whisper/whisper.go +++ b/whisper/whisper.go @@ -1,12 +1,12 @@ package whisper import ( - "bytes" "crypto/ecdsa" "errors" "sync" "time" + "github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/crypto" "github.com/ethereum/go-ethereum/crypto/ecies" "github.com/ethereum/go-ethereum/event/filter" @@ -15,26 +15,6 @@ import ( "gopkg.in/fatih/set.v0" ) -// MOVE ME -type Hash struct { - hash string -} - -var EmptyHash Hash - -func H(hash []byte) Hash { - return Hash{string(hash)} -} -func HS(hash string) Hash { - return Hash{hash} -} - -func (self Hash) Compare(other Hash) int { - return bytes.Compare([]byte(self.hash), []byte(other.hash)) -} - -// MOVE ME END - const ( statusMsg = 0x0 envelopesMsg = 0x01 @@ -55,7 +35,7 @@ type Whisper struct { filters *filter.Filters mmu sync.RWMutex - messages map[Hash]*Envelope + messages map[common.Hash]*Envelope expiry map[uint32]*set.SetNonTS quit chan struct{} @@ -65,7 +45,7 @@ type Whisper struct { func New() *Whisper { whisper := &Whisper{ - messages: make(map[Hash]*Envelope), + messages: make(map[common.Hash]*Envelope), filters: filter.New(), expiry: make(map[uint32]*set.SetNonTS), quit: make(chan struct{}), @@ -239,7 +219,7 @@ func (self *Whisper) expire() { } hashSet.Each(func(v interface{}) bool { - delete(self.messages, v.(Hash)) + delete(self.messages, v.(common.Hash)) return true }) self.expiry[then].Clear() -- cgit