Skip to content

Commit

Permalink
[FAB-6142] Move MakeChainCreationTranscation
Browse files Browse the repository at this point in the history
Presently, MakeChainCreationTransaction is a function of the
channelconfig package.  Although it does seem related to the
channelconfig and references many of the constants defined there, it is
actually unneeded during the runtime for either the orderer or peer
binaries, and is only used by CLI and tests.

Therefore, it is a better fit in the configtxgen/encoder package.

This CR also renames this method to MakeChannelCreationTransaction and
switches it to use the newer underlying config group generation
introduced earlier in this series.

Change-Id: Iece267073dd612c02cc8d1b2d7feea3c02731d8f
Signed-off-by: Jason Yellick <jyellick@us.ibm.com>
  • Loading branch information
Jason Yellick committed Oct 13, 2017
1 parent 52dc361 commit 2ef4823
Show file tree
Hide file tree
Showing 12 changed files with 148 additions and 153 deletions.
49 changes: 0 additions & 49 deletions common/channelconfig/template.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,9 @@ SPDX-License-Identifier: Apache-2.0
package channelconfig

import (
"fmt"

"github.com/hyperledger/fabric/common/cauthdsl"
"github.com/hyperledger/fabric/common/configtx"
"github.com/hyperledger/fabric/common/policies"
"github.com/hyperledger/fabric/common/util"
"github.com/hyperledger/fabric/msp"
cb "github.com/hyperledger/fabric/protos/common"
pb "github.com/hyperledger/fabric/protos/peer"
"github.com/hyperledger/fabric/protos/utils"
Expand Down Expand Up @@ -99,48 +95,3 @@ func (cct *channelCreationTemplate) Envelope(channelID string) (*cb.ConfigUpdate
}),
}, nil
}

// MakeChainCreationTransaction is a handy utility function for creating new chain transactions using the underlying Template framework
func MakeChainCreationTransaction(channelID string, consortium string, signer msp.SigningIdentity, orgs ...string) (*cb.Envelope, error) {
newChainTemplate := NewChainCreationTemplate(consortium, orgs)
newConfigUpdateEnv, err := newChainTemplate.Envelope(channelID)
if err != nil {
return nil, err
}

payloadSignatureHeader := &cb.SignatureHeader{}
if signer != nil {
sSigner, err := signer.Serialize()
if err != nil {
return nil, fmt.Errorf("Serialization of identity failed, err %s", err)
}

newConfigUpdateEnv.Signatures = []*cb.ConfigSignature{&cb.ConfigSignature{
SignatureHeader: utils.MarshalOrPanic(utils.MakeSignatureHeader(sSigner, utils.CreateNonceOrPanic())),
}}

newConfigUpdateEnv.Signatures[0].Signature, err = signer.Sign(util.ConcatenateBytes(newConfigUpdateEnv.Signatures[0].SignatureHeader, newConfigUpdateEnv.ConfigUpdate))
if err != nil {
return nil, err
}

payloadSignatureHeader = utils.MakeSignatureHeader(sSigner, utils.CreateNonceOrPanic())
}

payloadChannelHeader := utils.MakeChannelHeader(cb.HeaderType_CONFIG_UPDATE, msgVersion, channelID, epoch)
utils.SetTxID(payloadChannelHeader, payloadSignatureHeader)
payloadHeader := utils.MakePayloadHeader(payloadChannelHeader, payloadSignatureHeader)
payload := &cb.Payload{Header: payloadHeader, Data: utils.MarshalOrPanic(newConfigUpdateEnv)}
paylBytes := utils.MarshalOrPanic(payload)

var sig []byte
if signer != nil {
// sign the payload
sig, err = signer.Sign(paylBytes)
if err != nil {
return nil, err
}
}

return &cb.Envelope{Payload: paylBytes, Signature: sig}, nil
}
46 changes: 0 additions & 46 deletions common/channelconfig/template_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,7 @@ import (
"testing"

"github.com/hyperledger/fabric/common/configtx"
mmsp "github.com/hyperledger/fabric/common/mocks/msp"
cb "github.com/hyperledger/fabric/protos/common"
"github.com/hyperledger/fabric/protos/utils"

"github.com/golang/protobuf/proto"
"github.com/stretchr/testify/assert"
Expand Down Expand Up @@ -51,47 +49,3 @@ func TestNewChainTemplate(t *testing.T) {
}
}
}

func TestMakeChainCreationTransactionWithSigner(t *testing.T) {
channelID := "foo"

signer, err := mmsp.NewNoopMsp().GetDefaultSigningIdentity()
assert.NoError(t, err, "Creating noop MSP")

cct, err := MakeChainCreationTransaction(channelID, "test", signer)
assert.NoError(t, err, "Making chain creation tx")

assert.NotEmpty(t, cct.Signature, "Should have signature")

payload, err := utils.UnmarshalPayload(cct.Payload)
assert.NoError(t, err, "Unmarshaling payload")

configUpdateEnv, err := configtx.UnmarshalConfigUpdateEnvelope(payload.Data)
assert.NoError(t, err, "Unmarshaling ConfigUpdateEnvelope")

assert.NotEmpty(t, configUpdateEnv.Signatures, "Should have config env sigs")

sigHeader, err := utils.GetSignatureHeader(payload.Header.SignatureHeader)
assert.NoError(t, err, "Unmarshaling SignatureHeader")
assert.NotEmpty(t, sigHeader.Creator, "Creator specified")
}

