Skip to content

Commit

Permalink
[FAB-1617] [FAB-1619] Utilize configtx.Template
Browse files Browse the repository at this point in the history
https://jira.hyperledger.org/browse/FAB-1617
https://jira.hyperledger.org/browse/FAB-1619

This changeset removes some of the duplicated orderer bootstrap code
from the protos/utils package.  It does this by introducing a
common/configtx/test package which uses a sample config template to
produce a genesis block.

This can also be used in the future for clients wishing to create a
chain creation transaction.  This is demonstrated in the
broadcast_config client.

This removes the vestigal utils methods for creating configuration
transactions via the 'oldConfiguration' mechanisms.

Change-Id: If064ed3cfcdbd2e59e8fb6c7bf72c8af160dc5f6
Signed-off-by: Jason Yellick <jyellick@us.ibm.com>
  • Loading branch information
Jason Yellick committed Jan 13, 2017
1 parent ed33fec commit ebdfbf3
Show file tree
Hide file tree
Showing 16 changed files with 131 additions and 192 deletions.
5 changes: 4 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ GOSHIM_DEPS = $(shell ./scripts/goListFiles.sh $(PKGNAME)/core/chaincode/shim |
JAVASHIM_DEPS = $(shell git ls-files core/chaincode/shim/java)
PROTOS = $(shell git ls-files *.proto | grep -v vendor)
MSP_SAMPLECONFIG = $(shell git ls-files msp/sampleconfig/*.pem)
GENESIS_SAMPLECONFIG = $(shell git ls-files common/configtx/test/*.template)
PROJECT_FILES = $(shell git ls-files)
IMAGES = peer orderer ccenv javaenv testenv runtime

Expand Down Expand Up @@ -180,7 +181,8 @@ build/image/javaenv/payload: build/javashim.tar.bz2 \
settings.gradle
build/image/peer/payload: build/docker/bin/peer \
peer/core.yaml \
build/msp-sampleconfig.tar.bz2
build/msp-sampleconfig.tar.bz2 \
build/genesis-sampleconfig.tar.bz2
build/image/orderer/payload: build/docker/bin/orderer \
orderer/orderer.yaml
build/image/testenv/payload: build/gotools.tar.bz2
Expand Down Expand Up @@ -211,6 +213,7 @@ build/goshim.tar.bz2: $(GOSHIM_DEPS)
build/javashim.tar.bz2: $(JAVASHIM_DEPS)
build/protos.tar.bz2: $(PROTOS)
build/msp-sampleconfig.tar.bz2: $(MSP_SAMPLECONFIG)
build/genesis-sampleconfig.tar.bz2: $(GENESIS_SAMPLECONFIG)

build/%.tar.bz2:
@echo "Creating $@"
Expand Down
8 changes: 4 additions & 4 deletions common/configtx/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ func NewConfigurationManager(configtx *cb.ConfigurationEnvelope, pm policies.Man

chainID, seq, err := computeChainIDAndSequence(configtx)
if err != nil {
return nil, err
return nil, fmt.Errorf("Error computing chain ID and sequence: %s", err)
}

cm := &configurationManager{
Expand All @@ -139,7 +139,7 @@ func NewConfigurationManager(configtx *cb.ConfigurationEnvelope, pm policies.Man
err = cm.Apply(configtx)

if err != nil {
return nil, err
return nil, fmt.Errorf("Error applying config transaction: %s", err)
}

return cm, nil
Expand Down Expand Up @@ -202,7 +202,7 @@ func (cm *configurationManager) processConfig(configtx *cb.ConfigurationEnvelope
err = proto.Unmarshal(entry.ConfigurationItem, config)
if err != nil {
// Note that this is not reachable by test coverage because the unmarshal error would have already been found when computing the chainID and seqNo
return nil, err
return nil, fmt.Errorf("Error unmarshaling ConfigurationItem: %s", err)
}

// Get the modification policy for this config item if one was previously specified
Expand Down Expand Up @@ -249,7 +249,7 @@ func (cm *configurationManager) processConfig(configtx *cb.ConfigurationEnvelope
// Ensure the type handler agrees the config is well formed
err = cm.handlers[config.Type].ProposeConfig(config)
if err != nil {
return nil, err
return nil, fmt.Errorf("Error proposing configuration item %s of type %d: %s", config.Key, config.Type, err)
}

configMap[config.Type][config.Key] = config
Expand Down
18 changes: 17 additions & 1 deletion common/configtx/template.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,9 +109,10 @@ func (nct *newChainTemplate) Items(chainID string) ([]*cb.SignedConfigurationIte
creationPolicy := &cb.SignedConfigurationItem{
ConfigurationItem: utils.MarshalOrPanic(&cb.ConfigurationItem{
Header: utils.MakeChainHeader(cb.HeaderType_CONFIGURATION_ITEM, msgVersion, chainID, epoch),
Type: cb.ConfigurationItem_Orderer,
Key: CreationPolicyKey,
Value: utils.MarshalOrPanic(&ab.CreationPolicy{
Policy: CreationPolicyKey,
Policy: nct.creationPolicy,
Digest: HashItems(items),
}),
}),
Expand Down Expand Up @@ -147,3 +148,18 @@ func join(sets ...[]*cb.SignedConfigurationItem) []*cb.SignedConfigurationItem {

return result
}

// MakeChainCreationTransaction is a handy utility function for creating new chain transactions using the underlying Template framework
func MakeChainCreationTransaction(creationPolicy string, chainID string, templates ...Template) (*cb.Envelope, error) {
newChainTemplate := NewChainCreationTemplate(creationPolicy, NewCompositeTemplate(templates...))
signedConfigItems, err := newChainTemplate.Items(chainID)
if err != nil {
return nil, err
}

payloadChainHeader := utils.MakeChainHeader(cb.HeaderType_CONFIGURATION_TRANSACTION, msgVersion, chainID, epoch)
payloadSignatureHeader := utils.MakeSignatureHeader(nil, utils.CreateNonceOrPanic())
payloadHeader := utils.MakePayloadHeader(payloadChainHeader, payloadSignatureHeader)
payload := &cb.Payload{Header: payloadHeader, Data: utils.MarshalOrPanic(utils.MakeConfigurationEnvelope(signedConfigItems...))}
return &cb.Envelope{Payload: utils.MarshalOrPanic(payload), Signature: nil}, nil
}
65 changes: 65 additions & 0 deletions common/configtx/test/helper.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
/*
Copyright IBM Corp. 2016 All Rights Reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package test

import (
"io/ioutil"
"os"

"github.com/hyperledger/fabric/common/configtx"
"github.com/hyperledger/fabric/common/genesis"
cb "github.com/hyperledger/fabric/protos/common"
"github.com/hyperledger/fabric/protos/utils"

"github.com/golang/protobuf/proto"
)

var template configtx.Template

var genesisFactory genesis.Factory

// XXX This is a hacky singleton, which should go away, but is an artifact of using the existing utils implementation
type MSPTemplate struct{}

func (msp MSPTemplate) Items(chainID string) ([]*cb.SignedConfigurationItem, error) {
return []*cb.SignedConfigurationItem{utils.EncodeMSP(chainID)}, nil
}

func init() {

gopath := os.Getenv("GOPATH")
data, err := ioutil.ReadFile(gopath + "/src/github.com/hyperledger/fabric/common/configtx/test/orderer.template")
if err != nil {
peerConfig := os.Getenv("PEER_CFG_PATH")
data, err = ioutil.ReadFile(peerConfig + "/common/configtx/test/orderer.template")
if err != nil {
panic(err)
}
}
templateProto := &cb.ConfigurationTemplate{}
err = proto.Unmarshal(data, templateProto)
if err != nil {
panic(err)
}

template = configtx.NewSimpleTemplate(templateProto.Items...)
genesisFactory = genesis.NewFactoryImpl(configtx.NewCompositeTemplate(MSPTemplate{}, template))
}

func MakeGenesisBlock(chainID string) (*cb.Block, error) {
return genesisFactory.Block(chainID)
}
Binary file added common/configtx/test/orderer.template
Binary file not shown.
3 changes: 2 additions & 1 deletion core/chaincode/configer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import (
"google.golang.org/grpc"

"github.com/golang/protobuf/proto"
configtxtest "github.com/hyperledger/fabric/common/configtx/test"
"github.com/hyperledger/fabric/core/chaincode/shim"
"github.com/hyperledger/fabric/core/ledger/ledgermgmt"
"github.com/hyperledger/fabric/core/peer"
Expand Down Expand Up @@ -183,7 +184,7 @@ func TestConfigerInvokeUpdateConfigBlock(t *testing.T) {

func mockConfigBlock() []byte {
var blockBytes []byte
block, err := utils.MakeConfigurationBlock("mytestchainid")
block, err := configtxtest.MakeGenesisBlock("mytestchainid")
if err != nil {
blockBytes = nil
} else {
Expand Down
4 changes: 2 additions & 2 deletions core/peer/peer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ import (
"github.com/stretchr/testify/assert"
"google.golang.org/grpc"

configtxtest "github.com/hyperledger/fabric/common/configtx/test"
"github.com/hyperledger/fabric/gossip/service"
"github.com/hyperledger/fabric/protos/utils"
)

func TestInitialize(t *testing.T) {
Expand All @@ -40,7 +40,7 @@ func TestCreateChainFromBlock(t *testing.T) {
viper.Set("peer.fileSystemPath", "/var/hyperledger/test/")
defer os.RemoveAll("/var/hyperledger/test/")
testChainID := "mytestchainid"
block, err := utils.MakeConfigurationBlock(testChainID)
block, err := configtxtest.MakeGenesisBlock(testChainID)
if err != nil {
fmt.Printf("Failed to create a config block, err %s\n", err)
t.FailNow()
Expand Down
1 change: 1 addition & 0 deletions images/peer/Dockerfile.in
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,5 @@ RUN mkdir -p /var/hyperledger/db $PEER_CFG_PATH
COPY payload/peer /usr/local/bin
COPY payload/core.yaml $PEER_CFG_PATH
ADD payload/msp-sampleconfig.tar.bz2 $PEER_CFG_PATH
ADD payload/genesis-sampleconfig.tar.bz2 $PEER_CFG_PATH
CMD peer node start
12 changes: 8 additions & 4 deletions orderer/multichain/manager_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"testing"
"time"

"github.com/hyperledger/fabric/common/configtx"
"github.com/hyperledger/fabric/orderer/common/bootstrap/provisional"
"github.com/hyperledger/fabric/orderer/localconfig"
"github.com/hyperledger/fabric/orderer/rawledger"
Expand Down Expand Up @@ -192,12 +193,15 @@ func TestNewChain(t *testing.T) {

manager := NewManagerImpl(lf, consenters)

oldGenesisTx := utils.ExtractEnvelopeOrPanic(genesisBlock, 0)
oldGenesisTxPayload := utils.ExtractPayloadOrPanic(oldGenesisTx)
oldConfigEnv := utils.UnmarshalConfigurationEnvelopeOrPanic(oldGenesisTxPayload.Data)
generator := provisional.New(conf)
items := generator.TemplateItems()
simpleTemplate := configtx.NewSimpleTemplate(items...)

newChainID := "TestNewChain"
newChainMessage := utils.ChainCreationConfigurationTransaction(provisional.AcceptAllPolicyKey, newChainID, oldConfigEnv)
newChainMessage, err := configtx.MakeChainCreationTransaction(provisional.AcceptAllPolicyKey, newChainID, simpleTemplate)
if err != nil {
t.Fatalf("Error producing configuration transaction: %s", err)
}

status := manager.ProposeChain(newChainMessage)

Expand Down
5 changes: 2 additions & 3 deletions orderer/multichain/systemchain.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ import (
"github.com/hyperledger/fabric/orderer/common/sharedconfig"
cb "github.com/hyperledger/fabric/protos/common"
ab "github.com/hyperledger/fabric/protos/orderer"
"github.com/hyperledger/fabric/protos/utils"

"github.com/golang/protobuf/proto"
)
Expand Down Expand Up @@ -160,7 +159,7 @@ func (sc *systemChain) authorize(configEnvelope *cb.ConfigurationEnvelope) cb.St
return cb.Status_BAD_REQUEST
}

if creationConfigItem.Key != utils.CreationPolicyKey {
if creationConfigItem.Key != configtx.CreationPolicyKey {
logger.Debugf("Failing to validate chain creation because first configuration item was not the CreationPolicy")
return cb.Status_BAD_REQUEST
}
Expand All @@ -181,7 +180,7 @@ func (sc *systemChain) authorize(configEnvelope *cb.ConfigurationEnvelope) cb.St
}

if !ok {
logger.Debugf("Failed to validate chain creation because chain creation policy is not authorized for chain creation")
logger.Debugf("Failed to validate chain creation because chain creation policy (%s) is not authorized for chain creation", creationPolicy.Policy)
return cb.Status_FORBIDDEN
}

Expand Down
9 changes: 5 additions & 4 deletions orderer/multichain/systemchain_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"reflect"
"testing"

"github.com/hyperledger/fabric/common/configtx"
"github.com/hyperledger/fabric/common/policies"
coreutil "github.com/hyperledger/fabric/common/util"
"github.com/hyperledger/fabric/orderer/common/bootstrap/provisional"
Expand Down Expand Up @@ -113,7 +114,7 @@ func TestGoodProposal(t *testing.T) {
ChainID: newChainID,
Type: int32(cb.HeaderType_CONFIGURATION_ITEM),
},
Key: utils.CreationPolicyKey,
Key: configtx.CreationPolicyKey,
Type: cb.ConfigurationItem_Orderer,
Value: utils.MarshalOrPanic(&ab.CreationPolicy{
Policy: provisional.AcceptAllPolicyKey,
Expand Down Expand Up @@ -169,7 +170,7 @@ func TestProposalWithBadPolicy(t *testing.T) {
mcc.ms.mpm.mp = &mockPolicy{}

chainCreateTx := &cb.ConfigurationItem{
Key: utils.CreationPolicyKey,
Key: configtx.CreationPolicyKey,
Type: cb.ConfigurationItem_Orderer,

Value: utils.MarshalOrPanic(&ab.CreationPolicy{
Expand All @@ -193,7 +194,7 @@ func TestProposalWithMissingPolicy(t *testing.T) {
mcc.ms.msc.ChainCreatorsVal = []string{provisional.AcceptAllPolicyKey}

chainCreateTx := &cb.ConfigurationItem{
Key: utils.CreationPolicyKey,
Key: configtx.CreationPolicyKey,
Type: cb.ConfigurationItem_Orderer,
Value: utils.MarshalOrPanic(&ab.CreationPolicy{
Policy: provisional.AcceptAllPolicyKey,
Expand All @@ -217,7 +218,7 @@ func TestProposalWithBadDigest(t *testing.T) {
mcc.ms.msc.ChainCreatorsVal = []string{provisional.AcceptAllPolicyKey}

chainCreateTx := &cb.ConfigurationItem{
Key: utils.CreationPolicyKey,
Key: configtx.CreationPolicyKey,
Type: cb.ConfigurationItem_Orderer,
Value: utils.MarshalOrPanic(&ab.CreationPolicy{
Policy: provisional.AcceptAllPolicyKey,
Expand Down
15 changes: 9 additions & 6 deletions orderer/sample_clients/broadcast_config/newchain.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,17 +17,20 @@ limitations under the License.
package main

import (
"github.com/hyperledger/fabric/common/configtx"
"github.com/hyperledger/fabric/orderer/common/bootstrap/provisional"
cb "github.com/hyperledger/fabric/protos/common"
"github.com/hyperledger/fabric/protos/utils"
)

func newChainRequest(consensusType, creationPolicy, newChainID string) *cb.Envelope {
conf.Genesis.OrdererType = consensusType
genesisBlock := provisional.New(conf).GenesisBlock()
oldGenesisTx := utils.ExtractEnvelopeOrPanic(genesisBlock, 0)
oldGenesisTxPayload := utils.ExtractPayloadOrPanic(oldGenesisTx)
oldConfigEnv := utils.UnmarshalConfigurationEnvelopeOrPanic(oldGenesisTxPayload.Data)
generator := provisional.New(conf)
items := generator.TemplateItems()
simpleTemplate := configtx.NewSimpleTemplate(items...)

return utils.ChainCreationConfigurationTransaction(provisional.AcceptAllPolicyKey, newChainID, oldConfigEnv)
env, err := configtx.MakeChainCreationTransaction(creationPolicy, newChainID, simpleTemplate)
if err != nil {
panic(err)
}
return env
}
4 changes: 2 additions & 2 deletions peer/node/start.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import (
"syscall"
"time"

configtxtest "github.com/hyperledger/fabric/common/configtx/test"
"github.com/hyperledger/fabric/common/util"
"github.com/hyperledger/fabric/core"
"github.com/hyperledger/fabric/core/chaincode"
Expand All @@ -39,7 +40,6 @@ import (
"github.com/hyperledger/fabric/gossip/service"
"github.com/hyperledger/fabric/peer/common"
pb "github.com/hyperledger/fabric/protos/peer"
pbutils "github.com/hyperledger/fabric/protos/utils"
"github.com/spf13/cobra"
"github.com/spf13/viper"
"google.golang.org/grpc"
Expand Down Expand Up @@ -156,7 +156,7 @@ func serve(args []string) error {
if peerDefaultChain {
chainID := util.GetTestChainID()

block, err := pbutils.MakeConfigurationBlock(chainID)
block, err := configtxtest.MakeGenesisBlock(chainID)
if nil != err {
panic(fmt.Sprintf("Unable to create genesis block for [%s] due to [%s]", chainID, err))
}
Expand Down
Loading

0 comments on commit ebdfbf3

Please sign in to comment.