Skip to content

Commit 676ad37

Browse files
committed
Move configurationItem utilities to protos/utils
Change-Id: Ifbb990db6869a37e212149d97e2eddd2cb03ffeb Signed-off-by: tuand27613 <[email protected]>
1 parent cea4adf commit 676ad37

File tree

8 files changed

+121
-96
lines changed

8 files changed

+121
-96
lines changed

orderer/common/bootstrap/static/static.go

+21-21
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,9 @@ import (
2121
"github.com/hyperledger/fabric/orderer/common/cauthdsl"
2222
"github.com/hyperledger/fabric/orderer/common/configtx"
2323
"github.com/hyperledger/fabric/orderer/common/sharedconfig"
24-
"github.com/hyperledger/fabric/orderer/common/util"
2524
cb "github.com/hyperledger/fabric/protos/common"
2625
ab "github.com/hyperledger/fabric/protos/orderer"
26+
"github.com/hyperledger/fabric/protos/utils"
2727
)
2828

2929
// TestChainID is the default chain ID which is used by all statically bootstrapped networks
@@ -60,51 +60,51 @@ func New() bootstrap.Helper {
6060

6161
func (b *bootstrapper) encodeConsensusType() *cb.SignedConfigurationItem {
6262
configItemKey := sharedconfig.ConsensusTypeKey
63-
configItemValue := util.MarshalOrPanic(&ab.ConsensusType{Type: b.consensusType})
63+
configItemValue := utils.MarshalOrPanic(&ab.ConsensusType{Type: b.consensusType})
6464
modPolicy := configtx.DefaultModificationPolicyID
6565

66-
configItemChainHeader := util.MakeChainHeader(cb.HeaderType_CONFIGURATION_ITEM, msgVersion, b.chainID, b.epoch)
67-
configItem := util.MakeConfigurationItem(configItemChainHeader, cb.ConfigurationItem_Orderer, b.lastModified, modPolicy, configItemKey, configItemValue)
68-
return &cb.SignedConfigurationItem{ConfigurationItem: util.MarshalOrPanic(configItem), Signatures: nil}
66+
configItemChainHeader := utils.MakeChainHeader(cb.HeaderType_CONFIGURATION_ITEM, msgVersion, b.chainID, b.epoch)
67+
configItem := utils.MakeConfigurationItem(configItemChainHeader, cb.ConfigurationItem_Orderer, b.lastModified, modPolicy, configItemKey, configItemValue)
68+
return &cb.SignedConfigurationItem{ConfigurationItem: utils.MarshalOrPanic(configItem), Signatures: nil}
6969
}
7070

7171
func (b *bootstrapper) encodeBatchSize() *cb.SignedConfigurationItem {
7272
configItemKey := sharedconfig.BatchSizeKey
73-
configItemValue := util.MarshalOrPanic(&ab.BatchSize{Messages: b.batchSize})
73+
configItemValue := utils.MarshalOrPanic(&ab.BatchSize{Messages: b.batchSize})
7474
modPolicy := configtx.DefaultModificationPolicyID
7575

76-
configItemChainHeader := util.MakeChainHeader(cb.HeaderType_CONFIGURATION_ITEM, msgVersion, b.chainID, b.epoch)
77-
configItem := util.MakeConfigurationItem(configItemChainHeader, cb.ConfigurationItem_Orderer, b.lastModified, modPolicy, configItemKey, configItemValue)
78-
return &cb.SignedConfigurationItem{ConfigurationItem: util.MarshalOrPanic(configItem), Signatures: nil}
76+
configItemChainHeader := utils.MakeChainHeader(cb.HeaderType_CONFIGURATION_ITEM, msgVersion, b.chainID, b.epoch)
77+
configItem := utils.MakeConfigurationItem(configItemChainHeader, cb.ConfigurationItem_Orderer, b.lastModified, modPolicy, configItemKey, configItemValue)
78+
return &cb.SignedConfigurationItem{ConfigurationItem: utils.MarshalOrPanic(configItem), Signatures: nil}
7979
}
8080

8181
func (b *bootstrapper) lockDefaultModificationPolicy() *cb.SignedConfigurationItem {
8282
// Lock down the default modification policy to prevent any further policy modifications
8383
configItemKey := configtx.DefaultModificationPolicyID
84-
configItemValue := util.MarshalOrPanic(util.MakePolicyOrPanic(cauthdsl.RejectAllPolicy))
84+
configItemValue := utils.MarshalOrPanic(utils.MakePolicyOrPanic(cauthdsl.RejectAllPolicy))
8585
modPolicy := configtx.DefaultModificationPolicyID
8686

87-
configItemChainHeader := util.MakeChainHeader(cb.HeaderType_CONFIGURATION_ITEM, msgVersion, b.chainID, b.epoch)
88-
configItem := util.MakeConfigurationItem(configItemChainHeader, cb.ConfigurationItem_Policy, b.lastModified, modPolicy, configItemKey, configItemValue)
89-
return &cb.SignedConfigurationItem{ConfigurationItem: util.MarshalOrPanic(configItem), Signatures: nil}
87+
configItemChainHeader := utils.MakeChainHeader(cb.HeaderType_CONFIGURATION_ITEM, msgVersion, b.chainID, b.epoch)
88+
configItem := utils.MakeConfigurationItem(configItemChainHeader, cb.ConfigurationItem_Policy, b.lastModified, modPolicy, configItemKey, configItemValue)
89+
return &cb.SignedConfigurationItem{ConfigurationItem: utils.MarshalOrPanic(configItem), Signatures: nil}
9090
}
9191

9292
// GenesisBlock returns the genesis block to be used for bootstrapping
9393
func (b *bootstrapper) GenesisBlock() (*cb.Block, error) {
94-
configItemChainHeader := util.MakeChainHeader(cb.HeaderType_CONFIGURATION_ITEM, msgVersion, b.chainID, b.epoch)
94+
configItemChainHeader := utils.MakeChainHeader(cb.HeaderType_CONFIGURATION_ITEM, msgVersion, b.chainID, b.epoch)
9595

96-
configEnvelope := util.MakeConfigurationEnvelope(
96+
configEnvelope := utils.MakeConfigurationEnvelope(
9797
b.encodeConsensusType(),
9898
b.encodeBatchSize(),
9999
b.lockDefaultModificationPolicy(),
100100
)
101-
payloadChainHeader := util.MakeChainHeader(cb.HeaderType_CONFIGURATION_TRANSACTION, configItemChainHeader.Version, b.chainID, b.epoch)
102-
payloadSignatureHeader := util.MakeSignatureHeader(nil, util.CreateNonceOrPanic())
103-
payloadHeader := util.MakePayloadHeader(payloadChainHeader, payloadSignatureHeader)
104-
payload := &cb.Payload{Header: payloadHeader, Data: util.MarshalOrPanic(configEnvelope)}
105-
envelope := &cb.Envelope{Payload: util.MarshalOrPanic(payload), Signature: nil}
101+
payloadChainHeader := utils.MakeChainHeader(cb.HeaderType_CONFIGURATION_TRANSACTION, configItemChainHeader.Version, b.chainID, b.epoch)
102+
payloadSignatureHeader := utils.MakeSignatureHeader(nil, utils.CreateNonceOrPanic())
103+
payloadHeader := utils.MakePayloadHeader(payloadChainHeader, payloadSignatureHeader)
104+
payload := &cb.Payload{Header: payloadHeader, Data: utils.MarshalOrPanic(configEnvelope)}
105+
envelope := &cb.Envelope{Payload: utils.MarshalOrPanic(payload), Signature: nil}
106106

107-
blockData := &cb.BlockData{Data: [][]byte{util.MarshalOrPanic(envelope)}}
107+
blockData := &cb.BlockData{Data: [][]byte{utils.MarshalOrPanic(envelope)}}
108108

109109
return &cb.Block{
110110
Header: &cb.BlockHeader{

orderer/common/bootstrap/static/static_test.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,8 @@ import (
2323
"github.com/golang/protobuf/proto"
2424
"github.com/hyperledger/fabric/orderer/common/cauthdsl"
2525
"github.com/hyperledger/fabric/orderer/common/configtx"
26-
"github.com/hyperledger/fabric/orderer/common/util"
2726
cb "github.com/hyperledger/fabric/protos/common"
27+
"github.com/hyperledger/fabric/protos/utils"
2828
)
2929

3030
func TestGenesisBlockCreation(t *testing.T) {
@@ -73,14 +73,14 @@ func TestGenesisBlockData(t *testing.T) {
7373
t.Fatalf("Expected genesis block data length %d, got %d", expectedBlockDataLength, len(genesisBlock.Data.Data))
7474
}
7575

76-
envelope := util.ExtractEnvelopeOrPanic(genesisBlock, 0)
76+
envelope := utils.ExtractEnvelopeOrPanic(genesisBlock, 0)
7777

7878
envelopeSignature := envelope.Signature
7979
if !bytes.Equal(envelopeSignature, nil) {
8080
t.Fatalf("Expected envelope signature to be nil, got %x", envelopeSignature)
8181
}
8282

83-
payload := util.ExtractPayloadOrPanic(envelope)
83+
payload := utils.ExtractPayloadOrPanic(envelope)
8484

8585
signatureHeader := payload.Header.SignatureHeader
8686
if !bytes.Equal(signatureHeader.Creator, nil) {

orderer/common/sharedconfig/sharedconfig_test.go

+6-6
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,9 @@ import (
2121
"os/exec"
2222
"testing"
2323

24-
"github.com/hyperledger/fabric/orderer/common/util"
2524
cb "github.com/hyperledger/fabric/protos/common"
2625
ab "github.com/hyperledger/fabric/protos/orderer"
26+
"github.com/hyperledger/fabric/protos/utils"
2727
)
2828

2929
func doesFuncCrash(crasher func(), test string) bool {
@@ -85,12 +85,12 @@ func TestConsensusType(t *testing.T) {
8585
validMessage := &cb.ConfigurationItem{
8686
Type: cb.ConfigurationItem_Orderer,
8787
Key: ConsensusTypeKey,
88-
Value: util.MarshalOrPanic(&ab.ConsensusType{Type: endType}),
88+
Value: utils.MarshalOrPanic(&ab.ConsensusType{Type: endType}),
8989
}
9090
otherValidMessage := &cb.ConfigurationItem{
9191
Type: cb.ConfigurationItem_Orderer,
9292
Key: ConsensusTypeKey,
93-
Value: util.MarshalOrPanic(&ab.ConsensusType{Type: "bar"}),
93+
Value: utils.MarshalOrPanic(&ab.ConsensusType{Type: "bar"}),
9494
}
9595
m := NewManagerImpl()
9696
m.BeginConfig()
@@ -137,17 +137,17 @@ func TestBatchSize(t *testing.T) {
137137
zeroBatchSize := &cb.ConfigurationItem{
138138
Type: cb.ConfigurationItem_Orderer,
139139
Key: BatchSizeKey,
140-
Value: util.MarshalOrPanic(&ab.BatchSize{Messages: 0}),
140+
Value: utils.MarshalOrPanic(&ab.BatchSize{Messages: 0}),
141141
}
142142
negativeBatchSize := &cb.ConfigurationItem{
143143
Type: cb.ConfigurationItem_Orderer,
144144
Key: BatchSizeKey,
145-
Value: util.MarshalOrPanic(&ab.BatchSize{Messages: -1}),
145+
Value: utils.MarshalOrPanic(&ab.BatchSize{Messages: -1}),
146146
}
147147
validMessage := &cb.ConfigurationItem{
148148
Type: cb.ConfigurationItem_Orderer,
149149
Key: BatchSizeKey,
150-
Value: util.MarshalOrPanic(&ab.BatchSize{Messages: int32(endBatchSize)}),
150+
Value: utils.MarshalOrPanic(&ab.BatchSize{Messages: int32(endBatchSize)}),
151151
}
152152
m := NewManagerImpl()
153153
m.BeginConfig()

orderer/multichain/manager_test.go

+6-6
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,10 @@ import (
2323
"time"
2424

2525
"github.com/hyperledger/fabric/orderer/common/bootstrap/static"
26-
"github.com/hyperledger/fabric/orderer/common/util"
2726
"github.com/hyperledger/fabric/orderer/rawledger/ramledger"
2827
cb "github.com/hyperledger/fabric/protos/common"
2928
ab "github.com/hyperledger/fabric/protos/orderer"
29+
"github.com/hyperledger/fabric/protos/utils"
3030
)
3131

3232
var genesisBlock *cb.Block
@@ -50,7 +50,7 @@ func makeNormalTx(chainID string, i int) *cb.Envelope {
5050
Data: []byte(fmt.Sprintf("%d", i)),
5151
}
5252
return &cb.Envelope{
53-
Payload: util.MarshalOrPanic(payload),
53+
Payload: utils.MarshalOrPanic(payload),
5454
}
5555
}
5656

@@ -62,16 +62,16 @@ func makeConfigTx(chainID string, i int) *cb.Envelope {
6262
ChainID: chainID,
6363
},
6464
},
65-
Data: util.MarshalOrPanic(&cb.ConfigurationEnvelope{
65+
Data: utils.MarshalOrPanic(&cb.ConfigurationEnvelope{
6666
Items: []*cb.SignedConfigurationItem{&cb.SignedConfigurationItem{
67-
ConfigurationItem: util.MarshalOrPanic(&cb.ConfigurationItem{
67+
ConfigurationItem: utils.MarshalOrPanic(&cb.ConfigurationItem{
6868
Value: []byte(fmt.Sprintf("%d", i)),
6969
}),
7070
}},
7171
}),
7272
}
7373
return &cb.Envelope{
74-
Payload: util.MarshalOrPanic(payload),
74+
Payload: utils.MarshalOrPanic(payload),
7575
}
7676
}
7777

@@ -147,7 +147,7 @@ func TestManagerImpl(t *testing.T) {
147147
t.Fatalf("Could not retrieve block")
148148
}
149149
for i := 0; i < static.DefaultBatchSize; i++ {
150-
if !reflect.DeepEqual(util.ExtractEnvelopeOrPanic(block, i), messages[i]) {
150+
if !reflect.DeepEqual(utils.ExtractEnvelopeOrPanic(block, i), messages[i]) {
151151
t.Errorf("Block contents wrong at index %d", i)
152152
}
153153
}

orderer/common/util/util.go protos/utils/commonutils.go

+1-48
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ See the License for the specific language governing permissions and
1414
limitations under the License.
1515
*/
1616

17-
package util
17+
package utils
1818

1919
import (
2020
"fmt"
@@ -135,50 +135,3 @@ func MakePayloadHeader(ch *cb.ChainHeader, sh *cb.SignatureHeader) *cb.Header {
135135
SignatureHeader: sh,
136136
}
137137
}
138-
139-
// MakeConfigurationItem makes a ConfigurationItem.
140-
func MakeConfigurationItem(ch *cb.ChainHeader, configItemType cb.ConfigurationItem_ConfigurationType, lastModified uint64, modPolicyID string, key string, value []byte) *cb.ConfigurationItem {
141-
return &cb.ConfigurationItem{
142-
Header: ch,
143-
Type: configItemType,
144-
LastModified: lastModified,
145-
ModificationPolicy: modPolicyID,
146-
Key: key,
147-
Value: value,
148-
}
149-
}
150-
151-
// MakeConfigurationEnvelope makes a ConfigurationEnvelope.
152-
func MakeConfigurationEnvelope(items ...*cb.SignedConfigurationItem) *cb.ConfigurationEnvelope {
153-
return &cb.ConfigurationEnvelope{Items: items}
154-
}
155-
156-
// MakePolicyOrPanic creates a Policy proto message out of a SignaturePolicyEnvelope, and panics if this operation fails.
157-
// NOTE Expand this as more policy types as supported.
158-
func MakePolicyOrPanic(policyEnvelope interface{}) *cb.Policy {
159-
switch pe := policyEnvelope.(type) {
160-
case *cb.SignaturePolicyEnvelope:
161-
return &cb.Policy{
162-
Type: &cb.Policy_SignaturePolicy{
163-
SignaturePolicy: pe,
164-
},
165-
}
166-
default:
167-
panic("Unknown policy envelope type given")
168-
}
169-
}
170-
171-
// MakePolicy creates a Policy proto message out of a SignaturePolicyEnvelope.
172-
// NOTE Expand this as more policy types as supported.
173-
func MakePolicy(policyEnvelope interface{}) (*cb.Policy, error) {
174-
switch pe := policyEnvelope.(type) {
175-
case *cb.SignaturePolicyEnvelope:
176-
return &cb.Policy{
177-
Type: &cb.Policy_SignaturePolicy{
178-
SignaturePolicy: pe,
179-
},
180-
}, nil
181-
default:
182-
return nil, fmt.Errorf("Unknown policy envelope type given")
183-
}
184-
}

orderer/common/util/util_test.go protos/utils/commonutils_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ See the License for the specific language governing permissions and
1414
limitations under the License.
1515
*/
1616

17-
package util
17+
package utils
1818

1919
import (
2020
"bytes"

protos/utils/configtxutils_test.go

+13-11
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ func TestBreakOutBlockData(t *testing.T) {
4343
if len(payloads) != 1 {
4444
t.Errorf("TestBreakOutBlock did not unmarshall to array of 1 payloads\n")
4545
}
46-
if payloads[0].Header.ChainHeader.Version != 1 || payloads[0].Header.ChainHeader.Type != int32(pb.HeaderType_CONFIGURATION_TRANSACTION) || payloads[0].Header.ChainHeader.ChainID != "test" {
46+
if payloads[0].Header.ChainHeader.Version != 1 || payloads[0].Header.ChainHeader.ChainID != "test" {
4747
t.Errorf("TestBreakOutBlockData payload header is %+v . Expected type is %v and Version == 1\n", payloads[0].Header.ChainHeader, int32(pb.HeaderType_CONFIGURATION_TRANSACTION))
4848
}
4949
if !bytes.Equal(payloads[0].Data, []byte("test")) {
@@ -150,6 +150,7 @@ func testPayloadHeader() *pb.Header {
150150
}
151151
}
152152

153+
/*
153154
func testPayload() *pb.Payload {
154155
return &pb.Payload{
155156
Header: testPayloadHeader(),
@@ -163,6 +164,17 @@ func testEnvelope() *pb.Envelope {
163164
return &pb.Envelope{Payload: payloadBytes}
164165
}
165166
167+
func testBlock() *pb.Block {
168+
// No need to set the block's Header, or Metadata
169+
envelopeBytes, _ := proto.Marshal(testEnvelope())
170+
return &pb.Block{
171+
Data: &pb.BlockData{
172+
Data: [][]byte{envelopeBytes},
173+
},
174+
}
175+
}
176+
*/
177+
166178
func testPayloadConfigEnvelope() *pb.Payload {
167179
data, _ := proto.Marshal(testConfigurationEnvelope())
168180
return &pb.Payload{
@@ -192,16 +204,6 @@ func testConfigurationEnvelope() *pb.ConfigurationEnvelope {
192204
return makeConfigurationEnvelope(signedConfigItem)
193205
} // testConfigurationEnvelope
194206

195-
func testBlock() *pb.Block {
196-
// No need to set the block's Header, or Metadata
197-
envelopeBytes, _ := proto.Marshal(testEnvelope())
198-
return &pb.Block{
199-
Data: &pb.BlockData{
200-
Data: [][]byte{envelopeBytes},
201-
},
202-
}
203-
}
204-
205207
func makeConfigurationItem(ch *pb.ChainHeader, configItemType pb.ConfigurationItem_ConfigurationType, lastModified uint64, modPolicyID string, key string, value []byte) *pb.ConfigurationItem {
206208
return &pb.ConfigurationItem{
207209
Header: ch,

protos/utils/configurationutils.go

+70
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
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 utils
18+
19+
import (
20+
"fmt"
21+
22+
cb "github.com/hyperledger/fabric/protos/common"
23+
)
24+
25+
// MakeConfigurationItem makes a ConfigurationItem.
26+
func MakeConfigurationItem(ch *cb.ChainHeader, configItemType cb.ConfigurationItem_ConfigurationType, lastModified uint64, modPolicyID string, key string, value []byte) *cb.ConfigurationItem {
27+
return &cb.ConfigurationItem{
28+
Header: ch,
29+
Type: configItemType,
30+
LastModified: lastModified,
31+
ModificationPolicy: modPolicyID,
32+
Key: key,
33+
Value: value,
34+
}
35+
}
36+
37+
// MakeConfigurationEnvelope makes a ConfigurationEnvelope.
38+
func MakeConfigurationEnvelope(items ...*cb.SignedConfigurationItem) *cb.ConfigurationEnvelope {
39+
return &cb.ConfigurationEnvelope{Items: items}
40+
}
41+
42+
// MakePolicyOrPanic creates a Policy proto message out of a SignaturePolicyEnvelope, and panics if this operation fails.
43+
// NOTE Expand this as more policy types as supported.
44+
func MakePolicyOrPanic(policyEnvelope interface{}) *cb.Policy {
45+
switch pe := policyEnvelope.(type) {
46+
case *cb.SignaturePolicyEnvelope:
47+
return &cb.Policy{
48+
Type: &cb.Policy_SignaturePolicy{
49+
SignaturePolicy: pe,
50+
},
51+
}
52+
default:
53+
panic("Unknown policy envelope type given")
54+
}
55+
}
56+
57+
// MakePolicy creates a Policy proto message out of a SignaturePolicyEnvelope.
58+
// NOTE Expand this as more policy types as supported.
59+
func MakePolicy(policyEnvelope interface{}) (*cb.Policy, error) {
60+
switch pe := policyEnvelope.(type) {
61+
case *cb.SignaturePolicyEnvelope:
62+
return &cb.Policy{
63+
Type: &cb.Policy_SignaturePolicy{
64+
SignaturePolicy: pe,
65+
},
66+
}, nil
67+
default:
68+
return nil, fmt.Errorf("Unknown policy envelope type given")
69+
}
70+
}

0 commit comments

Comments
 (0)