Skip to content

Commit e8e849b

Browse files
committed
pss: refactor PssMsg to message package
1 parent e684263 commit e8e849b

File tree

13 files changed

+173
-116
lines changed

13 files changed

+173
-116
lines changed

pss/ARCHITECTURE.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
Pss provides devp2p functionality for swarm nodes without the need for a direct tcp connection between them.
44

5-
Messages are encapsulated in a devp2p message structure `PssMsg`. These capsules are forwarded from node to node using ordinary tcp devp2p until they reach their destination: The node or nodes who can successfully decrypt the message.
5+
Messages are encapsulated in a devp2p message structure `message.Message`. These capsules are forwarded from node to node using ordinary tcp devp2p until they reach their destination: The node or nodes who can successfully decrypt the message.
66

77
| Layer | Contents |
88
|-----------|-----------------|

pss/doc.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616

1717
// Pss provides devp2p functionality for swarm nodes without the need for a direct tcp connection between them.
1818
//
19-
// Messages are encapsulated in a devp2p message structure `PssMsg`. These capsules are forwarded from node to node using ordinary tcp devp2p until it reaches its destination: The node or nodes who can successfully decrypt the message.
19+
// Messages are encapsulated in a devp2p message structure `message.Message`. These capsules are forwarded from node to node using ordinary tcp devp2p until it reaches its destination: The node or nodes who can successfully decrypt the message.
2020
//
2121
// Routing of messages is done using swarm's own kademlia routing. Optionally routing can be turned off, forcing the message to be sent to all peers, similar to the behavior of the whisper protocol.
2222
//
@@ -50,11 +50,11 @@
5050
//
5151
// Under the hood, pss implements its own MsgReadWriter, which bridges MsgReadWriter.WriteMsg with Pss.SendRaw, and deftly adds an InjectMsg method which pipes incoming messages to appear on the MsgReadWriter.ReadMsg channel.
5252
//
53-
// An incoming connection is nothing more than an actual PssMsg appearing with a certain Topic. If a Handler har been registered to that Topic, the message will be passed to it. This constitutes a "new" connection if:
53+
// An incoming connection is nothing more than an actual message.Message appearing with a certain Topic. If a Handler har been registered to that Topic, the message will be passed to it. This constitutes a "new" connection if:
5454
//
5555
// - The pss node never called AddPeer with this combination of remote peer address and topic, and
5656
//
57-
// - The pss node never received a PssMsg from this remote peer with this specific Topic before.
57+
// - The pss node never received a message.Message from this remote peer with this specific Topic before.
5858
//
5959
// If it is a "new" connection, the protocol will be "run" on the remote peer, in the same manner as if it was pre-emptively added.
6060
//

