Skip to content

Commit 5da931c

Browse files
author
conghonglei
committed
[FAB-2707] make cli find orderer from chain config
peer cli will query cscc to find chain config if orderer endpoint is not configed in cli. Change-Id: I11fa1275f4fc496484a1926f66479e93ab5444a6 Signed-off-by: conghonglei <[email protected]>
1 parent add0af3 commit 5da931c

File tree

4 files changed

+139
-4
lines changed

4 files changed

+139
-4
lines changed

examples/e2e_cli/scripts/script.sh

+8-4
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,9 @@ installChaincode () {
123123
instantiateChaincode () {
124124
PEER=$1
125125
setGlobals $PEER
126-
if [ -z "$CORE_PEER_TLS_ENABLED" -o "$CORE_PEER_TLS_ENABLED" = "false" ]; then
126+
# while 'peer chaincode' command can get the orderer endpoint from the peer (if join was successful),
127+
# lets supply it directly as we know it using the "-o" option
128+
if [ -z "$CORE_PEER_TLS_ENABLED" -o "$CORE_PEER_TLS_ENABLED" = "false" ]; then
127129
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
128130
else
129131
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
@@ -165,9 +167,11 @@ chaincodeQuery () {
165167
}
166168

167169
chaincodeInvoke () {
168-
PEER=$1
169-
setGlobals $PEER
170-
if [ -z "$CORE_PEER_TLS_ENABLED" -o "$CORE_PEER_TLS_ENABLED" = "false" ]; then
170+
PEER=$1
171+
setGlobals $PEER
172+
# while 'peer chaincode' command can get the orderer endpoint from the peer (if join was successful),
173+
# lets supply it directly as we know it using the "-o" option
174+
if [ -z "$CORE_PEER_TLS_ENABLED" -o "$CORE_PEER_TLS_ENABLED" = "false" ]; then
171175
peer chaincode invoke -o orderer.example.com:7050 -C $CHANNEL_NAME -n mycc -c '{"Args":["invoke","a","b","10"]}' >&log.txt
172176
else
173177
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

peer/chaincode/common.go

+12
Original file line numberDiff line numberDiff line change
@@ -232,6 +232,18 @@ func InitCmdFactory(isEndorserRequired, isOrdererRequired bool) (*ChaincodeCmdFa
232232

233233
var broadcastClient common.BroadcastClient
234234
if isOrdererRequired {
235+
if len(orderingEndpoint) == 0 {
236+
orderingEndpoints, err := common.GetOrdererEndpointOfChain(chainID, signer, endorserClient)
237+
if err != nil {
238+
return nil, fmt.Errorf("Error getting (%s) orderer endpoint: %s", chainID, err)
239+
}
240+
if len(orderingEndpoints) == 0 {
241+
return nil, fmt.Errorf("Error no orderer endpoint got for %s", chainID)
242+
}
243+
logger.Infof("Get chain(%s) orderer endpoint: %s", chainID, orderingEndpoints[0])
244+
orderingEndpoint = orderingEndpoints[0]
245+
}
246+
235247
broadcastClient, err = common.GetBroadcastClient(orderingEndpoint, tls, caFile)
236248

237249
if err != nil {

peer/chaincode/common_test.go

+50
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,14 @@ import (
2020
"encoding/json"
2121
"testing"
2222

23+
"github.com/hyperledger/fabric/bccsp/factory"
24+
genesisconfig "github.com/hyperledger/fabric/common/configtx/tool/localconfig"
25+
"github.com/hyperledger/fabric/common/configtx/tool/provisional"
26+
"github.com/hyperledger/fabric/peer/common"
2327
pb "github.com/hyperledger/fabric/protos/peer"
28+
"github.com/hyperledger/fabric/protos/utils"
2429
"github.com/spf13/cobra"
30+
"github.com/stretchr/testify/assert"
2531
"github.com/stretchr/testify/require"
2632
)
2733

@@ -123,3 +129,47 @@ func TestCheckInvalidJSON(t *testing.T) {
123129
return
124130
}
125131
}
132+
133+
func TestGetOrdererEndpointFromConfigTx(t *testing.T) {
134+
initMSP()
135+
136+
signer, err := common.GetDefaultSigner()
137+
assert.NoError(t, err)
138+
139+
mockchain := "mockchain"
140+
factory.InitFactories(nil)
141+
config := genesisconfig.Load(genesisconfig.SampleInsecureProfile)
142+
pgen := provisional.New(config)
143+
genesisBlock := pgen.GenesisBlockForChannel(mockchain)
144+
145+
mockResponse := &pb.ProposalResponse{
146+
Response: &pb.Response{Status: 200, Payload: utils.MarshalOrPanic(genesisBlock)},
147+
Endorsement: &pb.Endorsement{},
148+
}
149+
mockEndorserClient := common.GetMockEndorserClient(mockResponse, nil)
150+
151+
ordererEndpoints, err := common.GetOrdererEndpointOfChain(mockchain, signer, mockEndorserClient)
152+
assert.NoError(t, err, "GetOrdererEndpointOfChain from genesis block")
153+
154+
assert.Equal(t, len(ordererEndpoints), 1)
155+
assert.Equal(t, ordererEndpoints[0], "127.0.0.1:7050")
156+
}
157+
158+
func TestGetOrdererEndpointFail(t *testing.T) {
159+
initMSP()
160+
161+
signer, err := common.GetDefaultSigner()
162+
assert.NoError(t, err)
163+
164+
mockchain := "mockchain"
165+
factory.InitFactories(nil)
166+
167+
mockResponse := &pb.ProposalResponse{
168+
Response: &pb.Response{Status: 404, Payload: []byte{}},
169+
Endorsement: &pb.Endorsement{},
170+
}
171+
mockEndorserClient := common.GetMockEndorserClient(mockResponse, nil)
172+
173+
_, err = common.GetOrdererEndpointOfChain(mockchain, signer, mockEndorserClient)
174+
assert.Error(t, err, "GetOrdererEndpointOfChain from invalid response")
175+
}

peer/common/common.go

+69
Original file line numberDiff line numberDiff line change
@@ -20,16 +20,22 @@ import (
2020
"fmt"
2121

2222
"github.com/hyperledger/fabric/bccsp/factory"
23+
"github.com/hyperledger/fabric/common/configtx"
24+
configtxapi "github.com/hyperledger/fabric/common/configtx/api"
2325
"github.com/hyperledger/fabric/common/errors"
2426
"github.com/hyperledger/fabric/common/flogging"
2527
"github.com/hyperledger/fabric/common/viperutil"
2628
"github.com/hyperledger/fabric/core/config"
2729
"github.com/hyperledger/fabric/core/peer"
30+
"github.com/hyperledger/fabric/core/scc/cscc"
2831
"github.com/hyperledger/fabric/msp"
2932
mspmgmt "github.com/hyperledger/fabric/msp/mgmt"
33+
pcommon "github.com/hyperledger/fabric/protos/common"
3034
pb "github.com/hyperledger/fabric/protos/peer"
35+
putils "github.com/hyperledger/fabric/protos/utils"
3136
logging "github.com/op/go-logging"
3237
"github.com/spf13/viper"
38+
"golang.org/x/net/context"
3339
)
3440

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

120126
return signer, err
121127
}
128+
129+
// GetOrdererEndpointOfChain returns orderer endpoints of given chain
130+
func GetOrdererEndpointOfChain(chainID string, signer msp.SigningIdentity, endorserClient pb.EndorserClient) ([]string, error) {
131+
132+
// query cscc for chain config block
133+
invocation := &pb.ChaincodeInvocationSpec{
134+
ChaincodeSpec: &pb.ChaincodeSpec{
135+
Type: pb.ChaincodeSpec_Type(pb.ChaincodeSpec_Type_value["GOLANG"]),
136+
ChaincodeId: &pb.ChaincodeID{Name: "cscc"},
137+
Input: &pb.ChaincodeInput{Args: [][]byte{[]byte(cscc.GetConfigBlock), []byte(chainID)}},
138+
},
139+
}
140+
141+
creator, err := signer.Serialize()
142+
if err != nil {
143+
return nil, fmt.Errorf("Error serializing identity for %s: %s", signer.GetIdentifier(), err)
144+
}
145+
146+
prop, _, err := putils.CreateProposalFromCIS(pcommon.HeaderType_CONFIG, "", invocation, creator)
147+
if err != nil {
148+
return nil, fmt.Errorf("Error creating GetConfigBlock proposal: %s", err)
149+
}
150+
151+
signedProp, err := putils.GetSignedProposal(prop, signer)
152+
if err != nil {
153+
return nil, fmt.Errorf("Error creating signed GetConfigBlock proposal: %s", err)
154+
}
155+
156+
proposalResp, err := endorserClient.ProcessProposal(context.Background(), signedProp)
157+
if err != nil {
158+
return nil, fmt.Errorf("Error endorsing GetConfigBlock: %s", err)
159+
}
160+
161+
if proposalResp == nil {
162+
return nil, fmt.Errorf("Error nil proposal response: %s", err)
163+
}
164+
165+
if proposalResp.Response.Status != 0 && proposalResp.Response.Status != 200 {
166+
return nil, fmt.Errorf("Error bad proposal response %d", proposalResp.Response.Status)
167+
}
168+
169+
// parse config block
170+
block, err := putils.GetBlockFromBlockBytes(proposalResp.Response.Payload)
171+
if err != nil {
172+
return nil, fmt.Errorf("Error unmarshaling config block: %s", err)
173+
}
174+
175+
envelopeConfig, err := putils.ExtractEnvelope(block, 0)
176+
if err != nil {
177+
return nil, fmt.Errorf("Error extracting config block envelope: %s", err)
178+
}
179+
configtxInitializer := configtx.NewInitializer()
180+
configtxManager, err := configtx.NewManagerImpl(
181+
envelopeConfig,
182+
configtxInitializer,
183+
[]func(cm configtxapi.Manager){},
184+
)
185+
if err != nil {
186+
return nil, fmt.Errorf("Error loadding config block: %s", err)
187+
}
188+
189+
return configtxManager.ChannelConfig().OrdererAddresses(), nil
190+
}

0 commit comments

Comments
 (0)