Skip to content

Commit 2c2a6de

Browse files
committed
[FAB-1320] - MSP config handler
This change set introduces an implementation of the configtx.Handler that handles the configuration of MSP managers. Change-Id: I921132f89dd216ddcca32b13cee1862b634ba7ce Signed-off-by: Alessandro Sorniotti <[email protected]>
1 parent 988b7f1 commit 2c2a6de

File tree

8 files changed

+240
-0
lines changed

8 files changed

+240
-0
lines changed

core/chaincode/ccproviderimpl.go

+16
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,19 @@
1+
/*
2+
Copyright IBM Corp. 2017 All Rights Reserved.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
117
package chaincode
218

319
import (

core/common/ccprovider/ccprovider.go

+16
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,19 @@
1+
/*
2+
Copyright IBM Corp. 2017 All Rights Reserved.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
117
package ccprovider
218

319
import (

core/mocks/ccprovider/ccprovider.go

+16
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,19 @@
1+
/*
2+
Copyright IBM Corp. 2017 All Rights Reserved.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
117
package ccprovider
218

319
import (

core/mocks/validator/validator.go

+16
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,19 @@
1+
/*
2+
Copyright IBM Corp. 2017 All Rights Reserved.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
117
package validator
218

319
import "github.com/hyperledger/fabric/protos/common"

core/peer/msp/config.go

+78
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,25 @@
1+
/*
2+
Copyright IBM Corp. 2017 All Rights Reserved.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
117
package mspmgmt
218

319
import (
20+
"fmt"
21+
22+
"github.com/golang/protobuf/proto"
423
"github.com/hyperledger/fabric/common/util"
524
"github.com/hyperledger/fabric/msp"
625
"github.com/hyperledger/fabric/protos/common"
@@ -60,3 +79,62 @@ func GetMSPManagerFromBlock(cid string, b *common.Block) (msp.MSPManager, error)
6079

6180
return mgr, nil
6281
}
82+
83+
// MSPConfigHandler
84+
type MSPConfigHandler struct {
85+
config []*mspprotos.MSPConfig
86+
proposedMgr msp.MSPManager
87+
committedMgr msp.MSPManager
88+
}
89+
90+
// BeginConfig called when a config proposal is begun
91+
func (bh *MSPConfigHandler) BeginConfig() {
92+
if bh.config != nil || bh.proposedMgr != nil {
93+
panic("Programming error, called BeginConfig while a proposal was in process")
94+
}
95+
bh.config = make([]*mspprotos.MSPConfig, 0)
96+
}
97+
98+
// RollbackConfig called when a config proposal is abandoned
99+
func (bh *MSPConfigHandler) RollbackConfig() {
100+
bh.config = nil
101+
bh.proposedMgr = nil
102+
}
103+
104+
// CommitConfig called when a config proposal is committed
105+
func (bh *MSPConfigHandler) CommitConfig() {
106+
if bh.config == nil {
107+
panic("Programming error, called CommitConfig with no proposal in process")
108+
}
109+
110+
bh.committedMgr = bh.proposedMgr
111+
bh.config = nil
112+
bh.proposedMgr = nil
113+
}
114+
115+
// ProposeConfig called when config is added to a proposal
116+
func (bh *MSPConfigHandler) ProposeConfig(configItem *common.ConfigurationItem) error {
117+
// we expect MSP type of config items
118+
if configItem.Key != msputils.MSPKey {
119+
return fmt.Errorf("Expected config item key %s, got %s", msputils.MSPKey, configItem.Key)
120+
}
121+
122+
mspconfig := &mspprotos.MSPConfig{}
123+
err := proto.Unmarshal(configItem.Value, mspconfig)
124+
if err != nil {
125+
return fmt.Errorf("Error unmarshalling msp config item, err %s", err)
126+
}
127+
128+
bh.config = append(bh.config, []*mspprotos.MSPConfig{mspconfig}...)
129+
// the only way to make sure that I have a
130+
// workable config is to toss the proposed
131+
// manager, create a new one, call setup on
132+
// it and return whatever error setup gives me
133+
bh.proposedMgr = msp.NewMSPManager()
134+
return bh.proposedMgr.Setup(bh.config)
135+
}
136+
137+
// GetMSPManager returns the currently committed MSP manager
138+
func (bh *MSPConfigHandler) GetMSPManager() msp.MSPManager {
139+
return bh.committedMgr
140+
}

core/peer/msp/mgmt.go

+16
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,19 @@
1+
/*
2+
Copyright IBM Corp. 2017 All Rights Reserved.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
117
package mspmgmt
218

319
import (

core/peer/msp/mspconfigmgr_test.go

+66
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
/*
2+
Copyright IBM Corp. 2017 All Rights Reserved.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package mspmgmt
18+
19+
import (
20+
"testing"
21+
22+
"github.com/golang/protobuf/proto"
23+
"github.com/hyperledger/fabric/common/configtx"
24+
"github.com/hyperledger/fabric/msp"
25+
"github.com/hyperledger/fabric/protos/common"
26+
"github.com/hyperledger/fabric/protos/msp/utils"
27+
"github.com/stretchr/testify/assert"
28+
)
29+
30+
func TestMSPConfigManager(t *testing.T) {
31+
conf, err := msp.GetLocalMspConfig("../../../msp/sampleconfig/")
32+
assert.NoError(t, err)
33+
34+
confBytes, err := proto.Marshal(conf)
35+
assert.NoError(t, err)
36+
37+
ci := &common.ConfigurationItem{Key: msputils.MSPKey, Value: confBytes}
38+
39+
// test success:
40+
41+
// begin/propose/commit
42+
var mspCH configtx.Handler
43+
mspCH = &MSPConfigHandler{}
44+
mspCH.BeginConfig()
45+
err = mspCH.ProposeConfig(ci)
46+
assert.NoError(t, err)
47+
mspCH.CommitConfig()
48+
49+
// get the manager we just created
50+
mgr := mspCH.(*MSPConfigHandler).GetMSPManager()
51+
52+
msps, err := mgr.GetMSPs()
53+
assert.NoError(t, err)
54+
55+
if msps == nil || len(msps) == 0 {
56+
t.Fatalf("There are no MSPS in the manager")
57+
}
58+
59+
// test failure
60+
// begin/propose/commit
61+
mspCH.BeginConfig()
62+
err = mspCH.ProposeConfig(ci)
63+
assert.NoError(t, err)
64+
err = mspCH.ProposeConfig(&common.ConfigurationItem{Key: msputils.MSPKey, Value: []byte("BARF!")})
65+
assert.Error(t, err)
66+
}

core/peer/msp/peermsp_test.go

+16
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,19 @@
1+
/*
2+
Copyright IBM Corp. 2017 All Rights Reserved.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
117
package mspmgmt
218

319
import (

0 commit comments

Comments
 (0)