Skip to content

Commit

Permalink
[FAB-1944] Part 3: Remove duplicate code
Browse files Browse the repository at this point in the history
https://jira.hyperledger.org/browse/FAB-1944

Some of the OrPanic methods were copy-pasted versions of the original
method, replacing error returns with panics.  This is code duplication
and should be removed.

Change-Id: Ib73914aa5392103ec698f1ced3bdf8dc11f3e6b5
Signed-off-by: Jason Yellick <[email protected]>
  • Loading branch information
Jason Yellick committed Feb 5, 2017
1 parent ee5ff49 commit a8486dc
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 20 deletions.
12 changes: 3 additions & 9 deletions protos/utils/blockutils.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,7 @@ func GetMetadataFromBlock(block *cb.Block, index cb.BlockMetadataIndex) (*cb.Met

// GetMetadataFromBlockOrPanic retrieves metadata at the specified index, or panics on error.
func GetMetadataFromBlockOrPanic(block *cb.Block, index cb.BlockMetadataIndex) *cb.Metadata {
md := &cb.Metadata{}
err := proto.Unmarshal(block.Metadata.Metadata[index], md)
md, err := GetMetadataFromBlock(block, index)
if err != nil {
panic(err)
}
Expand All @@ -81,16 +80,11 @@ func GetLastConfigurationIndexFromBlock(block *cb.Block) (uint64, error) {

// GetLastConfigurationIndexFromBlockOrPanic retrieves the index of the last configuration block as encoded in the block metadata, or panics on error.
func GetLastConfigurationIndexFromBlockOrPanic(block *cb.Block) uint64 {
md, err := GetMetadataFromBlock(block, cb.BlockMetadataIndex_LAST_CONFIGURATION)
if err != nil {
panic(err)
}
lc := &cb.LastConfiguration{}
err = proto.Unmarshal(md.Value, lc)
index, err := GetLastConfigurationIndexFromBlock(block)
if err != nil {
panic(err)
}
return lc.Index
return index
}

// GetBlockFromBlockBytes marshals the bytes into Block
Expand Down
17 changes: 6 additions & 11 deletions protos/utils/commonutils.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,14 +103,9 @@ func UnmarshalEnvelope(encoded []byte) (*cb.Envelope, error) {

// ExtractEnvelopeOrPanic retrieves the requested envelope from a given block and unmarshals it -- it panics if either of these operation fail.
func ExtractEnvelopeOrPanic(block *cb.Block, index int) *cb.Envelope {
envelopeCount := len(block.Data.Data)
if index < 0 || index >= envelopeCount {
panic("Envelope index out of bounds")
}
marshaledEnvelope := block.Data.Data[index]
envelope := &cb.Envelope{}
if err := proto.Unmarshal(marshaledEnvelope, envelope); err != nil {
panic(fmt.Errorf("Block data does not carry an envelope at index %d: %s", index, err))
envelope, err := ExtractEnvelope(block, index)
if err != nil {
panic(err)
}
return envelope
}
Expand All @@ -131,9 +126,9 @@ func ExtractEnvelope(block *cb.Block, index int) (*cb.Envelope, error) {

// ExtractPayloadOrPanic retrieves the payload of a given envelope and unmarshals it -- it panics if either of these operations fail.
func ExtractPayloadOrPanic(envelope *cb.Envelope) *cb.Payload {
payload := &cb.Payload{}
if err := proto.Unmarshal(envelope.Payload, payload); err != nil {
panic(fmt.Errorf("Envelope does not carry a Payload: %s", err))
payload, err := ExtractPayload(envelope)
if err != nil {
panic(err)
}
return payload
}
Expand Down

0 comments on commit a8486dc

Please sign in to comment.