Skip to content

Commit

Permalink
[FAB-8466] Use stdlib Context
Browse files Browse the repository at this point in the history
This change uses the standard library Context.
Leaking contexts are fixed.
Third party pin scripts are fixed for the new paths.

Change-Id: Ie0a2d93d4ca4a438cf18b9491f2c6d6b54deeb51
Signed-off-by: Troy Ronda <[email protected]>
  • Loading branch information
troyronda committed Feb 23, 2018
1 parent 86c95fa commit 1a42cd9
Show file tree
Hide file tree
Showing 22 changed files with 181 additions and 191 deletions.
2 changes: 1 addition & 1 deletion Gopkg.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 0 additions & 4 deletions Gopkg.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,6 @@
name = "github.com/golang/protobuf"
version = "1.0.0"

[[constraint]]
name = "golang.org/x/net"
branch = "master"

[[constraint]]
name = "github.com/cloudflare/cfssl"
branch = "master"
Expand Down
3 changes: 1 addition & 2 deletions internal/github.com/hyperledger/fabric-ca/util/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ import (
"io/ioutil"
"math/big"
mrand "math/rand"

"net/http"
"path/filepath"
"reflect"
Expand Down Expand Up @@ -142,7 +141,7 @@ func CreateToken(csp core.CryptoSuite, cert []byte, key core.Key, body []byte) (
//GenRSAToken signs the http body and cert with RSA using RSA private key
// @csp : BCCSP instance
/*
func GenRSAToken(csp apicryptosuite.CryptoSuite, cert []byte, key []byte, body []byte) (string, error) {
func GenRSAToken(csp core.CryptoSuite, cert []byte, key []byte, body []byte) (string, error) {
privKey, err := GetRSAPrivateKey(key)
if err != nil {
return "", err
Expand Down
4 changes: 2 additions & 2 deletions internal/github.com/hyperledger/fabric/msp/identities.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,15 @@ import (
"crypto/rand"
"crypto/x509"
"encoding/hex"

"encoding/pem"
"time"

"github.com/hyperledger/fabric-sdk-go/pkg/context/api/core"

"github.com/golang/protobuf/proto"
bccsp "github.com/hyperledger/fabric-sdk-go/internal/github.com/hyperledger/fabric/sdkpatch/cryptosuitebridge"
flogging "github.com/hyperledger/fabric-sdk-go/internal/github.com/hyperledger/fabric/sdkpatch/logbridge"
logging "github.com/hyperledger/fabric-sdk-go/internal/github.com/hyperledger/fabric/sdkpatch/logbridge"
"github.com/hyperledger/fabric-sdk-go/pkg/context/api/core"
"github.com/hyperledger/fabric-sdk-go/third_party/github.com/hyperledger/fabric/protos/msp"
"github.com/pkg/errors"
)
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion pkg/context/api/fab/orderer.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ SPDX-License-Identifier: Apache-2.0
package fab

import (
"context"

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

Expand All @@ -15,7 +17,7 @@ import (
type Orderer interface {
URL() string
SendBroadcast(envelope *SignedEnvelope) (*common.Status, error)
SendDeliver(envelope *SignedEnvelope) (chan *common.Block, chan error)
SendDeliver(envelope *SignedEnvelope) (chan *common.Block, chan error, context.CancelFunc)
}

// A SignedEnvelope can can be sent to an orderer for broadcasting
Expand Down
21 changes: 10 additions & 11 deletions pkg/fab/events/consumer/consumer.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,32 +7,29 @@ SPDX-License-Identifier: Apache-2.0
package consumer

import (
grpcContext "context"
"crypto/x509"
"io"
"sync"
"time"

grpcContext "golang.org/x/net/context"

"github.com/golang/protobuf/proto"
"github.com/golang/protobuf/ptypes"
"github.com/pkg/errors"
"google.golang.org/grpc"
"google.golang.org/grpc/credentials"
"google.golang.org/grpc/keepalive"

"github.com/golang/protobuf/ptypes"
consumer "github.com/hyperledger/fabric-sdk-go/internal/github.com/hyperledger/fabric/events/consumer"
"github.com/hyperledger/fabric-sdk-go/pkg/context"
"github.com/hyperledger/fabric-sdk-go/pkg/context/api/core"
"github.com/hyperledger/fabric-sdk-go/pkg/context/api/fab"
"github.com/hyperledger/fabric-sdk-go/pkg/core/config/comm"
ccomm "github.com/hyperledger/fabric-sdk-go/pkg/core/config/comm"
"github.com/hyperledger/fabric-sdk-go/pkg/core/config/urlutil"
"github.com/hyperledger/fabric-sdk-go/pkg/logging"
"github.com/hyperledger/fabric-sdk-go/third_party/github.com/hyperledger/fabric/protos/peer"
ehpb "github.com/hyperledger/fabric-sdk-go/third_party/github.com/hyperledger/fabric/protos/peer"

"crypto/x509"

consumer "github.com/hyperledger/fabric-sdk-go/internal/github.com/hyperledger/fabric/events/consumer"
"github.com/hyperledger/fabric-sdk-go/pkg/context"
"github.com/hyperledger/fabric-sdk-go/pkg/logging"
"github.com/pkg/errors"
)

var logger = logging.NewLogger("fabric_sdk_go")
Expand Down Expand Up @@ -111,7 +108,9 @@ func newEventsClientConnectionWithAddress(peerAddress string, cert *x509.Certifi
opts = append(opts, grpc.WithDefaultCallOptions(grpc.FailFast(failFast)))

ctx := grpcContext.Background()
ctx, _ = grpcContext.WithTimeout(ctx, config.TimeoutOrDefault(core.EventHubConnection))
ctx, cancel := grpcContext.WithTimeout(ctx, config.TimeoutOrDefault(core.EventHubConnection))
defer cancel()

conn, err := grpc.DialContext(ctx, urlutil.ToAddress(peerAddress), opts...)
if err != nil {
return nil, err
Expand Down
11 changes: 5 additions & 6 deletions pkg/fab/mocks/mockendorserserver.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,16 @@ SPDX-License-Identifier: Apache-2.0
package mocks

import (
"context"
"fmt"
"net"

"github.com/golang/protobuf/proto"
"golang.org/x/net/context"
"google.golang.org/grpc"

rwsetutil "github.com/hyperledger/fabric-sdk-go/third_party/github.com/hyperledger/fabric/core/ledger/kvledger/txmgmt/rwsetutil"
kvrwset "github.com/hyperledger/fabric-sdk-go/third_party/github.com/hyperledger/fabric/protos/ledger/rwset/kvrwset"

"fmt"
"net"

pb "github.com/hyperledger/fabric-sdk-go/third_party/github.com/hyperledger/fabric/protos/peer"
"google.golang.org/grpc"
)

// MockEndorserServer mock endoreser server to process endorsement proposals
Expand Down
6 changes: 3 additions & 3 deletions pkg/fab/mocks/mockorderer.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ SPDX-License-Identifier: Apache-2.0
package mocks

import (
"context"
"fmt"

"github.com/hyperledger/fabric-sdk-go/pkg/context/api/fab"
Expand Down Expand Up @@ -94,9 +95,8 @@ func (o *mockOrderer) SendBroadcast(envelope *fab.SignedEnvelope) (*common.Statu
}

// SendDeliver returns the channels for delivery of prepared mock values and errors (if any)
func (o *mockOrderer) SendDeliver(envelope *fab.SignedEnvelope) (chan *common.Block,
chan error) {
return o.Deliveries, o.DeliveryErrors
func (o *mockOrderer) SendDeliver(envelope *fab.SignedEnvelope) (chan *common.Block, chan error, context.CancelFunc) {
return o.Deliveries, o.DeliveryErrors, func() {}
}

func (o *mockOrderer) EnqueueSendBroadcastError(err error) {
Expand Down
60 changes: 27 additions & 33 deletions pkg/fab/orderer/deprecated_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,15 @@ import (

var kap keepalive.ClientParameters

func TestDeprecatedSendDeliver(t *testing.T) {
func TestDeprecatedSendDeliverHappy(t *testing.T) {
grpcServer := grpc.NewServer()
defer grpcServer.Stop()
mockServer, addr := startMockServer(t, grpcServer)
_, addr := startMockServer(t, grpcServer)

orderer, _ := NewOrderer("grpc://"+addr, "", "", mocks.NewMockConfig(), kap)
// Test deliver happy path
blocks, errs := orderer.SendDeliver(&fab.SignedEnvelope{})
blocks, errs, cancel := orderer.SendDeliver(&fab.SignedEnvelope{})
defer cancel()

select {
case block := <-blocks:
if string(block.Data.Data[0]) != "test" {
Expand All @@ -44,24 +45,21 @@ func TestDeprecatedSendDeliver(t *testing.T) {
case <-time.After(time.Second * 5):
t.Fatalf("Did not receive block or error from SendDeliver")
}
}

// Test deliver without valid envelope
blocks, errs = orderer.SendDeliver(nil)
select {
case block := <-blocks:
t.Fatalf("Expected error got block: %#v", block)
case err := <-errs:
if err == nil {
t.Fatalf("Expected error with nil envelope")
}
case <-time.After(time.Second * 5):
t.Fatalf("Did not receive block or error from SendDeliver")
}
func TestDeprecatedSendDeliverError(t *testing.T) {
grpcServer := grpc.NewServer()
defer grpcServer.Stop()
mockServer, addr := startMockServer(t, grpcServer)

orderer, _ := NewOrderer("grpc://"+addr, "", "", mocks.NewMockConfig(), kap)

// Test deliver with deliver error from OS
testError := errors.New("test error")
mockServer.DeliverError = testError
blocks, errs = orderer.SendDeliver(&fab.SignedEnvelope{})
blocks, errs, cancel := orderer.SendDeliver(&fab.SignedEnvelope{})
defer cancel()

select {
case block := <-blocks:
t.Fatalf("Expected error got block: %#v", block)
Expand All @@ -72,10 +70,14 @@ func TestDeprecatedSendDeliver(t *testing.T) {
case <-time.After(time.Second * 5):
t.Fatalf("Did not receive block or error from SendDeliver")
}
}

orderer, _ = NewOrderer(testOrdererURL+"invalid-test", "", "", mocks.NewMockConfig(), kap)
func TestDeprecatedSendDeliverBadURL(t *testing.T) {
orderer, _ := NewOrderer(testOrdererURL+"invalid-test", "", "", mocks.NewMockConfig(), kap)
// Test deliver happy path
blocks, errs = orderer.SendDeliver(&fab.SignedEnvelope{})
blocks, errs, cancel := orderer.SendDeliver(&fab.SignedEnvelope{})
defer cancel()

select {
case block := <-blocks:
t.Fatalf("This usecase was not supposed to receive blocks : %#v", block)
Expand All @@ -84,7 +86,6 @@ func TestDeprecatedSendDeliver(t *testing.T) {
case <-time.After(time.Second * 5):
t.Fatalf("Did not receive error from SendDeliver")
}

}

func TestDeprecatedNewOrdererWithTLS(t *testing.T) {
Expand Down Expand Up @@ -150,7 +151,8 @@ func TestDeprecatedSendDeliverServerBadResponse(t *testing.T) {
addr := startCustomizedMockServer(t, testOrdererURL, grpcServer, &broadcastServer)
orderer, _ := NewOrderer("grpc://"+addr, "", "", mocks.NewMockConfig(), kap)

blocks, errors := orderer.SendDeliver(&fab.SignedEnvelope{})
blocks, errors, cancel := orderer.SendDeliver(&fab.SignedEnvelope{})
defer cancel()

select {
case block := <-blocks:
Expand Down Expand Up @@ -180,7 +182,8 @@ func TestDeprecatedSendDeliverServerSuccessResponse(t *testing.T) {

orderer, _ := NewOrderer("grpc://"+addr, "", "", mocks.NewMockConfig(), kap)

blocks, errors := orderer.SendDeliver(&fab.SignedEnvelope{})
blocks, errors, cancel := orderer.SendDeliver(&fab.SignedEnvelope{})
defer cancel()

select {
case block := <-blocks:
Expand All @@ -205,7 +208,8 @@ func TestDeprecatedSendDeliverFailure(t *testing.T) {
addr := startCustomizedMockServer(t, testOrdererURL, grpcServer, &broadcastServer)
orderer, _ := NewOrderer("grpc://"+addr, "", "", mocks.NewMockConfig(), kap)

blocks, errors := orderer.SendDeliver(&fab.SignedEnvelope{})
blocks, errors, cancel := orderer.SendDeliver(&fab.SignedEnvelope{})
defer cancel()

select {
case block := <-blocks:
Expand Down Expand Up @@ -259,13 +263,3 @@ func TestDeprecatedSendBroadcastError(t *testing.T) {
}

}

func TestDeprecatedInterfaces(t *testing.T) {
var apiOrderer fab.Orderer
var orderer Orderer

apiOrderer = &orderer
if apiOrderer == nil {
t.Fatalf("this shouldn't happen.")
}
}
Loading

0 comments on commit 1a42cd9

Please sign in to comment.