-
Notifications
You must be signed in to change notification settings - Fork 0
/
util.go
96 lines (77 loc) · 2.89 KB
/
util.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
/*
Copyright SecureKey Technologies Inc. All Rights Reserved.
SPDX-License-Identifier: Apache-2.0
*/
package test
import (
"fmt"
"github.com/hyperledger/fabric-protos-go/peer"
"github.com/pkg/errors"
"github.com/aiguo186/fabric-sdk-go-gm/internal/github.com/aiguo186/fabric/protoutil"
"github.com/golang/protobuf/proto"
"github.com/hyperledger/fabric-protos-go/common"
"github.com/hyperledger/fabric-protos-go/orderer"
)
// AddACL adds an ACL config value to channel config
func AddACL(config *common.Config, policyName, policy string) error {
aclsConfigValue, ok := config.ChannelGroup.Groups["Application"].Values["ACLs"]
if !ok {
return errors.New("ACL missing from Application config")
}
acls := &peer.ACLs{}
err := proto.Unmarshal(aclsConfigValue.Value, acls)
if err != nil {
return err
}
acls.Acls[policyName] = &peer.APIResource{PolicyRef: policy}
aclsConfigValue.Value = protoutil.MarshalOrPanic(acls)
return nil
}
// VerifyACL verifies an ACL config value
func VerifyACL(config *common.Config, expectedPolicyName, expectedPolicy string) error {
aclsConfigValue, ok := config.ChannelGroup.Groups["Application"].Values["ACLs"]
if !ok {
return errors.New("ACL missing from Application config")
}
acls := &peer.ACLs{}
err := proto.Unmarshal(aclsConfigValue.Value, acls)
if err != nil {
return err
}
resource, ok := acls.Acls[expectedPolicyName]
if !ok {
return errors.Errorf("missing expected policy name: %s", expectedPolicyName)
}
if resource.PolicyRef != expectedPolicy {
return errors.Errorf("unexpected policy ref: %s, expected: %s", resource.PolicyRef, expectedPolicy)
}
return nil
}
// ModifyMaxMessageCount increments the orderer's BatchSize.MaxMessageCount in a channel config
func ModifyMaxMessageCount(config *common.Config) (uint32, error) {
// Modify Config
batchSizeBytes := config.ChannelGroup.Groups["Orderer"].Values["BatchSize"].Value
batchSize := &orderer.BatchSize{}
if err := proto.Unmarshal(batchSizeBytes, batchSize); err != nil {
return 0, err
}
batchSize.MaxMessageCount = batchSize.MaxMessageCount + 1
newMatchSizeBytes, err := proto.Marshal(batchSize)
if err != nil {
return 0, err
}
config.ChannelGroup.Groups["Orderer"].Values["BatchSize"].Value = newMatchSizeBytes
return batchSize.MaxMessageCount, nil
}
// VerifyMaxMessageCount verifies the orderer's BatchSize.MaxMessageCount in a channel config
func VerifyMaxMessageCount(config *common.Config, expected uint32) error {
batchSizeBytes := config.ChannelGroup.Groups["Orderer"].Values["BatchSize"].Value
batchSize := &orderer.BatchSize{}
if err := proto.Unmarshal(batchSizeBytes, batchSize); err != nil {
return err
}
if batchSize.MaxMessageCount != expected {
return fmt.Errorf("Unexpected MaxMessageCount. actual: %d, expected: %d", batchSize.MaxMessageCount, expected)
}
return nil
}