-
Notifications
You must be signed in to change notification settings - Fork 8.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[FAB-2226] Move anchor peers to app org level
https://jira.hyperledger.org/browse/FAB-2226 The anchor peers are per application organization, so should be stored at the app org level. Change-Id: I930244c44c759d628531af2f71da3ced43b85631 Signed-off-by: Jason Yellick <jyellick@us.ibm.com>
- Loading branch information
Jason Yellick
committed
Feb 15, 2017
1 parent
e99311d
commit 7e0b4bf
Showing
13 changed files
with
322 additions
and
170 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,106 @@ | ||
/* | ||
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 application | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/hyperledger/fabric/common/configtx/handlers" | ||
mspconfig "github.com/hyperledger/fabric/common/configtx/handlers/msp" | ||
cb "github.com/hyperledger/fabric/protos/common" | ||
pb "github.com/hyperledger/fabric/protos/peer" | ||
|
||
"github.com/golang/protobuf/proto" | ||
logging "github.com/op/go-logging" | ||
) | ||
|
||
// Application org config keys | ||
const ( | ||
// AnchorPeersKey is the key name for the AnchorPeers ConfigValue | ||
AnchorPeersKey = "AnchorPeers" | ||
) | ||
|
||
type applicationOrgConfig struct { | ||
anchorPeers []*pb.AnchorPeer | ||
} | ||
|
||
// SharedConfigImpl is an implementation of Manager and configtx.ConfigHandler | ||
// In general, it should only be referenced as an Impl for the configtx.Manager | ||
type ApplicationOrgConfig struct { | ||
*handlers.OrgConfig | ||
pendingConfig *applicationOrgConfig | ||
config *applicationOrgConfig | ||
|
||
mspConfig *mspconfig.MSPConfigHandler | ||
} | ||
|
||
// NewSharedConfigImpl creates a new SharedConfigImpl with the given CryptoHelper | ||
func NewApplicationOrgConfig(id string, mspConfig *mspconfig.MSPConfigHandler) *ApplicationOrgConfig { | ||
return &ApplicationOrgConfig{ | ||
OrgConfig: handlers.NewOrgConfig(id, mspConfig), | ||
config: &applicationOrgConfig{}, | ||
} | ||
} | ||
|
||
// AnchorPeers returns the list of valid orderer addresses to connect to to invoke Broadcast/Deliver | ||
func (oc *ApplicationOrgConfig) AnchorPeers() []*pb.AnchorPeer { | ||
return oc.config.anchorPeers | ||
} | ||
|
||
// BeginConfig is used to start a new config proposal | ||
func (oc *ApplicationOrgConfig) 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 = &applicationOrgConfig{} | ||
} | ||
|
||
// RollbackConfig is used to abandon a new config proposal | ||
func (oc *ApplicationOrgConfig) RollbackConfig() { | ||
logger.Debugf("Rolling back proposed org config") | ||
oc.pendingConfig = nil | ||
} | ||
|
||
// CommitConfig is used to commit a new config proposal | ||
func (oc *ApplicationOrgConfig) 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 *ApplicationOrgConfig) ProposeConfig(key string, configValue *cb.ConfigValue) error { | ||
switch key { | ||
case AnchorPeersKey: | ||
anchorPeers := &pb.AnchorPeers{} | ||
if err := proto.Unmarshal(configValue.Value, anchorPeers); err != nil { | ||
return fmt.Errorf("Unmarshaling error for %s: %s", key, err) | ||
} | ||
if logger.IsEnabledFor(logging.DEBUG) { | ||
logger.Debugf("Setting %s to %v", key, anchorPeers.AnchorPeers) | ||
} | ||
oc.pendingConfig.anchorPeers = anchorPeers.AnchorPeers | ||
default: | ||
return oc.OrgConfig.ProposeConfig(key, configValue) | ||
} | ||
|
||
return nil | ||
} |
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,86 @@ | ||
/* | ||
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 application | ||
|
||
import ( | ||
"testing" | ||
|
||
configtxapi "github.com/hyperledger/fabric/common/configtx/api" | ||
cb "github.com/hyperledger/fabric/protos/common" | ||
pb "github.com/hyperledger/fabric/protos/peer" | ||
|
||
logging "github.com/op/go-logging" | ||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func init() { | ||
logging.SetLevel(logging.DEBUG, "") | ||
} | ||
|
||
func makeInvalidConfigValue() *cb.ConfigValue { | ||
return &cb.ConfigValue{ | ||
Value: []byte("Garbage Data"), | ||
} | ||
} | ||
|
||
func groupToKeyValue(configGroup *cb.ConfigGroup) (string, *cb.ConfigValue) { | ||
for _, group := range configGroup.Groups[GroupKey].Groups { | ||
for key, value := range group.Values { | ||
return key, value | ||
} | ||
} | ||
panic("No value encoded") | ||
} | ||
|
||
func TestApplicationOrgInterface(t *testing.T) { | ||
_ = configtxapi.SubInitializer(NewApplicationOrgConfig("id", nil)) | ||
} | ||
|
||
func TestApplicationOrgDoubleBegin(t *testing.T) { | ||
m := NewApplicationOrgConfig("id", nil) | ||
m.BeginConfig() | ||
assert.Panics(t, m.BeginConfig, "Two begins back to back should have caused a panic") | ||
} | ||
|
||
func TestApplicationOrgCommitWithoutBegin(t *testing.T) { | ||
m := NewApplicationOrgConfig("id", nil) | ||
assert.Panics(t, m.CommitConfig, "Committing without beginning should have caused a panic") | ||
} | ||
|
||
func TestApplicationOrgRollback(t *testing.T) { | ||
m := NewApplicationOrgConfig("id", nil) | ||
m.pendingConfig = &applicationOrgConfig{} | ||
m.RollbackConfig() | ||
assert.Nil(t, m.pendingConfig, "Should have cleared pending config on rollback") | ||
} | ||
|
||
func TestApplicationOrgAnchorPeers(t *testing.T) { | ||
endVal := []*pb.AnchorPeer{ | ||
&pb.AnchorPeer{Host: "foo", Port: 234, Cert: []byte("foocert")}, | ||
&pb.AnchorPeer{Host: "bar", Port: 237, Cert: []byte("barcert")}, | ||
} | ||
invalidMessage := makeInvalidConfigValue() | ||
validMessage := TemplateAnchorPeers("id", endVal) | ||
m := NewApplicationOrgConfig("id", nil) | ||
m.BeginConfig() | ||
|
||
assert.Error(t, m.ProposeConfig(AnchorPeersKey, invalidMessage), "Should have failed on invalid message") | ||
assert.NoError(t, m.ProposeConfig(groupToKeyValue(validMessage)), "Should not have failed on invalid message") | ||
m.CommitConfig() | ||
|
||
assert.Equal(t, m.AnchorPeers(), endVal, "Did not set updated anchor peers") | ||
} |
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
Oops, something went wrong.