From 1ca3393ec3f7f772d9ce19750140b6a9166f2df0 Mon Sep 17 00:00:00 2001 From: Artem Barger Date: Tue, 28 Nov 2017 10:13:55 +0200 Subject: [PATCH] [FAB-7123] Clean golint warnings There are few golint warning on gossip module packages, this commit takes care to address them. Change-Id: I13a85ad990f981a0da42446962ac0993383cc8b5 Signed-off-by: Artem Barger --- gossip/comm/crypto.go | 6 +++--- gossip/common/common.go | 4 ++-- gossip/gossip/certstore.go | 4 ++-- gossip/gossip/gossip_test.go | 8 ++++---- gossip/gossip/orgs_test.go | 4 ++-- gossip/privdata/coordinator.go | 12 ++++++++---- gossip/privdata/coordinator_test.go | 4 ++-- gossip/privdata/dataretriever.go | 3 +++ gossip/privdata/pull.go | 11 +++++++---- gossip/privdata/pull_test.go | 3 +-- gossip/privdata/util.go | 4 ++-- gossip/service/gossip_service.go | 4 ++++ gossip/service/gossip_service_test.go | 4 ++-- gossip/state/state.go | 2 +- gossip/state/state_test.go | 4 ++-- 15 files changed, 45 insertions(+), 32 deletions(-) diff --git a/gossip/comm/crypto.go b/gossip/comm/crypto.go index 5d059fca4ed..b00fe078670 100644 --- a/gossip/comm/crypto.go +++ b/gossip/comm/crypto.go @@ -17,8 +17,6 @@ import ( "math/big" "os" - "errors" - "github.com/hyperledger/fabric/common/util" gutil "github.com/hyperledger/fabric/gossip/util" "golang.org/x/net/context" @@ -35,6 +33,8 @@ func writeFile(filename string, keyType string, data []byte) error { return pem.Encode(f, &pem.Block{Type: keyType, Bytes: data}) } +// GenerateCertificatesOrPanic generates a a random pair of public and private keys +// and return TLS certificate func GenerateCertificatesOrPanic() tls.Certificate { privKeyFile := fmt.Sprintf("key.%d.priv", gutil.RandomUInt64()) certKeyFile := fmt.Sprintf("cert.%d.pub", gutil.RandomUInt64()) @@ -76,7 +76,7 @@ func GenerateCertificatesOrPanic() tls.Certificate { panic(err) } if len(cert.Certificate) == 0 { - panic(errors.New("Certificate chain is empty")) + panic("Certificate chain is empty") } return cert } diff --git a/gossip/common/common.go b/gossip/common/common.go index 0b382dd2024..14c4d69d1ff 100644 --- a/gossip/common/common.go +++ b/gossip/common/common.go @@ -23,8 +23,8 @@ type PKIidType []byte // IsNotSameFilter generate filter function which // provides a predicate to identify whenever current id // equals to another one. -func (this PKIidType) IsNotSameFilter(that PKIidType) bool { - return !bytes.Equal(this, that) +func (id PKIidType) IsNotSameFilter(that PKIidType) bool { + return !bytes.Equal(id, that) } // MessageAcceptor is a predicate that is used to diff --git a/gossip/gossip/certstore.go b/gossip/gossip/certstore.go index eab29ff5c3e..e7e5c1be19a 100644 --- a/gossip/gossip/certstore.go +++ b/gossip/gossip/certstore.go @@ -44,11 +44,11 @@ func newCertStore(puller pull.Mediator, idMapper identity.Mapper, selfIdentity a certStore.logger.Panicf("Failed associating self PKIID to cert: %+v", errors.WithStack(err)) } - selfIdMsg, err := certStore.createIdentityMessage() + selfIDMsg, err := certStore.createIdentityMessage() if err != nil { certStore.logger.Panicf("Failed creating self identity message: %+v", errors.WithStack(err)) } - puller.Add(selfIdMsg) + puller.Add(selfIDMsg) puller.RegisterMsgHook(pull.RequestMsgType, func(_ []string, msgs []*proto.SignedGossipMessage, _ proto.ReceivedMessage) { for _, msg := range msgs { pkiID := common.PKIidType(msg.GetPeerIdentity().PkiId) diff --git a/gossip/gossip/gossip_test.go b/gossip/gossip/gossip_test.go index 3f8f75c40f5..a8d59540a69 100644 --- a/gossip/gossip/gossip_test.go +++ b/gossip/gossip/gossip_test.go @@ -236,9 +236,9 @@ func newGossipInstanceWithCustomMCS(portPrefix int, id int, maxMsgCount int, mcs PublishStateInfoInterval: time.Duration(1) * time.Second, RequestStateInfoInterval: time.Duration(1) * time.Second, } - selfId := api.PeerIdentityType(conf.InternalEndpoint) + selfID := api.PeerIdentityType(conf.InternalEndpoint) g := NewGossipServiceWithServer(conf, &orgCryptoService{}, mcs, - selfId, nil) + selfID, nil) return g } @@ -268,9 +268,9 @@ func newGossipInstanceWithOnlyPull(portPrefix int, id int, maxMsgCount int, boot } cryptoService := &naiveCryptoService{} - selfId := api.PeerIdentityType(conf.InternalEndpoint) + selfID := api.PeerIdentityType(conf.InternalEndpoint) g := NewGossipServiceWithServer(conf, &orgCryptoService{}, cryptoService, - selfId, nil) + selfID, nil) return g } diff --git a/gossip/gossip/orgs_test.go b/gossip/gossip/orgs_test.go index a18f8fd972b..3e34537f1e8 100644 --- a/gossip/gossip/orgs_test.go +++ b/gossip/gossip/orgs_test.go @@ -112,8 +112,8 @@ func newGossipInstanceWithExternalEndpoint(portPrefix int, id int, mcs *configur PublishStateInfoInterval: time.Duration(1) * time.Second, RequestStateInfoInterval: time.Duration(1) * time.Second, } - selfId := api.PeerIdentityType(conf.InternalEndpoint) - g := NewGossipServiceWithServer(conf, mcs, mcs, selfId, + selfID := api.PeerIdentityType(conf.InternalEndpoint) + g := NewGossipServiceWithServer(conf, mcs, mcs, selfID, nil) return g diff --git a/gossip/privdata/coordinator.go b/gossip/privdata/coordinator.go index cf3cfd6c154..a2d747fd232 100644 --- a/gossip/privdata/coordinator.go +++ b/gossip/privdata/coordinator.go @@ -99,10 +99,14 @@ func (d2s dig2sources) keys() []*gossip2.PvtDataDigest { return res } +// Fetcher interface which defines API to fetch missing +// private data elements type Fetcher interface { fetch(dig2src dig2sources) ([]*gossip2.PvtDataElement, error) } +// Support encapsulates set of interfaces to +// aggregate required functionality by single struct type Support struct { privdata.CollectionStore txvalidator.Validator @@ -573,14 +577,14 @@ func (data blockData) forEachTxn(txsFilter txValidationFlags, consumer blockCons func endorsersFromOrgs(ns string, col string, endorsers []*peer.Endorsement, orgs []string) []*peer.Endorsement { var res []*peer.Endorsement for _, e := range endorsers { - sId := &msp.SerializedIdentity{} - err := proto.Unmarshal(e.Endorser, sId) + sID := &msp.SerializedIdentity{} + err := proto.Unmarshal(e.Endorser, sID) if err != nil { logger.Warning("Failed unmarshalling endorser:", err) continue } - if !util.Contains(sId.Mspid, orgs) { - logger.Debug(sId.Mspid, "isn't among the collection's orgs:", orgs, "for namespace", ns, ",collection", col) + if !util.Contains(sID.Mspid, orgs) { + logger.Debug(sID.Mspid, "isn't among the collection's orgs:", orgs, "for namespace", ns, ",collection", col) continue } res = append(res, e) diff --git a/gossip/privdata/coordinator_test.go b/gossip/privdata/coordinator_test.go index deebdfb64ae..236c860fb08 100644 --- a/gossip/privdata/coordinator_test.go +++ b/gossip/privdata/coordinator_test.go @@ -220,8 +220,8 @@ func (fc *fetchCall) expectingEndorsers(orgs ...string) *fetchCall { fc.fetcher.expectedEndorsers = make(map[string]struct{}) } for _, org := range orgs { - sId := &msp.SerializedIdentity{Mspid: org, IdBytes: []byte(fmt.Sprintf("p0%s", org))} - b, _ := pb.Marshal(sId) + sID := &msp.SerializedIdentity{Mspid: org, IdBytes: []byte(fmt.Sprintf("p0%s", org))} + b, _ := pb.Marshal(sID) fc.fetcher.expectedEndorsers[string(b)] = struct{}{} } diff --git a/gossip/privdata/dataretriever.go b/gossip/privdata/dataretriever.go index 349fb675f7b..265e32406e1 100644 --- a/gossip/privdata/dataretriever.go +++ b/gossip/privdata/dataretriever.go @@ -3,6 +3,7 @@ Copyright IBM Corp. All Rights Reserved. SPDX-License-Identifier: Apache-2.0 */ + package privdata import ( @@ -40,6 +41,8 @@ type dataRetriever struct { store DataStore } +// NewDataRetriever constructing function for implementation of the +// StorageDataRetriever interface func NewDataRetriever(store DataStore) StorageDataRetriever { return &dataRetriever{store: store} } diff --git a/gossip/privdata/pull.go b/gossip/privdata/pull.go index dafa3159d8d..48e5b505dd9 100644 --- a/gossip/privdata/pull.go +++ b/gossip/privdata/pull.go @@ -27,11 +27,13 @@ import ( ) const ( - MembershipPollingBackoff = time.Second + membershipPollingBackoff = time.Second responseWaitTime = time.Second * 5 maxMembershipPollIterations = 5 ) +// PrivateDataRetriever interfacce which defines API capable +// of retrieving required private data type PrivateDataRetriever interface { // CollectionRWSet returns the bytes of CollectionPvtReadWriteSet for a given txID and collection from the transient store CollectionRWSet(dig *proto.PvtDataDigest) []util.PrivateRWSet @@ -67,6 +69,7 @@ type puller struct { PrivateDataRetriever } +// NewPuller creates new private data puller func NewPuller(cs privdata.CollectionStore, g gossip, dataRetriever PrivateDataRetriever, channel string) *puller { p := &puller{ pubSub: util.NewPubSub(), @@ -195,7 +198,7 @@ func (p *puller) waitForMembership() []discovery.NetworkMember { if polIteration == maxMembershipPollIterations { return nil } - time.Sleep(MembershipPollingBackoff) + time.Sleep(membershipPollingBackoff) } return members } @@ -354,10 +357,10 @@ func (dig2f digestToFilterMapping) flattenFilterValues() []filter.RoutingFilter } // String returns a string representation of t he digestToFilterMapping -func (dig2Filter digestToFilterMapping) String() string { +func (dig2f digestToFilterMapping) String() string { var buffer bytes.Buffer collection2TxID := make(map[string][]string) - for dig := range dig2Filter { + for dig := range dig2f { collection2TxID[dig.Collection] = append(collection2TxID[dig.Collection], dig.TxId) } for col, txIDs := range collection2TxID { diff --git a/gossip/privdata/pull_test.go b/gossip/privdata/pull_test.go index ce38274a8ff..0aba8f8a23d 100644 --- a/gossip/privdata/pull_test.go +++ b/gossip/privdata/pull_test.go @@ -152,9 +152,8 @@ func (g *mockGossip) PeerFilter(channel common.ChainID, messagePredicate api.Sub args := g.Called(channel, messagePredicate) if args.Get(1) != nil { return nil, args.Get(1).(error) - } else { - return args.Get(0).(filter.RoutingFilter), nil } + return args.Get(0).(filter.RoutingFilter), nil } } return func(member discovery.NetworkMember) bool { diff --git a/gossip/privdata/util.go b/gossip/privdata/util.go index 24794f5aabc..c6ac9768c36 100644 --- a/gossip/privdata/util.go +++ b/gossip/privdata/util.go @@ -72,8 +72,8 @@ func (bf *blockFactory) AddTxnWithEndorsement(txID string, nsName string, hash [ } if org != "" { - sId := &msp.SerializedIdentity{Mspid: org, IdBytes: []byte(fmt.Sprintf("p0%s", org))} - b, _ := proto.Marshal(sId) + sID := &msp.SerializedIdentity{Mspid: org, IdBytes: []byte(fmt.Sprintf("p0%s", org))} + b, _ := proto.Marshal(sID) ccPayload.Action.Endorsements = []*peer.Endorsement{ { Endorser: b, diff --git a/gossip/service/gossip_service.go b/gossip/service/gossip_service.go index 301dcf3a573..4e74fb2cbe9 100644 --- a/gossip/service/gossip_service.go +++ b/gossip/service/gossip_service.go @@ -197,6 +197,8 @@ func (g *gossipServiceImpl) NewConfigEventer() ConfigProcessor { return newConfigEventer(g) } +// Support aggregates functionality of several +// interfaces required by gossip service type Support struct { Validator txvalidator.Validator Committer committer.Committer @@ -204,6 +206,8 @@ type Support struct { Cs privdata.CollectionStore } +// DataStoreSupport aggregates interfaces capable +// of handling either incoming blocks or private data type DataStoreSupport struct { committer.Committer privdata2.TransientStore diff --git a/gossip/service/gossip_service_test.go b/gossip/service/gossip_service_test.go index 53cdc90e0dc..aca51107898 100644 --- a/gossip/service/gossip_service_test.go +++ b/gossip/service/gossip_service_test.go @@ -652,11 +652,11 @@ func newGossipInstance(portPrefix int, id int, maxMsgCount int, boot ...int) Gos PublishStateInfoInterval: time.Duration(1) * time.Second, RequestStateInfoInterval: time.Duration(1) * time.Second, } - selfId := api.PeerIdentityType(conf.InternalEndpoint) + selfID := api.PeerIdentityType(conf.InternalEndpoint) cryptoService := &naiveCryptoService{} gossip := gossip.NewGossipServiceWithServer(conf, &orgCryptoService{}, cryptoService, - selfId, nil) + selfID, nil) gossipService := &gossipServiceImpl{ mcs: cryptoService, diff --git a/gossip/state/state.go b/gossip/state/state.go index 917ae6e4203..2ae2d1b5bd0 100644 --- a/gossip/state/state.go +++ b/gossip/state/state.go @@ -154,7 +154,7 @@ func init() { logger = util.GetLogger(util.LoggingStateModule, "") } -// NewGossipCoordinatedStateProvider creates state provider with coordinator instance +// NewGossipStateProvider creates state provider with coordinator instance // to orchestrate arrival of private rwsets and blocks before committing them into the ledger. func NewGossipStateProvider(chainID string, services *ServicesMediator, ledger ledgerResources) GossipStateProvider { diff --git a/gossip/state/state_test.go b/gossip/state/state_test.go index 1c4ad7e87d4..51a68e826f8 100644 --- a/gossip/state/state_test.go +++ b/gossip/state/state_test.go @@ -1246,7 +1246,7 @@ func TestTransferOfPrivateRWSet(t *testing.T) { coord1.On("LedgerHeight", mock.Anything).Return(uint64(5), nil) - var data map[uint64]*testData = map[uint64]*testData{ + var data = map[uint64]*testData{ uint64(2): { block: &pcomm.Block{ Header: &pcomm.BlockHeader{ @@ -1422,7 +1422,7 @@ func (t testPeer) Comm() chan proto.ReceivedMessage { return t.commChannel } -var peers map[string]testPeer = map[string]testPeer{ +var peers = map[string]testPeer{ "peer1": { id: "peer1", gossipChannel: make(chan *proto.GossipMessage),