Skip to content

Commit

Permalink
[FAB-1938] add orderer endpoint param to peer cli
Browse files Browse the repository at this point in the history
Currently peer cli receives ordering service endpoint via
environmental variable CORE_PEER_COMMITTER_LEDGER_ORDERER.
Because ordering service endpoint will be provided with
configuration transaction in the ledger block the env.
variable will be depricate. This commit is 1 out of 3, takes
care to switch peer cli to use new command line parameter,
for example:

peer channel create -o ordering_host:port ....

Change-Id: I82fcb4f425d80489cb815b706b2d2d01b5e26827
Signed-off-by: Artem Barger <bartem@il.ibm.com>
  • Loading branch information
C0rWin committed Mar 1, 2017
1 parent be91ccc commit 7144508
Show file tree
Hide file tree
Showing 7 changed files with 37 additions and 34 deletions.
4 changes: 2 additions & 2 deletions examples/ccchecker/ccchecker.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,9 +90,9 @@ func CCCheckerInit() {
}

//CCCheckerRun main loops that will run the tests and cleanup
func CCCheckerRun(report bool, verbose bool) error {
func CCCheckerRun(orderingEndpoint string, report bool, verbose bool) error {
//connect with Broadcast client
bc, err := common.GetBroadcastClient()
bc, err := common.GetBroadcastClient(orderingEndpoint)
if err != nil {
return err
}
Expand Down
6 changes: 4 additions & 2 deletions examples/ccchecker/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,11 @@ var mainCmd = &cobra.Command{
},
}

var orderingEndpoint string

func main() {
mainFlags := mainCmd.PersistentFlags()

mainFlags.StringVarP(&orderingEndpoint, "orderer", "o", "", "Ordering service endpoint")
//initialize the env
InitCCCheckerEnv(mainFlags)

Expand All @@ -56,7 +58,7 @@ func main() {
func run(args []string) {
CCCheckerInit()
//TODO make parameters out of report and verbose
CCCheckerRun(true, true)
CCCheckerRun(orderingEndpoint, true, true)
fmt.Printf("Test complete\n")
return
}
2 changes: 2 additions & 0 deletions peer/chaincode/chaincode.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ func AddFlags(cmd *cobra.Command) {
fmt.Sprint("The name of the endorsement system chaincode to be used for this chaincode"))
flags.StringVarP(&vscc, "vscc", "V", common.UndefinedParamValue,
fmt.Sprint("The name of the verification system chaincode to be used for this chaincode"))
flags.StringVarP(&orderingEndpoint, "orderer", "o", "", "Ordering service endpoint")
}

// Cmd returns the cobra command for Chaincode
Expand Down Expand Up @@ -88,6 +89,7 @@ var (
escc string
vscc string
policyMarhsalled []byte
orderingEndpoint string
)

var chaincodeCmd = &cobra.Command{
Expand Down
2 changes: 1 addition & 1 deletion peer/chaincode/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -229,7 +229,7 @@ func InitCmdFactory() (*ChaincodeCmdFactory, error) {
return nil, fmt.Errorf("Error getting default signer: %s", err)
}

broadcastClient, err := common.GetBroadcastClient()
broadcastClient, err := common.GetBroadcastClient(orderingEndpoint)
if err != nil {
return nil, fmt.Errorf("Error getting broadcast client: %s", err)
}
Expand Down
23 changes: 12 additions & 11 deletions peer/channel/channel.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,14 @@ package channel

import (
"fmt"
"strings"

"github.com/hyperledger/fabric/msp"
"github.com/hyperledger/fabric/peer/common"
ab "github.com/hyperledger/fabric/protos/orderer"
pb "github.com/hyperledger/fabric/protos/peer"
"github.com/op/go-logging"
"github.com/spf13/cobra"
"github.com/spf13/viper"
"golang.org/x/net/context"
"google.golang.org/grpc"
)
Expand All @@ -39,17 +39,13 @@ var (
genesisBlockPath string

// create related variables
chainID string
channelTxFile string
chainID string
channelTxFile string
orderingEndpoint string
)

// Cmd returns the cobra command for Node
func Cmd(cf *ChannelCmdFactory) *cobra.Command {
//the "peer.committer.enabled" flag should really go away...
//basically we need the orderer for create and join
if !viper.GetBool("peer.committer.enabled") || viper.GetString("peer.committer.ledger.orderer") == "" {
panic("orderer not provided")
}

AddFlags(channelCmd)
channelCmd.AddCommand(joinCmd(cf))
Expand All @@ -66,6 +62,7 @@ func AddFlags(cmd *cobra.Command) {
flags.StringVarP(&genesisBlockPath, "blockpath", "b", common.UndefinedParamValue, "Path to file containing genesis block")
flags.StringVarP(&chainID, "chain", "c", common.UndefinedParamValue, "In case of a newChain command, the chain ID to create.")
flags.StringVarP(&channelTxFile, "file", "f", "", "Configuration transaction file generated by a tool such as configtxgen for submitting to orderer")
flags.StringVarP(&orderingEndpoint, "orderer", "o", "", "Ordering service endpoint")
}

var channelCmd = &cobra.Command{
Expand Down Expand Up @@ -97,7 +94,7 @@ func InitCmdFactory(isOrdererRequired bool) (*ChannelCmdFactory, error) {
}

cmdFact.BroadcastFactory = func() (common.BroadcastClient, error) {
return common.GetBroadcastClient()
return common.GetBroadcastClient(orderingEndpoint)
}

if err != nil {
Expand All @@ -111,8 +108,12 @@ func InitCmdFactory(isOrdererRequired bool) (*ChannelCmdFactory, error) {
return nil, fmt.Errorf("Error getting endorser client %s: %s", channelFuncName, err)
}
} else {
orderer := viper.GetString("peer.committer.ledger.orderer")
conn, err := grpc.Dial(orderer, grpc.WithInsecure())

if len(strings.Split(orderingEndpoint, ":")) != 2 {
return nil, fmt.Errorf("Ordering service endpoint %s is not valid or missing", orderingEndpoint)
}

conn, err := grpc.Dial(orderingEndpoint, grpc.WithInsecure())
if err != nil {
return nil, err
}
Expand Down
16 changes: 9 additions & 7 deletions peer/channel/create_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ import (

"github.com/golang/protobuf/proto"

"errors"

"github.com/hyperledger/fabric/msp/mgmt/testtools"
"github.com/hyperledger/fabric/peer/common"
cb "github.com/hyperledger/fabric/protos/common"
Expand Down Expand Up @@ -103,7 +105,7 @@ func TestCreateChain(t *testing.T) {

AddFlags(cmd)

args := []string{"-c", mockchain}
args := []string{"-c", mockchain, "-o", "localhost:7050"}
cmd.SetArgs(args)

if err := cmd.Execute(); err != nil {
Expand Down Expand Up @@ -134,7 +136,7 @@ func TestCreateChainWithDefaultAnchorPeers(t *testing.T) {

AddFlags(cmd)

args := []string{"-c", mockchain}
args := []string{"-c", mockchain, "-o", "localhost:7050"}
cmd.SetArgs(args)

if err := cmd.Execute(); err != nil {
Expand All @@ -155,7 +157,7 @@ func TestCreateChainBCFail(t *testing.T) {
t.Fatalf("Get default signer error: %v", err)
}

sendErr := fmt.Errorf("send create tx failed")
sendErr := errors.New("send create tx failed")

mockCF := &ChannelCmdFactory{
BroadcastFactory: func() (common.BroadcastClient, error) {
Expand All @@ -169,7 +171,7 @@ func TestCreateChainBCFail(t *testing.T) {

AddFlags(cmd)

args := []string{"-c", mockchain}
args := []string{"-c", mockchain, "-o", "localhost:7050"}
cmd.SetArgs(args)

expectedErrMsg := sendErr.Error()
Expand Down Expand Up @@ -206,7 +208,7 @@ func TestCreateChainDeliverFail(t *testing.T) {

AddFlags(cmd)

args := []string{"-c", mockchain}
args := []string{"-c", mockchain, "-o", "localhost:7050"}
cmd.SetArgs(args)

expectedErrMsg := recvErr.Error()
Expand Down Expand Up @@ -277,7 +279,7 @@ func TestCreateChainFromTx(t *testing.T) {

AddFlags(cmd)

args := []string{"-c", mockchannel, "-f", file}
args := []string{"-c", mockchannel, "-f", file, "-o", "localhost:7050"}
cmd.SetArgs(args)

if _, err = createTxFile(file, cb.HeaderType_CONFIG_UPDATE, mockchannel); err != nil {
Expand Down Expand Up @@ -321,7 +323,7 @@ func TestCreateChainInvalidTx(t *testing.T) {

AddFlags(cmd)

args := []string{"-c", mockchannel, "-f", file}
args := []string{"-c", mockchannel, "-f", file, "-o", "localhost:7050"}
cmd.SetArgs(args)

//bad type CONFIG
Expand Down
18 changes: 7 additions & 11 deletions peer/common/ordererclient.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,11 @@ package common

import (
"fmt"
"strings"
"time"

cb "github.com/hyperledger/fabric/protos/common"
ab "github.com/hyperledger/fabric/protos/orderer"
"github.com/spf13/viper"
"golang.org/x/net/context"
"google.golang.org/grpc"
)
Expand All @@ -39,29 +39,25 @@ type broadcastClient struct {
}

// GetBroadcastClient creates a simple instance of the BroadcastClient interface
func GetBroadcastClient() (BroadcastClient, error) {
var orderer string
if viper.GetBool("peer.committer.enabled") {
orderer = viper.GetString("peer.committer.ledger.orderer")
}
func GetBroadcastClient(orderingEndpoint string) (BroadcastClient, error) {

if orderer == "" {
return nil, fmt.Errorf("Can't get orderer address")
if len(strings.Split(orderingEndpoint, ":")) != 2 {
return nil, fmt.Errorf("Ordering service endpoint %s is not valid or missing", orderingEndpoint)
}

var opts []grpc.DialOption
opts = append(opts, grpc.WithInsecure())
opts = append(opts, grpc.WithTimeout(3*time.Second))
opts = append(opts, grpc.WithBlock())

conn, err := grpc.Dial(orderer, opts...)
conn, err := grpc.Dial(orderingEndpoint, opts...)
if err != nil {
return nil, fmt.Errorf("Error connecting to %s due to %s", orderer, err)
return nil, fmt.Errorf("Error connecting to %s due to %s", orderingEndpoint, err)
}
client, err := ab.NewAtomicBroadcastClient(conn).Broadcast(context.TODO())
if err != nil {
conn.Close()
return nil, fmt.Errorf("Error connecting to %s due to %s", orderer, err)
return nil, fmt.Errorf("Error connecting to %s due to %s", orderingEndpoint, err)
}

return &broadcastClient{conn: conn, client: client}, nil
Expand Down

0 comments on commit 7144508

Please sign in to comment.