Skip to content

Commit

Permalink
[FAB-17619] encapsulate config proto in new type
Browse files Browse the repository at this point in the history
- Add new type to wrap base and modified cb.Config
- Add pointer receiver to all exported functions that used cb.Config
- Remove unused param and rename getMSPConfigForOrg()

Signed-off-by: Tiffany Harris <[email protected]>
  • Loading branch information
stephyee authored and sykesm committed Mar 26, 2020
1 parent 441fd13 commit 79fb075
Show file tree
Hide file tree
Showing 19 changed files with 655 additions and 311 deletions.
13 changes: 6 additions & 7 deletions integration/config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ import (

docker "github.com/fsouza/go-dockerclient"
"github.com/golang/protobuf/proto"
cb "github.com/hyperledger/fabric-protos-go/common"
"github.com/hyperledger/fabric/integration/nwo"
"github.com/hyperledger/fabric/integration/nwo/commands"
"github.com/hyperledger/fabric/pkg/config"
Expand Down Expand Up @@ -145,15 +144,16 @@ var _ = Describe("Config", func() {
for _, peer := range network.AnchorsForChannel("testchannel") {
By("getting the current channel config")
channelConfig := nwo.GetConfig(network, peer, orderer, "testchannel")
updatedChannelConfig := proto.Clone(channelConfig).(*cb.Config)

c := config.New(channelConfig)

By("adding the anchor peer for " + peer.Organization)
host, port := peerHostPort(network, peer)
err = config.AddAnchorPeer(updatedChannelConfig, peer.Organization, config.Address{Host: host, Port: port})
err = c.AddAnchorPeer(peer.Organization, config.Address{Host: host, Port: port})
Expect(err).NotTo(HaveOccurred())

By("computing the config update")
configUpdate, err := config.ComputeUpdate(channelConfig, updatedChannelConfig, "testchannel")
configUpdate, err := c.ComputeUpdate("testchannel")
Expect(err).NotTo(HaveOccurred())

By("creating a detached signature")
Expand All @@ -167,6 +167,7 @@ var _ = Describe("Config", func() {

By("creating a signed config update envelope with the detached signature")
configUpdateEnvelope, err := config.CreateSignedConfigUpdateEnvelope(configUpdate, signingIdentity, signature)
Expect(err).NotTo(HaveOccurred())
configUpdateBytes, err := proto.Marshal(configUpdateEnvelope)
Expect(err).NotTo(HaveOccurred())
tempFile, err := ioutil.TempFile("", "add-anchor-peer")
Expand Down Expand Up @@ -194,9 +195,7 @@ var _ = Describe("Config", func() {

By("ensuring the active channel config matches the submitted config")
finalChannelConfig := nwo.GetConfig(network, peer, orderer, "testchannel")
configUpdate, err = config.ComputeUpdate(updatedChannelConfig, finalChannelConfig, "testchannel")
Expect(configUpdate).To(BeNil())
Expect(err).To(MatchError("failed to compute update: no differences detected between original and updated config"))
Expect(c.Updated()).To(Equal(finalChannelConfig))
}
})
})
Expand Down
28 changes: 22 additions & 6 deletions pkg/config/application.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,8 @@ func AddApplicationOrg(config *cb.Config, org Organization) error {
// AddAnchorPeer adds an anchor peer to an existing channel config transaction.
// It must add the anchor peer to an existing org and the anchor peer must not already
// exist in the org.
func AddAnchorPeer(config *cb.Config, orgName string, newAnchorPeer Address) error {
applicationOrgGroup, ok := config.ChannelGroup.Groups[ApplicationGroupKey].Groups[orgName]
func (c *ConfigTx) AddAnchorPeer(orgName string, newAnchorPeer Address) error {
applicationOrgGroup, ok := c.updated.ChannelGroup.Groups[ApplicationGroupKey].Groups[orgName]
if !ok {
return fmt.Errorf("application org %s does not exist in channel config", orgName)
}
Expand Down Expand Up @@ -84,8 +84,8 @@ func AddAnchorPeer(config *cb.Config, orgName string, newAnchorPeer Address) err

// RemoveAnchorPeer removes an anchor peer from an existing channel config transaction.
// The removed anchor peer and org it belongs to must both already exist.
func RemoveAnchorPeer(config *cb.Config, orgName string, anchorPeerToRemove Address) error {
applicationOrgGroup, ok := config.ChannelGroup.Groups[ApplicationGroupKey].Groups[orgName]
func (c *ConfigTx) RemoveAnchorPeer(orgName string, anchorPeerToRemove Address) error {
applicationOrgGroup, ok := c.updated.ChannelGroup.Groups[ApplicationGroupKey].Groups[orgName]
if !ok {
return fmt.Errorf("application org %s does not exist in channel config", orgName)
}
Expand Down Expand Up @@ -129,8 +129,8 @@ func RemoveAnchorPeer(config *cb.Config, orgName string, anchorPeerToRemove Addr
}

// GetAnchorPeers retrieves existing anchor peers from a application organization.
func GetAnchorPeers(config *cb.Config, orgName string) ([]Address, error) {
applicationOrgGroup, ok := config.ChannelGroup.Groups[ApplicationGroupKey].Groups[orgName]
func (c *ConfigTx) GetAnchorPeers(orgName string) ([]Address, error) {
applicationOrgGroup, ok := c.base.ChannelGroup.Groups[ApplicationGroupKey].Groups[orgName]
if !ok {
return nil, fmt.Errorf("application org %s does not exist in channel config", orgName)
}
Expand Down Expand Up @@ -158,6 +158,21 @@ func GetAnchorPeers(config *cb.Config, orgName string) ([]Address, error) {
return anchorPeers, nil
}

// AddApplicationOrg adds an organization to an existing config's Application configuration.
// Will not error if organization already exists.
func (c *ConfigTx) AddApplicationOrg(org Organization) error {
appGroup := c.updated.ChannelGroup.Groups[ApplicationGroupKey]

orgGroup, err := newOrgConfigGroup(org)
if err != nil {
return fmt.Errorf("failed to create application org %s: %v", org.Name, err)
}

appGroup.Groups[org.Name] = orgGroup

return nil
}

// newApplicationGroup returns the application component of the channel configuration.
// By default, it sets the mod_policy of all elements to "Admins".
func newApplicationGroup(application Application) (*cb.ConfigGroup, error) {
Expand Down Expand Up @@ -225,5 +240,6 @@ func getApplicationOrg(config *cb.Config, orgName string) (*cb.ConfigGroup, erro
if !ok {
return nil, fmt.Errorf("application org with name '%s' not found", orgName)
}

return org, nil
}
61 changes: 50 additions & 11 deletions pkg/config/application_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,11 @@ func TestAddAnchorPeer(t *testing.T) {
},
}

c := ConfigTx{
base: config,
updated: config,
}

newOrg1AnchorPeer := Address{
Host: "host3",
Port: 123,
Expand Down Expand Up @@ -300,10 +305,10 @@ func TestAddAnchorPeer(t *testing.T) {
err = protolator.DeepUnmarshalJSON(bytes.NewBufferString(expectedUpdatedConfigJSON), expectedUpdatedConfig)
gt.Expect(err).ToNot(HaveOccurred())

err = AddAnchorPeer(config, "Org1", newOrg1AnchorPeer)
err = c.AddAnchorPeer("Org1", newOrg1AnchorPeer)
gt.Expect(err).NotTo(HaveOccurred())

err = AddAnchorPeer(config, "Org2", newOrg2AnchorPeer)
err = c.AddAnchorPeer("Org2", newOrg2AnchorPeer)
gt.Expect(err).NotTo(HaveOccurred())

gt.Expect(config).To(Equal(expectedUpdatedConfig))
Expand Down Expand Up @@ -370,11 +375,16 @@ func TestAddAnchorPeerFailure(t *testing.T) {
},
}

c := ConfigTx{
base: config,
updated: config,
}

if tt.configMod != nil {
tt.configMod(gt, config)
}

err = AddAnchorPeer(config, tt.orgName, tt.newAnchorPeer)
err = c.AddAnchorPeer(tt.orgName, tt.newAnchorPeer)
gt.Expect(err).To(MatchError(tt.expectedErr))
})
}
Expand All @@ -400,6 +410,11 @@ func TestRemoveAnchorPeer(t *testing.T) {
},
}

c := ConfigTx{
base: config,
updated: config,
}

expectedUpdatedConfigJSON := `
{
"channel_group": {
Expand Down Expand Up @@ -497,14 +512,14 @@ func TestRemoveAnchorPeer(t *testing.T) {
}
`
anchorPeer1 := Address{Host: "host1", Port: 123}
err = AddAnchorPeer(config, "Org1", anchorPeer1)
err = c.AddAnchorPeer("Org1", anchorPeer1)
gt.Expect(err).NotTo(HaveOccurred())
expectedUpdatedConfig := &cb.Config{}

err = protolator.DeepUnmarshalJSON(bytes.NewBufferString(expectedUpdatedConfigJSON), expectedUpdatedConfig)
gt.Expect(err).ToNot(HaveOccurred())

err = RemoveAnchorPeer(config, "Org1", anchorPeer1)
err = c.RemoveAnchorPeer("Org1", anchorPeer1)
gt.Expect(err).NotTo(HaveOccurred())

gt.Expect(config).To(Equal(expectedUpdatedConfig))
Expand Down Expand Up @@ -553,7 +568,12 @@ func TestRemoveAnchorPeerFailure(t *testing.T) {
},
}

err = RemoveAnchorPeer(config, tt.orgName, tt.anchorPeerToRemove)
c := ConfigTx{
base: config,
updated: config,
}

err = c.RemoveAnchorPeer(tt.orgName, tt.anchorPeerToRemove)
gt.Expect(err).To(MatchError(tt.expectedErr))
})
}
Expand All @@ -575,11 +595,15 @@ func TestGetAnchorPeer(t *testing.T) {
}

expectedAnchorPeer := Address{Host: "host1", Port: 123}
c := ConfigTx{
base: config,
updated: config,
}

err = AddAnchorPeer(config, "Org1", expectedAnchorPeer)
err = c.AddAnchorPeer("Org1", expectedAnchorPeer)
gt.Expect(err).NotTo(HaveOccurred())

anchorPeers, err := GetAnchorPeers(config, "Org1")
anchorPeers, err := c.GetAnchorPeers("Org1")
gt.Expect(err).NotTo(HaveOccurred())
gt.Expect(len(anchorPeers)).To(Equal(1))
gt.Expect(anchorPeers[0]).To(Equal(expectedAnchorPeer))
Expand Down Expand Up @@ -609,6 +633,11 @@ func TestGetAnchorPeerFailures(t *testing.T) {
ChannelGroup: channelGroup,
}

c := ConfigTx{
base: config,
updated: config,
}

for _, test := range []struct {
name string
orgName string
Expand All @@ -629,7 +658,7 @@ func TestGetAnchorPeerFailures(t *testing.T) {
t.Run(test.name, func(t *testing.T) {
t.Parallel()
gt := NewGomegaWithT(t)
_, err := GetAnchorPeers(config, test.orgName)
_, err := c.GetAnchorPeers(test.orgName)
gt.Expect(err).To(MatchError(test.expectedErr))
})
}
Expand Down Expand Up @@ -675,6 +704,11 @@ func TestAddApplicationOrg(t *testing.T) {
},
}

c := ConfigTx{
base: config,
updated: config,
}

org := Organization{
Name: "Org3",
Policies: applicationOrgStandardPolicies(),
Expand Down Expand Up @@ -831,7 +865,7 @@ func TestAddApplicationOrg(t *testing.T) {
}
`, certBase64, crlBase64, pkBase64)

err = AddApplicationOrg(config, org)
err = c.AddApplicationOrg(org)
gt.Expect(err).NotTo(HaveOccurred())

actualApplicationConfigGroup := config.ChannelGroup.Groups[ApplicationGroupKey].Groups["Org3"]
Expand All @@ -857,10 +891,15 @@ func TestAddApplicationOrgFailures(t *testing.T) {
},
}

c := ConfigTx{
base: config,
updated: config,
}

org := Organization{
Name: "Org3",
}

err = AddApplicationOrg(config, org)
err = c.AddApplicationOrg(org)
gt.Expect(err).To(MatchError("failed to create application org Org3: no policies defined"))
}
23 changes: 12 additions & 11 deletions pkg/config/capabilities.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,45 +16,45 @@ import (

// GetChannelCapabilities returns a map of enabled channel capabilities
// from a config transaction.
func GetChannelCapabilities(config *cb.Config) (map[string]bool, error) {
capabilites, err := getCapabilities(config.ChannelGroup)
func (c *ConfigTx) GetChannelCapabilities() (map[string]bool, error) {
capabilities, err := getCapabilities(c.base.ChannelGroup)
if err != nil {
return nil, fmt.Errorf("retrieving channel capabilities: %v", err)
}

return capabilites, nil
return capabilities, nil
}

// GetOrdererCapabilities returns a map of enabled orderer capabilities
// from a config transaction.
func GetOrdererCapabilities(config *cb.Config) (map[string]bool, error) {
orderer, ok := config.ChannelGroup.Groups[OrdererGroupKey]
func (c *ConfigTx) GetOrdererCapabilities() (map[string]bool, error) {
orderer, ok := c.base.ChannelGroup.Groups[OrdererGroupKey]
if !ok {
return nil, errors.New("orderer missing from config")
}

capabilites, err := getCapabilities(orderer)
capabilities, err := getCapabilities(orderer)
if err != nil {
return nil, fmt.Errorf("retrieving orderer capabilities: %v", err)
}

return capabilites, nil
return capabilities, nil
}

// GetApplicationCapabilities returns a map of enabled application capabilities
// from a config transaction.
func GetApplicationCapabilities(config *cb.Config) (map[string]bool, error) {
application, ok := config.ChannelGroup.Groups[ApplicationGroupKey]
func (c *ConfigTx) GetApplicationCapabilities() (map[string]bool, error) {
application, ok := c.base.ChannelGroup.Groups[ApplicationGroupKey]
if !ok {
return nil, errors.New("application missing from config")
}

capabilites, err := getCapabilities(application)
capabilities, err := getCapabilities(application)
if err != nil {
return nil, fmt.Errorf("retrieving application capabilities: %v", err)
}

return capabilites, nil
return capabilities, nil
}

func getCapabilities(configGroup *cb.ConfigGroup) (map[string]bool, error) {
Expand All @@ -65,6 +65,7 @@ func getCapabilities(configGroup *cb.ConfigGroup) (map[string]bool, error) {
}

capabilitiesProto := &cb.Capabilities{}

err := proto.Unmarshal(capabilitiesValue.Value, capabilitiesProto)
if err != nil {
return nil, fmt.Errorf("unmarshalling capabilities: %v", err)
Expand Down
Loading

0 comments on commit 79fb075

Please sign in to comment.