Skip to content

Commit ebdfbf3

Browse files
author
Jason Yellick
committed
[FAB-1617] [FAB-1619] Utilize configtx.Template
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 <[email protected]>
1 parent ed33fec commit ebdfbf3

File tree

16 files changed

+131
-192
lines changed

16 files changed

+131
-192
lines changed

Makefile

+4-1
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ GOSHIM_DEPS = $(shell ./scripts/goListFiles.sh $(PKGNAME)/core/chaincode/shim |
6161
JAVASHIM_DEPS = $(shell git ls-files core/chaincode/shim/java)
6262
PROTOS = $(shell git ls-files *.proto | grep -v vendor)
6363
MSP_SAMPLECONFIG = $(shell git ls-files msp/sampleconfig/*.pem)
64+
GENESIS_SAMPLECONFIG = $(shell git ls-files common/configtx/test/*.template)
6465
PROJECT_FILES = $(shell git ls-files)
6566
IMAGES = peer orderer ccenv javaenv testenv runtime
6667

@@ -180,7 +181,8 @@ build/image/javaenv/payload: build/javashim.tar.bz2 \
180181
settings.gradle
181182
build/image/peer/payload: build/docker/bin/peer \
182183
peer/core.yaml \
183-
build/msp-sampleconfig.tar.bz2
184+
build/msp-sampleconfig.tar.bz2 \
185+
build/genesis-sampleconfig.tar.bz2
184186
build/image/orderer/payload: build/docker/bin/orderer \
185187
orderer/orderer.yaml
186188
build/image/testenv/payload: build/gotools.tar.bz2
@@ -211,6 +213,7 @@ build/goshim.tar.bz2: $(GOSHIM_DEPS)
211213
build/javashim.tar.bz2: $(JAVASHIM_DEPS)
212214
build/protos.tar.bz2: $(PROTOS)
213215
build/msp-sampleconfig.tar.bz2: $(MSP_SAMPLECONFIG)
216+
build/genesis-sampleconfig.tar.bz2: $(GENESIS_SAMPLECONFIG)
214217

215218
build/%.tar.bz2:
216219
@echo "Creating $@"

common/configtx/manager.go

+4-4
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ func NewConfigurationManager(configtx *cb.ConfigurationEnvelope, pm policies.Man
125125

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

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

141141
if err != nil {
142-
return nil, err
142+
return nil, fmt.Errorf("Error applying config transaction: %s", err)
143143
}
144144

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

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

255255
configMap[config.Type][config.Key] = config

common/configtx/template.go

+17-1
Original file line numberDiff line numberDiff line change
@@ -109,9 +109,10 @@ func (nct *newChainTemplate) Items(chainID string) ([]*cb.SignedConfigurationIte
109109
creationPolicy := &cb.SignedConfigurationItem{
110110
ConfigurationItem: utils.MarshalOrPanic(&cb.ConfigurationItem{
111111
Header: utils.MakeChainHeader(cb.HeaderType_CONFIGURATION_ITEM, msgVersion, chainID, epoch),
112+
Type: cb.ConfigurationItem_Orderer,
112113
Key: CreationPolicyKey,
113114
Value: utils.MarshalOrPanic(&ab.CreationPolicy{
114-
Policy: CreationPolicyKey,
115+
Policy: nct.creationPolicy,
115116
Digest: HashItems(items),
116117
}),
117118
}),
@@ -147,3 +148,18 @@ func join(sets ...[]*cb.SignedConfigurationItem) []*cb.SignedConfigurationItem {
147148

148149
return result
149150
}
151+
152+
// MakeChainCreationTransaction is a handy utility function for creating new chain transactions using the underlying Template framework
153+
func MakeChainCreationTransaction(creationPolicy string, chainID string, templates ...Template) (*cb.Envelope, error) {
154+
newChainTemplate := NewChainCreationTemplate(creationPolicy, NewCompositeTemplate(templates...))
155+
signedConfigItems, err := newChainTemplate.Items(chainID)
156+
if err != nil {
157+
return nil, err
158+
}
159+
160+
payloadChainHeader := utils.MakeChainHeader(cb.HeaderType_CONFIGURATION_TRANSACTION, msgVersion, chainID, epoch)
161+
payloadSignatureHeader := utils.MakeSignatureHeader(nil, utils.CreateNonceOrPanic())
162+
payloadHeader := utils.MakePayloadHeader(payloadChainHeader, payloadSignatureHeader)
163+
payload := &cb.Payload{Header: payloadHeader, Data: utils.MarshalOrPanic(utils.MakeConfigurationEnvelope(signedConfigItems...))}
164+
return &cb.Envelope{Payload: utils.MarshalOrPanic(payload), Signature: nil}, nil
165+
}

common/configtx/test/helper.go

+65
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
/*
2+
Copyright IBM Corp. 2016 All Rights Reserved.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package test
18+
19+
import (
20+
"io/ioutil"
21+
"os"
22+
23+
"github.com/hyperledger/fabric/common/configtx"
24+
"github.com/hyperledger/fabric/common/genesis"
25+
cb "github.com/hyperledger/fabric/protos/common"
26+
"github.com/hyperledger/fabric/protos/utils"
27+
28+
"github.com/golang/protobuf/proto"
29+
)
30+
31+
var template configtx.Template
32+
33+
var genesisFactory genesis.Factory
34+
35+
// XXX This is a hacky singleton, which should go away, but is an artifact of using the existing utils implementation
36+
type MSPTemplate struct{}
37+
38+
func (msp MSPTemplate) Items(chainID string) ([]*cb.SignedConfigurationItem, error) {
39+
return []*cb.SignedConfigurationItem{utils.EncodeMSP(chainID)}, nil
40+
}
41+
42+
func init() {
43+
44+
gopath := os.Getenv("GOPATH")
45+
data, err := ioutil.ReadFile(gopath + "/src/github.com/hyperledger/fabric/common/configtx/test/orderer.template")
46+
if err != nil {
47+
peerConfig := os.Getenv("PEER_CFG_PATH")
48+
data, err = ioutil.ReadFile(peerConfig + "/common/configtx/test/orderer.template")
49+
if err != nil {
50+
panic(err)
51+
}
52+
}
53+
templateProto := &cb.ConfigurationTemplate{}
54+
err = proto.Unmarshal(data, templateProto)
55+
if err != nil {
56+
panic(err)
57+
}
58+
59+
template = configtx.NewSimpleTemplate(templateProto.Items...)
60+
genesisFactory = genesis.NewFactoryImpl(configtx.NewCompositeTemplate(MSPTemplate{}, template))
61+
}
62+
63+
func MakeGenesisBlock(chainID string) (*cb.Block, error) {
64+
return genesisFactory.Block(chainID)
65+
}

common/configtx/test/orderer.template

629 Bytes
Binary file not shown.

core/chaincode/configer_test.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ import (
2727
"google.golang.org/grpc"
2828

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

184185
func mockConfigBlock() []byte {
185186
var blockBytes []byte
186-
block, err := utils.MakeConfigurationBlock("mytestchainid")
187+
block, err := configtxtest.MakeGenesisBlock("mytestchainid")
187188
if err != nil {
188189
blockBytes = nil
189190
} else {

core/peer/peer_test.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,8 @@ import (
2626
"github.com/stretchr/testify/assert"
2727
"google.golang.org/grpc"
2828

29+
configtxtest "github.com/hyperledger/fabric/common/configtx/test"
2930
"github.com/hyperledger/fabric/gossip/service"
30-
"github.com/hyperledger/fabric/protos/utils"
3131
)
3232

3333
func TestInitialize(t *testing.T) {
@@ -40,7 +40,7 @@ func TestCreateChainFromBlock(t *testing.T) {
4040
viper.Set("peer.fileSystemPath", "/var/hyperledger/test/")
4141
defer os.RemoveAll("/var/hyperledger/test/")
4242
testChainID := "mytestchainid"
43-
block, err := utils.MakeConfigurationBlock(testChainID)
43+
block, err := configtxtest.MakeGenesisBlock(testChainID)
4444
if err != nil {
4545
fmt.Printf("Failed to create a config block, err %s\n", err)
4646
t.FailNow()

images/peer/Dockerfile.in

+1
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,5 @@ RUN mkdir -p /var/hyperledger/db $PEER_CFG_PATH
44
COPY payload/peer /usr/local/bin
55
COPY payload/core.yaml $PEER_CFG_PATH
66
ADD payload/msp-sampleconfig.tar.bz2 $PEER_CFG_PATH
7+
ADD payload/genesis-sampleconfig.tar.bz2 $PEER_CFG_PATH
78
CMD peer node start

orderer/multichain/manager_test.go

+8-4
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import (
2121
"testing"
2222
"time"
2323

24+
"github.com/hyperledger/fabric/common/configtx"
2425
"github.com/hyperledger/fabric/orderer/common/bootstrap/provisional"
2526
"github.com/hyperledger/fabric/orderer/localconfig"
2627
"github.com/hyperledger/fabric/orderer/rawledger"
@@ -192,12 +193,15 @@ func TestNewChain(t *testing.T) {
192193

193194
manager := NewManagerImpl(lf, consenters)
194195

195-
oldGenesisTx := utils.ExtractEnvelopeOrPanic(genesisBlock, 0)
196-
oldGenesisTxPayload := utils.ExtractPayloadOrPanic(oldGenesisTx)
197-
oldConfigEnv := utils.UnmarshalConfigurationEnvelopeOrPanic(oldGenesisTxPayload.Data)
196+
generator := provisional.New(conf)
197+
items := generator.TemplateItems()
198+
simpleTemplate := configtx.NewSimpleTemplate(items...)
198199

199200
newChainID := "TestNewChain"
200-
newChainMessage := utils.ChainCreationConfigurationTransaction(provisional.AcceptAllPolicyKey, newChainID, oldConfigEnv)
201+
newChainMessage, err := configtx.MakeChainCreationTransaction(provisional.AcceptAllPolicyKey, newChainID, simpleTemplate)
202+
if err != nil {
203+
t.Fatalf("Error producing configuration transaction: %s", err)
204+
}
201205

202206
status := manager.ProposeChain(newChainMessage)
203207

orderer/multichain/systemchain.go

+2-3
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@ import (
2525
"github.com/hyperledger/fabric/orderer/common/sharedconfig"
2626
cb "github.com/hyperledger/fabric/protos/common"
2727
ab "github.com/hyperledger/fabric/protos/orderer"
28-
"github.com/hyperledger/fabric/protos/utils"
2928

3029
"github.com/golang/protobuf/proto"
3130
)
@@ -160,7 +159,7 @@ func (sc *systemChain) authorize(configEnvelope *cb.ConfigurationEnvelope) cb.St
160159
return cb.Status_BAD_REQUEST
161160
}
162161

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

183182
if !ok {
184-
logger.Debugf("Failed to validate chain creation because chain creation policy is not authorized for chain creation")
183+
logger.Debugf("Failed to validate chain creation because chain creation policy (%s) is not authorized for chain creation", creationPolicy.Policy)
185184
return cb.Status_FORBIDDEN
186185
}
187186

orderer/multichain/systemchain_test.go

+5-4
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import (
2020
"reflect"
2121
"testing"
2222

23+
"github.com/hyperledger/fabric/common/configtx"
2324
"github.com/hyperledger/fabric/common/policies"
2425
coreutil "github.com/hyperledger/fabric/common/util"
2526
"github.com/hyperledger/fabric/orderer/common/bootstrap/provisional"
@@ -113,7 +114,7 @@ func TestGoodProposal(t *testing.T) {
113114
ChainID: newChainID,
114115
Type: int32(cb.HeaderType_CONFIGURATION_ITEM),
115116
},
116-
Key: utils.CreationPolicyKey,
117+
Key: configtx.CreationPolicyKey,
117118
Type: cb.ConfigurationItem_Orderer,
118119
Value: utils.MarshalOrPanic(&ab.CreationPolicy{
119120
Policy: provisional.AcceptAllPolicyKey,
@@ -169,7 +170,7 @@ func TestProposalWithBadPolicy(t *testing.T) {
169170
mcc.ms.mpm.mp = &mockPolicy{}
170171

171172
chainCreateTx := &cb.ConfigurationItem{
172-
Key: utils.CreationPolicyKey,
173+
Key: configtx.CreationPolicyKey,
173174
Type: cb.ConfigurationItem_Orderer,
174175

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

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

219220
chainCreateTx := &cb.ConfigurationItem{
220-
Key: utils.CreationPolicyKey,
221+
Key: configtx.CreationPolicyKey,
221222
Type: cb.ConfigurationItem_Orderer,
222223
Value: utils.MarshalOrPanic(&ab.CreationPolicy{
223224
Policy: provisional.AcceptAllPolicyKey,

orderer/sample_clients/broadcast_config/newchain.go

+9-6
Original file line numberDiff line numberDiff line change
@@ -17,17 +17,20 @@ limitations under the License.
1717
package main
1818

1919
import (
20+
"github.com/hyperledger/fabric/common/configtx"
2021
"github.com/hyperledger/fabric/orderer/common/bootstrap/provisional"
2122
cb "github.com/hyperledger/fabric/protos/common"
22-
"github.com/hyperledger/fabric/protos/utils"
2323
)
2424

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

32-
return utils.ChainCreationConfigurationTransaction(provisional.AcceptAllPolicyKey, newChainID, oldConfigEnv)
31+
env, err := configtx.MakeChainCreationTransaction(creationPolicy, newChainID, simpleTemplate)
32+
if err != nil {
33+
panic(err)
34+
}
35+
return env
3336
}

peer/node/start.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ import (
2727
"syscall"
2828
"time"
2929

30+
configtxtest "github.com/hyperledger/fabric/common/configtx/test"
3031
"github.com/hyperledger/fabric/common/util"
3132
"github.com/hyperledger/fabric/core"
3233
"github.com/hyperledger/fabric/core/chaincode"
@@ -39,7 +40,6 @@ import (
3940
"github.com/hyperledger/fabric/gossip/service"
4041
"github.com/hyperledger/fabric/peer/common"
4142
pb "github.com/hyperledger/fabric/protos/peer"
42-
pbutils "github.com/hyperledger/fabric/protos/utils"
4343
"github.com/spf13/cobra"
4444
"github.com/spf13/viper"
4545
"google.golang.org/grpc"
@@ -156,7 +156,7 @@ func serve(args []string) error {
156156
if peerDefaultChain {
157157
chainID := util.GetTestChainID()
158158

159-
block, err := pbutils.MakeConfigurationBlock(chainID)
159+
block, err := configtxtest.MakeGenesisBlock(chainID)
160160
if nil != err {
161161
panic(fmt.Sprintf("Unable to create genesis block for [%s] due to [%s]", chainID, err))
162162
}

0 commit comments

Comments
 (0)