Skip to content

Commit

Permalink
Add function to create application channel genesis block (#31)
Browse files Browse the repository at this point in the history
FAB-18027

Signed-off-by: Will Lahti <wtlahti@us.ibm.com>
  • Loading branch information
wlahti authored Jun 25, 2020
1 parent 822f633 commit 4bcdec8
Show file tree
Hide file tree
Showing 4 changed files with 932 additions and 117 deletions.
24 changes: 22 additions & 2 deletions configtx/application.go
Original file line number Diff line number Diff line change
Expand Up @@ -434,9 +434,10 @@ func (a *ApplicationOrg) CreateMSPCRL(signingIdentity *SigningIdentity, certs ..
return msp.newMSPCRL(signingIdentity, certs...)
}

// newApplicationGroup returns the application component of the channel configuration.
// newApplicationGroupTemplate returns the application component of the channel
// configuration with only the names of the application organizations.
// By default, it sets the mod_policy of all elements to "Admins".
func newApplicationGroup(application Application) (*cb.ConfigGroup, error) {
func newApplicationGroupTemplate(application Application) (*cb.ConfigGroup, error) {
var err error

applicationGroup := newConfigGroup()
Expand Down Expand Up @@ -467,6 +468,25 @@ func newApplicationGroup(application Application) (*cb.ConfigGroup, error) {
return applicationGroup, nil
}

// newApplicationGroup returns the application component of the channel
// configuration with the entire configuration for application organizations.
// By default, it sets the mod_policy of all elements to "Admins".
func newApplicationGroup(application Application) (*cb.ConfigGroup, error) {
applicationGroup, err := newApplicationGroupTemplate(application)
if err != nil {
return nil, err
}

for _, org := range application.Organizations {
applicationGroup.Groups[org.Name], err = newOrgConfigGroup(org)
if err != nil {
return nil, fmt.Errorf("org group '%s': %v", org.Name, err)
}
}

return applicationGroup, nil
}

// aclValues returns the config definition for an application's resources based ACL definitions.
// It is a value for the /Channel/Application/.
func aclValues(acls map[string]string) *standardConfigValue {
Expand Down
65 changes: 23 additions & 42 deletions configtx/application_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ import (
"github.com/hyperledger/fabric-config/protolator"
"github.com/hyperledger/fabric-config/protolator/protoext/peerext"
cb "github.com/hyperledger/fabric-protos-go/common"
mb "github.com/hyperledger/fabric-protos-go/msp"
. "github.com/onsi/gomega"
)

Expand Down Expand Up @@ -100,7 +99,7 @@ func TestNewApplicationGroup(t *testing.T) {
}
`

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

expectedApplication := &cb.ConfigGroup{}
Expand Down Expand Up @@ -199,7 +198,7 @@ func TestNewApplicationGroupFailure(t *testing.T) {
application, _ := baseApplication(t)
tt.applicationMod(&application)

configGrp, err := newApplicationGroup(application)
configGrp, err := newApplicationGroupTemplate(application)
gt.Expect(err).To(MatchError(tt.expectedErr))
gt.Expect(configGrp).To(BeNil())
})
Expand All @@ -213,7 +212,7 @@ func TestAddAnchorPeer(t *testing.T) {

baseApplicationConf, _ := baseApplication(t)

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

config := &cb.Config{
Expand Down Expand Up @@ -376,7 +375,7 @@ func TestRemoveAnchorPeer(t *testing.T) {

baseApplicationConf, _ := baseApplication(t)

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

config := &cb.Config{
Expand Down Expand Up @@ -531,7 +530,7 @@ func TestRemoveAnchorPeerFailure(t *testing.T) {

baseApplicationConf, _ := baseApplication(t)

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

applicationGroup.Groups["Org1"].Values = tt.configValues
Expand Down Expand Up @@ -560,7 +559,7 @@ func TestAnchorPeers(t *testing.T) {
channelGroup := newConfigGroup()

application, _ := baseApplication(t)
applicationGroup, err := newApplicationGroup(application)
applicationGroup, err := newApplicationGroupTemplate(application)
gt.Expect(err).NotTo(HaveOccurred())

channelGroup.Groups[ApplicationGroupKey] = applicationGroup
Expand Down Expand Up @@ -629,7 +628,7 @@ func TestSetACL(t *testing.T) {

channelGroup := newConfigGroup()
baseApplication, _ := baseApplication(t)
applicationGroup, err := newApplicationGroup(baseApplication)
applicationGroup, err := newApplicationGroupTemplate(baseApplication)

channelGroup.Groups[ApplicationGroupKey] = applicationGroup
config := &cb.Config{
Expand Down Expand Up @@ -693,7 +692,7 @@ func TestRemoveACL(t *testing.T) {
baseApplication, _ := baseApplication(t)
baseApplication.ACLs["acl2"] = "acl2Value"
baseApplication.ACLs["acl3"] = "acl3Value"
applicationGroup, err := newApplicationGroup(baseApplication)
applicationGroup, err := newApplicationGroupTemplate(baseApplication)

channelGroup.Groups[ApplicationGroupKey] = applicationGroup
config := &cb.Config{
Expand Down Expand Up @@ -904,7 +903,7 @@ func TestSetApplicationOrgFailures(t *testing.T) {
gt := NewGomegaWithT(t)

application, _ := baseApplication(t)
appGroup, err := newApplicationGroup(application)
appGroup, err := newApplicationGroupTemplate(application)
gt.Expect(err).NotTo(HaveOccurred())

config := &cb.Config{
Expand All @@ -930,7 +929,7 @@ func TestApplicationConfiguration(t *testing.T) {
gt := NewGomegaWithT(t)

baseApplicationConf, _ := baseApplication(t)
applicationGroup, err := newApplicationGroup(baseApplicationConf)
applicationGroup, err := newApplicationGroupTemplate(baseApplicationConf)
gt.Expect(err).NotTo(HaveOccurred())

config := &cb.Config{
Expand Down Expand Up @@ -986,7 +985,7 @@ func TestApplicationConfigurationFailure(t *testing.T) {
gt := NewGomegaWithT(t)

baseApplicationConf, _ := baseApplication(t)
applicationGroup, err := newApplicationGroup(baseApplicationConf)
applicationGroup, err := newApplicationGroupTemplate(baseApplicationConf)
gt.Expect(err).NotTo(HaveOccurred())

config := &cb.Config{
Expand Down Expand Up @@ -1016,7 +1015,7 @@ func TestApplicationACLs(t *testing.T) {
gt := NewGomegaWithT(t)

baseApplicationConf, _ := baseApplication(t)
applicationGroup, err := newApplicationGroup(baseApplicationConf)
applicationGroup, err := newApplicationGroupTemplate(baseApplicationConf)
gt.Expect(err).NotTo(HaveOccurred())

config := &cb.Config{
Expand All @@ -1040,7 +1039,7 @@ func TestApplicationACLsFailure(t *testing.T) {
gt := NewGomegaWithT(t)

baseApplicationConf, _ := baseApplication(t)
applicationGroup, err := newApplicationGroup(baseApplicationConf)
applicationGroup, err := newApplicationGroupTemplate(baseApplicationConf)
gt.Expect(err).NotTo(HaveOccurred())

config := &cb.Config{
Expand Down Expand Up @@ -1068,7 +1067,7 @@ func TestApplicationCapabilities(t *testing.T) {
gt := NewGomegaWithT(t)

baseApplicationConf, _ := baseApplication(t)
applicationGroup, err := newApplicationGroup(baseApplicationConf)
applicationGroup, err := newApplicationGroupTemplate(baseApplicationConf)
gt.Expect(err).NotTo(HaveOccurred())

config := &cb.Config{
Expand Down Expand Up @@ -1277,7 +1276,7 @@ func TestAddApplicationCapability(t *testing.T) {
gt := NewGomegaWithT(t)

baseApp, _ := baseApplication(t)
appGroup, err := newApplicationGroup(baseApp)
appGroup, err := newApplicationGroupTemplate(baseApp)
gt.Expect(err).NotTo(HaveOccurred())

config := &cb.Config{
Expand Down Expand Up @@ -1341,7 +1340,7 @@ func TestAddApplicationCapabilityFailures(t *testing.T) {
gt := NewGomegaWithT(t)

baseApp, _ := baseApplication(t)
appGroup, err := newApplicationGroup(baseApp)
appGroup, err := newApplicationGroupTemplate(baseApp)
gt.Expect(err).NotTo(HaveOccurred())
tt.applicationGroup(appGroup)

Expand All @@ -1367,7 +1366,7 @@ func TestRemoveApplicationCapability(t *testing.T) {
gt := NewGomegaWithT(t)

baseApp, _ := baseApplication(t)
appGroup, err := newApplicationGroup(baseApp)
appGroup, err := newApplicationGroupTemplate(baseApp)
gt.Expect(err).NotTo(HaveOccurred())

config := &cb.Config{
Expand Down Expand Up @@ -1505,7 +1504,7 @@ func TestRemoveApplicationCapabilityFailures(t *testing.T) {
gt := NewGomegaWithT(t)

baseApp, _ := baseApplication(t)
appGroup, err := newApplicationGroup(baseApp)
appGroup, err := newApplicationGroupTemplate(baseApp)
gt.Expect(err).NotTo(HaveOccurred())
tt.applicationGroup(appGroup)

Expand Down Expand Up @@ -1743,7 +1742,7 @@ func TestSetApplicationPolicy(t *testing.T) {
channelGroup := newConfigGroup()
application, _ := baseApplication(t)

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

channelGroup.Groups[ApplicationGroupKey] = applicationGroup
Expand Down Expand Up @@ -1788,7 +1787,7 @@ func TestSetApplicationPolicyFailures(t *testing.T) {
channelGroup := newConfigGroup()
application, _ := baseApplication(t)

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

channelGroup.Groups[ApplicationGroupKey] = applicationGroup
Expand All @@ -1812,7 +1811,7 @@ func TestRemoveApplicationPolicy(t *testing.T) {
channelGroup := newConfigGroup()
application, _ := baseApplication(t)

applicationGroup, err := newApplicationGroup(application)
applicationGroup, err := newApplicationGroupTemplate(application)
gt.Expect(err).NotTo(HaveOccurred())
applicationGroup.Policies["TestPolicy"] = applicationGroup.Policies[AdminsPolicyKey]

Expand Down Expand Up @@ -1854,7 +1853,7 @@ func TestRemoveApplicationPolicyFailures(t *testing.T) {
channelGroup := newConfigGroup()
application, _ := baseApplication(t)

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

applicationGroup.Policies[EndorsementPolicyKey] = &cb.ConfigPolicy{
Expand All @@ -1879,28 +1878,10 @@ func TestApplicationMSP(t *testing.T) {

gt := NewGomegaWithT(t)

expectedMSP, _ := baseMSP(t)

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

// We need to add the base MSP config to the base application since
// newApplicationGroup doesn't apply MSP configuration
applicationOrgGroup := applicationGroup.Groups["Org1"]
fabricMSPConfig, err := expectedMSP.toProto()
gt.Expect(err).NotTo(HaveOccurred())

conf, err := proto.Marshal(fabricMSPConfig)
gt.Expect(err).NotTo(HaveOccurred())

mspConfig := &mb.MSPConfig{
Config: conf,
}

err = setValue(applicationOrgGroup, mspValue(mspConfig), AdminsPolicyKey)
gt.Expect(err).NotTo(HaveOccurred())

config := &cb.Config{
ChannelGroup: &cb.ConfigGroup{
Groups: map[string]*cb.ConfigGroup{
Expand All @@ -1913,7 +1894,7 @@ func TestApplicationMSP(t *testing.T) {

msp, err := c.Application().Organization("Org1").MSP()
gt.Expect(err).NotTo(HaveOccurred())
gt.Expect(msp).To(Equal(expectedMSP))
gt.Expect(msp).To(Equal(application.Organizations[0].MSP))
}

func TestSetApplicationMSPFailure(t *testing.T) {
Expand Down
Loading

0 comments on commit 4bcdec8

Please sign in to comment.