Skip to content

Commit

Permalink
[FAB-17582] Use custom API type for FabricMSPConfig proto
Browse files Browse the repository at this point in the history
- Custom API type is more consumer friendly. It accepts x509
certificates and ECDSA keys instead of byte slices and handles PEM
encoding them to byte slices for the FabricMSPConfig proto.
- General cleanup for application channel creation.
- Cleanup for AddOrgToConsortium
- Cleanup GetAnchorPeers logic/tests

Signed-off-by: Danny Cao <dcao@us.ibm.com>
Signed-off-by: Tiffany Harris <tiffany.harris@ibm.com>
  • Loading branch information
stephyee authored and sykesm committed Mar 10, 2020
1 parent f410279 commit 7315556
Show file tree
Hide file tree
Showing 8 changed files with 823 additions and 393 deletions.
6 changes: 3 additions & 3 deletions pkg/config/application.go
Original file line number Diff line number Diff line change
Expand Up @@ -143,16 +143,16 @@ func RemoveAnchorPeer(config *cb.Config, orgName string, anchorPeerToRemove *Anc
return fmt.Errorf("could not find anchor peer %s:%d in %s's anchor peer endpoints", anchorPeerToRemove.Host, anchorPeerToRemove.Port, orgName)
}

// GetAnchorPeer retrieves existing anchor peers from a application organization.
func GetAnchorPeer(config *cb.Config, orgName string) ([]*AnchorPeer, error) {
// GetAnchorPeers retrieves existing anchor peers from a application organization.
func GetAnchorPeers(config *cb.Config, orgName string) ([]*AnchorPeer, error) {
applicationOrgGroup, ok := config.ChannelGroup.Groups[ApplicationGroupKey].Groups[orgName]
if !ok {
return nil, fmt.Errorf("application org %s does not exist in channel config", orgName)
}

anchorPeerConfigValue, ok := applicationOrgGroup.Values[AnchorPeersKey]
if !ok {
return nil, fmt.Errorf("application org %s does not have anchor peer", orgName)
return nil, fmt.Errorf("application org %s does not have anchor peers", orgName)
}

anchorPeersProto := &pb.AnchorPeers{}
Expand Down
53 changes: 21 additions & 32 deletions pkg/config/application_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import (

"github.com/golang/protobuf/proto"
cb "github.com/hyperledger/fabric-protos-go/common"
mb "github.com/hyperledger/fabric-protos-go/msp"
pb "github.com/hyperledger/fabric-protos-go/peer"
"github.com/hyperledger/fabric/common/tools/protolator"
. "github.com/onsi/gomega"
Expand Down Expand Up @@ -549,43 +548,45 @@ func TestRemoveAnchorPeerFailure(t *testing.T) {

func TestGetAnchorPeer(t *testing.T) {
t.Parallel()

gt := NewGomegaWithT(t)

channelGroup := newConfigGroup()
applicationGroup := newConfigGroup()

application := baseApplication()
for _, org := range application.Organizations {
orgGroup, err := newOrgConfigGroup(org)
gt.Expect(err).NotTo(HaveOccurred())
applicationGroup.Groups[org.Name] = orgGroup
}
applicationGroup, err := newApplicationGroup(baseApplication())
gt.Expect(err).NotTo(HaveOccurred())

channelGroup.Groups[ApplicationGroupKey] = applicationGroup
config := &cb.Config{
ChannelGroup: channelGroup,
}

expectedAnchorPeer := &AnchorPeer{Host: "host1", Port: 123}

anchorPeers, err := GetAnchorPeer(config, "Org1")
err = AddAnchorPeer(config, "Org1", expectedAnchorPeer)
gt.Expect(err).NotTo(HaveOccurred())

anchorPeers, err := GetAnchorPeers(config, "Org1")
gt.Expect(err).NotTo(HaveOccurred())
gt.Expect(len(anchorPeers)).To(Equal(1))
gt.Expect(anchorPeers[0]).To(Equal(expectedAnchorPeer))

}

func TestGetAnchorPeerFailures(t *testing.T) {
t.Parallel()

gt := NewGomegaWithT(t)

channelGroup := newConfigGroup()
applicationGroup := newConfigGroup()

applicationGroup, err := newApplicationGroup(baseApplication())
gt.Expect(err).NotTo(HaveOccurred())

orgNoAnchor := &Organization{
Name: "Org1",
ID: "Org1MSP",
Policies: applicationOrgStandardPolicies(),
MSPConfig: &mb.FabricMSPConfig{},
Name: "Org1",
ID: "Org1MSP",
Policies: applicationOrgStandardPolicies(),
MSP: baseMSP(),
}
orgGroup, err := newOrgConfigGroup(orgNoAnchor)
gt.Expect(err).NotTo(HaveOccurred())
Expand All @@ -609,17 +610,17 @@ func TestGetAnchorPeerFailures(t *testing.T) {
expectedErr: "application org bad-org does not exist in channel config",
},
{
name: "When org does not have an anchor peer",
name: "When org config group does not have an anchor peers value",
config: config,
orgName: "Org1",
expectedErr: "application org Org1 does not have anchor peer",
expectedErr: "application org Org1 does not have anchor peers",
},
} {
test := test
t.Run(test.name, func(t *testing.T) {
t.Parallel()
gt := NewGomegaWithT(t)
_, err := GetAnchorPeer(test.config, test.orgName)
_, err := GetAnchorPeers(test.config, test.orgName)
gt.Expect(err).To(MatchError(test.expectedErr))
})
}
Expand All @@ -630,22 +631,10 @@ func baseApplication() *Application {
Policies: standardPolicies(),
Organizations: []*Organization{
{
Name: "Org1",
ID: "Org1MSP",
Policies: applicationOrgStandardPolicies(),
AnchorPeers: []*AnchorPeer{
{Host: "host1", Port: 123},
},
MSPConfig: &mb.FabricMSPConfig{},
Name: "Org1",
},
{
Name: "Org2",
ID: "Org2MSP",
Policies: applicationOrgStandardPolicies(),
AnchorPeers: []*AnchorPeer{
{Host: "host2", Port: 123},
},
MSPConfig: &mb.FabricMSPConfig{},
Name: "Org2",
},
},
Capabilities: map[string]bool{
Expand Down
19 changes: 12 additions & 7 deletions pkg/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,10 @@ type Policy struct {

// Organization is an organization in the channel configuration.
type Organization struct {
Name string
ID string
Policies map[string]*Policy
MSPConfig *mb.FabricMSPConfig
Name string
ID string
Policies map[string]*Policy
MSP *MSP

AnchorPeers []*AnchorPeer
OrdererEndpoints []string
Expand Down Expand Up @@ -230,7 +230,12 @@ func newOrgConfigGroup(org *Organization) (*cb.ConfigGroup, error) {
return nil, err
}

conf, err := proto.Marshal(org.MSPConfig)
fabricMSPConfig, err := org.MSP.toProto()
if err != nil {
return nil, fmt.Errorf("converting fabric msp config to proto: %v", err)
}

conf, err := proto.Marshal(fabricMSPConfig)
if err != nil {
return nil, fmt.Errorf("marshalling msp config: %v", err)
}
Expand All @@ -247,7 +252,7 @@ func newOrgConfigGroup(org *Organization) (*cb.ConfigGroup, error) {

// OrdererEndpoints are orderer org specific and are only added when specified for orderer orgs
if len(org.OrdererEndpoints) > 0 {
err = addValue(orgGroup, endpointsValue(org.OrdererEndpoints), AdminsPolicyKey)
err := addValue(orgGroup, endpointsValue(org.OrdererEndpoints), AdminsPolicyKey)
if err != nil {
return nil, err
}
Expand All @@ -266,7 +271,7 @@ func newOrgConfigGroup(org *Organization) (*cb.ConfigGroup, error) {
// This helps prevent a delta from the orderer system channel when computing
// more complex channel creation transactions
if len(anchorProtos) > 0 {
err = addValue(orgGroup, anchorPeersValue(anchorProtos), AdminsPolicyKey)
err := addValue(orgGroup, anchorPeersValue(anchorProtos), AdminsPolicyKey)
if err != nil {
return nil, fmt.Errorf("failed to add anchor peers value: %v", err)
}
Expand Down
Loading

0 comments on commit 7315556

Please sign in to comment.