From ee5ff496900ab63028d413bd1241f6dec48a0152 Mon Sep 17 00:00:00 2001 From: Jason Yellick Date: Mon, 30 Jan 2017 17:01:56 -0500 Subject: [PATCH] [FAB-1944] Part 2: Remove unused utils methods https://jira.hyperledger.org/browse/FAB-1944 This is part of an ongoing series intended to remove cruft from the protos/utils package. This one in particular removes functions which were largely not being used, or only had one user and were too generic. More to be slashed from this package in the future. Change-Id: I6d1dd38b055cb33ea5c817c0dc9a7a68f39f56a6 Signed-off-by: Jason Yellick --- core/peer/peer.go | 2 +- protos/utils/blockutils.go | 57 ++++--------------------- protos/utils/configtxutils.go | 63 +++------------------------ protos/utils/configtxutils_test.go | 65 +--------------------------- protos/utils/configurationutils.go | 68 ------------------------------ 5 files changed, 17 insertions(+), 238 deletions(-) delete mode 100644 protos/utils/configurationutils.go diff --git a/core/peer/peer.go b/core/peer/peer.go index a27fb3c4583..a8dfe2f4923 100644 --- a/core/peer/peer.go +++ b/core/peer/peer.go @@ -154,7 +154,7 @@ func getCurrConfigBlockFromLedger(ledger ledger.PeerLedger) (*common.Block, erro // createChain creates a new chain object and insert it into the chains func createChain(cid string, ledger ledger.PeerLedger, cb *common.Block) error { - configEnvelope, _, err := utils.BreakOutBlockToConfigurationEnvelope(cb) + configEnvelope, err := utils.BreakOutBlockToConfigurationEnvelope(cb) if err != nil { return err } diff --git a/protos/utils/blockutils.go b/protos/utils/blockutils.go index 7cb8149f649..8416f91830e 100644 --- a/protos/utils/blockutils.go +++ b/protos/utils/blockutils.go @@ -119,38 +119,7 @@ func InitBlockMetadata(block *cb.Block) { } } -const ( - AnchorPeerConfItemKey = "AnchorPeers" - epoch = uint64(0) - messageVersion = int32(1) - lastModified = uint64(0) - mspKey = "MSP" - xxxDefaultModificationPolicyID = "DefaultModificationPolicy" // Break an import cycle during work to remove the below configtx construction methods -) - -func createConfigItem(chainID string, - configItemKey string, - configItemValue []byte, - modPolicy string, configItemType cb.ConfigurationItem_ConfigurationType) *cb.ConfigurationItem { - - ciChainHeader := MakeChainHeader(cb.HeaderType_CONFIGURATION_ITEM, - messageVersion, chainID, epoch) - configItem := MakeConfigurationItem(ciChainHeader, - configItemType, lastModified, modPolicy, - configItemKey, configItemValue) - - return configItem -} - -func createSignedConfigItem(chainID string, - configItemKey string, - configItemValue []byte, - modPolicy string, configItemType cb.ConfigurationItem_ConfigurationType) *cb.SignedConfigurationItem { - configItem := createConfigItem(chainID, configItemKey, configItemValue, modPolicy, configItemType) - return &cb.SignedConfigurationItem{ - ConfigurationItem: MarshalOrPanic(configItem), - Signatures: nil} -} +const xxxDefaultModificationPolicyID = "DefaultModificationPolicy" // Break an import cycle during work to remove the below configtx construction methods // GetTESTMSPConfigPath This function is needed to locate the MSP test configuration when running // in CI build env or local with "make unit-test". A better way to manage this @@ -171,25 +140,15 @@ func EncodeMSPUnsigned(chainID string) *cb.ConfigurationItem { if err != nil { panic(fmt.Sprintf("GetLocalMspConfig failed, err %s", err)) } - // TODO: once https://gerrit.hyperledger.org/r/#/c/3941 is merged, change this to MSP - // Right now we don't have an MSP type there - return createConfigItem(chainID, - mspKey, - MarshalOrPanic(conf), - xxxDefaultModificationPolicyID, cb.ConfigurationItem_MSP) + return &cb.ConfigurationItem{ + Type: cb.ConfigurationItem_MSP, + Key: "DEFAULT", // XXX this should really be computed dynamically, but it's better than the old wrong "MSP" + Value: MarshalOrPanic(conf), + ModificationPolicy: xxxDefaultModificationPolicyID, + } } // EncodeMSP gets the signed configuration item with the default MSP func EncodeMSP(chainID string) *cb.SignedConfigurationItem { - cfgPath := GetTESTMSPConfigPath() - conf, err := msp.GetLocalMspConfig(cfgPath) - if err != nil { - panic(fmt.Sprintf("GetLocalMspConfig failed, err %s", err)) - } - // TODO: once https://gerrit.hyperledger.org/r/#/c/3941 is merged, change this to MSP - // Right now we don't have an MSP type there - return createSignedConfigItem(chainID, - mspKey, - MarshalOrPanic(conf), - xxxDefaultModificationPolicyID, cb.ConfigurationItem_MSP) + return &cb.SignedConfigurationItem{ConfigurationItem: MarshalOrPanic(EncodeMSPUnsigned(chainID))} } diff --git a/protos/utils/configtxutils.go b/protos/utils/configtxutils.go index bca1cf51eb8..d7f98e5cdde 100644 --- a/protos/utils/configtxutils.go +++ b/protos/utils/configtxutils.go @@ -24,12 +24,6 @@ import ( "github.com/golang/protobuf/proto" ) -// no need to break out block header as none of its parts are serialized - -// no need to break out block metadata as it's just a byte slice - -// no need to break Block into constituents. Nothing to unmarshall - // BreakOutBlockDataOrPanic executes BreakOutBlockData() but panics on error func BreakOutBlockDataOrPanic(blockData *pb.BlockData) ([]*pb.Payload, [][]byte) { payloads, envelopeSignatures, err := BreakOutBlockData(blockData) @@ -92,68 +86,23 @@ func BreakOutPayloadDataToConfigurationEnvelope(payloadData []byte) (*pb.Configu return configEnvelope, nil } //BreakOutPayloadToConfigurationEnvelope -// BreakOutConfigEnvelopeToConfigItemsOrPanic calls BreakOutConfigEnvelopeToConfigItems() but panics on error -func BreakOutConfigEnvelopeToConfigItemsOrPanic(configEnvelope *pb.ConfigurationEnvelope) ([]*pb.ConfigurationItem, [][]*pb.ConfigurationSignature) { - configItems, configSignatures, err := BreakOutConfigEnvelopeToConfigItems(configEnvelope) - if err != nil { - panic(err) - } - return configItems, configSignatures -} // BreakOutConfigEnvelopeToConfigItemsOrPanic - -// BreakOutConfigEnvelopeToConfigItems decomposes a ConfigurationEnvelope to its constituent ConfigurationItems and ConfigurationSignatures -// Note that a ConfigurationItem can have multiple signatures so each element in the returned ConfigurationItems slice is associated with a slice of ConfigurationSignatures -func BreakOutConfigEnvelopeToConfigItems(configEnvelope *pb.ConfigurationEnvelope) ([]*pb.ConfigurationItem, [][]*pb.ConfigurationSignature, error) { - if configEnvelope == nil { - return nil, nil, fmt.Errorf("BreakOutConfigEnvelopeToConfigItems received null input\n") - } - - var configItems []*pb.ConfigurationItem - var configSignatures [][]*pb.ConfigurationSignature - - var err error - var configItem *pb.ConfigurationItem - for i, signedConfigItem := range configEnvelope.Items { - configItem = &pb.ConfigurationItem{} - err = proto.Unmarshal(signedConfigItem.ConfigurationItem, configItem) - if err != nil { - return nil, nil, fmt.Errorf("BreakOutConfigEnvelopToConfigItems cannot unmarshall signedConfigurationItem: %v\n", err) - } - configItems = append(configItems, configItem) - for _, signedConfigItemSignature := range signedConfigItem.Signatures { - configSignatures[i] = append(configSignatures[i], signedConfigItemSignature) - } - } - - return configItems, configSignatures, nil -} // BreakOutConfigEnvelopeToConfigItems - -// BreakOutBlockToConfigurationEnvelopeOrPanic calls BreakOutBlockToConfigurationEnvelope() but panics on error -func BreakOutBlockToConfigurationEnvelopeOrPanic(block *pb.Block) (*pb.ConfigurationEnvelope, []byte) { - configEnvelope, envelopeSignature, err := BreakOutBlockToConfigurationEnvelope(block) - if err != nil { - panic(err) - } - return configEnvelope, envelopeSignature -} // BreakOutBlockToConfigurationEnvelopeOrPanic - // BreakOutBlockToConfigurationEnvelope decomposes a configuration transaction Block to its ConfigurationEnvelope -func BreakOutBlockToConfigurationEnvelope(block *pb.Block) (*pb.ConfigurationEnvelope, []byte, error) { +func BreakOutBlockToConfigurationEnvelope(block *pb.Block) (*pb.ConfigurationEnvelope, error) { if block == nil || block.Data == nil || len(block.Data.Data) > 1 { - return nil, nil, fmt.Errorf("Block.BlockData is not an array of 1. This is not a configuration transaction\n") + return nil, fmt.Errorf("Block.BlockData is not an array of 1. This is not a configuration transaction\n") } - payloads, envelopeSignatures, _ := BreakOutBlockData(block.Data) + payloads, _, _ := BreakOutBlockData(block.Data) if payloads[0].Header.ChainHeader.Type != int32(pb.HeaderType_CONFIGURATION_TRANSACTION) { - return nil, nil, fmt.Errorf("Payload Header type is not configuration_transaction. This is not a configuration transaction\n") + return nil, fmt.Errorf("Payload Header type is not configuration_transaction. This is not a configuration transaction\n") } configEnvelope, err := BreakOutPayloadDataToConfigurationEnvelope(payloads[0].Data) if err != nil { - return nil, nil, fmt.Errorf("Error breaking out configurationEnvelope: %v\n", err) + return nil, fmt.Errorf("Error breaking out configurationEnvelope: %v\n", err) } - return configEnvelope, envelopeSignatures[0], nil + return configEnvelope, nil } // BreakOutPayloadToConfigurationEnvelope // UnmarshalConfigurationItemOrPanic unmarshals bytes to a ConfigurationItem or panics on error diff --git a/protos/utils/configtxutils_test.go b/protos/utils/configtxutils_test.go index 0487aa0be77..2df0eb37c92 100644 --- a/protos/utils/configtxutils_test.go +++ b/protos/utils/configtxutils_test.go @@ -76,44 +76,8 @@ func TestBreakOutPayloadDataToConfigurationEnvelope(t *testing.T) { } } // TestBreakOutPayloadDataToConfigurationEnvelope -func TestBreakOutConfigEnvelopeToConfigItemsPanic(t *testing.T) { - defer func() { - if r := recover(); r == nil { - t.Errorf("TestBreakOutConfigEnvelopeToConfigItemsPanic should have panicked") - } - }() - _, _ = BreakOutConfigEnvelopeToConfigItemsOrPanic(nil) -} // TestBreakOutConfigEnvelopeToConfigItemsPanic - -func TestBreakOutConfigEnvelopeToConfigItemsBadData(t *testing.T) { - _, _, err := BreakOutConfigEnvelopeToConfigItems(nil) - if err == nil { - t.Errorf("TestBreakOutConfigEnvelopeToConfigItemsBadData not handling nil input\n") - } -} // TestBreakOutConfigEnvelopeToConfigItemsBadData - -func TestBreakOutConfigEnvelopeToConfigItems(t *testing.T) { - configEnv := testConfigurationEnvelope() - configItems, _, _ := BreakOutConfigEnvelopeToConfigItems(configEnv) // TODO: test signatures - if len(configItems) != 1 { - t.Errorf("TestBreakOutPayloadDataToConfigurationEnvelope did not return array of 1 config item\n") - } - if configItems[0].Type != pb.ConfigurationItem_Orderer || configItems[0].Key != "abc" || !bytes.Equal(configItems[0].Value, []byte("test")) { - t.Errorf("TestBreakOutConfigEnvelopeToConfigItems configItem type,Key,Value do not match original %+v\n. Expected orderer, 'abc', 'test'", configItems[0]) - } -} // TestBreakOutConfigEnvelopeToConfigItems - -func TestBreakOutBlockToConfigurationEnvelopePanic(t *testing.T) { - defer func() { - if r := recover(); r == nil { - t.Errorf("TestBreakOutBlockToConfigurationEnvelopePanic should have panicked") - } - }() - _, _ = BreakOutBlockToConfigurationEnvelopeOrPanic(nil) -} // TestBreakOutBlockToConfigurationEnvelopePanic - func TestBreakOutBlockToConfigurationEnvelopeBadData(t *testing.T) { - _, _, err := BreakOutBlockToConfigurationEnvelope(nil) + _, err := BreakOutBlockToConfigurationEnvelope(nil) if err == nil { t.Errorf("TestBreakOutBlockToConfigurationEnvelopeBadData should have rejected null input\n") } @@ -121,7 +85,7 @@ func TestBreakOutBlockToConfigurationEnvelopeBadData(t *testing.T) { func TestBreakOutBlockToConfigurationEnvelope(t *testing.T) { block := testConfigurationBlock() - configEnvelope, _, _ := BreakOutBlockToConfigurationEnvelope(block) // TODO: test envelope signature + configEnvelope, _ := BreakOutBlockToConfigurationEnvelope(block) // TODO: test envelope signature if len(configEnvelope.Items) != 1 { t.Errorf("TestBreakOutBlockToConfigurationEnvelope should have an array of 1 signedConfigurationItems\n") } @@ -147,31 +111,6 @@ func testPayloadHeader() *pb.Header { } } -/* -func testPayload() *pb.Payload { - return &pb.Payload{ - Header: testPayloadHeader(), - Data: []byte("test"), - } -} - -func testEnvelope() *pb.Envelope { - // No need to set the signature - payloadBytes, _ := proto.Marshal(testPayload()) - return &pb.Envelope{Payload: payloadBytes} -} - -func testBlock() *pb.Block { - // No need to set the block's Header, or Metadata - envelopeBytes, _ := proto.Marshal(testEnvelope()) - return &pb.Block{ - Data: &pb.BlockData{ - Data: [][]byte{envelopeBytes}, - }, - } -} -*/ - func testPayloadConfigEnvelope() *pb.Payload { data, _ := proto.Marshal(testConfigurationEnvelope()) return &pb.Payload{ diff --git a/protos/utils/configurationutils.go b/protos/utils/configurationutils.go deleted file mode 100644 index b23d794d16f..00000000000 --- a/protos/utils/configurationutils.go +++ /dev/null @@ -1,68 +0,0 @@ -/* -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 utils - -import ( - "fmt" - - cb "github.com/hyperledger/fabric/protos/common" - - "github.com/golang/protobuf/proto" -) - -// MakeConfigurationItem makes a ConfigurationItem. -func MakeConfigurationItem(ch *cb.ChainHeader, configItemType cb.ConfigurationItem_ConfigurationType, lastModified uint64, modPolicyID string, key string, value []byte) *cb.ConfigurationItem { - return &cb.ConfigurationItem{ - Type: configItemType, - LastModified: lastModified, - ModificationPolicy: modPolicyID, - Key: key, - Value: value, - } -} - -// MakeConfigurationEnvelope makes a ConfigurationEnvelope. -func MakeConfigurationEnvelope(items ...*cb.SignedConfigurationItem) *cb.ConfigurationEnvelope { - return &cb.ConfigurationEnvelope{Items: items} -} - -// MakePolicyOrPanic creates a Policy proto message out of a SignaturePolicyEnvelope, and panics if this operation fails. -func MakePolicyOrPanic(policyEnvelope interface{}) *cb.Policy { - policy, err := MakePolicy(policyEnvelope) - if err != nil { - panic(err) - } - return policy -} - -// MakePolicy creates a Policy proto message out of a SignaturePolicyEnvelope. -// NOTE Expand this as more policy types as supported. -func MakePolicy(policyEnvelope interface{}) (*cb.Policy, error) { - switch pe := policyEnvelope.(type) { - case *cb.SignaturePolicyEnvelope: - m, err := proto.Marshal(pe) - if err != nil { - return nil, err - } - return &cb.Policy{ - Type: int32(cb.Policy_SIGNATURE), - Policy: m, - }, nil - default: - return nil, fmt.Errorf("Unknown policy envelope type given") - } -}