Skip to content

Commit

Permalink
[FAB-2707] make cli find orderer from chain config
Browse files Browse the repository at this point in the history
peer cli will query cscc to find chain config if orderer endpoint is not
configed in cli.

Change-Id: I11fa1275f4fc496484a1926f66479e93ab5444a6
Signed-off-by: conghonglei <conghonglei@wanda.cn>
  • Loading branch information
conghonglei committed May 5, 2017
1 parent add0af3 commit 5da931c
Show file tree
Hide file tree
Showing 4 changed files with 139 additions and 4 deletions.
12 changes: 8 additions & 4 deletions examples/e2e_cli/scripts/script.sh
Expand Up @@ -123,7 +123,9 @@ installChaincode () {
instantiateChaincode () {
PEER=$1
setGlobals $PEER
if [ -z "$CORE_PEER_TLS_ENABLED" -o "$CORE_PEER_TLS_ENABLED" = "false" ]; then
# while 'peer chaincode' command can get the orderer endpoint from the peer (if join was successful),
# lets supply it directly as we know it using the "-o" option
if [ -z "$CORE_PEER_TLS_ENABLED" -o "$CORE_PEER_TLS_ENABLED" = "false" ]; then
peer chaincode instantiate -o orderer.example.com:7050 -C $CHANNEL_NAME -n mycc -v 1.0 -c '{"Args":["init","a","100","b","200"]}' -P "OR ('Org1MSP.member','Org2MSP.member')" >&log.txt
else
peer chaincode instantiate -o orderer.example.com:7050 --tls $CORE_PEER_TLS_ENABLED --cafile $ORDERER_CA -C $CHANNEL_NAME -n mycc -v 1.0 -c '{"Args":["init","a","100","b","200"]}' -P "OR ('Org1MSP.member','Org2MSP.member')" >&log.txt
Expand Down Expand Up @@ -165,9 +167,11 @@ chaincodeQuery () {
}

chaincodeInvoke () {
PEER=$1
setGlobals $PEER
if [ -z "$CORE_PEER_TLS_ENABLED" -o "$CORE_PEER_TLS_ENABLED" = "false" ]; then
PEER=$1
setGlobals $PEER
# while 'peer chaincode' command can get the orderer endpoint from the peer (if join was successful),
# lets supply it directly as we know it using the "-o" option
if [ -z "$CORE_PEER_TLS_ENABLED" -o "$CORE_PEER_TLS_ENABLED" = "false" ]; then
peer chaincode invoke -o orderer.example.com:7050 -C $CHANNEL_NAME -n mycc -c '{"Args":["invoke","a","b","10"]}' >&log.txt
else
peer chaincode invoke -o orderer.example.com:7050 --tls $CORE_PEER_TLS_ENABLED --cafile $ORDERER_CA -C $CHANNEL_NAME -n mycc -c '{"Args":["invoke","a","b","10"]}' >&log.txt
Expand Down
12 changes: 12 additions & 0 deletions peer/chaincode/common.go
Expand Up @@ -232,6 +232,18 @@ func InitCmdFactory(isEndorserRequired, isOrdererRequired bool) (*ChaincodeCmdFa

var broadcastClient common.BroadcastClient
if isOrdererRequired {
if len(orderingEndpoint) == 0 {
orderingEndpoints, err := common.GetOrdererEndpointOfChain(chainID, signer, endorserClient)
if err != nil {
return nil, fmt.Errorf("Error getting (%s) orderer endpoint: %s", chainID, err)
}
if len(orderingEndpoints) == 0 {
return nil, fmt.Errorf("Error no orderer endpoint got for %s", chainID)
}
logger.Infof("Get chain(%s) orderer endpoint: %s", chainID, orderingEndpoints[0])
orderingEndpoint = orderingEndpoints[0]
}

broadcastClient, err = common.GetBroadcastClient(orderingEndpoint, tls, caFile)

if err != nil {
Expand Down
50 changes: 50 additions & 0 deletions peer/chaincode/common_test.go
Expand Up @@ -20,8 +20,14 @@ import (
"encoding/json"
"testing"

"github.com/hyperledger/fabric/bccsp/factory"
genesisconfig "github.com/hyperledger/fabric/common/configtx/tool/localconfig"
"github.com/hyperledger/fabric/common/configtx/tool/provisional"
"github.com/hyperledger/fabric/peer/common"
pb "github.com/hyperledger/fabric/protos/peer"
"github.com/hyperledger/fabric/protos/utils"
"github.com/spf13/cobra"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

Expand Down Expand Up @@ -123,3 +129,47 @@ func TestCheckInvalidJSON(t *testing.T) {
return
}
}

func TestGetOrdererEndpointFromConfigTx(t *testing.T) {
initMSP()

signer, err := common.GetDefaultSigner()
assert.NoError(t, err)

mockchain := "mockchain"
factory.InitFactories(nil)
config := genesisconfig.Load(genesisconfig.SampleInsecureProfile)
pgen := provisional.New(config)
genesisBlock := pgen.GenesisBlockForChannel(mockchain)

mockResponse := &pb.ProposalResponse{
Response: &pb.Response{Status: 200, Payload: utils.MarshalOrPanic(genesisBlock)},
Endorsement: &pb.Endorsement{},
}
mockEndorserClient := common.GetMockEndorserClient(mockResponse, nil)

ordererEndpoints, err := common.GetOrdererEndpointOfChain(mockchain, signer, mockEndorserClient)
assert.NoError(t, err, "GetOrdererEndpointOfChain from genesis block")

assert.Equal(t, len(ordererEndpoints), 1)
assert.Equal(t, ordererEndpoints[0], "127.0.0.1:7050")
}

func TestGetOrdererEndpointFail(t *testing.T) {
initMSP()

signer, err := common.GetDefaultSigner()
assert.NoError(t, err)

mockchain := "mockchain"
factory.InitFactories(nil)

mockResponse := &pb.ProposalResponse{
Response: &pb.Response{Status: 404, Payload: []byte{}},
Endorsement: &pb.Endorsement{},
}
mockEndorserClient := common.GetMockEndorserClient(mockResponse, nil)

_, err = common.GetOrdererEndpointOfChain(mockchain, signer, mockEndorserClient)
assert.Error(t, err, "GetOrdererEndpointOfChain from invalid response")
}
69 changes: 69 additions & 0 deletions peer/common/common.go
Expand Up @@ -20,16 +20,22 @@ import (
"fmt"

"github.com/hyperledger/fabric/bccsp/factory"
"github.com/hyperledger/fabric/common/configtx"
configtxapi "github.com/hyperledger/fabric/common/configtx/api"
"github.com/hyperledger/fabric/common/errors"
"github.com/hyperledger/fabric/common/flogging"
"github.com/hyperledger/fabric/common/viperutil"
"github.com/hyperledger/fabric/core/config"
"github.com/hyperledger/fabric/core/peer"
"github.com/hyperledger/fabric/core/scc/cscc"
"github.com/hyperledger/fabric/msp"
mspmgmt "github.com/hyperledger/fabric/msp/mgmt"
pcommon "github.com/hyperledger/fabric/protos/common"
pb "github.com/hyperledger/fabric/protos/peer"
putils "github.com/hyperledger/fabric/protos/utils"
logging "github.com/op/go-logging"
"github.com/spf13/viper"
"golang.org/x/net/context"
)

// UndefinedParamValue defines what undefined parameters in the command line will initialise to
Expand Down Expand Up @@ -119,3 +125,66 @@ func GetDefaultSigner() (msp.SigningIdentity, error) {

return signer, err
}

// GetOrdererEndpointOfChain returns orderer endpoints of given chain
func GetOrdererEndpointOfChain(chainID string, signer msp.SigningIdentity, endorserClient pb.EndorserClient) ([]string, error) {

// query cscc for chain config block
invocation := &pb.ChaincodeInvocationSpec{
ChaincodeSpec: &pb.ChaincodeSpec{
Type: pb.ChaincodeSpec_Type(pb.ChaincodeSpec_Type_value["GOLANG"]),
ChaincodeId: &pb.ChaincodeID{Name: "cscc"},
Input: &pb.ChaincodeInput{Args: [][]byte{[]byte(cscc.GetConfigBlock), []byte(chainID)}},
},
}

creator, err := signer.Serialize()
if err != nil {
return nil, fmt.Errorf("Error serializing identity for %s: %s", signer.GetIdentifier(), err)
}

prop, _, err := putils.CreateProposalFromCIS(pcommon.HeaderType_CONFIG, "", invocation, creator)
if err != nil {
return nil, fmt.Errorf("Error creating GetConfigBlock proposal: %s", err)
}

signedProp, err := putils.GetSignedProposal(prop, signer)
if err != nil {
return nil, fmt.Errorf("Error creating signed GetConfigBlock proposal: %s", err)
}

proposalResp, err := endorserClient.ProcessProposal(context.Background(), signedProp)
if err != nil {
return nil, fmt.Errorf("Error endorsing GetConfigBlock: %s", err)
}

if proposalResp == nil {
return nil, fmt.Errorf("Error nil proposal response: %s", err)
}

if proposalResp.Response.Status != 0 && proposalResp.Response.Status != 200 {
return nil, fmt.Errorf("Error bad proposal response %d", proposalResp.Response.Status)
}

// parse config block
block, err := putils.GetBlockFromBlockBytes(proposalResp.Response.Payload)
if err != nil {
return nil, fmt.Errorf("Error unmarshaling config block: %s", err)
}

envelopeConfig, err := putils.ExtractEnvelope(block, 0)
if err != nil {
return nil, fmt.Errorf("Error extracting config block envelope: %s", err)
}
configtxInitializer := configtx.NewInitializer()
configtxManager, err := configtx.NewManagerImpl(
envelopeConfig,
configtxInitializer,
[]func(cm configtxapi.Manager){},
)
if err != nil {
return nil, fmt.Errorf("Error loadding config block: %s", err)
}

return configtxManager.ChannelConfig().OrdererAddresses(), nil
}

0 comments on commit 5da931c

Please sign in to comment.