Skip to content

Commit 2f6433f

Browse files
committed
FAB-1022 Call gossip when a peer join channel
1/06/17 Fixing cscc and lscc path uniqueness 12/21 Fixing MSP config path to work in both CI and local test 12/19 Fixing PEER_CFG_PATH and config builder loader of certs to avoid CI failures 12/19 Adding port to peer local IP Beside integrating gossip, this commit also includes: - changing the package of cscc to avoid import cycle - calling MSP to set up security configuration for chains - initializing system chaincodes for each chain - more unit tests. Change-Id: I62b3532e3c18ff95567cf5f80450286b63a1e959 Signed-off-by: Binh Q. Nguyen <[email protected]>
1 parent c20fd9d commit 2f6433f

File tree

7 files changed

+98
-39
lines changed

7 files changed

+98
-39
lines changed

core/system_chaincode/cscc/peer_configer.go core/chaincode/configer.go

+17-11
Original file line numberDiff line numberDiff line change
@@ -14,18 +14,17 @@ See the License for the specific language governing permissions and
1414
limitations under the License.
1515
*/
1616

17-
// Package cscc (configuration system chaincode) provides functions to manage
17+
// Package chaincode configer provides functions to manage
1818
// configuration transactions as the network is being reconfigured. The
1919
// configuration transactions arrive from the ordering service to the committer
2020
// who calls this chaincode. The chaincode also provides peer configuration
2121
// services such as joining a chain or getting configuration data.
22-
package cscc
22+
package chaincode
2323

