-
Notifications
You must be signed in to change notification settings - Fork 8.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[FAB-2198] Adjust gossip membership layer
Continuation of: https://gerrit.hyperledger.org/r/#/c/5903/ Introduce envelopes to gossip message The gossip membership module has a storage of GossipMessages. It needs to be refactored to hold SignedMessages instead, but SignedMessages don't have any meaningful methods because they are only envelopes to GossipMessages. Therefore, this commit introduces an internal message type to the gossip membership layer, that has both the SignedGossipMessage and the GossipMessage as pointers. Also- previously the cachedMembership field was used but now can't be used because it's a raw protobuf structure. Therefore, I refactored it to a 'membershipStore' which is just a nice wrapper around a map from PKI-ID --> message. Signed-off-by: Yacov Manevich <yacovm@il.ibm.com> Change-Id: I2251c912b6a4610f6565f51c67bbb89d5d26b94f
- Loading branch information
Showing
3 changed files
with
201 additions
and
69 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 |
---|---|---|
@@ -0,0 +1,60 @@ | ||
/* | ||
Copyright IBM Corp. 2017 All Rights Reserved. | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
package discovery | ||
|
||
import ( | ||
"github.com/hyperledger/fabric/gossip/common" | ||
proto "github.com/hyperledger/fabric/protos/gossip" | ||
) | ||
|
||
type message struct { | ||
*proto.SignedGossipMessage | ||
*proto.GossipMessage | ||
} | ||
|
||
type membershipStore map[string]*message | ||
|
||
// msgByID returns a message stored by a certain ID, or nil | ||
// if such an ID isn't found | ||
func (m membershipStore) msgByID(pkiID common.PKIidType) *message { | ||
if msg, exists := m[string(pkiID)]; exists { | ||
return msg | ||
} | ||
return nil | ||
} | ||
|
||
// Put associates msg with the given pkiID | ||
func (m membershipStore) Put(pkiID common.PKIidType, msg *message) { | ||
m[string(pkiID)] = msg | ||
} | ||
|
||
// Remove removes a message with a given pkiID | ||
func (m membershipStore) Remove(pkiID common.PKIidType) { | ||
delete(m, string(pkiID)) | ||
} | ||
|
||
// ToSlice returns a slice backed by the elements | ||
// of the membershipStore | ||
func (m membershipStore) ToSlice() []*message { | ||
members := make([]*message, len(m)) | ||
i := 0 | ||
for _, member := range m { | ||
members[i] = member | ||
i++ | ||
} | ||
return members | ||
} |
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,95 @@ | ||
/* | ||
Copyright IBM Corp. 2017 All Rights Reserved. | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
package discovery | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/hyperledger/fabric/gossip/common" | ||
proto "github.com/hyperledger/fabric/protos/gossip" | ||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestMembershipStore(t *testing.T) { | ||
membershipStore := make(membershipStore, 0) | ||
|
||
id1 := common.PKIidType("id1") | ||
id2 := common.PKIidType("id2") | ||
|
||
msg1 := &message{} | ||
msg2 := &message{SignedGossipMessage: &proto.SignedGossipMessage{}} | ||
|
||
// Test initially created store is empty | ||
assert.Nil(t, membershipStore.msgByID(id1)) | ||
assert.Len(t, membershipStore, 0) | ||
// Test put works as expected | ||
membershipStore.Put(id1, msg1) | ||
assert.NotNil(t, membershipStore.msgByID(id1)) | ||
// Test msgByID returns the right instance stored | ||
membershipStore.Put(id2, msg2) | ||
assert.Equal(t, msg1, membershipStore.msgByID(id1)) | ||
assert.NotEqual(t, msg2, membershipStore.msgByID(id1)) | ||
// Test capacity grows | ||
assert.Len(t, membershipStore, 2) | ||
// Test remove works | ||
membershipStore.Remove(id1) | ||
assert.Nil(t, membershipStore.msgByID(id1)) | ||
assert.Len(t, membershipStore, 1) | ||
// Test returned instance is not a copy | ||
msg3 := &message{GossipMessage: &proto.GossipMessage{}} | ||
msg3Clone := &message{GossipMessage: &proto.GossipMessage{}} | ||
id3 := common.PKIidType("id3") | ||
membershipStore.Put(id3, msg3) | ||
assert.Equal(t, msg3Clone, msg3) | ||
membershipStore.msgByID(id3).Channel = []byte{0, 1, 2, 3} | ||
assert.NotEqual(t, msg3Clone, msg3) | ||
} | ||
|
||
func TestToSlice(t *testing.T) { | ||
membershipStore := make(membershipStore, 0) | ||
id1 := common.PKIidType("id1") | ||
id2 := common.PKIidType("id2") | ||
id3 := common.PKIidType("id3") | ||
id4 := common.PKIidType("id4") | ||
|
||
msg1 := &message{} | ||
msg2 := &message{SignedGossipMessage: &proto.SignedGossipMessage{}} | ||
msg3 := &message{GossipMessage: &proto.GossipMessage{}} | ||
msg4 := &message{GossipMessage: &proto.GossipMessage{}, SignedGossipMessage: &proto.SignedGossipMessage{}} | ||
|
||
membershipStore.Put(id1, msg1) | ||
membershipStore.Put(id2, msg2) | ||
membershipStore.Put(id3, msg3) | ||
membershipStore.Put(id4, msg4) | ||
|
||
assert.Len(t, membershipStore.ToSlice(), 4) | ||
|
||
existsInSlice := func(slice []*message, msg *message) bool { | ||
for _, m := range slice { | ||
if assert.ObjectsAreEqual(m, msg) { | ||
return true | ||
} | ||
} | ||
return false | ||
} | ||
|
||
expectedMsgs := []*message{msg1, msg2, msg3, msg4} | ||
for _, msg := range membershipStore.ToSlice() { | ||
assert.True(t, existsInSlice(expectedMsgs, msg)) | ||
} | ||
|
||
} |