pss/forwarding_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -248,7 +248,7 @@ func testForwardMsg(t *testing.T, ps *Pss, c *testCase) {
248248
resultMap := make(map[pot.Address]int)
249249

250250
defer func() { sendFunc = sendMsg }()
251-
sendFunc = func(_ *Pss, sp *network.Peer, _ *PssMsg) bool {
251+
sendFunc = func(_ *Pss, sp *network.Peer, _ *message.Message) bool {
252252
if tries < nFails {
253253
tries++
254254
return false
@@ -347,8 +347,8 @@ func newTestDiscoveryPeer(addr pot.Address, kad *network.Kademlia) *network.Peer
347347
return network.NewPeer(bp, kad)
348348
}
349349

350-
func newTestMsg(addr []byte) *PssMsg {
351-
msg := newPssMsg(message.Flags{})
350+
func newTestMsg(addr []byte) *message.Message {
351+
msg := message.New(message.Flags{})
352352
msg.To = addr[:]
353353
msg.Expire = uint32(time.Now().Add(time.Second * 60).Unix())
354354
msg.Topic = [4]byte{}

pss/handshake.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -291,7 +291,7 @@ func (ctl *HandshakeController) getSymKey(symkeyid string) *handshakeKey {
291291
return ctl.symKeyIndex[symkeyid]
292292
}
293293

294-
// Passed as a PssMsg handler for the topic handshake is activated on
294+
// Passed as a message.Message handler for the topic handshake is activated on
295295
// Handles incoming key exchange messages and
296296
// counts message usage by symmetric key (expiry limit control)
297297
// Only returns error if key handler fails

pss/keystore.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,7 @@ func (ks *KeyStore) getPeerAddress(keyid string, topic message.Topic) (PssAddres
147147
// If successful, returns the payload of the message and the id
148148
// of the symmetric key used to decrypt the message.
149149
// It fails if decryption of the message fails or if the message is corrupted/not valid.
150-
func (ks *KeyStore) processSym(pssMsg *PssMsg) ([]byte, string, PssAddress, error) {
150+
func (ks *KeyStore) processSym(pssMsg *message.Message) ([]byte, string, PssAddress, error) {
151151
metrics.GetOrRegisterCounter("pss.process.sym", nil).Inc(1)
152152

153153
for i := ks.symKeyDecryptCacheCursor; i > ks.symKeyDecryptCacheCursor-cap(ks.symKeyDecryptCache) && i > 0; i-- {
@@ -185,7 +185,7 @@ func (ks *KeyStore) processSym(pssMsg *PssMsg) ([]byte, string, PssAddress, erro
185185
// If successful, returns the payload of the message and the hex representation of
186186
// the public key used to decrypt the message.
187187
// It fails if decryption of message fails, or if the message is corrupted.
188-
func (p *Pss) processAsym(pssMsg *PssMsg) ([]byte, string, PssAddress, error) {
188+
func (p *Pss) processAsym(pssMsg *message.Message) ([]byte, string, PssAddress, error) {
189189
metrics.GetOrRegisterCounter("pss.process.asym", nil).Inc(1)
190190

191191
unwrapParams := &crypto.UnwrapParams{

pss/message/flags_test.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,12 @@ import (
1010
"github.com/ethersphere/swarm/pss/message"
1111
)
1212

13+
var bools = []bool{true, false}
14+
1315
func TestFlags(tx *testing.T) {
1416
t := ut.BeginTest(tx, false)
1517
defer t.FinishTest()
1618

17-
bools := []bool{true, false}
1819
for _, r := range bools {
1920
for _, s := range bools {
2021
f := message.Flags{

pss/message/message.go

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
package message
2+
3+
import (
4+
"fmt"
5+
6+
"github.com/ethereum/go-ethereum/common"
7+
"golang.org/x/crypto/sha3"
8+
)
9+
10+
// Message encapsulates messages transported over pss.
11+
type Message struct {
12+
To []byte
13+
Flags Flags
14+
Expire uint32
15+
Topic Topic
16+
Payload []byte
17+
}
18+
19+
const digestLength = 32 // byte length of digest used for pss cache (currently same as swarm chunk hash)
20+
21+
// Digest holds the digest of a message used for caching
22+
type Digest [digestLength]byte
23+
24+
// New creates a new PSS message
25+
func New(flags Flags) *Message {
26+
return &Message{
27+
Flags: flags,
28+
}
29+
}
30+
31+
// Digest computes a message digest for use as a cache key
32+
func (msg *Message) Digest() Digest {
33+
hasher := sha3.NewLegacyKeccak256()
34+
hasher.Write(msg.To)
35+
hasher.Write(msg.Topic[:])
36+
hasher.Write(msg.Payload)
37+
key := hasher.Sum(nil)
38+
d := Digest{}
39+
copy(d[:], key[:digestLength])
40+
return d
41+
}
42+
43+
// String representation of a PSS message
44+
func (msg *Message) String() string {
45+
return fmt.Sprintf("PssMsg: Recipient: %s, Topic: %v", common.ToHex(msg.To), msg.Topic.String())
46+
}

pss/message/message_test.go

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
package message_test
2+
3+
import (
4+
"encoding/hex"
5+
"fmt"
6+
"testing"
7+
8+
"github.com/epiclabs-io/ut"
9+
"github.com/ethereum/go-ethereum/common"
10+
"github.com/ethereum/go-ethereum/rlp"
11+
"github.com/ethersphere/swarm/pss/message"
12+
)
13+
14+
func TestMessage(tx *testing.T) {
15+
t := ut.BeginTest(tx, false) // set to true to generate test results
16+
defer t.FinishTest()
17+
18+
// generate some test messages deterministically
19+
for i, topicString := range someTopics {
20+
flags := message.Flags{
21+
Raw: i&0x1 == 0,
22+
Symmetric: i&0x3 == 0,
23+
}
24+
25+
msg := message.New(flags)
26+
msg.To = ut.RandomArray(i, common.AddressLength)
27+
msg.Expire = uint32(i)
28+
msg.Topic = message.NewTopic([]byte(topicString))
29+
msg.Payload = ut.RandomArray(i*9361, i*10)
30+
31+
// test digest function:
32+
digest := msg.Digest()
33+
t.EqualsKey(fmt.Sprintf("msg%d-digest", i), hex.EncodeToString(digest[:]))
34+
35+
// test stringer:
36+
st := msg.String()
37+
t.EqualsKey(fmt.Sprintf("msg%d-string", i), st)
38+
39+
// Test RLP encoding:
40+
bytes, err := rlp.EncodeToBytes(&msg)
41+
t.Ok(err)
42+
t.EqualsKey(fmt.Sprintf("msg%d-rlp", i), hex.EncodeToString(bytes))
43+
44+
// Test decoding:
45+
var msg2 message.Message
46+
err = rlp.DecodeBytes(bytes, &msg2)
47+
t.Ok(err)
48+
t.Equals(msg, &msg2)
49+
50+
}
51+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
{
2+
"msg0-digest": "4b34781cfa28a5ad653855567273675eabb8535461e57e4f4bfc81504d0a828d",
3+
"msg0-rlp": "dd94fa12f92afbe00f8508d0e83bab9cf8cebf42e25e03808491273d4980",
4+
"msg0-string": "PssMsg: Recipient: 0xfa12f92afbe00f8508d0e83bab9cf8cebf42e25e, Topic: 0x91273d49",
5+
"msg1-digest": "7f076bc036335b5d587d48c985d1b6ef8cd7015d6e484d0c7a72faddaa2aceaa",
6+
"msg1-rlp": "e794210fc7bb818639ac48a4c6afa2f1581a8b9525e2000184ba78973d8aa84f7f80296fda3fd8df",
7+
"msg1-string": "PssMsg: Recipient: 0x210fc7bb818639ac48a4c6afa2f1581a8b9525e2, Topic: 0xba78973d",
8+
"msg2-digest": "a3cb8298779bef44c33461f072c54391a39c09b7a726e55d60384d7484760559",
9+
"msg2-rlp": "f194e2aadcd868ce028477f86e430140149b0300a9a5020284a6b46dd094f4b754a41bd4d5d11330e2924ff403c95bb84fa5",
10+
"msg2-string": "PssMsg: Recipient: 0xe2aadcd868ce028477f86e430140149b0300a9a5, Topic: 0xa6b46dd0",
11+
"msg3-digest": "a82a894a753dffad41330dc1abbc85e5bc1791c393eba682eaf3cee56e6b0d9a",
12+
"msg3-rlp": "f83b9460f9e0fa212bac5db82b22cee5272ee19a067256000384f013aa4b9e2fb3c9afcd593f3c5d3a96fecc1b7672562cc1b8828888269264bb976ed2",
13+
"msg3-string": "PssMsg: Recipient: 0x60f9e0fa212bac5db82b22cee5272ee19a067256, Topic: 0xf013aa4b",
14+
"msg4-digest": "8ba6836253a10cf02e5031695ab39917e816b9677d53b4e4b2af5e439b05d362",
15+
"msg4-rlp": "f845941dd4751f899d743d0780c9644375aae21132781803048426f57386a834dab59240ba3bcec68fd648a62ba94062413e5b5f89c0441b5809fff0a51dd1084e8f06fce30971",
16+
"msg4-string": "PssMsg: Recipient: 0x1dd4751f899d743d0780c9644375aae211327818, Topic: 0x26f57386"
17+
}

pss/message/topic_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,12 @@ import (
99
)
1010

1111
// test that topic conversion functions give predictable results
12+
var someTopics = []string{"These", "are", "some", "topics", "A topic can be very long as well, longer than TopicLength"}
13+
1214
func TestTopic(tx *testing.T) {
1315
t := ut.BeginTest(tx, false) // set to true to generate test results
1416
defer t.FinishTest()
1517

16-
someTopics := []string{"These", "are", "some", "topics", "A topic can be very long as well, longer than TopicLength"}
17-
1818
for i, topicString := range someTopics {
1919
topic := message.NewTopic([]byte(topicString))
2020

0 commit comments

Comments
 (0)