Skip to content

Commit 9f7cd75

Browse files
gluk256fjl
authored andcommitted
whisper/whisperv6: initial commit (clone of v5) (#15324)
1 parent 0131bd6 commit 9f7cd75

18 files changed

Lines changed: 5589 additions & 0 deletions

‎whisper/whisperv6/api.go‎

Lines changed: 591 additions & 0 deletions
Large diffs are not rendered by default.
Lines changed: 206 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,206 @@
1+
// Copyright 2016 The go-ethereum Authors
2+
// This file is part of the go-ethereum library.
3+
//
4+
// The go-ethereum library is free software: you can redistribute it and/or modify
5+
// it under the terms of the GNU Lesser General Public License as published by
6+
// the Free Software Foundation, either version 3 of the License, or
7+
// (at your option) any later version.
8+
//
9+
// The go-ethereum library is distributed in the hope that it will be useful,
10+
// but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12+
// GNU Lesser General Public License for more details.
13+
//
14+
// You should have received a copy of the GNU Lesser General Public License
15+
// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
16+
17+
package whisperv6
18+
19+
import (
20+
"testing"
21+
22+
"github.com/ethereum/go-ethereum/crypto"
23+
)
24+
25+
func BenchmarkDeriveKeyMaterial(b *testing.B) {
26+
for i := 0; i < b.N; i++ {
27+
deriveKeyMaterial([]byte("test"), 0)
28+
}
29+
}
30+
31+
func BenchmarkEncryptionSym(b *testing.B) {
32+
InitSingleTest()
33+
34+
params, err := generateMessageParams()
35+
if err != nil {
36+
b.Fatalf("failed generateMessageParams with seed %d: %s.", seed, err)
37+
}
38+
39+
for i := 0; i < b.N; i++ {
40+
msg, _ := NewSentMessage(params)
41+
_, err := msg.Wrap(params)
42+
if err != nil {
43+
b.Errorf("failed Wrap with seed %d: %s.", seed, err)
44+
b.Errorf("i = %d, len(msg.Raw) = %d, params.Payload = %d.", i, len(msg.Raw), len(params.Payload))
45+
return
46+
}
47+
}
48+
}
49+
50+
func BenchmarkEncryptionAsym(b *testing.B) {
51+
InitSingleTest()
52+
53+
params, err := generateMessageParams()
54+
if err != nil {
55+
b.Fatalf("failed generateMessageParams with seed %d: %s.", seed, err)
56+
}
57+
key, err := crypto.GenerateKey()
58+
if err != nil {
59+
b.Fatalf("failed GenerateKey with seed %d: %s.", seed, err)
60+
}
61+
params.KeySym = nil
62+
params.Dst = &key.PublicKey
63+
64+
for i := 0; i < b.N; i++ {
65+
msg, _ := NewSentMessage(params)
66+
_, err := msg.Wrap(params)
67+
if err != nil {
68+
b.Fatalf("failed Wrap with seed %d: %s.", seed, err)
69+
}
70+
}
71+
}
72+
73+
func BenchmarkDecryptionSymValid(b *testing.B) {
74+
InitSingleTest()
75+
76+
params, err := generateMessageParams()
77+
if err != nil {
78+
b.Fatalf("failed generateMessageParams with seed %d: %s.", seed, err)
79+
}
80+
msg, _ := NewSentMessage(params)
81+
env, err := msg.Wrap(params)
82+
if err != nil {
83+
b.Fatalf("failed Wrap with seed %d: %s.", seed, err)
84+
}
85+
f := Filter{KeySym: params.KeySym}
86+
87+
for i := 0; i < b.N; i++ {
88+
msg := env.Open(&f)
89+
if msg == nil {
90+
b.Fatalf("failed to open with seed %d.", seed)
91+
}
92+
}
93+
}
94+
95+
func BenchmarkDecryptionSymInvalid(b *testing.B) {
96+
InitSingleTest()
97+
98+
params, err := generateMessageParams()
99+
if err != nil {
100+
b.Fatalf("failed generateMessageParams with seed %d: %s.", seed, err)
101+
}
102+
msg, _ := NewSentMessage(params)
103+
env, err := msg.Wrap(params)
104+
if err != nil {
105+
b.Fatalf("failed Wrap with seed %d: %s.", seed, err)
106+
}
107+
f := Filter{KeySym: []byte("arbitrary stuff here")}
108+
109+
for i := 0; i < b.N; i++ {
110+
msg := env.Open(&f)
111+
if msg != nil {
112+
b.Fatalf("opened envelope with invalid key, seed: %d.", seed)
113+
}
114+
}
115+
}
116+
117+
func BenchmarkDecryptionAsymValid(b *testing.B) {
118+
InitSingleTest()
119+
120+
params, err := generateMessageParams()
121+
if err != nil {
122+
b.Fatalf("failed generateMessageParams with seed %d: %s.", seed, err)
123+
}
124+
key, err := crypto.GenerateKey()
125+
if err != nil {
126+
b.Fatalf("failed GenerateKey with seed %d: %s.", seed, err)
127+
}
128+
f := Filter{KeyAsym: key}
129+
params.KeySym = nil
130+
params.Dst = &key.PublicKey
131+
msg, _ := NewSentMessage(params)
132+
env, err := msg.Wrap(params)
133+
if err != nil {
134+
b.Fatalf("failed Wrap with seed %d: %s.", seed, err)
135+
}
136+
137+
for i := 0; i < b.N; i++ {
138+
msg := env.Open(&f)
139+
if msg == nil {
140+
b.Fatalf("fail to open, seed: %d.", seed)
141+
}
142+
}
143+
}
144+
145+
func BenchmarkDecryptionAsymInvalid(b *testing.B) {
146+
InitSingleTest()
147+
148+
params, err := generateMessageParams()
149+
if err != nil {
150+
b.Fatalf("failed generateMessageParams with seed %d: %s.", seed, err)
151+
}
152+
key, err := crypto.GenerateKey()
153+
if err != nil {
154+
b.Fatalf("failed GenerateKey with seed %d: %s.", seed, err)
155+
}
156+
params.KeySym = nil
157+
params.Dst = &key.PublicKey
158+
msg, _ := NewSentMessage(params)
159+
env, err := msg.Wrap(params)
160+
if err != nil {
161+
b.Fatalf("failed Wrap with seed %d: %s.", seed, err)
162+
}
163+
164+
key, err = crypto.GenerateKey()
165+
if err != nil {
166+
b.Fatalf("failed GenerateKey with seed %d: %s.", seed, err)
167+
}
168+
f := Filter{KeyAsym: key}
169+
170+
for i := 0; i < b.N; i++ {
171+
msg := env.Open(&f)
172+
if msg != nil {
173+
b.Fatalf("opened envelope with invalid key, seed: %d.", seed)
174+
}
175+
}
176+
}
177+
178+
func increment(x []byte) {
179+
for i := 0; i < len(x); i++ {
180+
x[i]++
181+
if x[i] != 0 {
182+
break
183+
}
184+
}
185+
}
186+
187+
func BenchmarkPoW(b *testing.B) {
188+
InitSingleTest()
189+
190+
params, err := generateMessageParams()
191+
if err != nil {
192+
b.Fatalf("failed generateMessageParams with seed %d: %s.", seed, err)
193+
}
194+
params.Payload = make([]byte, 32)
195+
params.PoW = 10.0
196+
params.TTL = 1
197+
198+
for i := 0; i < b.N; i++ {
199+
increment(params.Payload)
200+
msg, _ := NewSentMessage(params)
201+
_, err := msg.Wrap(params)
202+
if err != nil {
203+
b.Fatalf("failed Wrap with seed %d: %s.", seed, err)
204+
}
205+
}
206+
}

‎whisper/whisperv6/config.go‎

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
// Copyright 2017 The go-ethereum Authors
2+
// This file is part of the go-ethereum library.
3+
//
4+
// The go-ethereum library is free software: you can redistribute it and/or modify
5+
// it under the terms of the GNU Lesser General Public License as published by
6+
// the Free Software Foundation, either version 3 of the License, or
7+
// (at your option) any later version.
8+
//
9+
// The go-ethereum library is distributed in the hope that it will be useful,
10+
// but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12+
// GNU Lesser General Public License for more details.
13+
//
14+
// You should have received a copy of the GNU Lesser General Public License
15+
// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
16+
17+
package whisperv6
18+
19+
type Config struct {
20+
MaxMessageSize uint32 `toml:",omitempty"`
21+
MinimumAcceptedPOW float64 `toml:",omitempty"`
22+
}
23+
24+
var DefaultConfig = Config{
25+
MaxMessageSize: DefaultMaxMessageSize,
26+
MinimumAcceptedPOW: DefaultMinimumPoW,
27+
}

‎whisper/whisperv6/doc.go‎

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
// Copyright 2016 The go-ethereum Authors
2+
// This file is part of the go-ethereum library.
3+
//
4+
// The go-ethereum library is free software: you can redistribute it and/or modify
5+
// it under the terms of the GNU Lesser General Public License as published by
6+
// the Free Software Foundation, either version 3 of the License, or
7+
// (at your option) any later version.
8+
//
9+
// The go-ethereum library is distributed in the hope that it will be useful,
10+
// but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12+
// GNU Lesser General Public License for more details.
13+
//
14+
// You should have received a copy of the GNU Lesser General Public License
15+
// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
16+
17+
/*
18+
Package whisper implements the Whisper protocol (version 6).
19+
20+
Whisper combines aspects of both DHTs and datagram messaging systems (e.g. UDP).
21+
As such it may be likened and compared to both, not dissimilar to the
22+
matter/energy duality (apologies to physicists for the blatant abuse of a
23+
fundamental and beautiful natural principle).
24+
25+
Whisper is a pure identity-based messaging system. Whisper provides a low-level
26+
(non-application-specific) but easily-accessible API without being based upon
27+
or prejudiced by the low-level hardware attributes and characteristics,
28+
particularly the notion of singular endpoints.
29+
*/
30+
package whisperv6
31+
32+
import (
33+
"fmt"
34+
"time"
35+
)
36+
37+
const (
38+
EnvelopeVersion = uint64(0)
39+
ProtocolVersion = uint64(5)
40+
ProtocolVersionStr = "5.0"
41+
ProtocolName = "shh"
42+
43+
statusCode = 0 // used by whisper protocol
44+
messagesCode = 1 // normal whisper message
45+
p2pCode = 2 // peer-to-peer message (to be consumed by the peer, but not forwarded any further)
46+
p2pRequestCode = 3 // peer-to-peer message, used by Dapp protocol
47+
NumberOfMessageCodes = 64
48+
49+
paddingMask = byte(3)
50+
signatureFlag = byte(4)
51+
52+
TopicLength = 4
53+
signatureLength = 65
54+
aesKeyLength = 32
55+
AESNonceLength = 12
56+
keyIdSize = 32
57+
58+
MaxMessageSize = uint32(10 * 1024 * 1024) // maximum accepted size of a message.
59+
DefaultMaxMessageSize = uint32(1024 * 1024)
60+
DefaultMinimumPoW = 0.2
61+
62+
padSizeLimit = 256 // just an arbitrary number, could be changed without breaking the protocol (must not exceed 2^24)
63+
messageQueueLimit = 1024
64+
65+
expirationCycle = time.Second
66+
transmissionCycle = 300 * time.Millisecond
67+
68+
DefaultTTL = 50 // seconds
69+
SynchAllowance = 10 // seconds
70+
)
71+
72+
type unknownVersionError uint64
73+
74+
func (e unknownVersionError) Error() string {
75+
return fmt.Sprintf("invalid envelope version %d", uint64(e))
76+
}
77+
78+
// MailServer represents a mail server, capable of
79+
// archiving the old messages for subsequent delivery
80+
// to the peers. Any implementation must ensure that both
81+
// functions are thread-safe. Also, they must return ASAP.
82+
// DeliverMail should use directMessagesCode for delivery,
83+
// in order to bypass the expiry checks.
84+
type MailServer interface {
85+
Archive(env *Envelope)
86+
DeliverMail(whisperPeer *Peer, request *Envelope)
87+
}

0 commit comments

Comments
 (0)