Skip to content

Commit

Permalink
[FAB-1320] - MSP config handler
Browse files Browse the repository at this point in the history
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 <ale.linux@sopit.net>
  • Loading branch information
ale-linux committed Jan 16, 2017
1 parent 988b7f1 commit 2c2a6de
Show file tree
Hide file tree
Showing 8 changed files with 240 additions and 0 deletions.
16 changes: 16 additions & 0 deletions core/chaincode/ccproviderimpl.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,19 @@
/*
Copyright IBM Corp. 2017 All Rights Reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package chaincode

import (
Expand Down
16 changes: 16 additions & 0 deletions core/common/ccprovider/ccprovider.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,19 @@
/*
Copyright IBM Corp. 2017 All Rights Reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package ccprovider

import (
Expand Down
16 changes: 16 additions & 0 deletions core/mocks/ccprovider/ccprovider.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,19 @@
/*
Copyright IBM Corp. 2017 All Rights Reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package ccprovider

import (
Expand Down
16 changes: 16 additions & 0 deletions core/mocks/validator/validator.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,19 @@
/*
Copyright IBM Corp. 2017 All Rights Reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package validator

import "github.com/hyperledger/fabric/protos/common"
Expand Down
78 changes: 78 additions & 0 deletions core/peer/msp/config.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,25 @@
/*
Copyright IBM Corp. 2017 All Rights Reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package mspmgmt

import (
"fmt"

"github.com/golang/protobuf/proto"
"github.com/hyperledger/fabric/common/util"
"github.com/hyperledger/fabric/msp"
"github.com/hyperledger/fabric/protos/common"
Expand Down Expand Up @@ -60,3 +79,62 @@ func GetMSPManagerFromBlock(cid string, b *common.Block) (msp.MSPManager, error)

return mgr, nil
}

// MSPConfigHandler
type MSPConfigHandler struct {
config []*mspprotos.MSPConfig
proposedMgr msp.MSPManager
committedMgr msp.MSPManager
}

// BeginConfig called when a config proposal is begun
func (bh *MSPConfigHandler) BeginConfig() {
if bh.config != nil || bh.proposedMgr != nil {
panic("Programming error, called BeginConfig while a proposal was in process")
}
bh.config = make([]*mspprotos.MSPConfig, 0)
}

// RollbackConfig called when a config proposal is abandoned
func (bh *MSPConfigHandler) RollbackConfig() {
bh.config = nil
bh.proposedMgr = nil
}

// CommitConfig called when a config proposal is committed
func (bh *MSPConfigHandler) CommitConfig() {
if bh.config == nil {
panic("Programming error, called CommitConfig with no proposal in process")
}

bh.committedMgr = bh.proposedMgr
bh.config = nil
bh.proposedMgr = nil
}

// ProposeConfig called when config is added to a proposal
func (bh *MSPConfigHandler) ProposeConfig(configItem *common.ConfigurationItem) error {
// we expect MSP type of config items
if configItem.Key != msputils.MSPKey {
return fmt.Errorf("Expected config item key %s, got %s", msputils.MSPKey, configItem.Key)
}

mspconfig := &mspprotos.MSPConfig{}
err := proto.Unmarshal(configItem.Value, mspconfig)
if err != nil {
return fmt.Errorf("Error unmarshalling msp config item, err %s", err)
}

bh.config = append(bh.config, []*mspprotos.MSPConfig{mspconfig}...)
// the only way to make sure that I have a
// workable config is to toss the proposed
// manager, create a new one, call setup on
// it and return whatever error setup gives me
bh.proposedMgr = msp.NewMSPManager()
return bh.proposedMgr.Setup(bh.config)
}

// GetMSPManager returns the currently committed MSP manager
func (bh *MSPConfigHandler) GetMSPManager() msp.MSPManager {
return bh.committedMgr
}
16 changes: 16 additions & 0 deletions core/peer/msp/mgmt.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,19 @@
/*
Copyright IBM Corp. 2017 All Rights Reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package mspmgmt

import (
Expand Down
66 changes: 66 additions & 0 deletions core/peer/msp/mspconfigmgr_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
/*
Copyright IBM Corp. 2017 All Rights Reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package mspmgmt

import (
"testing"

"github.com/golang/protobuf/proto"
"github.com/hyperledger/fabric/common/configtx"
"github.com/hyperledger/fabric/msp"
"github.com/hyperledger/fabric/protos/common"
"github.com/hyperledger/fabric/protos/msp/utils"
"github.com/stretchr/testify/assert"
)

func TestMSPConfigManager(t *testing.T) {
conf, err := msp.GetLocalMspConfig("../../../msp/sampleconfig/")
assert.NoError(t, err)

confBytes, err := proto.Marshal(conf)
assert.NoError(t, err)

ci := &common.ConfigurationItem{Key: msputils.MSPKey, Value: confBytes}

// test success:

// begin/propose/commit
var mspCH configtx.Handler
mspCH = &MSPConfigHandler{}
mspCH.BeginConfig()
err = mspCH.ProposeConfig(ci)
assert.NoError(t, err)
mspCH.CommitConfig()

// get the manager we just created
mgr := mspCH.(*MSPConfigHandler).GetMSPManager()

msps, err := mgr.GetMSPs()
assert.NoError(t, err)

if msps == nil || len(msps) == 0 {
t.Fatalf("There are no MSPS in the manager")
}

// test failure
// begin/propose/commit
mspCH.BeginConfig()
err = mspCH.ProposeConfig(ci)
assert.NoError(t, err)
err = mspCH.ProposeConfig(&common.ConfigurationItem{Key: msputils.MSPKey, Value: []byte("BARF!")})
assert.Error(t, err)
}
16 changes: 16 additions & 0 deletions core/peer/msp/peermsp_test.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,19 @@
/*
Copyright IBM Corp. 2017 All Rights Reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package mspmgmt

import (
Expand Down

0 comments on commit 2c2a6de

Please sign in to comment.