-
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.
Some Gossip code refactoring and log msg formatting
1) ToGossipMessage now returns a more meaningful error and the methods that call it now log an error that is based on the context 2) Follow-up fix of a CR comment: https://gerrit.hyperledger.org/r/#/c/5965/13/gossip/comm/comm_impl.go@532 3) selfNetworkMember() accidently logged a log entry that says it is starting a new gossip service, but actually the method is invoked more than once- so multiple messages were logged instead of only one, so I extracted the log invocation to the constructor method. 4) The SignedGossipMessage has a payload which is a byte slice. No reason to log it, so I implemented a String() method for the SignedGossipMessage that just logs the length of the Payload and signature, and of the secret payload. 5) The Payload of the DataMessage that contains a ledger block should be printed as the length of the block and its sequence. I implemented String() method for GossipMessage_DataMsg. 6) Some messages contain a snapshot of envelopes, or payloads and are printed as a slice of tuples of payload (bytes) and signatures (bytes). This is ugly, fills the logs and doesn't give any info. I made the String() of GossipMessage print only the number of items in case it contains such a message. Signed-off-by: Yacov Manevich <[email protected]> Change-Id: Ie59b08ed94ef2f602b36d2ba3ab9b7260bf3f947
- Loading branch information
Showing
8 changed files
with
234 additions
and
10 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
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
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
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,149 @@ | ||
/* | ||
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 gossip | ||
|
||
import ( | ||
"fmt" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestToString(t *testing.T) { | ||
// Ensure we don't print the byte content when we | ||
// log messages. | ||
// Each payload or signature contains '2' so we would've logged | ||
// them if not for the overloading of the String() method in SignedGossipMessage | ||
|
||
// The following line proves that the envelopes constructed in this test | ||
// have "2" in them when they are printed | ||
assert.Contains(t, fmt.Sprintf("%v", envelopes()[0]), "2") | ||
// and the following does the same for payloads: | ||
dMsg := &DataMessage{ | ||
Payload: &Payload{ | ||
SeqNum: 3, | ||
Data: []byte{2, 2, 2, 2, 2}, | ||
}, | ||
} | ||
assert.Contains(t, fmt.Sprintf("%v", dMsg), "2") | ||
|
||
// Now we construct all types of messages that have envelopes or payloads in them | ||
// and see that "2" is not outputted into their formatting even though it is found | ||
// as a sub-message of the outer message. | ||
|
||
sMsg := &SignedGossipMessage{ | ||
GossipMessage: &GossipMessage{ | ||
Tag: GossipMessage_EMPTY, | ||
Nonce: 5, | ||
Channel: []byte("A"), | ||
Content: &GossipMessage_DataMsg{ | ||
DataMsg: &DataMessage{ | ||
Payload: &Payload{ | ||
SeqNum: 3, | ||
Data: []byte{2, 2, 2, 2, 2}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
Envelope: &Envelope{ | ||
Payload: []byte{0, 1, 2, 3, 4, 5, 6}, | ||
Signature: []byte{0, 1, 2}, | ||
SecretEnvelope: &SecretEnvelope{ | ||
Payload: []byte{0, 1, 2, 3, 4, 5}, | ||
Signature: []byte{0, 1, 2}, | ||
}, | ||
}, | ||
} | ||
assert.NotContains(t, fmt.Sprintf("%v", sMsg), "2") | ||
|
||
sMsg = &SignedGossipMessage{ | ||
GossipMessage: &GossipMessage{ | ||
Channel: []byte("A"), | ||
Tag: GossipMessage_EMPTY, | ||
Nonce: 5, | ||
Content: &GossipMessage_DataUpdate{ | ||
DataUpdate: &DataUpdate{ | ||
Nonce: 11, | ||
MsgType: PullMsgType_BlockMessage, | ||
Data: envelopes(), | ||
}, | ||
}, | ||
}, | ||
Envelope: envelopes()[0], | ||
} | ||
assert.NotContains(t, fmt.Sprintf("%v", sMsg), "2") | ||
|
||
sMsg = &SignedGossipMessage{ | ||
GossipMessage: &GossipMessage{ | ||
Channel: []byte("A"), | ||
Tag: GossipMessage_EMPTY, | ||
Nonce: 5, | ||
Content: &GossipMessage_MemRes{ | ||
MemRes: &MembershipResponse{ | ||
Alive: envelopes(), | ||
Dead: envelopes(), | ||
}, | ||
}, | ||
}, | ||
Envelope: envelopes()[0], | ||
} | ||
assert.NotContains(t, fmt.Sprintf("%v", sMsg), "2") | ||
|
||
sMsg = &SignedGossipMessage{ | ||
GossipMessage: &GossipMessage{ | ||
Channel: []byte("A"), | ||
Tag: GossipMessage_EMPTY, | ||
Nonce: 5, | ||
Content: &GossipMessage_StateSnapshot{ | ||
StateSnapshot: &StateInfoSnapshot{ | ||
Elements: envelopes(), | ||
}, | ||
}, | ||
}, | ||
Envelope: envelopes()[0], | ||
} | ||
assert.NotContains(t, fmt.Sprintf("%v", sMsg), "2") | ||
|
||
sMsg = &SignedGossipMessage{ | ||
GossipMessage: &GossipMessage{ | ||
Channel: []byte("A"), | ||
Tag: GossipMessage_EMPTY, | ||
Nonce: 5, | ||
Content: &GossipMessage_StateResponse{ | ||
StateResponse: &RemoteStateResponse{ | ||
Payloads: []*Payload{ | ||
{Data: []byte{2, 2, 2}}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
Envelope: envelopes()[0], | ||
} | ||
assert.NotContains(t, fmt.Sprintf("%v", sMsg), "2") | ||
} | ||
|
||
func envelopes() []*Envelope { | ||
return []*Envelope{ | ||
{Payload: []byte{2, 2, 2}, | ||
Signature: []byte{2, 2, 2}, | ||
SecretEnvelope: &SecretEnvelope{ | ||
Payload: []byte{2, 2, 2}, | ||
Signature: []byte{2, 2, 2}, | ||
}, | ||
}, | ||
} | ||
} |