Skip to content

Commit 847bc16

Browse files
committed
[FAB-3279]: Fix gossip related golint warnings
There are quite a few gossip related golint warnings produced during the validation. This commit takes care to fix them. Change-Id: I9c224cc7bb709688f2d7ff93d4740472ebea9d3c Signed-off-by: Artem Barger <[email protected]>
1 parent e4074ce commit 847bc16

17 files changed

+64
-52
lines changed

gossip/comm/comm_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -192,7 +192,7 @@ func TestViperConfig(t *testing.T) {
192192
viper.AutomaticEnv()
193193
err := viper.ReadInConfig()
194194
if err != nil { // Handle errors reading the config file
195-
panic(fmt.Errorf("Fatal error config file: %s \n", err))
195+
panic(fmt.Errorf("fatal error config file: %s", err))
196196
}
197197

198198
assert.Equal(t, time.Duration(2)*time.Second, util.GetDurationOrDefault("peer.gossip.connTimeout", 0))

gossip/discovery/discovery.go

+4-4
Original file line numberDiff line numberDiff line change
@@ -90,11 +90,11 @@ func (n *NetworkMember) String() string {
9090
// PreferredEndpoint computes the endpoint to connect to,
9191
// while preferring internal endpoint over the standard
9292
// endpoint
93-
func (nm NetworkMember) PreferredEndpoint() string {
94-
if nm.InternalEndpoint != "" {
95-
return nm.InternalEndpoint
93+
func (n NetworkMember) PreferredEndpoint() string {
94+
if n.InternalEndpoint != "" {
95+
return n.InternalEndpoint
9696
}
97-
return nm.Endpoint
97+
return n.Endpoint
9898
}
9999

100100
// Discovery is the interface that represents a discovery module

gossip/election/election.go

+8
Original file line numberDiff line numberDiff line change
@@ -383,18 +383,25 @@ func (le *leaderElectionSvcImpl) Stop() {
383383
le.stopWG.Wait()
384384
}
385385

386+
// SetStartupGracePeriod configures startup grace period interval,
387+
// the period of time to wait until election algorithm will start
386388
func SetStartupGracePeriod(t time.Duration) {
387389
viper.Set("peer.gossip.election.startupGracePeriod", t)
388390
}
389391

392+
// SetMembershipSampleInterval setups/initializes the frequency the
393+
// membership view should be checked
390394
func SetMembershipSampleInterval(t time.Duration) {
391395
viper.Set("peer.gossip.election.membershipSampleInterval", t)
392396
}
393397

398+
// SetLeaderAliveThreshold configures leader election alive threshold
394399
func SetLeaderAliveThreshold(t time.Duration) {
395400
viper.Set("peer.gossip.election.leaderAliveThreshold", t)
396401
}
397402

403+
// SetLeaderElectionDuration configures expected leadership election duration,
404+
// interval to wait until leader election will be completed
398405
func SetLeaderElectionDuration(t time.Duration) {
399406
viper.Set("peer.gossip.election.leaderElectionDuration", t)
400407
}
@@ -419,6 +426,7 @@ func getLeaderElectionDuration() time.Duration {
419426
return util.GetDurationOrDefault("peer.gossip.election.leaderElectionDuration", time.Second*5)
420427
}
421428

429+
// GetMsgExpirationTimeout return leadership message expiration timeout
422430
func GetMsgExpirationTimeout() time.Duration {
423431
return getLeaderAliveThreshold() * 10
424432
}

gossip/gossip/certstore_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ func TestCertStoreShouldSucceed(t *testing.T) {
109109
}
110110

111111
func testCertificateUpdate(t *testing.T, updateFactory func(uint64) proto.ReceivedMessage, shouldSucceed bool) {
112-
config := pull.PullConfig{
112+
config := pull.Config{
113113
MsgType: proto.PullMsgType_IDENTITY_MSG,
114114
PeerCountToSelect: 1,
115115
PullInterval: time.Millisecond * 500,

gossip/gossip/channel/channel.go

+7-7
Original file line numberDiff line numberDiff line change
@@ -257,7 +257,7 @@ func (gc *gossipChannel) publishStateInfo() {
257257
}
258258

259259
func (gc *gossipChannel) createBlockPuller() pull.Mediator {
260-
conf := pull.PullConfig{
260+
conf := pull.Config{
261261
MsgType: proto.PullMsgType_BLOCK_MSG,
262262
Channel: []byte(gc.chainID),
263263
ID: gc.GetConf().ID,
@@ -491,7 +491,7 @@ func (gc *gossipChannel) handleStateInfSnapshot(m *proto.GossipMessage, sender c
491491
return
492492
}
493493

494-
expectedMAC := ChannelMAC(si.PkiId, gc.chainID)
494+
expectedMAC := GenerateMAC(si.PkiId, gc.chainID)
495495
if !bytes.Equal(si.ChannelMAC, expectedMAC) {
496496
gc.logger.Warning("Channel", chanName, ": StateInfo message", stateInf,
497497
", has an invalid MAC. Expected", expectedMAC, ", got", si.ChannelMAC, ", sent from", sender)
@@ -561,7 +561,7 @@ func (gc *gossipChannel) verifyMsg(msg proto.ReceivedMessage) bool {
561561

562562
if m.IsStateInfoMsg() {
563563
si := m.GetStateInfo()
564-
expectedMAC := ChannelMAC(si.PkiId, gc.chainID)
564+
expectedMAC := GenerateMAC(si.PkiId, gc.chainID)
565565
if !bytes.Equal(expectedMAC, si.ChannelMAC) {
566566
gc.logger.Warning("Message contains wrong channel MAC(", si.ChannelMAC, "), expected", expectedMAC)
567567
return false
@@ -571,7 +571,7 @@ func (gc *gossipChannel) verifyMsg(msg proto.ReceivedMessage) bool {
571571

572572
if m.IsStateInfoPullRequestMsg() {
573573
sipr := m.GetStateInfoPullReq()
574-
expectedMAC := ChannelMAC(msg.GetConnectionInfo().ID, gc.chainID)
574+
expectedMAC := GenerateMAC(msg.GetConnectionInfo().ID, gc.chainID)
575575
if !bytes.Equal(expectedMAC, sipr.ChannelMAC) {
576576
gc.logger.Warning("Message contains wrong channel MAC(", sipr.ChannelMAC, "), expected", expectedMAC)
577577
return false
@@ -592,7 +592,7 @@ func (gc *gossipChannel) createStateInfoRequest() *proto.SignedGossipMessage {
592592
Nonce: 0,
593593
Content: &proto.GossipMessage_StateInfoPullReq{
594594
StateInfoPullReq: &proto.StateInfoPullRequest{
595-
ChannelMAC: ChannelMAC(gc.pkiID, gc.chainID),
595+
ChannelMAC: GenerateMAC(gc.pkiID, gc.chainID),
596596
},
597597
},
598598
}).NoopSign()
@@ -658,9 +658,9 @@ func (cache *stateInfoCache) Add(msg *proto.SignedGossipMessage) bool {
658658
return added
659659
}
660660

661-
// ChannelMAC returns a byte slice that is derived from the peer's PKI-ID
661+
// GenerateMAC returns a byte slice that is derived from the peer's PKI-ID
662662
// and a channel name
663-
func ChannelMAC(pkiID common.PKIidType, channelID common.ChainID) []byte {
663+
func GenerateMAC(pkiID common.PKIidType, channelID common.ChainID) []byte {
664664
// Hash is computed on (PKI-ID || channel ID)
665665
preImage := append([]byte(pkiID), []byte(channelID)...)
666666
return common_utils.ComputeSHA256(preImage)

gossip/gossip/channel/channel_test.go

+5-5
Original file line numberDiff line numberDiff line change
@@ -434,7 +434,7 @@ func TestChannelPeerNotInChannel(t *testing.T) {
434434

435435
// Ensure we don't respond to a StateInfoRequest in the wrong channel from a peer in the right org
436436
req2 := gc.(*gossipChannel).createStateInfoRequest()
437-
req2.GetStateInfoPullReq().ChannelMAC = ChannelMAC(pkiIDInOrg1, common.ChainID("B"))
437+
req2.GetStateInfoPullReq().ChannelMAC = GenerateMAC(pkiIDInOrg1, common.ChainID("B"))
438438
invalidReceivedMsg2 := &receivedMsg{
439439
msg: req2,
440440
PKIID: pkiIDInOrg1,
@@ -809,7 +809,7 @@ func TestChannelStateInfoSnapshot(t *testing.T) {
809809
Tag: proto.GossipMessage_CHAN_OR_ORG,
810810
Content: &proto.GossipMessage_StateInfoPullReq{
811811
StateInfoPullReq: &proto.StateInfoPullRequest{
812-
ChannelMAC: append(ChannelMAC(pkiIDInOrg1, channelA), 1),
812+
ChannelMAC: append(GenerateMAC(pkiIDInOrg1, channelA), 1),
813813
},
814814
},
815815
}).NoopSign(),
@@ -832,7 +832,7 @@ func TestChannelStateInfoSnapshot(t *testing.T) {
832832
Tag: proto.GossipMessage_CHAN_OR_ORG,
833833
Content: &proto.GossipMessage_StateInfoPullReq{
834834
StateInfoPullReq: &proto.StateInfoPullRequest{
835-
ChannelMAC: ChannelMAC(pkiIDInOrg1, channelA),
835+
ChannelMAC: GenerateMAC(pkiIDInOrg1, channelA),
836836
},
837837
},
838838
}).NoopSign(),
@@ -1083,7 +1083,7 @@ func TestChannelGetPeers(t *testing.T) {
10831083
// and ensure that the StateInfo message doesn't count
10841084
gc = NewGossipChannel(pkiIDInOrg1, cs, channelA, adapter, &joinChanMsg{})
10851085
msg := &receivedMsg{PKIID: pkiIDInOrg1, msg: createStateInfoMsg(1, pkiIDInOrg1, channelA)}
1086-
msg.GetGossipMessage().GetStateInfo().ChannelMAC = ChannelMAC(pkiIDinOrg2, channelA)
1086+
msg.GetGossipMessage().GetStateInfo().ChannelMAC = GenerateMAC(pkiIDinOrg2, channelA)
10871087
gc.HandleMessage(msg)
10881088
assert.Len(t, gc.GetPeers(), 0)
10891089
}
@@ -1201,7 +1201,7 @@ func createStateInfoMsg(ledgerHeight int, pkiID common.PKIidType, channel common
12011201
Tag: proto.GossipMessage_CHAN_OR_ORG,
12021202
Content: &proto.GossipMessage_StateInfo{
12031203
StateInfo: &proto.StateInfo{
1204-
ChannelMAC: ChannelMAC(pkiID, channel),
1204+
ChannelMAC: GenerateMAC(pkiID, channel),
12051205
Timestamp: &proto.PeerTime{IncNumber: uint64(time.Now().UnixNano()), SeqNum: 1},
12061206
Metadata: []byte(fmt.Sprintf("%d", ledgerHeight)),
12071207
PkiId: []byte(pkiID),

gossip/gossip/chanstate.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ func (cs *channelState) getGossipChannelByMAC(receivedMAC []byte, pkiID common.P
8686
cs.RLock()
8787
defer cs.RUnlock()
8888
for chanName, gc := range cs.channels {
89-
mac := channel.ChannelMAC(pkiID, common.ChainID(chanName))
89+
mac := channel.GenerateMAC(pkiID, common.ChainID(chanName))
9090
if bytes.Equal(mac, receivedMAC) {
9191
return gc
9292
}

gossip/gossip/gossip_impl.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -912,7 +912,7 @@ func (sa *discoverySecurityAdapter) validateAliveMsgSignature(m *proto.SignedGos
912912
}
913913

914914
func (g *gossipServiceImpl) createCertStorePuller() pull.Mediator {
915-
conf := pull.PullConfig{
915+
conf := pull.Config{
916916
MsgType: proto.PullMsgType_IDENTITY_MSG,
917917
Channel: []byte(""),
918918
ID: g.conf.InternalEndpoint,
@@ -952,7 +952,7 @@ func (g *gossipServiceImpl) createCertStorePuller() pull.Mediator {
952952
func (g *gossipServiceImpl) createStateInfoMsg(metadata []byte, chainID common.ChainID) (*proto.SignedGossipMessage, error) {
953953
pkiID := g.comm.GetPKIid()
954954
stateInfMsg := &proto.StateInfo{
955-
ChannelMAC: channel.ChannelMAC(pkiID, chainID),
955+
ChannelMAC: channel.GenerateMAC(pkiID, chainID),
956956
Metadata: metadata,
957957
PkiId: g.comm.GetPKIid(),
958958
Timestamp: &proto.PeerTime{

gossip/gossip/orgs_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ func (c *configurableCryptoService) OrgByPeerIdentity(identity api.PeerIdentityT
6161

6262
// VerifyByChannel verifies a peer's signature on a message in the context
6363
// of a specific channel
64-
func (cs *configurableCryptoService) VerifyByChannel(_ common.ChainID, identity api.PeerIdentityType, _, _ []byte) error {
64+
func (c *configurableCryptoService) VerifyByChannel(_ common.ChainID, identity api.PeerIdentityType, _, _ []byte) error {
6565
return nil
6666
}
6767

gossip/gossip/pull/pullstore.go

+21-21
Original file line numberDiff line numberDiff line change
@@ -31,14 +31,14 @@ import (
3131

3232
// Constants go here.
3333
const (
34-
HelloMsgType PullMsgType = iota
34+
HelloMsgType MsgType = iota
3535
DigestMsgType
3636
RequestMsgType
3737
ResponseMsgType
3838
)
3939

40-
// PullMsgType defines the type of a message that is sent to the PullStore
41-
type PullMsgType int
40+
// MsgType defines the type of a message that is sent to the PullStore
41+
type MsgType int
4242

4343
// MessageHook defines a function that will run after a certain pull message is received
4444
type MessageHook func(itemIDs []string, items []*proto.SignedGossipMessage, msg proto.ReceivedMessage)
@@ -55,6 +55,16 @@ type MembershipService interface {
5555
GetMembership() []discovery.NetworkMember
5656
}
5757

58+
// Config defines the configuration of the pull mediator
59+
type Config struct {
60+
ID string
61+
PullInterval time.Duration // Duration between pull invocations
62+
PeerCountToSelect int // Number of peers to initiate pull with
63+
Tag proto.GossipMessage_Tag
64+
Channel common.ChainID
65+
MsgType proto.PullMsgType
66+
}
67+
5868
// DigestFilter filters digests to be sent to a remote peer, that
5969
// sent a hello with the following message
6070
type DigestFilter func(helloMsg proto.ReceivedMessage) func(digestItem string) bool
@@ -68,16 +78,6 @@ func (df DigestFilter) byContext() algo.DigestFilter {
6878
}
6979
}
7080

71-
// PullConfig defines the configuration of the pull mediator
72-
type PullConfig struct {
73-
ID string
74-
PullInterval time.Duration // Duration between pull invocations
75-
PeerCountToSelect int // Number of peers to initiate pull with
76-
Tag proto.GossipMessage_Tag
77-
Channel common.ChainID
78-
MsgType proto.PullMsgType
79-
}
80-
8181
// PullAdapter defines methods of the pullStore to interact
8282
// with various modules of gossip
8383
type PullAdapter struct {
@@ -99,7 +99,7 @@ type Mediator interface {
9999
Stop()
100100

101101
// RegisterMsgHook registers a message hook to a specific type of pull message
102-
RegisterMsgHook(PullMsgType, MessageHook)
102+
RegisterMsgHook(MsgType, MessageHook)
103103

104104
// Add adds a GossipMessage to the Mediator
105105
Add(*proto.SignedGossipMessage)
@@ -115,18 +115,18 @@ type Mediator interface {
115115
type pullMediatorImpl struct {
116116
sync.RWMutex
117117
Sender
118-
msgType2Hook map[PullMsgType][]MessageHook
118+
msgType2Hook map[MsgType][]MessageHook
119119
idExtractor proto.IdentifierExtractor
120120
msgCons proto.MsgConsumer
121-
config PullConfig
121+
config Config
122122
logger *logging.Logger
123123
itemID2Msg map[string]*proto.SignedGossipMessage
124124
memBvc MembershipService
125125
engine *algo.PullEngine
126126
}
127127

128128
// NewPullMediator returns a new Mediator
129-
func NewPullMediator(config PullConfig, adapter PullAdapter) Mediator {
129+
func NewPullMediator(config Config, adapter PullAdapter) Mediator {
130130
digFilter := adapter.DigFilter
131131

132132
acceptAllFilter := func(_ proto.ReceivedMessage) func(string) bool {
@@ -141,7 +141,7 @@ func NewPullMediator(config PullConfig, adapter PullAdapter) Mediator {
141141

142142
p := &pullMediatorImpl{
143143
msgCons: adapter.MsgCons,
144-
msgType2Hook: make(map[PullMsgType][]MessageHook),
144+
msgType2Hook: make(map[MsgType][]MessageHook),
145145
idExtractor: adapter.IdExtractor,
146146
config: config,
147147
logger: util.GetLogger(util.LoggingPullModule, config.ID),
@@ -169,7 +169,7 @@ func (p *pullMediatorImpl) HandleMessage(m proto.ReceivedMessage) {
169169

170170
itemIDs := []string{}
171171
items := []*proto.SignedGossipMessage{}
172-
var pullMsgType PullMsgType
172+
var pullMsgType MsgType
173173

174174
if helloMsg := msg.GetHello(); helloMsg != nil {
175175
pullMsgType = HelloMsgType
@@ -216,7 +216,7 @@ func (p *pullMediatorImpl) Stop() {
216216
}
217217

218218
// RegisterMsgHook registers a message hook to a specific type of pull message
219-
func (p *pullMediatorImpl) RegisterMsgHook(pullMsgType PullMsgType, hook MessageHook) {
219+
func (p *pullMediatorImpl) RegisterMsgHook(pullMsgType MsgType, hook MessageHook) {
220220
p.Lock()
221221
defer p.Unlock()
222222
p.msgType2Hook[pullMsgType] = append(p.msgType2Hook[pullMsgType], hook)
@@ -348,7 +348,7 @@ func (p *pullMediatorImpl) peersWithEndpoints(endpoints ...string) []*comm.Remot
348348
return peers
349349
}
350350

351-
func (p *pullMediatorImpl) hooksByMsgType(msgType PullMsgType) []MessageHook {
351+
func (p *pullMediatorImpl) hooksByMsgType(msgType MsgType) []MessageHook {
352352
p.RLock()
353353
defer p.RUnlock()
354354
returnedHooks := []MessageHook{}

gossip/gossip/pull/pullstore_test.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ func createPullInstanceWithFilters(endpoint string, peer2PullInst map[string]*pu
121121

122122
peer2PullInst[endpoint] = inst
123123

124-
conf := PullConfig{
124+
conf := Config{
125125
MsgType: proto.PullMsgType_BLOCK_MSG,
126126
Channel: []byte(""),
127127
ID: endpoint,
@@ -179,7 +179,7 @@ func TestRegisterMsgHook(t *testing.T) {
179179

180180
receivedMsgTypes := util.NewSet()
181181

182-
for _, msgType := range []PullMsgType{HelloMsgType, DigestMsgType, RequestMsgType, ResponseMsgType} {
182+
for _, msgType := range []MsgType{HelloMsgType, DigestMsgType, RequestMsgType, ResponseMsgType} {
183183
mType := msgType
184184
inst1.mediator.RegisterMsgHook(mType, func(_ []string, items []*proto.SignedGossipMessage, _ proto.ReceivedMessage) {
185185
receivedMsgTypes.Add(mType)

gossip/integration/integration_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ func setupTestEnv() {
7777
viper.AutomaticEnv()
7878
err := viper.ReadInConfig()
7979
if err != nil { // Handle errors reading the config file
80-
panic(fmt.Errorf("Fatal error config file: %s \n", err))
80+
panic(fmt.Errorf("fatal error config file: %s", err))
8181
}
8282
}
8383

gossip/service/gossip_service.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ func InitGossipService(peerIdentity []byte, endpoint string, s *grpc.Server, mcs
125125
InitGossipServiceCustomDeliveryFactory(peerIdentity, endpoint, s, &deliveryFactoryImpl{}, mcs, bootPeers...)
126126
}
127127

128-
// InitGossipService initialize gossip service with customize delivery factory
128+
// InitGossipServiceCustomDeliveryFactory initialize gossip service with customize delivery factory
129129
// implementation, might be useful for testing and mocking purposes
130130
func InitGossipServiceCustomDeliveryFactory(peerIdentity []byte, endpoint string, s *grpc.Server, factory DeliveryServiceFactory, mcs api.MessageCryptoService, bootPeers ...string) {
131131
once.Do(func() {

gossip/service/gossip_service_test.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -449,12 +449,12 @@ func (jmc *joinChanMsg) SequenceNumber() uint64 {
449449
}
450450

451451
// Members returns the organizations of the channel
452-
func (jcm *joinChanMsg) Members() []api.OrgIdentityType {
452+
func (jmc *joinChanMsg) Members() []api.OrgIdentityType {
453453
return []api.OrgIdentityType{orgInChannelA}
454454
}
455455

456456
// AnchorPeersOf returns the anchor peers of the given organization
457-
func (jcm *joinChanMsg) AnchorPeersOf(org api.OrgIdentityType) []api.AnchorPeer {
457+
func (jmc *joinChanMsg) AnchorPeersOf(org api.OrgIdentityType) []api.AnchorPeer {
458458
return []api.AnchorPeer{}
459459
}
460460

gossip/state/state.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -562,7 +562,7 @@ func (s *GossipStateProviderImpl) selectPeerToRequestFrom(height uint64) (*comm.
562562

563563
n := len(peers)
564564
if n == 0 {
565-
return nil, errors.New("There are no peers to ask for missing blocks from.")
565+
return nil, errors.New("there are no peers to ask for missing blocks from")
566566
}
567567

568568
// Select peers to ask for blocks

gossip/util/misc.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,7 @@ func GetIntOrDefault(key string, defVal int) int {
151151
return defVal
152152
}
153153

154-
// GetIntOrDefault returns the Duration value from config if present otherwise default value
154+
// GetDurationOrDefault returns the Duration value from config if present otherwise default value
155155
func GetDurationOrDefault(key string, defVal time.Duration) time.Duration {
156156
if val := viper.GetDuration(key); val != 0 {
157157
return val

0 commit comments

Comments
 (0)