Skip to content

Commit

Permalink
Convert unnecessary config type references to values
Browse files Browse the repository at this point in the history
* A lot of the user facing API was using pointer types when there was no
need to pass by reference, this commit addresses a couple of these cases

Signed-off-by: Danny Cao <[email protected]>
Signed-off-by: Dereck Luo <[email protected]>
  • Loading branch information
caod123 authored and sykesm committed Mar 10, 2020
1 parent 7315556 commit 845b5ed
Show file tree
Hide file tree
Showing 11 changed files with 112 additions and 161 deletions.
6 changes: 3 additions & 3 deletions integration/config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,11 +69,11 @@ var _ = Describe("Config", func() {
org1peer0 := network.Peer("Org1", "peer0")

By("setting up the channel")
channel := &config.Channel{
channel := config.Channel{
ChannelID: "testchannel",
Consortium: "SampleConsortium",
Application: &config.Application{
Organizations: []*config.Organization{
Application: config.Application{
Organizations: []config.Organization{
{
Name: "Org1",
},
Expand Down
14 changes: 7 additions & 7 deletions pkg/config/application.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import (
// Application is a copy of the orderer configuration with the addition of an anchor peers
// list in the organization definition.
type Application struct {
Organizations []*Organization
Organizations []Organization
Capabilities map[string]bool
Policies map[string]*Policy
ACLs map[string]string
Expand All @@ -31,7 +31,7 @@ type AnchorPeer struct {

// 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) {
func newApplicationGroup(application Application) (*cb.ConfigGroup, error) {
var err error

applicationGroup := newConfigGroup()
Expand Down Expand Up @@ -65,7 +65,7 @@ func newApplicationGroup(application *Application) (*cb.ConfigGroup, 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 *AnchorPeer) error {
func AddAnchorPeer(config *cb.Config, orgName string, newAnchorPeer AnchorPeer) error {
applicationOrgGroup, ok := config.ChannelGroup.Groups[ApplicationGroupKey].Groups[orgName]
if !ok {
return fmt.Errorf("application org %s does not exist in channel config", orgName)
Expand Down Expand Up @@ -108,7 +108,7 @@ func AddAnchorPeer(config *cb.Config, orgName string, newAnchorPeer *AnchorPeer)

// 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 *AnchorPeer) error {
func RemoveAnchorPeer(config *cb.Config, orgName string, anchorPeerToRemove AnchorPeer) error {
applicationOrgGroup, ok := config.ChannelGroup.Groups[ApplicationGroupKey].Groups[orgName]
if !ok {
return fmt.Errorf("application org %s does not exist in channel config", orgName)
Expand Down Expand Up @@ -144,7 +144,7 @@ func RemoveAnchorPeer(config *cb.Config, orgName string, anchorPeerToRemove *Anc
}

// GetAnchorPeers retrieves existing anchor peers from a application organization.
func GetAnchorPeers(config *cb.Config, orgName string) ([]*AnchorPeer, error) {
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)
Expand All @@ -161,9 +161,9 @@ func GetAnchorPeers(config *cb.Config, orgName string) ([]*AnchorPeer, error) {
return nil, fmt.Errorf("failed unmarshalling %s's anchor peer endpoints: %v", orgName, err)
}

anchorPeers := []*AnchorPeer{}
anchorPeers := []AnchorPeer{}
for _, ap := range anchorPeersProto.AnchorPeers {
anchorPeers = append(anchorPeers, &AnchorPeer{
anchorPeers = append(anchorPeers, AnchorPeer{
Host: ap.Host,
Port: int(ap.Port),
})
Expand Down
38 changes: 19 additions & 19 deletions pkg/config/application_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ func TestNewApplicationGroupFailure(t *testing.T) {
gt := NewGomegaWithT(t)

application := baseApplication()
tt.applicationMod(application)
tt.applicationMod(&application)

configGrp, err := newApplicationGroup(application)
gt.Expect(err).To(MatchError(tt.expectedErr))
Expand Down Expand Up @@ -155,12 +155,12 @@ func TestAddAnchorPeer(t *testing.T) {
},
}

newOrg1AnchorPeer := &AnchorPeer{
newOrg1AnchorPeer := AnchorPeer{
Host: "host3",
Port: 123,
}

newOrg2AnchorPeer := &AnchorPeer{
newOrg2AnchorPeer := AnchorPeer{
Host: "host4",
Port: 123,
}
Expand Down Expand Up @@ -303,14 +303,14 @@ func TestAddAnchorPeerFailure(t *testing.T) {
testName string
orgName string
configMod func(*GomegaWithT, *cb.Config)
newAnchorPeer *AnchorPeer
newAnchorPeer AnchorPeer
expectedErr string
}{
{
testName: "When the org for the application does not exist",
orgName: "BadOrg",
configMod: nil,
newAnchorPeer: &AnchorPeer{Host: "host3", Port: 123},
newAnchorPeer: AnchorPeer{Host: "host3", Port: 123},
expectedErr: "application org BadOrg does not exist in channel config",
},
{
Expand All @@ -332,7 +332,7 @@ func TestAddAnchorPeerFailure(t *testing.T) {
Value: v,
}
},
newAnchorPeer: &AnchorPeer{Host: "host1", Port: 123},
newAnchorPeer: AnchorPeer{Host: "host1", Port: 123},
expectedErr: "application org Org1 already contains anchor peer endpoint host1:123",
},
}
Expand Down Expand Up @@ -483,7 +483,7 @@ func TestRemoveAnchorPeer(t *testing.T) {
"sequence": "0"
}
`
anchorPeer1 := &AnchorPeer{Host: "host1", Port: 123}
anchorPeer1 := AnchorPeer{Host: "host1", Port: 123}
err = AddAnchorPeer(config, "Org1", anchorPeer1)
gt.Expect(err).NotTo(HaveOccurred())
expectedUpdatedConfig := &cb.Config{}
Expand All @@ -503,19 +503,19 @@ func TestRemoveAnchorPeerFailure(t *testing.T) {
tests := []struct {
testName string
orgName string
anchorPeerToRemove *AnchorPeer
anchorPeerToRemove AnchorPeer
expectedErr string
}{
{
testName: "When the org for the application does not exist",
orgName: "BadOrg",
anchorPeerToRemove: &AnchorPeer{Host: "host1", Port: 123},
anchorPeerToRemove: AnchorPeer{Host: "host1", Port: 123},
expectedErr: "application org BadOrg does not exist in channel config",
},
{
testName: "When the anchor peer being removed doesn't exist in the org",
orgName: "Org1",
anchorPeerToRemove: &AnchorPeer{Host: "host2", Port: 123},
anchorPeerToRemove: AnchorPeer{Host: "host2", Port: 123},
expectedErr: "could not find anchor peer host2:123 in Org1's anchor peer endpoints",
},
}
Expand Down Expand Up @@ -557,13 +557,13 @@ func TestGetAnchorPeer(t *testing.T) {
gt.Expect(err).NotTo(HaveOccurred())

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

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

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

anchorPeers, err := GetAnchorPeers(config, "Org1")
Expand All @@ -582,7 +582,7 @@ func TestGetAnchorPeerFailures(t *testing.T) {
applicationGroup, err := newApplicationGroup(baseApplication())
gt.Expect(err).NotTo(HaveOccurred())

orgNoAnchor := &Organization{
orgNoAnchor := Organization{
Name: "Org1",
ID: "Org1MSP",
Policies: applicationOrgStandardPolicies(),
Expand All @@ -593,13 +593,13 @@ func TestGetAnchorPeerFailures(t *testing.T) {
applicationGroup.Groups[orgNoAnchor.Name] = orgGroup

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

for _, test := range []struct {
name string
config *cb.Config
config cb.Config
orgName string
expectedErr string
}{
Expand All @@ -626,10 +626,10 @@ func TestGetAnchorPeerFailures(t *testing.T) {
}
}

func baseApplication() *Application {
return &Application{
func baseApplication() Application {
return Application{
Policies: standardPolicies(),
Organizations: []*Organization{
Organizations: []Organization{
{
Name: "Org1",
},
Expand Down
40 changes: 17 additions & 23 deletions pkg/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,9 @@ import (
// Channel is a channel configuration.
type Channel struct {
Consortium string
Application *Application
Orderer *Orderer
Consortiums []*Consortium
Application Application
Orderer Orderer
Consortiums []Consortium
Capabilities map[string]bool
Policies map[string]*Policy
ChannelID string
Expand All @@ -46,9 +46,9 @@ type Organization struct {
Name string
ID string
Policies map[string]*Policy
MSP *MSP
MSP MSP

AnchorPeers []*AnchorPeer
AnchorPeers []AnchorPeer
OrdererEndpoints []string
}

Expand All @@ -64,13 +64,9 @@ type standardConfigPolicy struct {

// NewCreateChannelTx creates a create channel tx using the provided application channel
// configuration and returns an unsigned envelope for an application channel creation transaction.
func NewCreateChannelTx(channelConfig *Channel) (*cb.Envelope, error) {
func NewCreateChannelTx(channelConfig Channel) (*cb.Envelope, error) {
var err error

if channelConfig == nil {
return nil, errors.New("channel config is required")
}

channelID := channelConfig.ChannelID

if channelID == "" {
Expand Down Expand Up @@ -106,7 +102,7 @@ func NewCreateChannelTx(channelConfig *Channel) (*cb.Envelope, error) {

// SignConfigUpdate signs the given configuration update with a
// specified signing identity and returns a config signature.
func SignConfigUpdate(configUpdate *cb.ConfigUpdate, signingIdentity *SigningIdentity) (*cb.ConfigSignature, error) {
func SignConfigUpdate(configUpdate *cb.ConfigUpdate, signingIdentity SigningIdentity) (*cb.ConfigSignature, error) {
signatureHeader, err := signatureHeader(signingIdentity)
if err != nil {
return nil, fmt.Errorf("failed to create signature header: %v", err)
Expand Down Expand Up @@ -139,7 +135,7 @@ func SignConfigUpdate(configUpdate *cb.ConfigUpdate, signingIdentity *SigningIde
}

// CreateSignedConfigUpdateEnvelope creates a signed configuration update envelope.
func CreateSignedConfigUpdateEnvelope(configUpdate *cb.ConfigUpdate, signingIdentity *SigningIdentity,
func CreateSignedConfigUpdateEnvelope(configUpdate *cb.ConfigUpdate, signingIdentity SigningIdentity,
signatures ...*cb.ConfigSignature) (*cb.Envelope, error) {
update, err := proto.Marshal(configUpdate)
if err != nil {
Expand All @@ -160,7 +156,7 @@ func CreateSignedConfigUpdateEnvelope(configUpdate *cb.ConfigUpdate, signingIden
return signedEnvelope, nil
}

func signatureHeader(signingIdentity *SigningIdentity) (*cb.SignatureHeader, error) {
func signatureHeader(signingIdentity SigningIdentity) (*cb.SignatureHeader, error) {
buffer := bytes.NewBuffer(nil)

err := pem.Encode(buffer, &pem.Block{Type: "CERTIFICATE", Bytes: signingIdentity.Certificate.Raw})
Expand Down Expand Up @@ -197,7 +193,7 @@ func newNonce() ([]byte, error) {
}

// newChannelGroup defines the root of the channel configuration.
func newChannelGroup(channelConfig *Channel) (*cb.ConfigGroup, error) {
func newChannelGroup(channelConfig Channel) (*cb.ConfigGroup, error) {
var err error

channelGroup := newConfigGroup()
Expand All @@ -209,11 +205,9 @@ func newChannelGroup(channelConfig *Channel) (*cb.ConfigGroup, error) {
}
}

if channelConfig.Application != nil {
channelGroup.Groups[ApplicationGroupKey], err = newApplicationGroup(channelConfig.Application)
if err != nil {
return nil, fmt.Errorf("failed to create application group: %v", err)
}
channelGroup.Groups[ApplicationGroupKey], err = newApplicationGroup(channelConfig.Application)
if err != nil {
return nil, fmt.Errorf("failed to create application group: %v", err)
}

return channelGroup, nil
Expand All @@ -222,7 +216,7 @@ func newChannelGroup(channelConfig *Channel) (*cb.ConfigGroup, error) {
// newOrgConfigGroup returns an config group for a organization.
// It defines the crypto material for the organization (its MSP).
// It sets the mod_policy of all elements to "Admins".
func newOrgConfigGroup(org *Organization) (*cb.ConfigGroup, error) {
func newOrgConfigGroup(org Organization) (*cb.ConfigGroup, error) {
orgGroup := newConfigGroup()
orgGroup.ModPolicy = AdminsPolicyKey

Expand Down Expand Up @@ -444,7 +438,7 @@ func mspValue(mspDef *mb.MSPConfig) *standardConfigValue {
// defaultConfigTemplate generates a config template based on the assumption that
// the input profile is a channel creation template and no system channel context
// is available.
func defaultConfigTemplate(channelConfig *Channel) (*cb.ConfigGroup, error) {
func defaultConfigTemplate(channelConfig Channel) (*cb.ConfigGroup, error) {
channelGroup, err := newChannelGroup(channelConfig)
if err != nil {
return nil, err
Expand All @@ -463,7 +457,7 @@ func defaultConfigTemplate(channelConfig *Channel) (*cb.ConfigGroup, error) {
// newChannelCreateConfigUpdate generates a ConfigUpdate which can be sent to the orderer to create a new channel.
// Optionally, the channel group of the ordering system channel may be passed in, and the resulting ConfigUpdate
// will extract the appropriate versions from this file.
func newChannelCreateConfigUpdate(channelID string, channelConfig *Channel, templateConfig *cb.ConfigGroup) (*cb.ConfigUpdate, error) {
func newChannelCreateConfigUpdate(channelID string, channelConfig Channel, templateConfig *cb.ConfigGroup) (*cb.ConfigUpdate, error) {
newChannelGroup, err := newChannelGroup(channelConfig)
if err != nil {
return nil, err
Expand Down Expand Up @@ -602,7 +596,7 @@ func concatenateBytes(data ...[]byte) []byte {
func createSignedEnvelopeWithTLSBinding(
txType cb.HeaderType,
channelID string,
signingIdentity *SigningIdentity,
signingIdentity SigningIdentity,
envelope proto.Message,
) (*cb.Envelope, error) {
channelHeader := &cb.ChannelHeader{
Expand Down
Loading

0 comments on commit 845b5ed

Please sign in to comment.