Skip to content

Commit a8486dc

Browse files
author
Jason Yellick
committed
[FAB-1944] Part 3: Remove duplicate code
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]>
1 parent ee5ff49 commit a8486dc

File tree

2 files changed

+9
-20
lines changed

2 files changed

+9
-20
lines changed

protos/utils/blockutils.go

+3-9
Original file line numberDiff line numberDiff line change
@@ -57,8 +57,7 @@ func GetMetadataFromBlock(block *cb.Block, index cb.BlockMetadataIndex) (*cb.Met
5757

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

8281
// GetLastConfigurationIndexFromBlockOrPanic retrieves the index of the last configuration block as encoded in the block metadata, or panics on error.
8382
func GetLastConfigurationIndexFromBlockOrPanic(block *cb.Block) uint64 {
84-
md, err := GetMetadataFromBlock(block, cb.BlockMetadataIndex_LAST_CONFIGURATION)
85-
if err != nil {
86-
panic(err)
87-
}
88-
lc := &cb.LastConfiguration{}
89-
err = proto.Unmarshal(md.Value, lc)
83+
index, err := GetLastConfigurationIndexFromBlock(block)
9084
if err != nil {
9185
panic(err)
9286
}
93-
return lc.Index
87+
return index
9488
}
9589

9690
// GetBlockFromBlockBytes marshals the bytes into Block

protos/utils/commonutils.go

+6-11
Original file line numberDiff line numberDiff line change
@@ -103,14 +103,9 @@ func UnmarshalEnvelope(encoded []byte) (*cb.Envelope, error) {
103103

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

132127
// ExtractPayloadOrPanic retrieves the payload of a given envelope and unmarshals it -- it panics if either of these operations fail.
133128
func ExtractPayloadOrPanic(envelope *cb.Envelope) *cb.Payload {
134-
payload := &cb.Payload{}
135-
if err := proto.Unmarshal(envelope.Payload, payload); err != nil {
136-
panic(fmt.Errorf("Envelope does not carry a Payload: %s", err))
129+
payload, err := ExtractPayload(envelope)
130+
if err != nil {
131+
panic(err)
137132
}
138133
return payload
139134
}

0 commit comments

Comments
 (0)