Skip to content

Commit

Permalink
[FAB-6140] Remove the provisional config encoder
Browse files Browse the repository at this point in the history
With the introduction of FAB-6139 a new more readable and flexible
config encoder is available.  This means all existing usage of the old
deprecated provisional encoder should be removed.  This CR does that.

Change-Id: If64e0e7dc920c709cc39a111547c61c3ae4dc3f0
Signed-off-by: Jason Yellick <jyellick@us.ibm.com>
  • Loading branch information
Jason Yellick committed Oct 13, 2017
1 parent 8a52d63 commit 95b95e9
Show file tree
Hide file tree
Showing 36 changed files with 185 additions and 622 deletions.
4 changes: 2 additions & 2 deletions common/channelconfig/realconfig_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ import (
"testing"

newchannelconfig "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/tools/configtxgen/provisional"
"github.com/hyperledger/fabric/protos/utils"

"github.com/stretchr/testify/assert"
Expand All @@ -34,7 +34,7 @@ func TestWithRealConfigtx(t *testing.T) {
Port: 7,
},
}
gb := provisional.New(conf).GenesisBlockForChannel("foo")
gb := encoder.New(conf).GenesisBlockForChannel("foo")
env := utils.ExtractEnvelopeOrPanic(gb, 0)
_, err := newchannelconfig.NewBundleFromEnvelope(env)
assert.NoError(t, err)
Expand Down
83 changes: 33 additions & 50 deletions common/configtx/test/helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,76 +8,59 @@ package test

import (
"github.com/hyperledger/fabric/common/channelconfig"
"github.com/hyperledger/fabric/common/configtx"
"github.com/hyperledger/fabric/common/flogging"
"github.com/hyperledger/fabric/common/genesis"
"github.com/hyperledger/fabric/common/tools/configtxgen/encoder"
genesisconfig "github.com/hyperledger/fabric/common/tools/configtxgen/localconfig"
"github.com/hyperledger/fabric/common/tools/configtxgen/provisional"
cf "github.com/hyperledger/fabric/core/config"
"github.com/hyperledger/fabric/msp"
cb "github.com/hyperledger/fabric/protos/common"
mspproto "github.com/hyperledger/fabric/protos/msp"
pb "github.com/hyperledger/fabric/protos/peer"
"github.com/hyperledger/fabric/protos/utils"
)

var logger = flogging.MustGetLogger("common/configtx/test")

const (
// AcceptAllPolicyKey is the key of the AcceptAllPolicy.
AcceptAllPolicyKey = "AcceptAllPolicy"
)

