Skip to content

Commit

Permalink
[FAB-9312] Resolve metalinter warnings
Browse files Browse the repository at this point in the history
- fix fab pkg

Change-Id: I1a9bbb398b8e9712cd9b1e715477d7506b04461c
Signed-off-by: Firas Qutishat <[email protected]>
  • Loading branch information
fqutishat committed Apr 10, 2018
1 parent 3a2cb7c commit 03bf99b
Show file tree
Hide file tree
Showing 15 changed files with 304 additions and 279 deletions.
44 changes: 32 additions & 12 deletions pkg/fab/ccpackager/gopackager/packager.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ import (
"github.com/hyperledger/fabric-sdk-go/pkg/fab/resource/api"
"github.com/pkg/errors"

"fmt"

pb "github.com/hyperledger/fabric-sdk-go/third_party/github.com/hyperledger/fabric/protos/peer"
)

Expand Down Expand Up @@ -101,10 +103,8 @@ func findSource(goPath string, filePath string) ([]*Descriptor, error) {
return nil

})
if err != nil {
return descriptors, err
}
return descriptors, nil

return descriptors, err
}

// -------------------------------------------------------------------------
Expand Down Expand Up @@ -138,26 +138,42 @@ func generateTarGz(descriptors []*Descriptor) ([]byte, error) {
logger.Debugf("generateTarGz for %s", v.fqp)
err := packEntry(tw, gw, v)
if err != nil {
closeStream(tw, gw)
err1 := closeStream(tw, gw)
if err1 != nil {
return nil, errors.Wrap(err, fmt.Sprintf("packEntry failed and close error %v", err1))
}
return nil, errors.Wrap(err, "packEntry failed")
}
}
closeStream(tw, gw)
err := closeStream(tw, gw)
if err != nil {
return nil, errors.Wrap(err, "closeStream failed")
}
return codePackage.Bytes(), nil

}

