Skip to content

Commit

Permalink
[FAB-17515] Support configuring BlockValidation policy for orderer group
Browse files Browse the repository at this point in the history
* Before we used to hardcode the BlockValidation policy to an
ImplicitMetaAnyPolicy and we also did not enforce that the
BlockValidation policy existed

Signed-off-by: Will Lahti <wtlahti@us.ibm.com>
Signed-off-by: Danny Cao <dcao@us.ibm.com>
(cherry picked from commit 0c1821d)
  • Loading branch information
caod123 authored and sykesm committed Feb 24, 2020
1 parent 618a4ce commit 7282895
Show file tree
Hide file tree
Showing 2 changed files with 106 additions and 19 deletions.
17 changes: 12 additions & 5 deletions internal/configtxgen/encoder/encoder.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,17 @@ func addPolicy(cg *cb.ConfigGroup, policy policies.ConfigPolicy, modPolicy strin
}
}

func AddOrdererPolicies(cg *cb.ConfigGroup, policyMap map[string]*genesisconfig.Policy, modPolicy string) error {
switch {
case policyMap == nil:
return errors.Errorf("no policies defined")
case policyMap[BlockValidationPolicyKey] == nil:
return errors.Errorf("no BlockValidation policy defined")
}

return AddPolicies(cg, policyMap, modPolicy)
}