func getConfigDir() string {
mspDir, err := cf.GetDevMspDir()
// MakeGenesisBlock creates a genesis block using the test templates for the given chainID
func MakeGenesisBlock(chainID string) (*cb.Block, error) {
profile := genesisconfig.Load(genesisconfig.SampleDevModeSoloProfile)
channelGroup, err := encoder.NewChannelGroup(profile)
if err != nil {
logger.Panicf("Could not find genesis.yaml, try setting GOPATH correctly")
logger.Panicf("Error creating channel config: %s", err)
}

return mspDir
}

// MakeGenesisBlock creates a genesis block using the test templates for the given chainID
func MakeGenesisBlock(chainID string) (*cb.Block, error) {
return genesis.NewFactoryImpl(CompositeTemplate()).Block(chainID)
return genesis.NewFactoryImpl(channelGroup).Block(chainID)
}

// MakeGenesisBlockWithMSPs creates a genesis block using the MSPs provided for the given chainID
func MakeGenesisBlockFromMSPs(chainID string, appMSPConf, ordererMSPConf *mspproto.MSPConfig,
appOrgID, ordererOrgID string) (*cb.Block, error) {
appOrgTemplate := configtx.NewSimpleTemplate(channelconfig.TemplateGroupMSP([]string{channelconfig.ApplicationGroupKey, appOrgID}, appMSPConf))
ordererOrgTemplate := configtx.NewSimpleTemplate(channelconfig.TemplateGroupMSP([]string{channelconfig.OrdererGroupKey, ordererOrgID}, ordererMSPConf))
composite := configtx.NewCompositeTemplate(OrdererTemplate(), appOrgTemplate, ApplicationOrgTemplate(), ordererOrgTemplate)
return genesis.NewFactoryImpl(composite).Block(chainID)
}

// OrderererTemplate returns the test orderer template
func OrdererTemplate() configtx.Template {
genConf := genesisconfig.Load(genesisconfig.SampleInsecureSoloProfile)
return provisional.New(genConf).ChannelTemplate()
}

// sampleOrgID apparently _must_ be set to DEFAULT or things break
// Beware when changing!
const sampleOrgID = "DEFAULT"

// ApplicationOrgTemplate returns the SAMPLE org with MSP template
func ApplicationOrgTemplate() configtx.Template {
mspConf, err := msp.GetLocalMspConfig(getConfigDir(), nil, sampleOrgID)
profile := genesisconfig.Load(genesisconfig.SampleDevModeSoloProfile)
profile.Orderer.Organizations = nil
channelGroup, err := encoder.NewChannelGroup(profile)
if err != nil {
logger.Panicf("Could not load sample MSP config: %s", err)
logger.Panicf("Error creating channel config: %s", err)
}
return configtx.NewSimpleTemplate(channelconfig.TemplateGroupMSP([]string{channelconfig.ApplicationGroupKey, sampleOrgID}, mspConf))
}

// OrdererOrgTemplate returns the SAMPLE org with MSP template
func OrdererOrgTemplate() configtx.Template {
mspConf, err := msp.GetLocalMspConfig(getConfigDir(), nil, sampleOrgID)
if err != nil {
logger.Panicf("Could not load sample MSP config: %s", err)
ordererOrg := cb.NewConfigGroup()
ordererOrg.ModPolicy = channelconfig.AdminsPolicyKey
ordererOrg.Values[channelconfig.MSPKey] = &cb.ConfigValue{
Value: utils.MarshalOrPanic(channelconfig.MSPValue(ordererMSPConf).Value()),
ModPolicy: channelconfig.AdminsPolicyKey,
}
return configtx.NewSimpleTemplate(channelconfig.TemplateGroupMSP([]string{channelconfig.OrdererGroupKey, sampleOrgID}, mspConf))
}

// CompositeTemplate returns the composite template of peer, orderer, and MSP
func CompositeTemplate() configtx.Template {
return configtx.NewCompositeTemplate(OrdererTemplate(), ApplicationOrgTemplate(), OrdererOrgTemplate())
applicationOrg := cb.NewConfigGroup()
applicationOrg.ModPolicy = channelconfig.AdminsPolicyKey
applicationOrg.Values[channelconfig.MSPKey] = &cb.ConfigValue{
Value: utils.MarshalOrPanic(channelconfig.MSPValue(appMSPConf).Value()),
ModPolicy: channelconfig.AdminsPolicyKey,
}
applicationOrg.Values[channelconfig.AnchorPeersKey] = &cb.ConfigValue{
Value: utils.MarshalOrPanic(channelconfig.AnchorPeersValue([]*pb.AnchorPeer{}).Value()),
ModPolicy: channelconfig.AdminsPolicyKey,
}

channelGroup.Groups[channelconfig.OrdererGroupKey].Groups[ordererOrgID] = ordererOrg
channelGroup.Groups[channelconfig.ApplicationGroupKey].Groups[ordererOrgID] = applicationOrg

return genesis.NewFactoryImpl(channelGroup).Block(chainID)
}
82 changes: 0 additions & 82 deletions common/configtx/test/helper_test.go

This file was deleted.

21 changes: 4 additions & 17 deletions common/genesis/genesis.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,6 @@ limitations under the License.
package genesis

import (
"github.com/golang/protobuf/proto"
"github.com/hyperledger/fabric/common/configtx"
cb "github.com/hyperledger/fabric/protos/common"
"github.com/hyperledger/fabric/protos/utils"
)
Expand All @@ -37,32 +35,21 @@ type Factory interface {
}

type factory struct {
template configtx.Template
channelGroup *cb.ConfigGroup
}

// NewFactoryImpl creates a new Factory.
func NewFactoryImpl(template configtx.Template) Factory {
return &factory{template: template}
func NewFactoryImpl(channelGroup *cb.ConfigGroup) Factory {
return &factory{channelGroup: channelGroup}
}

// Block constructs and returns a genesis block for a given channel ID.
func (f *factory) Block(channelID string) (*cb.Block, error) {
configEnv, err := f.template.Envelope(channelID)
if err != nil {
return nil, err
}

configUpdate := &cb.ConfigUpdate{}
err = proto.Unmarshal(configEnv.ConfigUpdate, configUpdate)
if err != nil {
return nil, err
}

payloadChannelHeader := utils.MakeChannelHeader(cb.HeaderType_CONFIG, msgVersion, channelID, epoch)
payloadSignatureHeader := utils.MakeSignatureHeader(nil, utils.CreateNonceOrPanic())
utils.SetTxID(payloadChannelHeader, payloadSignatureHeader)
payloadHeader := utils.MakePayloadHeader(payloadChannelHeader, payloadSignatureHeader)
payload := &cb.Payload{Header: payloadHeader, Data: utils.MarshalOrPanic(&cb.ConfigEnvelope{Config: &cb.Config{ChannelGroup: configUpdate.WriteSet}})}
payload := &cb.Payload{Header: payloadHeader, Data: utils.MarshalOrPanic(&cb.ConfigEnvelope{Config: &cb.Config{ChannelGroup: f.channelGroup}})}
envelope := &cb.Envelope{Payload: utils.MarshalOrPanic(payload), Signature: nil}

block := cb.NewBlock(0, nil)
Expand Down
6 changes: 3 additions & 3 deletions common/genesis/genesis_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,19 +19,19 @@ package genesis
import (
"testing"

"github.com/hyperledger/fabric/common/configtx"
cb "github.com/hyperledger/fabric/protos/common"
"github.com/hyperledger/fabric/protos/utils"
"github.com/stretchr/testify/assert"
)

func TestBasicSanity(t *testing.T) {
impl := NewFactoryImpl(configtx.NewSimpleTemplate())
impl := NewFactoryImpl(cb.NewConfigGroup())
_, err := impl.Block("testchainid")
assert.NoError(t, err, "Basic sanity fails")
}

func TestForTransactionID(t *testing.T) {
impl := NewFactoryImpl(configtx.NewSimpleTemplate())
impl := NewFactoryImpl(cb.NewConfigGroup())
block, _ := impl.Block("testchainid")
configEnv, _ := utils.ExtractEnvelope(block, 0)
configEnvPayload, _ := utils.ExtractPayload(configEnv)
Expand Down
35 changes: 35 additions & 0 deletions common/tools/configtxgen/encoder/encoder.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"github.com/hyperledger/fabric/common/cauthdsl"
"github.com/hyperledger/fabric/common/channelconfig"
"github.com/hyperledger/fabric/common/flogging"
"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/msp"
Expand Down Expand Up @@ -272,3 +273,37 @@ func NewConsortiumGroup(conf *genesisconfig.Consortium) (*cb.ConfigGroup, error)
consortiumGroup.ModPolicy = ordererAdminsPolicyName
return consortiumGroup, nil
}

// Bootstrapper is a wrapper around NewChannelConfigGroup which can produce genesis blocks
type Bootstrapper struct {
channelGroup *cb.ConfigGroup
}

// New creates a new Bootstrapper for generating genesis blocks
func New(config *genesisconfig.Profile) *Bootstrapper {
channelGroup, err := NewChannelGroup(config)
if err != nil {
logger.Panicf("Error creating channel group: %s", err)
}
return &Bootstrapper{
channelGroup: channelGroup,
}
}

// GenesisBlock produces a genesis block for the default test chain id
func (bs *Bootstrapper) GenesisBlock() *cb.Block {
block, err := genesis.NewFactoryImpl(bs.channelGroup).Block(genesisconfig.TestChainID)
if err != nil {
logger.Panicf("Error creating genesis block from channel group: %s", err)
}
return block
}

// GenesisBlockForChannel produces a genesis block for a given channel ID
func (bs *Bootstrapper) GenesisBlockForChannel(channelID string) *cb.Block {
block, err := genesis.NewFactoryImpl(bs.channelGroup).Block(channelID)
if err != nil {
logger.Panicf("Error creating genesis block from channel group: %s", err)
}
return block
}
42 changes: 13 additions & 29 deletions common/tools/configtxgen/encoder/encoder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,8 @@ import (
"github.com/hyperledger/fabric/common/channelconfig"
"github.com/hyperledger/fabric/common/flogging"
genesisconfig "github.com/hyperledger/fabric/common/tools/configtxgen/localconfig"
"github.com/hyperledger/fabric/common/tools/configtxgen/provisional"
cb "github.com/hyperledger/fabric/protos/common"
"github.com/hyperledger/fabric/protos/utils"

"github.com/golang/protobuf/proto"
"github.com/stretchr/testify/assert"
)

Expand All @@ -41,22 +38,6 @@ func hasModPolicySet(t *testing.T, groupName string, cg *cb.ConfigGroup) {
}

func TestConfigParsing(t *testing.T) {
config := genesisconfig.Load(genesisconfig.SampleDevModeSoloProfile)
group, err := NewChannelGroup(config)
assert.NoError(t, err)
assert.NotNil(t, group)

_, err = channelconfig.NewBundle("test", &cb.Config{
ChannelGroup: group,
})
assert.NoError(t, err)

hasModPolicySet(t, "Channel", group)
}

// This test will be removed with the legacy provisional package, but demonstrates
// that the old and new encoders produce identical output
func TestEquivalentParsing(t *testing.T) {
for _, profile := range []string{
genesisconfig.SampleInsecureSoloProfile,
genesisconfig.SampleSingleMSPSoloProfile,
Expand All @@ -65,15 +46,18 @@ func TestEquivalentParsing(t *testing.T) {
genesisconfig.SampleSingleMSPKafkaProfile,
genesisconfig.SampleDevModeKafkaProfile,
} {
config := genesisconfig.Load(profile)
group, err := NewChannelGroup(config)
assert.NoError(t, err)
assert.NotNil(t, group)

gb := provisional.New(config).GenesisBlockForChannel("foo")
env := utils.ExtractEnvelopeOrPanic(gb, 0)
configEnv := &cb.ConfigEnvelope{}
utils.UnmarshalEnvelopeOfType(env, cb.HeaderType_CONFIG, configEnv)
assert.True(t, proto.Equal(configEnv.Config.ChannelGroup, group))
t.Run(profile, func(t *testing.T) {
config := genesisconfig.Load(profile)
group, err := NewChannelGroup(config)
assert.NoError(t, err)
assert.NotNil(t, group)

_, err = channelconfig.NewBundle("test", &cb.Config{
ChannelGroup: group,
})
assert.NoError(t, err)

hasModPolicySet(t, "Channel", group)
})
}
}
Loading

0 comments on commit 95b95e9

Please sign in to comment.