func closeStream(tw *tar.Writer, gw *gzip.Writer) {
tw.Close()
gw.Close()
func closeStream(tw io.Closer, gw io.Closer) error {
err := tw.Close()
if err != nil {
return err
}
err = gw.Close()
return err
}

func packEntry(tw *tar.Writer, gw *gzip.Writer, descriptor *Descriptor) error {
file, err := os.Open(descriptor.fqp)
if err != nil {
return err
}
defer file.Close()
defer func() {
err := file.Close()
if err != nil {
logger.Warnf("error file close %v", err)
}
}()

if stat, err := file.Stat(); err == nil {

// now lets create the header as needed for this file within the tarball
Expand All @@ -179,8 +195,12 @@ func packEntry(tw *tar.Writer, gw *gzip.Writer, descriptor *Descriptor) error {
if _, err := io.Copy(tw, file); err != nil {
return err
}
tw.Flush()
gw.Flush()
if err := tw.Flush(); err != nil {
return err
}
if err := gw.Flush(); err != nil {
return err
}

}
return nil
Expand Down
9 changes: 0 additions & 9 deletions pkg/fab/channel/ledger.go
Original file line number Diff line number Diff line change
Expand Up @@ -228,15 +228,6 @@ func (c *Ledger) QueryConfigBlock(reqCtx reqContext.Context, targets []fab.Propo
return createCommonBlock(tprs[0])
}

func collectProposalResponses(tprs []*fab.TransactionProposalResponse) [][]byte {
responses := [][]byte{}
for _, tpr := range tprs {
responses = append(responses, tpr.ProposalResponse.GetResponse().Payload)
}

return responses
}

func queryChaincode(reqCtx reqContext.Context, channelID string, request fab.ChaincodeInvokeRequest, targets []fab.ProposalProcessor, verifier ResponseVerifier) ([]*fab.TransactionProposalResponse, error) {
ctx, ok := contextImpl.RequestClientContext(reqCtx)
if !ok {
Expand Down
16 changes: 9 additions & 7 deletions pkg/fab/channel/ledger_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@ SPDX-License-Identifier: Apache-2.0
package channel

import (
reqContext "context"
"errors"
"fmt"
"strings"
"testing"

"time"

"github.com/golang/protobuf/proto"
Expand Down Expand Up @@ -175,13 +175,13 @@ func TestQueryConfig(t *testing.T) {
peer := mocks.MockPeer{MockName: "Peer1", MockURL: "http://peer1.com", MockRoles: []string{}, MockCert: nil, Payload: payload, Status: 200}

// fail with min endorsers
res, err := channel.QueryConfigBlock(reqCtx, []fab.ProposalProcessor{&peer}, &TransactionProposalResponseVerifier{MinResponses: 2})
_, err = channel.QueryConfigBlock(reqCtx, []fab.ProposalProcessor{&peer}, &TransactionProposalResponseVerifier{MinResponses: 2})
if err == nil {
t.Fatalf("Should have failed with since there's one endorser and at least two are required")
}

// success with one endorser
res, err = channel.QueryConfigBlock(reqCtx, []fab.ProposalProcessor{&peer}, &TransactionProposalResponseVerifier{MinResponses: 1})
res, err := channel.QueryConfigBlock(reqCtx, []fab.ProposalProcessor{&peer}, &TransactionProposalResponseVerifier{MinResponses: 1})
if err != nil || res == nil {
t.Fatalf("Test QueryConfig failed: %v", err)
}
Expand All @@ -196,6 +196,11 @@ func TestQueryConfig(t *testing.T) {
}

// Create different config block payload
createDifferentConfigBlockPayload(t, peer2, channel, reqCtx, peer)

}

func createDifferentConfigBlockPayload(t *testing.T, peer2 mocks.MockPeer, channel *Ledger, reqCtx reqContext.Context, peer mocks.MockPeer) {
builder2 := &mocks.MockConfigBlockBuilder{
MockConfigGroupBuilder: mocks.MockConfigGroupBuilder{
ModPolicy: "Admins",
Expand All @@ -208,19 +213,16 @@ func TestQueryConfig(t *testing.T) {
Index: 0,
LastConfigIndex: 0,
}

payload2, err := proto.Marshal(builder2.Build())
if err != nil {
t.Fatalf("Failed to marshal mock block 2")
}

// peer 2 now had different payload; query config block should fail
peer2.Payload = payload2
res, err = channel.QueryConfigBlock(reqCtx, []fab.ProposalProcessor{&peer, &peer2}, &TransactionProposalResponseVerifier{MinResponses: 2})
_, err = channel.QueryConfigBlock(reqCtx, []fab.ProposalProcessor{&peer, &peer2}, &TransactionProposalResponseVerifier{MinResponses: 2})
if err == nil {
t.Fatalf("Should have failed for different block payloads")
}

}

func TestQueryConfigBlockDifferentMetadata(t *testing.T) {
Expand Down
7 changes: 4 additions & 3 deletions pkg/fab/channel/membership/membership.go
Original file line number Diff line number Diff line change
Expand Up @@ -145,11 +145,9 @@ func loadMSPs(mspConfigs []*mb.MSPConfig, cs core.CryptoSuite) ([]msp.MSP, error
}

// get the application org names
var orgs []string
orgUnits := fabricConfig.OrganizationalUnitIdentifiers
for _, orgUnit := range orgUnits {
logger.Debugf("loadMSPs - found org of :: %s", orgUnit.OrganizationalUnitIdentifier)
orgs = append(orgs, orgUnit.OrganizationalUnitIdentifier)
}

// TODO: Do something with orgs
Expand Down Expand Up @@ -193,7 +191,10 @@ func addCertsToConfig(config fab.EndpointConfig, pemCerts []byte) {
if err != nil {
logger.Warn("%v", err)
}
config.TLSCACertPool(cert)
_, err = config.TLSCACertPool(cert)
if err != nil {
logger.Warnf("TLSCACertPool failed %v", err)
}

}
}
14 changes: 0 additions & 14 deletions pkg/fab/channel/membership/membership_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -276,20 +276,6 @@ Vnsqn32+nmoGO6dn1CvwtUTBMAoGCCqGSM49BAMCA0gAMEUCIQCH8+Vw0L38dv/v
5fDo4hNhETQ=
-----END CERTIFICATE-----`

var expiredCertificate = `-----BEGIN CERTIFICATE-----
MIIB/TCCAaOgAwIBAgIBATAKBggqhkjOPQQDAjBOMRMwEQYDVQQKDArOoyBBY21l
IENvMRkwFwYDVQQDExB0ZXN0LmV4YW1wbGUuY29tMQ8wDQYDVQQqEwZHb3BoZXIx
CzAJBgNVBAYTAk5MMB4XDTE4MDMyMDE5NDE0NVoXDTE4MDMyMDIxNDE0NVowTjET
MBEGA1UECgwKzqMgQWNtZSBDbzEZMBcGA1UEAxMQdGVzdC5leGFtcGxlLmNvbTEP
MA0GA1UEKhMGR29waGVyMQswCQYDVQQGEwJOTDBZMBMGByqGSM49AgEGCCqGSM49
AwEHA0IABKuBweTX12BpJ4zbhYflCWUnEAWLmOsPohPOF38Bf2efHET0+MjD3fqR
FAXOxTxeDXCRJOrEavnimpFUZCAOxvCjcjBwMA4GA1UdDwEB/wQEAwICBDAmBgNV
HSUEHzAdBggrBgEFBQcDAgYIKwYBBQUHAwEGAioDBgOBCwEwDwYDVR0TAQH/BAUw
AwEB/zANBgNVHQ4EBgQEAQIDBDAWBgMqAwQED2V4dHJhIGV4dGVuc2lvbjAKBggq
hkjOPQQDAgNIADBFAiA9WgC9FDstiVmU6JreuN9AKGDhOjuC069A5A5K9fNLEwIh
AJ4v8ZQD4r2oPaiJtxJOPLx4OYGEPDUWmhdj3MkWUBZd
-----END CERTIFICATE-----`

type validity struct {
NotBefore, NotAfter time.Time
}
Expand Down
5 changes: 4 additions & 1 deletion pkg/fab/chconfig/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,10 @@ func NewCacheKey(ctx fab.ClientContext, pvdr Provider, channelID string) (CacheK
}

h := sha256.New()
h.Write(identity)
_, err = h.Write(identity)
if err != nil {
return nil, err
}
hash := h.Sum([]byte(channelID))

return &cacheKey{
Expand Down
Loading

0 comments on commit 03bf99b

Please sign in to comment.