func AddPolicies(cg *cb.ConfigGroup, policyMap map[string]*genesisconfig.Policy, modPolicy string) error {
switch {
case policyMap == nil:
Expand Down Expand Up @@ -169,13 +180,9 @@ func NewChannelGroup(conf *genesisconfig.Profile) (*cb.ConfigGroup, error) {
// It sets the mod_policy of all elements to "Admins". This group is always present in any channel configuration.
func NewOrdererGroup(conf *genesisconfig.Orderer) (*cb.ConfigGroup, error) {
ordererGroup := protoutil.NewConfigGroup()
if err := AddPolicies(ordererGroup, conf.Policies, channelconfig.AdminsPolicyKey); err != nil {
if err := AddOrdererPolicies(ordererGroup, conf.Policies, channelconfig.AdminsPolicyKey); err != nil {
return nil, errors.Wrapf(err, "error adding policies to orderer group")
}
ordererGroup.Policies[BlockValidationPolicyKey] = &cb.ConfigPolicy{
Policy: policies.ImplicitMetaAnyPolicy(channelconfig.WritersPolicyKey).Value(),
ModPolicy: channelconfig.AdminsPolicyKey,
}
addValue(ordererGroup, channelconfig.BatchSizeValue(
conf.BatchSize.MaxMessageCount,
conf.BatchSize.AbsoluteMaxBytes,
Expand Down
108 changes: 94 additions & 14 deletions internal/configtxgen/encoder/encoder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,66 @@ func CreateStandardPolicies() map[string]*genesisconfig.Policy {
}
}

func CreateStandardOrdererPolicies() map[string]*genesisconfig.Policy {
policies := CreateStandardPolicies()

policies["BlockValidation"] = &genesisconfig.Policy{
Type: "ImplicitMeta",
Rule: "ANY Admins",
}

return policies
}

var _ = Describe("Encoder", func() {
Describe("AddOrdererPolicies", func() {
var (
cg *cb.ConfigGroup
policies map[string]*genesisconfig.Policy
)

BeforeEach(func() {
cg = protoutil.NewConfigGroup()
policies = CreateStandardOrdererPolicies()
})

It("adds the block validation policy to the group", func() {
err := encoder.AddOrdererPolicies(cg, policies, "Admins")
Expect(err).NotTo(HaveOccurred())
Expect(len(cg.Policies)).To(Equal(4))

Expect(cg.Policies["BlockValidation"].Policy).To(Equal(&cb.Policy{
Type: int32(cb.Policy_IMPLICIT_META),
Value: protoutil.MarshalOrPanic(&cb.ImplicitMetaPolicy{
SubPolicy: "Admins",
Rule: cb.ImplicitMetaPolicy_ANY,
}),
}))
})

Context("when the policy map is nil", func() {
BeforeEach(func() {
policies = nil
})

It("returns an error", func() {
err := encoder.AddOrdererPolicies(cg, policies, "Admins")
Expect(err).To(MatchError("no policies defined"))
})
})

Context("when the policy map is missing 'BlockValidation'", func() {
BeforeEach(func() {
delete(policies, "BlockValidation")
})

It("returns an error", func() {
err := encoder.AddOrdererPolicies(cg, policies, "Admins")
Expect(err).To(MatchError("no BlockValidation policy defined"))
})
})
})

Describe("AddPolicies", func() {
var (
cg *cb.ConfigGroup
Expand All @@ -58,13 +117,34 @@ var _ = Describe("Encoder", func() {
policies = CreateStandardPolicies()
})

It("adds the policies to the group", func() {
It("adds the standard policies to the group", func() {
err := encoder.AddPolicies(cg, policies, "Admins")
Expect(err).NotTo(HaveOccurred())
Expect(len(cg.Policies)).To(Equal(3))
Expect(cg.Policies["Admins"]).NotTo(BeNil())
Expect(cg.Policies["Readers"]).NotTo(BeNil())
Expect(cg.Policies["Writers"]).NotTo(BeNil())

Expect(cg.Policies["Admins"].Policy).To(Equal(&cb.Policy{
Type: int32(cb.Policy_IMPLICIT_META),
Value: protoutil.MarshalOrPanic(&cb.ImplicitMetaPolicy{
SubPolicy: "Admins",
Rule: cb.ImplicitMetaPolicy_ANY,
}),
}))

Expect(cg.Policies["Readers"].Policy).To(Equal(&cb.Policy{
Type: int32(cb.Policy_IMPLICIT_META),
Value: protoutil.MarshalOrPanic(&cb.ImplicitMetaPolicy{
SubPolicy: "Readers",
Rule: cb.ImplicitMetaPolicy_ANY,
}),
}))

Expect(cg.Policies["Writers"].Policy).To(Equal(&cb.Policy{
Type: int32(cb.Policy_IMPLICIT_META),
Value: protoutil.MarshalOrPanic(&cb.ImplicitMetaPolicy{
SubPolicy: "Writers",
Rule: cb.ImplicitMetaPolicy_ANY,
}),
}))
})

Context("when the policy map is nil", func() {
Expand Down Expand Up @@ -161,7 +241,7 @@ var _ = Describe("Encoder", func() {
},
Orderer: &genesisconfig.Orderer{
OrdererType: "solo",
Policies: CreateStandardPolicies(),
Policies: CreateStandardOrdererPolicies(),
Addresses: []string{"foo.com:7050", "bar.com:8050"},
},
Consortiums: map[string]*genesisconfig.Consortium{
Expand Down Expand Up @@ -279,7 +359,7 @@ var _ = Describe("Encoder", func() {
Policies: CreateStandardPolicies(),
},
},
Policies: CreateStandardPolicies(),
Policies: CreateStandardOrdererPolicies(),
Capabilities: map[string]bool{
"FakeCapability": true,
},
Expand Down Expand Up @@ -986,7 +1066,7 @@ var _ = Describe("Encoder", func() {
Policies: CreateStandardPolicies(),
Orderer: &genesisconfig.Orderer{
OrdererType: "solo",
Policies: CreateStandardPolicies(),
Policies: CreateStandardOrdererPolicies(),
},
Application: &genesisconfig.Application{
Organizations: []*genesisconfig.Organization{
Expand Down Expand Up @@ -1019,7 +1099,7 @@ var _ = Describe("Encoder", func() {
Policies: CreateStandardPolicies(),
Orderer: &genesisconfig.Orderer{
OrdererType: "kafka",
Policies: CreateStandardPolicies(),
Policies: CreateStandardOrdererPolicies(),
},
Consortiums: map[string]*genesisconfig.Consortium{
"SampleConsortium": {
Expand Down Expand Up @@ -1097,7 +1177,7 @@ var _ = Describe("Encoder", func() {
Policies: CreateStandardPolicies(),
Orderer: &genesisconfig.Orderer{
OrdererType: "solo",
Policies: CreateStandardPolicies(),
Policies: CreateStandardOrdererPolicies(),
},
Application: &genesisconfig.Application{
Policies: CreateStandardPolicies(),
Expand Down Expand Up @@ -1161,7 +1241,7 @@ var _ = Describe("Encoder", func() {
Consortium: "SampleConsortium",
Orderer: &genesisconfig.Orderer{
OrdererType: "solo",
Policies: CreateStandardPolicies(),
Policies: CreateStandardOrdererPolicies(),
},
Application: &genesisconfig.Application{
Organizations: []*genesisconfig.Organization{
Expand All @@ -1182,7 +1262,7 @@ var _ = Describe("Encoder", func() {
Policies: CreateStandardPolicies(),
Orderer: &genesisconfig.Orderer{
OrdererType: "kafka",
Policies: CreateStandardPolicies(),
Policies: CreateStandardOrdererPolicies(),
},
Consortiums: map[string]*genesisconfig.Consortium{
"SampleConsortium": {
Expand Down Expand Up @@ -1376,7 +1456,7 @@ var _ = Describe("Encoder", func() {
Policies: CreateStandardPolicies(),
Orderer: &genesisconfig.Orderer{
OrdererType: "solo",
Policies: CreateStandardPolicies(),
Policies: CreateStandardOrdererPolicies(),
},
}
})
Expand Down Expand Up @@ -1425,7 +1505,7 @@ var _ = Describe("Encoder", func() {
Policies: CreateStandardPolicies(),
Orderer: &genesisconfig.Orderer{
OrdererType: "solo",
Policies: CreateStandardPolicies(),
Policies: CreateStandardOrdererPolicies(),
},
}
})
Expand Down Expand Up @@ -1456,7 +1536,7 @@ var _ = Describe("Encoder", func() {
bs = encoder.New(&genesisconfig.Profile{
Policies: CreateStandardPolicies(),
Orderer: &genesisconfig.Orderer{
Policies: CreateStandardPolicies(),
Policies: CreateStandardOrdererPolicies(),
OrdererType: "solo",
},
})
Expand Down

0 comments on commit 7282895

Please sign in to comment.