-
Notifications
You must be signed in to change notification settings - Fork 186
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Publishing option for signing a message with a custom private key #486
Merged
Merged
Changes from 6 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
b90598f
- added PublishWithSk functionality on the Topic implementation
iulianpascalau 702b796
Merge branch 'upstream-master' into publish-with-provided-sk
iulianpascalau 3907872
- fixes after merge
iulianpascalau 6cab88e
- complete refactor: used publishing options instead of a new function
iulianpascalau 433864f
- added one more check in unit test
iulianpascalau d31b062
- bugfix: use local variables instead of fields
iulianpascalau 7a9e99c
- renamed tests
iulianpascalau File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 |
---|---|---|
|
@@ -14,6 +14,7 @@ import ( | |
|
||
"github.com/libp2p/go-libp2p-core/peer" | ||
pb "github.com/libp2p/go-libp2p-pubsub/pb" | ||
tnet "github.com/libp2p/go-libp2p-testing/net" | ||
) | ||
|
||
func getTopics(psubs []*PubSub, topicID string, opts ...TopicOpt) []*Topic { | ||
|
@@ -381,7 +382,7 @@ func TestSubscriptionLeaveNotification(t *testing.T) { | |
|
||
// Test removing peers and verifying that they cause events | ||
subs[1].Cancel() | ||
hosts[2].Close() | ||
_ = hosts[2].Close() | ||
psubs[0].BlacklistPeer(hosts[3].ID()) | ||
|
||
leavingPeers := make(map[peer.ID]struct{}) | ||
|
@@ -878,7 +879,7 @@ func TestWithTopicMsgIdFunction(t *testing.T) { | |
})) | ||
connectAll(t, hosts) | ||
|
||
topicsA := getTopics(pubsubs, topicA) // uses global msgIdFn | ||
topicsA := getTopics(pubsubs, topicA) // uses global msgIdFn | ||
topicsB := getTopics(pubsubs, topicB, WithTopicMessageIdFn(func(pmsg *pb.Message) string { // uses custom | ||
hash := sha1.Sum(pmsg.Data) | ||
return string(hash[:]) | ||
|
@@ -920,3 +921,101 @@ func TestWithTopicMsgIdFunction(t *testing.T) { | |
t.Fatal("msg ids are equal") | ||
} | ||
} | ||
|
||
func TestTopic_PublishWithKeyInvalidParameters(t *testing.T) { | ||
t.Parallel() | ||
|
||
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second) | ||
defer cancel() | ||
|
||
const topic = "foobar" | ||
const numHosts = 5 | ||
|
||
virtualPeer := tnet.RandPeerNetParamsOrFatal(t) | ||
hosts := getNetHosts(t, ctx, numHosts) | ||
topics := getTopics(getPubsubs(ctx, hosts), topic) | ||
|
||
t.Run("nil sign private key should error", func(t *testing.T) { | ||
withVirtualKey := WithSecretKeyAndPeerId(nil, virtualPeer.ID) | ||
err := topics[0].Publish(ctx, []byte("buff"), withVirtualKey) | ||
if err != ErrNilSignKey { | ||
t.Fatal("error should have been of type errNilSignKey") | ||
} | ||
}) | ||
t.Run("empty peer ID should error", func(t *testing.T) { | ||
withVirtualKey := WithSecretKeyAndPeerId(virtualPeer.PrivKey, "") | ||
err := topics[0].Publish(ctx, []byte("buff"), withVirtualKey) | ||
if err != ErrEmptyPeerID { | ||
t.Fatal("error should have been of type errEmptyPeerID") | ||
} | ||
}) | ||
} | ||
|
||
func TestTopicRelay_PublishWithKey(t *testing.T) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. here too There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. done |
||
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second) | ||
defer cancel() | ||
|
||
const topic = "foobar" | ||
const numHosts = 5 | ||
|
||
virtualPeer := tnet.RandPeerNetParamsOrFatal(t) | ||
hosts := getNetHosts(t, ctx, numHosts) | ||
topics := getTopics(getPubsubs(ctx, hosts), topic) | ||
|
||
// [0.Rel] - [1.Rel] - [2.Sub] | ||
// | | ||
// [3.Rel] - [4.Sub] | ||
|
||
connect(t, hosts[0], hosts[1]) | ||
connect(t, hosts[1], hosts[2]) | ||
connect(t, hosts[1], hosts[3]) | ||
connect(t, hosts[3], hosts[4]) | ||
|
||
time.Sleep(time.Millisecond * 100) | ||
|
||
var subs []*Subscription | ||
|
||
for i, topicValue := range topics { | ||
if i == 2 || i == 4 { | ||
sub, err := topicValue.Subscribe() | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
subs = append(subs, sub) | ||
} else { | ||
_, err := topicValue.Relay() | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
} | ||
} | ||
|
||
time.Sleep(time.Millisecond * 100) | ||
|
||
for i := 0; i < 100; i++ { | ||
msg := []byte("message") | ||
|
||
owner := rand.Intn(len(topics)) | ||
|
||
withVirtualKey := WithSecretKeyAndPeerId(virtualPeer.PrivKey, virtualPeer.ID) | ||
err := topics[owner].Publish(ctx, msg, withVirtualKey) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
for _, sub := range subs { | ||
received, errSub := sub.Next(ctx) | ||
if errSub != nil { | ||
t.Fatal(errSub) | ||
} | ||
|
||
if !bytes.Equal(msg, received.Data) { | ||
t.Fatal("received message is other than expected") | ||
} | ||
if string(received.From) != string(virtualPeer.ID) { | ||
t.Fatal("received message is not from the virtual peer") | ||
} | ||
} | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
can we remove the underscore?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
sure
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this is the Elrond pattern used in tests :)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done