-
Notifications
You must be signed in to change notification settings - Fork 8.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[FAB-2225] Create organization config handler
https://jira.hyperledger.org/browse/FAB-2225 In order to support organization scoped config (ie AnchorPeers), it's necessary to introduce the notion of organization config. This CR only introduces the notion of the organization handler. Change-Id: I4637f114f36a458fcba82b01b7756d3e4d64da20 Signed-off-by: Jason Yellick <jyellick@us.ibm.com>
- Loading branch information
Jason Yellick
committed
Feb 15, 2017
1 parent
02322a1
commit e99311d
Showing
6 changed files
with
203 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,105 @@ | ||
/* | ||
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 handlers | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/hyperledger/fabric/common/configtx/api" | ||
mspconfig "github.com/hyperledger/fabric/common/configtx/handlers/msp" | ||
"github.com/hyperledger/fabric/msp" | ||
cb "github.com/hyperledger/fabric/protos/common" | ||
|
||
"github.com/op/go-logging" | ||
) | ||
|
||
// Org config keys | ||
const ( | ||
// MSPKey is value key for marshaled *mspconfig.MSPConfig | ||
MSPKey = "MSP" | ||
) | ||
|
||
var logger = logging.MustGetLogger("common/configtx/handlers") | ||
|
||
type orgConfig struct { | ||
msp msp.MSP | ||
} | ||
|
||
// SharedConfigImpl is an implementation of Manager and configtx.ConfigHandler | ||
// In general, it should only be referenced as an Impl for the configtx.Manager | ||
type OrgConfig struct { | ||
id string | ||
pendingConfig *orgConfig | ||
config *orgConfig | ||
|
||
mspConfig *mspconfig.MSPConfigHandler | ||
} | ||
|
||
// NewSharedConfigImpl creates a new SharedConfigImpl with the given CryptoHelper | ||
func NewOrgConfig(id string, mspConfig *mspconfig.MSPConfigHandler) *OrgConfig { | ||
return &OrgConfig{ | ||
id: id, | ||
config: &orgConfig{}, | ||
mspConfig: mspConfig, | ||
} | ||
} | ||
|
||
// BeginConfig is used to start a new config proposal | ||
func (oc *OrgConfig) BeginConfig() { | ||
logger.Debugf("Beginning a possible new org config") | ||
if oc.pendingConfig != nil { | ||
logger.Panicf("Programming error, cannot call begin in the middle of a proposal") | ||
} | ||
oc.pendingConfig = &orgConfig{} | ||
} | ||
|
||
// RollbackConfig is used to abandon a new config proposal | ||
func (oc *OrgConfig) RollbackConfig() { | ||
logger.Debugf("Rolling back proposed org config") | ||
oc.pendingConfig = nil | ||
} | ||
|
||
// CommitConfig is used to commit a new config proposal | ||
func (oc *OrgConfig) CommitConfig() { | ||
logger.Debugf("Committing new org config") | ||
if oc.pendingConfig == nil { | ||
logger.Panicf("Programming error, cannot call commit without an existing proposal") | ||
} | ||
oc.config = oc.pendingConfig | ||
oc.pendingConfig = nil | ||
} | ||
|
||
// ProposeConfig is used to add new config to the config proposal | ||
func (oc *OrgConfig) ProposeConfig(key string, configValue *cb.ConfigValue) error { | ||
switch key { | ||
case MSPKey: | ||
logger.Debugf("Initializing org MSP for id %s", oc.id) | ||
return oc.mspConfig.ProposeConfig(key, configValue) | ||
default: | ||
logger.Warningf("Uknown org config item with key %s", key) | ||
} | ||
return nil | ||
} | ||
|
||
// Handler returns the associated api.Handler for the given path | ||
func (oc *OrgConfig) Handler(path []string) (api.Handler, error) { | ||
if len(path) == 0 { | ||
return oc, nil | ||
} | ||
|
||
return nil, fmt.Errorf("Organizations do not further nesting") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
/* | ||
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 handlers | ||
|
||
import ( | ||
"testing" | ||
|
||
configtxapi "github.com/hyperledger/fabric/common/configtx/api" | ||
|
||
logging "github.com/op/go-logging" | ||
) | ||
|
||
func init() { | ||
logging.SetLevel(logging.DEBUG, "") | ||
} | ||
|
||
func TestInterface(t *testing.T) { | ||
_ = configtxapi.SubInitializer(NewOrgConfig("id", nil)) | ||
} | ||
|
||
func TestDoubleBegin(t *testing.T) { | ||
defer func() { | ||
if err := recover(); err == nil { | ||
t.Fatalf("Should have panicked on multiple begin configs") | ||
} | ||
}() | ||
|
||
m := NewOrgConfig("id", nil) | ||
m.BeginConfig() | ||
m.BeginConfig() | ||
} | ||
|
||
func TestCommitWithoutBegin(t *testing.T) { | ||
defer func() { | ||
if err := recover(); err == nil { | ||
t.Fatalf("Should have panicked on multiple begin configs") | ||
} | ||
}() | ||
|
||
m := NewOrgConfig("id", nil) | ||
m.CommitConfig() | ||
} | ||
|
||
func TestRollback(t *testing.T) { | ||
m := NewOrgConfig("id", nil) | ||
m.pendingConfig = &orgConfig{} | ||
m.RollbackConfig() | ||
if m.pendingConfig != nil { | ||
t.Fatalf("Should have cleared pending config on rollback") | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters