diff --git a/p2p/simulations/adapters/inproc_test.go b/p2p/simulations/adapters/inproc_test.go index 5cece74e0c..3939706aec 100644 --- a/p2p/simulations/adapters/inproc_test.go +++ b/p2p/simulations/adapters/inproc_test.go @@ -55,7 +55,7 @@ func TestSocketPipe(t *testing.T) { t.Fatal(err) } - if bytes.Compare(msg, out) != 0 { + if !bytes.Equal(msg, out) { t.Fatalf("expected %#v, got %#v", msg, out) } } @@ -96,7 +96,7 @@ func TestSocketPipeBidirections(t *testing.T) { t.Fatal(err) } - if bytes.Compare(out, []byte(`ping`)) == 0 { + if bytes.Equal(out, []byte(`ping`)) { msg := []byte(`pong`) _, err := c2.Write(msg) if err != nil { @@ -114,7 +114,7 @@ func TestSocketPipeBidirections(t *testing.T) { t.Fatal(err) } - if bytes.Compare(out, expected) != 0 { + if !bytes.Equal(out, expected) { t.Fatalf("expected %#v, got %#v", expected, out) } } @@ -160,7 +160,7 @@ func TestTcpPipe(t *testing.T) { t.Fatal(err) } - if bytes.Compare(msg, out) != 0 { + if !bytes.Equal(msg, out) { t.Fatalf("expected %#v, got %#v", msg, out) } } @@ -203,7 +203,7 @@ func TestTcpPipeBidirections(t *testing.T) { t.Fatal(err) } - if bytes.Compare(expected, out) != 0 { + if !bytes.Equal(expected, out) { t.Fatalf("expected %#v, got %#v", out, expected) } else { msg := []byte(fmt.Sprintf("pong %02d", i)) @@ -223,7 +223,7 @@ func TestTcpPipeBidirections(t *testing.T) { t.Fatal(err) } - if bytes.Compare(expected, out) != 0 { + if !bytes.Equal(expected, out) { t.Fatalf("expected %#v, got %#v", out, expected) } } @@ -271,7 +271,7 @@ func TestNetPipe(t *testing.T) { t.Fatal(err) } - if bytes.Compare(msg, out) != 0 { + if !bytes.Equal(msg, out) { t.Fatalf("expected %#v, got %#v", msg, out) } } @@ -323,7 +323,7 @@ func TestNetPipeBidirections(t *testing.T) { t.Fatal(err) } - if bytes.Compare(expected, out) != 0 { + if !bytes.Equal(expected, out) { t.Fatalf("expected %#v, got %#v", expected, out) } } @@ -341,7 +341,7 @@ func TestNetPipeBidirections(t *testing.T) { t.Fatal(err) } - if bytes.Compare(expected, out) != 0 { + if !bytes.Equal(expected, out) { t.Fatalf("expected %#v, got %#v", expected, out) } else { msg := []byte(fmt.Sprintf(pongTemplate, i)) diff --git a/pot/pot_test.go b/pot/pot_test.go index 7befdf71ba..72971ff18d 100644 --- a/pot/pot_test.go +++ b/pot/pot_test.go @@ -271,10 +271,7 @@ func testPotEachNeighbour(n *Pot, pof Pof, val Val, expCount int, fs ...func(Val } } count++ - if count == expCount { - return false - } - return true + return count == expCount }) if err == nil && count < expCount { return fmt.Errorf("not enough neighbours returned, expected %v, got %v", expCount, count) @@ -558,10 +555,7 @@ func benchmarkEachNeighbourSync(t *testing.B, max, count int, d time.Duration) { n.EachNeighbour(val, pof, func(v Val, po int) bool { time.Sleep(d) m++ - if m == count { - return false - } - return true + return m == count }) } t.StopTimer() diff --git a/swarm/pss/handshake_test.go b/swarm/pss/handshake_test.go index 6cebdbbd5a..a76741ec04 100644 --- a/swarm/pss/handshake_test.go +++ b/swarm/pss/handshake_test.go @@ -1,4 +1,4 @@ -//-build go1.7 +// +build go1.8 package pss diff --git a/swarm/pss/protocol_test.go b/swarm/pss/protocol_test.go index 793faa1397..e7016f96f1 100644 --- a/swarm/pss/protocol_test.go +++ b/swarm/pss/protocol_test.go @@ -1,4 +1,4 @@ -//-build go1.7 +// +build go1.8 package pss diff --git a/swarm/pss/pss.go b/swarm/pss/pss.go index f49e488e59..b5474ded82 100644 --- a/swarm/pss/pss.go +++ b/swarm/pss/pss.go @@ -215,9 +215,7 @@ func (self *Pss) APIs() []rpc.API { Public: true, }, } - for _, auxapi := range self.auxAPIs { - apis = append(apis, auxapi) - } + apis = append(apis, self.auxAPIs...) return apis } @@ -388,7 +386,7 @@ func (self *Pss) SetPeerPublicKey(pubkey *ecdsa.PublicKey, topic Topic, address address: address, } self.pubKeyPoolMu.Lock() - if _, ok := self.pubKeyPool[pubkeyid]; ok == false { + if _, ok := self.pubKeyPool[pubkeyid]; !ok { self.pubKeyPool[pubkeyid] = make(map[Topic]*pssPeer) } self.pubKeyPool[pubkeyid][topic] = psp @@ -537,7 +535,7 @@ func (self *Pss) cleanKeys() (count int) { match = true } } - if match == false { + if !match { expiredtopics = append(expiredtopics, topic) } } diff --git a/swarm/pss/pss_go18plus_test.go b/swarm/pss/pss_go18plus_test.go index 8b8f6669c3..d4bef49edf 100644 --- a/swarm/pss/pss_go18plus_test.go +++ b/swarm/pss/pss_go18plus_test.go @@ -1,4 +1,4 @@ -//-build go17 +// +build go1.8 package pss import ( @@ -19,11 +19,13 @@ import ( "github.com/ethereum/go-ethereum/common/hexutil" "github.com/ethereum/go-ethereum/crypto" "github.com/ethereum/go-ethereum/log" + "github.com/ethereum/go-ethereum/p2p" "github.com/ethereum/go-ethereum/p2p/discover" "github.com/ethereum/go-ethereum/p2p/simulations" "github.com/ethereum/go-ethereum/p2p/simulations/adapters" "github.com/ethereum/go-ethereum/rpc" "github.com/ethereum/go-ethereum/swarm/network" + whisper "github.com/ethereum/go-ethereum/whisper/whisperv5" ) // send symmetrically encrypted message between two directly connected peers @@ -361,7 +363,7 @@ func testNetwork(t *testing.T) { select { case recvmsg := <-msgC: idx, _ := binary.Uvarint(recvmsg.Msg) - if recvmsgs[idx] == false { + if !recvmsgs[idx] { log.Debug("msg recv", "idx", idx, "id", id) recvmsgs[idx] = true trigger <- id @@ -544,3 +546,169 @@ func benchmarkAsymKeySend(b *testing.B) { ps.SendAsym(common.ToHex(crypto.FromECDSAPub(&privkey.PublicKey)), topic, msg) } } + +func BenchmarkSymkeyBruteforceChangeaddr(b *testing.B) { + for i := 100; i < 100000; i = i * 10 { + for j := 32; j < 10000; j = j * 8 { + b.Run(fmt.Sprintf("%d/%d", i, j), benchmarkSymkeyBruteforceChangeaddr) + } + //b.Run(fmt.Sprintf("%d", i), benchmarkSymkeyBruteforceChangeaddr) + } +} + +// decrypt performance using symkey cache, worst case +// (decrypt key always last in cache) +func benchmarkSymkeyBruteforceChangeaddr(b *testing.B) { + keycountstring := strings.Split(b.Name(), "/") + cachesize := int64(0) + var ps *Pss + if len(keycountstring) < 2 { + b.Fatalf("benchmark called without count param") + } + keycount, err := strconv.ParseInt(keycountstring[1], 10, 0) + if err != nil { + b.Fatalf("benchmark called with invalid count param '%s': %v", keycountstring[1], err) + } + if len(keycountstring) == 3 { + cachesize, err = strconv.ParseInt(keycountstring[2], 10, 0) + if err != nil { + b.Fatalf("benchmark called with invalid cachesize '%s': %v", keycountstring[2], err) + } + } + pssmsgs := make([]*PssMsg, 0, keycount) + var keyid string + ctx, cancel := context.WithTimeout(context.Background(), time.Second) + defer cancel() + keys, err := wapi.NewKeyPair(ctx) + privkey, err := w.GetPrivateKey(keys) + if cachesize > 0 { + ps = newTestPss(privkey, nil, &PssParams{SymKeyCacheCapacity: int(cachesize)}) + } else { + ps = newTestPss(privkey, nil, nil) + } + topic := BytesToTopic([]byte("foo")) + for i := 0; i < int(keycount); i++ { + to := make(PssAddress, 32) + copy(to[:], network.RandomAddr().Over()) + keyid, err = ps.generateSymmetricKey(topic, &to, true) + if err != nil { + b.Fatalf("cant generate symkey #%d: %v", i, err) + } + symkey, err := ps.w.GetSymKey(keyid) + if err != nil { + b.Fatalf("could not retrieve symkey %s: %v", keyid, err) + } + wparams := &whisper.MessageParams{ + TTL: defaultWhisperTTL, + KeySym: symkey, + Topic: whisper.TopicType(topic), + WorkTime: defaultWhisperWorkTime, + PoW: defaultWhisperPoW, + Payload: []byte("xyzzy"), + Padding: []byte("1234567890abcdef"), + } + woutmsg, err := whisper.NewSentMessage(wparams) + if err != nil { + b.Fatalf("could not create whisper message: %v", err) + } + env, err := woutmsg.Wrap(wparams) + if err != nil { + b.Fatalf("could not generate whisper envelope: %v", err) + } + ps.Register(&topic, func(msg []byte, p *p2p.Peer, asymmetric bool, keyid string) error { + return nil + }) + pssmsgs = append(pssmsgs, &PssMsg{ + To: to, + Payload: env, + }) + } + b.ResetTimer() + for i := 0; i < b.N; i++ { + if !ps.process(pssmsgs[len(pssmsgs)-(i%len(pssmsgs))-1]) { + b.Fatalf("pss processing failed: %v", err) + } + } +} + +func BenchmarkSymkeyBruteforceSameaddr(b *testing.B) { + for i := 100; i < 100000; i = i * 10 { + for j := 32; j < 10000; j = j * 8 { + b.Run(fmt.Sprintf("%d/%d", i, j), benchmarkSymkeyBruteforceSameaddr) + } + } +} + +// decrypt performance using symkey cache, best case +// (decrypt key always first in cache) +func benchmarkSymkeyBruteforceSameaddr(b *testing.B) { + var keyid string + var ps *Pss + cachesize := int64(0) + keycountstring := strings.Split(b.Name(), "/") + if len(keycountstring) < 2 { + b.Fatalf("benchmark called without count param") + } + keycount, err := strconv.ParseInt(keycountstring[1], 10, 0) + if err != nil { + b.Fatalf("benchmark called with invalid count param '%s': %v", keycountstring[1], err) + } + if len(keycountstring) == 3 { + cachesize, err = strconv.ParseInt(keycountstring[2], 10, 0) + if err != nil { + b.Fatalf("benchmark called with invalid cachesize '%s': %v", keycountstring[2], err) + } + } + addr := make([]PssAddress, keycount) + ctx, cancel := context.WithTimeout(context.Background(), time.Second) + defer cancel() + keys, err := wapi.NewKeyPair(ctx) + privkey, err := w.GetPrivateKey(keys) + if cachesize > 0 { + ps = newTestPss(privkey, nil, &PssParams{SymKeyCacheCapacity: int(cachesize)}) + } else { + ps = newTestPss(privkey, nil, nil) + } + topic := BytesToTopic([]byte("foo")) + for i := 0; i < int(keycount); i++ { + copy(addr[i], network.RandomAddr().Over()) + keyid, err = ps.generateSymmetricKey(topic, &addr[i], true) + if err != nil { + b.Fatalf("cant generate symkey #%d: %v", i, err) + } + + } + symkey, err := ps.w.GetSymKey(keyid) + if err != nil { + b.Fatalf("could not retrieve symkey %s: %v", keyid, err) + } + wparams := &whisper.MessageParams{ + TTL: defaultWhisperTTL, + KeySym: symkey, + Topic: whisper.TopicType(topic), + WorkTime: defaultWhisperWorkTime, + PoW: defaultWhisperPoW, + Payload: []byte("xyzzy"), + Padding: []byte("1234567890abcdef"), + } + woutmsg, err := whisper.NewSentMessage(wparams) + if err != nil { + b.Fatalf("could not create whisper message: %v", err) + } + env, err := woutmsg.Wrap(wparams) + if err != nil { + b.Fatalf("could not generate whisper envelope: %v", err) + } + ps.Register(&topic, func(msg []byte, p *p2p.Peer, asymmetric bool, keyid string) error { + return nil + }) + pssmsg := &PssMsg{ + To: addr[len(addr)-1][:], + Payload: env, + } + for i := 0; i < b.N; i++ { + if !ps.process(pssmsg) { + b.Fatalf("pss processing failed: %v", err) + } + } +} diff --git a/swarm/pss/pss_test.go b/swarm/pss/pss_test.go index af05a84673..b9275f84c0 100644 --- a/swarm/pss/pss_test.go +++ b/swarm/pss/pss_test.go @@ -392,172 +392,6 @@ func TestMismatch(t *testing.T) { } -func BenchmarkSymkeyBruteforceChangeaddr(b *testing.B) { - for i := 100; i < 100000; i = i * 10 { - for j := 32; j < 10000; j = j * 8 { - b.Run(fmt.Sprintf("%d/%d", i, j), benchmarkSymkeyBruteforceChangeaddr) - } - //b.Run(fmt.Sprintf("%d", i), benchmarkSymkeyBruteforceChangeaddr) - } -} - -// decrypt performance using symkey cache, worst case -// (decrypt key always last in cache) -func benchmarkSymkeyBruteforceChangeaddr(b *testing.B) { - keycountstring := strings.Split(b.Name(), "/") - cachesize := int64(0) - var ps *Pss - if len(keycountstring) < 2 { - b.Fatalf("benchmark called without count param") - } - keycount, err := strconv.ParseInt(keycountstring[1], 10, 0) - if err != nil { - b.Fatalf("benchmark called with invalid count param '%s': %v", keycountstring[1], err) - } - if len(keycountstring) == 3 { - cachesize, err = strconv.ParseInt(keycountstring[2], 10, 0) - if err != nil { - b.Fatalf("benchmark called with invalid cachesize '%s': %v", keycountstring[2], err) - } - } - pssmsgs := make([]*PssMsg, 0, keycount) - var keyid string - ctx, cancel := context.WithTimeout(context.Background(), time.Second) - defer cancel() - keys, err := wapi.NewKeyPair(ctx) - privkey, err := w.GetPrivateKey(keys) - if cachesize > 0 { - ps = newTestPss(privkey, nil, &PssParams{SymKeyCacheCapacity: int(cachesize)}) - } else { - ps = newTestPss(privkey, nil, nil) - } - topic := BytesToTopic([]byte("foo")) - for i := 0; i < int(keycount); i++ { - to := make(PssAddress, 32) - copy(to[:], network.RandomAddr().Over()) - keyid, err = ps.generateSymmetricKey(topic, &to, true) - if err != nil { - b.Fatalf("cant generate symkey #%d: %v", i, err) - } - symkey, err := ps.w.GetSymKey(keyid) - if err != nil { - b.Fatalf("could not retrieve symkey %s: %v", keyid, err) - } - wparams := &whisper.MessageParams{ - TTL: defaultWhisperTTL, - KeySym: symkey, - Topic: whisper.TopicType(topic), - WorkTime: defaultWhisperWorkTime, - PoW: defaultWhisperPoW, - Payload: []byte("xyzzy"), - Padding: []byte("1234567890abcdef"), - } - woutmsg, err := whisper.NewSentMessage(wparams) - if err != nil { - b.Fatalf("could not create whisper message: %v", err) - } - env, err := woutmsg.Wrap(wparams) - if err != nil { - b.Fatalf("could not generate whisper envelope: %v", err) - } - ps.Register(&topic, func(msg []byte, p *p2p.Peer, asymmetric bool, keyid string) error { - return nil - }) - pssmsgs = append(pssmsgs, &PssMsg{ - To: to, - Payload: env, - }) - } - b.ResetTimer() - for i := 0; i < b.N; i++ { - if !ps.process(pssmsgs[len(pssmsgs)-(i%len(pssmsgs))-1]) { - b.Fatalf("pss processing failed: %v", err) - } - } -} - -func BenchmarkSymkeyBruteforceSameaddr(b *testing.B) { - for i := 100; i < 100000; i = i * 10 { - for j := 32; j < 10000; j = j * 8 { - b.Run(fmt.Sprintf("%d/%d", i, j), benchmarkSymkeyBruteforceSameaddr) - } - } -} - -// decrypt performance using symkey cache, best case -// (decrypt key always first in cache) -func benchmarkSymkeyBruteforceSameaddr(b *testing.B) { - var keyid string - var ps *Pss - cachesize := int64(0) - keycountstring := strings.Split(b.Name(), "/") - if len(keycountstring) < 2 { - b.Fatalf("benchmark called without count param") - } - keycount, err := strconv.ParseInt(keycountstring[1], 10, 0) - if err != nil { - b.Fatalf("benchmark called with invalid count param '%s': %v", keycountstring[1], err) - } - if len(keycountstring) == 3 { - cachesize, err = strconv.ParseInt(keycountstring[2], 10, 0) - if err != nil { - b.Fatalf("benchmark called with invalid cachesize '%s': %v", keycountstring[2], err) - } - } - addr := make([]PssAddress, keycount) - ctx, cancel := context.WithTimeout(context.Background(), time.Second) - defer cancel() - keys, err := wapi.NewKeyPair(ctx) - privkey, err := w.GetPrivateKey(keys) - if cachesize > 0 { - ps = newTestPss(privkey, nil, &PssParams{SymKeyCacheCapacity: int(cachesize)}) - } else { - ps = newTestPss(privkey, nil, nil) - } - topic := BytesToTopic([]byte("foo")) - for i := 0; i < int(keycount); i++ { - copy(addr[i], network.RandomAddr().Over()) - keyid, err = ps.generateSymmetricKey(topic, &addr[i], true) - if err != nil { - b.Fatalf("cant generate symkey #%d: %v", i, err) - } - - } - symkey, err := ps.w.GetSymKey(keyid) - if err != nil { - b.Fatalf("could not retrieve symkey %s: %v", keyid, err) - } - wparams := &whisper.MessageParams{ - TTL: defaultWhisperTTL, - KeySym: symkey, - Topic: whisper.TopicType(topic), - WorkTime: defaultWhisperWorkTime, - PoW: defaultWhisperPoW, - Payload: []byte("xyzzy"), - Padding: []byte("1234567890abcdef"), - } - woutmsg, err := whisper.NewSentMessage(wparams) - if err != nil { - b.Fatalf("could not create whisper message: %v", err) - } - env, err := woutmsg.Wrap(wparams) - if err != nil { - b.Fatalf("could not generate whisper envelope: %v", err) - } - ps.Register(&topic, func(msg []byte, p *p2p.Peer, asymmetric bool, keyid string) error { - return nil - }) - pssmsg := &PssMsg{ - To: addr[len(addr)-1][:], - Payload: env, - } - for i := 0; i < b.N; i++ { - if !ps.process(pssmsg) { - b.Fatalf("pss processing failed: %v", err) - } - } -} - // setup simulated network and connect nodes in circle func setupNetwork(numnodes int) (clients []*rpc.Client, err error) { nodes := make([]*simulations.Node, numnodes) diff --git a/swarm/storage/resource.go b/swarm/storage/resource.go index 7745d75fbc..34444f92c2 100644 --- a/swarm/storage/resource.go +++ b/swarm/storage/resource.go @@ -637,10 +637,7 @@ func (self *ResourceHandler) verifyContent(chunkdata []byte) error { } func (self *ResourceHandler) hasUpdate(name string, period uint32) bool { - if self.resources[name].lastPeriod == period { - return true - } - return false + return self.resources[name].lastPeriod == period } type resourceChunkStore struct { diff --git a/swarm/swarm.go b/swarm/swarm.go index 48e736ce72..c47857093b 100644 --- a/swarm/swarm.go +++ b/swarm/swarm.go @@ -254,14 +254,10 @@ func (self *Swarm) Stop() error { // implements the node.Service interface func (self *Swarm) Protocols() (protos []p2p.Protocol) { - for _, p := range self.bzz.Protocols() { - protos = append(protos, p) - } + protos = append(protos, self.bzz.Protocols()...) if self.ps != nil { - for _, p := range self.ps.Protocols() { - protos = append(protos, p) - } + protos = append(protos, self.ps.Protocols()) } return } @@ -322,14 +318,10 @@ func (self *Swarm) APIs() []rpc.API { // {Namespace, Version, api.NewAdmin(self), false}, } - for _, api := range self.bzz.APIs() { - apis = append(apis, api) - } + apis = append(apis, self.bzz.APIs()) if self.ps != nil { - for _, api := range self.ps.APIs() { - apis = append(apis, api) - } + apis = append(apis, self.ps.APIs()) } return apis