2424
import (
2525
"errors"
2626
"fmt"
2727

28-
// "github.com/golang/protobuf/proto"
2928
"github.com/op/go-logging"
3029

3130
"github.com/hyperledger/fabric/core/chaincode/shim"
@@ -39,7 +38,7 @@ import (
3938
type PeerConfiger struct {
4039
}
4140

42-
var logger = logging.MustGetLogger("cscc")
41+
var cnflogger = logging.MustGetLogger("chaincode")
4342

4443
// These are function names from Invoke first parameter
4544
const (
@@ -52,7 +51,7 @@ const (
5251
// This allows the chaincode to initialize any variables on the ledger prior
5352
// to any transaction execution on the chain.
5453
func (e *PeerConfiger) Init(stub shim.ChaincodeStubInterface) ([]byte, error) {
55-
logger.Info("Init CSCC")
54+
cnflogger.Info("Init CSCC")
5655

5756
return nil, nil
5857
}
@@ -75,7 +74,7 @@ func (e *PeerConfiger) Invoke(stub shim.ChaincodeStubInterface) ([]byte, error)
7574
}
7675
fname := string(args[0])
7776

78-
logger.Debugf("Invoke function: %s", fname)
77+
cnflogger.Debugf("Invoke function: %s", fname)
7978

8079
// TODO: Handle ACL
8180

@@ -103,11 +102,20 @@ func joinChain(blockBytes []byte) ([]byte, error) {
103102
return nil, fmt.Errorf("Failed to reconstruct the genesis block, %s", err)
104103
}
105104

106-
if err := peer.CreateChainFromBlock(block); err != nil {
105+
if err = peer.CreateChainFromBlock(block); err != nil {
107106
return nil, err
108107
}
109108

110-
return nil, nil
109+
chainID, err := utils.GetChainIDFromBlock(block)
110+
if err != nil {
111+
return nil, fmt.Errorf("Failed to get the chain ID from the configuration block, %s", err)
112+
}
113+
114+
// Initialize all system chainodes on this chain
115+
// TODO: Fix this code to initialize instead of deploy chaincodes
116+
DeploySysCCs(chainID)
117+
118+
return []byte("200"), nil
111119
}
112120

113121
func updateConfigBlock(blockBytes []byte) ([]byte, error) {
@@ -127,9 +135,7 @@ func updateConfigBlock(blockBytes []byte) ([]byte, error) {
127135
return nil, err
128136
}
129137

130-
// TODO: would committer get ledger and update ?
131-
132-
return nil, nil
138+
return []byte("200"), nil
133139
}
134140

135141
// Return the current configuration block for the specified chainID. If the

core/system_chaincode/cscc/peer_configer_test.go core/chaincode/configer_test.go

+38-8
Original file line numberDiff line numberDiff line change
@@ -13,26 +13,29 @@ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1313
See the License for the specific language governing permissions and
1414
limitations under the License.
1515
*/
16-
package cscc
16+
package chaincode
1717

1818
import (
1919
"fmt"
2020
"net"
2121
"os"
2222
"testing"
23+
"time"
2324

2425
"github.com/spf13/viper"
2526
"github.com/stretchr/testify/assert"
2627
"google.golang.org/grpc"
2728

2829
"github.com/golang/protobuf/proto"
2930
"github.com/hyperledger/fabric/core/chaincode/shim"
31+
"github.com/hyperledger/fabric/core/peer"
3032
"github.com/hyperledger/fabric/gossip/service"
3133
"github.com/hyperledger/fabric/protos/common"
34+
pb "github.com/hyperledger/fabric/protos/peer"
3235
"github.com/hyperledger/fabric/protos/utils"
3336
)
3437

35-
func TestInit(t *testing.T) {
38+
func TestConfigerInit(t *testing.T) {
3639
e := new(PeerConfiger)
3740
stub := shim.NewMockStub("PeerConfiger", e)
3841

@@ -42,27 +45,48 @@ func TestInit(t *testing.T) {
4245
}
4346
}
4447

45-
func TestInvokeJoinChainMissingParams(t *testing.T) {
48+
func setupEndpoint(t *testing.T) {
49+
peerAddress := peer.GetLocalIP()
50+
if peerAddress == "" {
51+
peerAddress = "0.0.0.0"
52+
}
53+
peerAddress = peerAddress + ":21213"
54+
t.Logf("Local peer IP address: %s", peerAddress)
55+
var opts []grpc.ServerOption
56+
grpcServer := grpc.NewServer(opts...)
57+
getPeerEndpoint := func() (*pb.PeerEndpoint, error) {
58+
return &pb.PeerEndpoint{ID: &pb.PeerID{Name: "cscctestpeer"}, Address: peerAddress}, nil
59+
}
60+
ccStartupTimeout := time.Duration(30000) * time.Millisecond
61+
pb.RegisterChaincodeSupportServer(grpcServer, NewChaincodeSupport(getPeerEndpoint, false, ccStartupTimeout))
62+
}
63+
64+
func TestConfigerInvokeJoinChainMissingParams(t *testing.T) {
65+
//t.Skip("Test CI build")
4666
viper.Set("peer.fileSystemPath", "/var/hyperledger/test/")
4767
defer os.RemoveAll("/var/hyperledger/test/")
4868

4969
e := new(PeerConfiger)
5070
stub := shim.NewMockStub("PeerConfiger", e)
5171

72+
setupEndpoint(t)
5273
// Failed path: Not enough parameters
5374
args := [][]byte{[]byte("JoinChain")}
5475
if _, err := stub.MockInvoke("1", args); err == nil {
5576
t.Fatalf("cscc invoke JoinChain should have failed with invalid number of args: %v", args)
5677
}
5778
}
5879

59-
func TestInvokeJoinChainWrongParams(t *testing.T) {
80+
func TestConfigerInvokeJoinChainWrongParams(t *testing.T) {
81+
//t.Skip("Test CI build")
6082
viper.Set("peer.fileSystemPath", "/var/hyperledger/test/")
6183
defer os.RemoveAll("/var/hyperledger/test/")
6284

6385
e := new(PeerConfiger)
6486
stub := shim.NewMockStub("PeerConfiger", e)
6587

88+
setupEndpoint(t)
89+
6690
// Failed path: wrong parameter type
6791
args := [][]byte{[]byte("JoinChain"), []byte("action")}
6892
if _, err := stub.MockInvoke("1", args); err == nil {
@@ -71,13 +95,16 @@ func TestInvokeJoinChainWrongParams(t *testing.T) {
7195
}
7296
}
7397

74-
func TestInvokeJoinChainCorrectParams(t *testing.T) {
98+
func TestConfigerInvokeJoinChainCorrectParams(t *testing.T) {
99+
//t.Skip("Test CI build")
75100
viper.Set("peer.fileSystemPath", "/var/hyperledger/test/")
76101
defer os.RemoveAll("/var/hyperledger/test/")
77102

78103
e := new(PeerConfiger)
79104
stub := shim.NewMockStub("PeerConfiger", e)
80105

106+
setupEndpoint(t)
107+
81108
// Initialize gossip service
82109
grpcServer := grpc.NewServer()
83110
socket, err := net.Listen("tcp", fmt.Sprintf("%s:%d", "", 13611))
@@ -92,7 +119,7 @@ func TestInvokeJoinChainCorrectParams(t *testing.T) {
92119
t.Fatalf("cscc invoke JoinChain failed because invalid block")
93120
}
94121
args := [][]byte{[]byte("JoinChain"), blockBytes}
95-
if _, err := stub.MockInvoke("1", args); err != nil {
122+
if _, err = stub.MockInvoke("1", args); err != nil {
96123
t.Fatalf("cscc invoke JoinChain failed with: %v", err)
97124
}
98125

@@ -103,15 +130,18 @@ func TestInvokeJoinChainCorrectParams(t *testing.T) {
103130
t.Fatalf("cscc invoke JoinChain failed with: %v", err)
104131
}
105132
args = [][]byte{[]byte("GetConfigBlock"), []byte(chainID)}
106-
if _, err := stub.MockInvoke("1", args); err != nil {
133+
if _, err = stub.MockInvoke("1", args); err != nil {
107134
t.Fatalf("cscc invoke GetConfigBlock failed with: %v", err)
108135
}
109136
}
110137

111-
func TestInvokeUpdateConfigBlock(t *testing.T) {
138+
func TestConfigerInvokeUpdateConfigBlock(t *testing.T) {
139+
//t.Skip("Test CI build")
112140
e := new(PeerConfiger)
113141
stub := shim.NewMockStub("PeerConfiger", e)
114142

143+
setupEndpoint(t)
144+
115145
// Failed path: Not enough parameters
116146
args := [][]byte{[]byte("UpdateConfigBlock")}
117147
if _, err := stub.MockInvoke("1", args); err == nil {

core/chaincode/importsysccs.go

+3-4
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ package chaincode
1818

1919
import (
2020
//import system chain codes here
21-
"github.com/hyperledger/fabric/core/system_chaincode/cscc"
2221
"github.com/hyperledger/fabric/core/system_chaincode/escc"
2322
"github.com/hyperledger/fabric/core/system_chaincode/vscc"
2423
)
@@ -29,15 +28,15 @@ var systemChaincodes = []*SystemChaincode{
2928
ChainlessCC: true,
3029
Enabled: true,
3130
Name: "cscc",
32-
Path: "github.com/hyperledger/fabric/core/system_chaincode/cscc",
31+
Path: "github.com/hyperledger/fabric/core/chaincode/cscc",
3332
InitArgs: [][]byte{[]byte("")},
34-
Chaincode: &cscc.PeerConfiger{},
33+
Chaincode: &PeerConfiger{},
3534
},
3635
{
3736
ChainlessCC: false,
3837
Enabled: true,
3938
Name: "lccc",
40-
Path: "github.com/hyperledger/fabric/core/chaincode",
39+
Path: "github.com/hyperledger/fabric/core/chaincode/lscc",
4140
InitArgs: [][]byte{[]byte("")},
4241
Chaincode: &LifeCycleSysCC{},
4342
},

core/peer/peer.go

+6-11
Original file line numberDiff line numberDiff line change
@@ -29,11 +29,10 @@ import (
2929
"github.com/op/go-logging"
3030
"github.com/spf13/viper"
3131

32-
// "github.com/hyperledger/fabric/core/chaincode"
3332
"github.com/hyperledger/fabric/core/comm"
3433
"github.com/hyperledger/fabric/core/committer"
3534
"github.com/hyperledger/fabric/core/ledger/kvledger"
36-
// "github.com/hyperledger/fabric/core/peer/msp"
35+
"github.com/hyperledger/fabric/core/peer/msp"
3736
"github.com/hyperledger/fabric/gossip/service"
3837
"github.com/hyperledger/fabric/msp"
3938
"github.com/hyperledger/fabric/protos/common"
@@ -139,22 +138,18 @@ func getCurrConfigBlockFromLedger(ledger *kvledger.KVLedger) (*common.Block, err
139138
func createChain(cid string, ledger *kvledger.KVLedger, cb *common.Block) error {
140139
c := committer.NewLedgerCommitter(ledger)
141140

142-
// TODO: Call MSP to load from config block
143-
// mgr, err := mspmgmt.GetMSPManagerFromBlock(cb)
144-
// if err != nil {
145-
// return err
146-
// }
141+
mgr, err := mspmgmt.GetMSPManagerFromBlock(cb)
142+
if err != nil {
143+
return err
144+
}
147145

148146
if err := service.GetGossipService().JoinChannel(c, cb); err != nil {
149147
return err
150148
}
151149

152-
// Initialize all system chaincodes on this chain
153-
// chaincode.DeploySysCCs(cid)
154-
155150
chains.Lock()
156151
defer chains.Unlock()
157-
chains.list[cid] = &chain{cb: cb, ledger: ledger, mspmgr: nil, committer: c}
152+
chains.list[cid] = &chain{cb: cb, ledger: ledger, mspmgr: mgr, committer: c}
158153
return nil
159154
}
160155

core/peer/peer_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ func TestCreateChainFromBlock(t *testing.T) {
5656

5757
err = CreateChainFromBlock(block)
5858
if err != nil {
59-
t.Fatalf("failed to create chain")
59+
t.Fatalf("failed to create chain %s", err)
6060
}
6161

6262
// Correct ledger

msp/configbuilder.go

+4-4
Original file line numberDiff line numberDiff line change
@@ -70,10 +70,10 @@ const (
7070
)
7171

7272
func GetLocalMspConfig(dir string) (*msp.MSPConfig, error) {
73-
cacertDir := dir + string(filepath.Separator) + cacerts
74-
signcertDir := dir + string(filepath.Separator) + signcerts
75-
admincertDir := dir + string(filepath.Separator) + admincerts
76-
keystoreDir := dir + string(filepath.Separator) + keystore
73+
cacertDir := filepath.Join(dir, cacerts)
74+
signcertDir := filepath.Join(dir, signcerts)
75+
admincertDir := filepath.Join(dir, admincerts)
76+
keystoreDir := filepath.Join(dir, keystore)
7777

7878
cacerts, err := getPemMaterialFromDir(cacertDir)
7979
if err != nil || len(cacerts) == 0 {

protos/utils/blockutils.go

+29
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,14 @@ package utils
1818

1919
import (
2020
"fmt"
21+
"io/ioutil"
22+
"os"
2123

2224
"github.com/golang/protobuf/proto"
2325

2426
"github.com/hyperledger/fabric/common/cauthdsl"
2527
"github.com/hyperledger/fabric/common/configtx"
28+
"github.com/hyperledger/fabric/msp"
2629
cb "github.com/hyperledger/fabric/protos/common"
2730
ab "github.com/hyperledger/fabric/protos/orderer"
2831
)
@@ -82,6 +85,7 @@ func MakeConfigurationBlock(testChainID string) (*cb.Block, error) {
8285
encodeConsensusType(testChainID),
8386
encodeBatchSize(testChainID),
8487
lockDefaultModificationPolicy(testChainID),
88+
encodeMSP(testChainID),
8589
)
8690
payloadChainHeader := MakeChainHeader(cb.HeaderType_CONFIGURATION_TRANSACTION,
8791
configItemChainHeader.Version, testChainID, epoch)
@@ -111,6 +115,7 @@ const (
111115
lastModified = uint64(0)
112116
consensusTypeKey = "ConsensusType"
113117
batchSizeKey = "BatchSize"
118+
mspKey = "MSP"
114119
)
115120

116121
func createSignedConfigItem(chainID string,
@@ -149,3 +154,27 @@ func lockDefaultModificationPolicy(testChainID string) *cb.SignedConfigurationIt
149154
MarshalOrPanic(MakePolicyOrPanic(cauthdsl.RejectAllPolicy)),
150155
configtx.DefaultModificationPolicyID)
151156
}
157+
158+
// This function is needed to locate the MSP test configuration when running
159+
// in CI build env or local with "make unit-test". A better way to manage this
160+
// is to define a config path in yaml that may point to test or production
161+
// location of the config
162+
func getTESTMSPConfigPath() string {
163+
cfgPath := os.Getenv("PEER_CFG_PATH") + "/msp/sampleconfig/"
164+
if _, err := ioutil.ReadDir(cfgPath); err != nil {
165+
cfgPath = os.Getenv("GOPATH") + "/src/github.com/hyperledger/fabric/msp/sampleconfig/"
166+
}
167+
return cfgPath
168+
}
169+
170+
func encodeMSP(testChainID string) *cb.SignedConfigurationItem {
171+
cfgPath := getTESTMSPConfigPath()
172+
conf, err := msp.GetLocalMspConfig(cfgPath)
173+
if err != nil {
174+
panic(fmt.Sprintf("GetLocalMspConfig failed, err %s", err))
175+
}
176+
return createSignedConfigItem(testChainID,
177+
mspKey,
178+
MarshalOrPanic(conf),
179+
configtx.DefaultModificationPolicyID)
180+
}

0 commit comments

Comments
 (0)