Skip to content

Commit

Permalink
[FAB-1944] Part 2: Remove unused utils methods
Browse files Browse the repository at this point in the history
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 <[email protected]>
  • Loading branch information
Jason Yellick committed Feb 5, 2017
1 parent fda7f99 commit ee5ff49
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 238 deletions.
2 changes: 1 addition & 1 deletion core/peer/peer.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
57 changes: 8 additions & 49 deletions protos/utils/blockutils.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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))}
}
63 changes: 6 additions & 57 deletions protos/utils/configtxutils.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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
Expand Down
65 changes: 2 additions & 63 deletions protos/utils/configtxutils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,52 +76,16 @@ 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")
}
} // TestBreakOutBlockToConfigurationEnvelopeBadData

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")
}
Expand All @@ -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{
Expand Down
68 changes: 0 additions & 68 deletions protos/utils/configurationutils.go

This file was deleted.

0 comments on commit ee5ff49

Please sign in to comment.