func TestMakeChainCreationTransactionNoSigner(t *testing.T) {
channelID := "foo"
cct, err := MakeChainCreationTransaction(channelID, "test", nil)
assert.NoError(t, err, "Making chain creation tx")

assert.Empty(t, cct.Signature, "Should have empty signature")

payload, err := utils.UnmarshalPayload(cct.Payload)
assert.NoError(t, err, "Unmarshaling payload")

configUpdateEnv, err := configtx.UnmarshalConfigUpdateEnvelope(payload.Data)
assert.NoError(t, err, "Unmarshaling ConfigUpdateEnvelope")

assert.Empty(t, configUpdateEnv.Signatures, "Should have no config env sigs")

sigHeader, err := utils.GetSignatureHeader(payload.Header.SignatureHeader)
assert.NoError(t, err, "Unmarshaling SignatureHeader")
assert.Empty(t, sigHeader.Creator, "No creator specified")
}
52 changes: 52 additions & 0 deletions common/tools/configtxgen/encoder/encoder.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
"github.com/hyperledger/fabric/common/genesis"
"github.com/hyperledger/fabric/common/policies"
genesisconfig "github.com/hyperledger/fabric/common/tools/configtxgen/localconfig"
"github.com/hyperledger/fabric/common/util"
"github.com/hyperledger/fabric/msp"
cb "github.com/hyperledger/fabric/protos/common"
pb "github.com/hyperledger/fabric/protos/peer"
Expand All @@ -25,6 +26,9 @@ import (
const (
pkgLogID = "common/tools/configtxgen/encoder"
ordererAdminsPolicyName = "/Channel/Orderer/Admins"

msgVersion = int32(0)
epoch = 0
)

var logger = flogging.MustGetLogger(pkgLogID)
Expand Down Expand Up @@ -341,6 +345,54 @@ func NewChannelCreateConfigUpdate(channelID, consortiumName string, orgs []strin
}, nil
}

// MakeChannelCreationTransaction is a handy utility function for creating transactions for channel creation
func MakeChannelCreationTransaction(channelID string, consortium string, signer msp.SigningIdentity, orderingSystemChannelConfigGroup *cb.ConfigGroup, orgs ...string) (*cb.Envelope, error) {
newChannelConfigUpdate, err := NewChannelCreateConfigUpdate(channelID, consortium, orgs, orderingSystemChannelConfigGroup)
if err != nil {
return nil, errors.Wrap(err, "config update generation failure")
}

newConfigUpdateEnv := &cb.ConfigUpdateEnvelope{
ConfigUpdate: utils.MarshalOrPanic(newChannelConfigUpdate),
}

payloadSignatureHeader := &cb.SignatureHeader{}
if signer != nil {
sSigner, err := signer.Serialize()
if err != nil {
return nil, errors.Wrap(err, "serialization of identity failed")
}

newConfigUpdateEnv.Signatures = []*cb.ConfigSignature{&cb.ConfigSignature{
SignatureHeader: utils.MarshalOrPanic(utils.MakeSignatureHeader(sSigner, utils.CreateNonceOrPanic())),
}}

newConfigUpdateEnv.Signatures[0].Signature, err = signer.Sign(util.ConcatenateBytes(newConfigUpdateEnv.Signatures[0].SignatureHeader, newConfigUpdateEnv.ConfigUpdate))
if err != nil {
return nil, errors.Wrap(err, "signature failure over config update")
}

payloadSignatureHeader = utils.MakeSignatureHeader(sSigner, utils.CreateNonceOrPanic())
}

payloadChannelHeader := utils.MakeChannelHeader(cb.HeaderType_CONFIG_UPDATE, msgVersion, channelID, epoch)
utils.SetTxID(payloadChannelHeader, payloadSignatureHeader)
payloadHeader := utils.MakePayloadHeader(payloadChannelHeader, payloadSignatureHeader)
payload := &cb.Payload{Header: payloadHeader, Data: utils.MarshalOrPanic(newConfigUpdateEnv)}
paylBytes := utils.MarshalOrPanic(payload)

var sig []byte
if signer != nil {
// sign the payload
sig, err = signer.Sign(paylBytes)
if err != nil {
return nil, errors.Wrap(err, "signature failure over config update envelope")
}
}

return &cb.Envelope{Payload: paylBytes, Signature: sig}, nil
}

