-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- added config and types - added md5 func - added tests
- Loading branch information
Showing
5 changed files
with
110 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,22 +1,63 @@ | ||
package gossip | ||
|
||
import ( | ||
"crypto/md5" | ||
"crypto/sha256" | ||
"encoding/hex" | ||
|
||
pubsub "github.com/libp2p/go-libp2p-pubsub" | ||
pubsub_pb "github.com/libp2p/go-libp2p-pubsub/pb" | ||
) | ||
|
||
// msgIDSha256 uses sha256 hash of the message content | ||
func MsgIDSha256(size int) pubsub.MsgIdFunction { | ||
type MsgIDSize int | ||
type MsgIDFuncType string | ||
|
||
const ( | ||
MsgIDSha256Type MsgIDFuncType = "sha256" | ||
MsgIDMD5Type MsgIDFuncType = "md5" | ||
) | ||
|
||
var DefaultMsgIDFn = MsgIDSha256(20) | ||
|
||
func MsgIDFn(tp MsgIDFuncType, size MsgIDSize) pubsub.MsgIdFunction { | ||
switch tp { | ||
case MsgIDSha256Type: | ||
return MsgIDSha256(size) | ||
case MsgIDMD5Type: | ||
return MsgIDMD5(size) | ||
default: | ||
return DefaultMsgIDFn | ||
} | ||
} | ||
|
||
// MsgIDSha256 uses sha256 hash of the message content | ||
func MsgIDSha256(size MsgIDSize) pubsub.MsgIdFunction { | ||
return func(pmsg *pubsub_pb.Message) string { | ||
msg := pmsg.GetData() | ||
if len(msg) == 0 { | ||
return "" | ||
} | ||
// TODO: optimize, e.g. by using a pool of hashers | ||
h := sha256.Sum256(msg) | ||
if msgSize := MsgIDSize(len(h)); size > msgSize { | ||
size = msgSize | ||
} | ||
return hex.EncodeToString(h[:size]) | ||
} | ||
} | ||
|
||
// MsgIDSMD5 uses md5 hash of the message content | ||
func MsgIDMD5(size MsgIDSize) pubsub.MsgIdFunction { | ||
return func(pmsg *pubsub_pb.Message) string { | ||
msg := pmsg.GetData() | ||
if len(msg) == 0 { | ||
return "" | ||
} | ||
// TODO: optimize, e.g. by using a pool of hashers | ||
h := md5.Sum(msg) | ||
if msgSize := MsgIDSize(len(h)); size > msgSize { | ||
size = msgSize | ||
} | ||
return hex.EncodeToString(h[:size]) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
package gossip | ||
|
||
import ( | ||
"testing" | ||
|
||
pubsub_pb "github.com/libp2p/go-libp2p-pubsub/pb" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestMsgID(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
msgID MsgIDFuncType | ||
size MsgIDSize | ||
input []byte | ||
want string | ||
}{ | ||
{ | ||
name: "sha256", | ||
msgID: MsgIDSha256Type, | ||
size: 20, | ||
input: []byte("hello world"), | ||
want: "b94d27b9934d3e08a52e52d7da7dabfac484efe3", | ||
}, | ||
{ | ||
name: "md5", | ||
msgID: MsgIDMD5Type, | ||
size: 10, | ||
input: []byte("hello world"), | ||
want: "5eb63bbbe01eeed093cb", | ||
}, | ||
{ | ||
name: "default", | ||
msgID: "", | ||
size: 0, | ||
input: []byte("hello world"), | ||
want: "b94d27b9934d3e08a52e52d7da7dabfac484efe3", | ||
}, | ||
{ | ||
name: "size overflow", | ||
msgID: MsgIDMD5Type, | ||
size: 100, | ||
input: []byte("hello world"), | ||
want: "5eb63bbbe01eeed093cb22bb8f5acdc3", | ||
}, | ||
} | ||
|
||
for _, tc := range tests { | ||
t.Run(tc.name, func(t *testing.T) { | ||
msgIDFn := MsgIDFn(tc.msgID, tc.size) | ||
got := msgIDFn(&pubsub_pb.Message{ | ||
Data: tc.input, | ||
}) | ||
require.Equal(t, tc.want, got) | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters