github.com/adnan-c/fabric_e2e_couchdb@v0.6.1-preview.0.20170228180935-21ce6b23cf91/protos/utils/blockutils.go (about) 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 "github.com/golang/protobuf/proto" 23 cb "github.com/hyperledger/fabric/protos/common" 24 ) 25 26 // GetChainIDFromBlock returns chain ID in the block 27 func GetChainIDFromBlock(block *cb.Block) (string, error) { 28 if block.Data == nil || block.Data.Data == nil || len(block.Data.Data) == 0 { 29 return "", fmt.Errorf("Failed to find chain ID because the block is empty.") 30 } 31 var err error 32 envelope := &cb.Envelope{} 33 if err = proto.Unmarshal(block.Data.Data[0], envelope); err != nil { 34 return "", fmt.Errorf("Error reconstructing envelope(%s)", err) 35 } 36 payload := &cb.Payload{} 37 if err = proto.Unmarshal(envelope.Payload, payload); err != nil { 38 return "", fmt.Errorf("Error reconstructing payload(%s)", err) 39 } 40 41 chdr, err := UnmarshalChannelHeader(payload.Header.ChannelHeader) 42 if err != nil { 43 return "", err 44 } 45 46 return chdr.ChannelId, nil 47 } 48 49 // GetMetadataFromBlock retrieves metadata at the specified index. 50 func GetMetadataFromBlock(block *cb.Block, index cb.BlockMetadataIndex) (*cb.Metadata, error) { 51 md := &cb.Metadata{} 52 err := proto.Unmarshal(block.Metadata.Metadata[index], md) 53 if err != nil { 54 return nil, err 55 } 56 return md, nil 57 } 58 59 // GetMetadataFromBlockOrPanic retrieves metadata at the specified index, or panics on error. 60 func GetMetadataFromBlockOrPanic(block *cb.Block, index cb.BlockMetadataIndex) *cb.Metadata { 61 md, err := GetMetadataFromBlock(block, index) 62 if err != nil { 63 panic(err) 64 } 65 return md 66 } 67 68 // GetLastConfigIndexFromBlock retrieves the index of the last config block as encoded in the block metadata 69 func GetLastConfigIndexFromBlock(block *cb.Block) (uint64, error) { 70 md, err := GetMetadataFromBlock(block, cb.BlockMetadataIndex_LAST_CONFIG) 71 if err != nil { 72 return 0, err 73 } 74 lc := &cb.LastConfig{} 75 err = proto.Unmarshal(md.Value, lc) 76 if err != nil { 77 return 0, err 78 } 79 return lc.Index, nil 80 } 81 82 // GetLastConfigIndexFromBlockOrPanic retrieves the index of the last config block as encoded in the block metadata, or panics on error. 83 func GetLastConfigIndexFromBlockOrPanic(block *cb.Block) uint64 { 84 index, err := GetLastConfigIndexFromBlock(block) 85 if err != nil { 86 panic(err) 87 } 88 return index 89 } 90 91 // GetBlockFromBlockBytes marshals the bytes into Block 92 func GetBlockFromBlockBytes(blockBytes []byte) (*cb.Block, error) { 93 block := &cb.Block{} 94 err := proto.Unmarshal(blockBytes, block) 95 return block, err 96 } 97 98 // CopyBlockMetadata copies metadata from one block into another 99 func CopyBlockMetadata(src *cb.Block, dst *cb.Block) { 100 dst.Metadata = src.Metadata 101 // Once copied initialize with rest of the 102 // required metadata positions. 103 InitBlockMetadata(dst) 104 } 105 106 // InitBlockMetadata copies metadata from one block into another 107 func InitBlockMetadata(block *cb.Block) { 108 if block.Metadata == nil { 109 block.Metadata = &cb.BlockMetadata{Metadata: [][]byte{[]byte{}, []byte{}, []byte{}}} 110 } else if len(block.Metadata.Metadata) < int(cb.BlockMetadataIndex_TRANSACTIONS_FILTER+1) { 111 for i := int(len(block.Metadata.Metadata)); i <= int(cb.BlockMetadataIndex_TRANSACTIONS_FILTER); i++ { 112 block.Metadata.Metadata = append(block.Metadata.Metadata, []byte{}) 113 } 114 } 115 }