// Bootstrapper is a wrapper around NewChannelConfigGroup which can produce genesis blocks
type Bootstrapper struct {
channelGroup *cb.ConfigGroup
Expand Down
46 changes: 46 additions & 0 deletions common/tools/configtxgen/encoder/encoder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,10 @@ import (
"github.com/hyperledger/fabric/common/channelconfig"
"github.com/hyperledger/fabric/common/configtx"
"github.com/hyperledger/fabric/common/flogging"
mmsp "github.com/hyperledger/fabric/common/mocks/msp"
genesisconfig "github.com/hyperledger/fabric/common/tools/configtxgen/localconfig"
cb "github.com/hyperledger/fabric/protos/common"
"github.com/hyperledger/fabric/protos/utils"

"github.com/golang/protobuf/proto"
"github.com/stretchr/testify/assert"
Expand Down Expand Up @@ -140,3 +142,47 @@ func TestCompatability(t *testing.T) {
oldUpdate.IsolatedData = nil
assert.True(t, proto.Equal(oldUpdate, configUpdate))
}

func TestMakeChannelCreationTransactionWithSigner(t *testing.T) {
channelID := "foo"

signer, err := mmsp.NewNoopMsp().GetDefaultSigningIdentity()
assert.NoError(t, err, "Creating noop MSP")

cct, err := MakeChannelCreationTransaction(channelID, "test", signer, nil)
assert.NoError(t, err, "Making chain creation tx")

assert.NotEmpty(t, cct.Signature, "Should have signature")

payload, err := utils.UnmarshalPayload(cct.Payload)
assert.NoError(t, err, "Unmarshaling payload")

configUpdateEnv, err := configtx.UnmarshalConfigUpdateEnvelope(payload.Data)
assert.NoError(t, err, "Unmarshaling ConfigUpdateEnvelope")

assert.NotEmpty(t, configUpdateEnv.Signatures, "Should have config env sigs")

sigHeader, err := utils.GetSignatureHeader(payload.Header.SignatureHeader)
assert.NoError(t, err, "Unmarshaling SignatureHeader")
assert.NotEmpty(t, sigHeader.Creator, "Creator specified")
}

func TestMakeChannelCreationTransactionNoSigner(t *testing.T) {
channelID := "foo"
cct, err := MakeChannelCreationTransaction(channelID, "test", nil, nil)
assert.NoError(t, err, "Making chain creation tx")

assert.Empty(t, cct.Signature, "Should have empty signature")

payload, err := utils.UnmarshalPayload(cct.Payload)
assert.NoError(t, err, "Unmarshaling payload")

configUpdateEnv, err := configtx.UnmarshalConfigUpdateEnvelope(payload.Data)
assert.NoError(t, err, "Unmarshaling ConfigUpdateEnvelope")

assert.Empty(t, configUpdateEnv.Signatures, "Should have no config env sigs")

sigHeader, err := utils.GetSignatureHeader(payload.Header.SignatureHeader)
assert.NoError(t, err, "Unmarshaling SignatureHeader")
assert.Empty(t, sigHeader.Creator, "No creator specified")
}
2 changes: 1 addition & 1 deletion common/tools/configtxgen/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ func doOutputChannelCreateTx(conf *genesisconfig.Profile, channelID string, outp
for _, org := range conf.Application.Organizations {
orgNames = append(orgNames, org.Name)
}
configtx, err := channelconfig.MakeChainCreationTransaction(channelID, conf.Consortium, nil, orgNames...)
configtx, err := encoder.MakeChannelCreationTransaction(channelID, conf.Consortium, nil, nil, orgNames...)
if err != nil {
return fmt.Errorf("Error generating configtx: %s", err)
}
Expand Down
6 changes: 3 additions & 3 deletions core/common/validation/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ package validation
import (
"testing"

"github.com/hyperledger/fabric/common/channelconfig"
"github.com/hyperledger/fabric/common/tools/configtxgen/encoder"
genesisconfig "github.com/hyperledger/fabric/common/tools/configtxgen/localconfig"
"github.com/hyperledger/fabric/common/util"
cb "github.com/hyperledger/fabric/protos/common"
Expand All @@ -29,9 +29,9 @@ import (

func TestValidateConfigTx(t *testing.T) {
chainID := util.GetTestChainID()
chCrtEnv, err := channelconfig.MakeChainCreationTransaction(genesisconfig.SampleConsortiumName, chainID, signer)
chCrtEnv, err := encoder.MakeChannelCreationTransaction(genesisconfig.SampleConsortiumName, chainID, signer, nil)
if err != nil {
t.Fatalf("MakeChainCreationTransaction failed, err %s", err)
t.Fatalf("MakeChannelCreationTransaction failed, err %s", err)
return
}

Expand Down
2 changes: 1 addition & 1 deletion orderer/common/msgprocessor/systemchannel_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -670,7 +670,7 @@ func TestNewChannelConfig(t *testing.T) {

// Successful
t.Run("Success", func(t *testing.T) {
createTx, err := channelconfig.MakeChainCreationTransaction("foo", genesisconfig.SampleConsortiumName, nil, genesisconfig.SampleOrgName)
createTx, err := encoder.MakeChannelCreationTransaction("foo", genesisconfig.SampleConsortiumName, nil, nil, genesisconfig.SampleOrgName)
assert.Nil(t, err)
res, err := templator.NewChannelConfig(createTx)
assert.Nil(t, err)
Expand Down
Loading

0 comments on commit 2ef4823

Please sign in to comment.