github.com/adnan-c/fabric_e2e_couchdb@v0.6.1-preview.0.20170228180935-21ce6b23cf91/common/configtx/util.go (about)

     1  /*
     2  Copyright IBM Corp. 2017 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 configtx
    18  
    19  import (
    20  	"fmt"
    21  
    22  	cb "github.com/hyperledger/fabric/protos/common"
    23  	"github.com/hyperledger/fabric/protos/utils"
    24  
    25  	"github.com/golang/protobuf/proto"
    26  )
    27  
    28  // UnmarshalConfig attempts to unmarshal bytes to a *cb.Config
    29  func UnmarshalConfig(data []byte) (*cb.Config, error) {
    30  	config := &cb.Config{}
    31  	err := proto.Unmarshal(data, config)
    32  	if err != nil {
    33  		return nil, err
    34  	}
    35  	return config, nil
    36  }
    37  
    38  // UnmarshalConfig attempts to unmarshal bytes to a *cb.Config
    39  func UnmarshalConfigOrPanic(data []byte) *cb.Config {
    40  	result, err := UnmarshalConfig(data)
    41  	if err != nil {
    42  		panic(err)
    43  	}
    44  	return result
    45  }
    46  
    47  // UnmarshalConfigUpdate attempts to unmarshal bytes to a *cb.ConfigUpdate
    48  func UnmarshalConfigUpdate(data []byte) (*cb.ConfigUpdate, error) {
    49  	configUpdate := &cb.ConfigUpdate{}
    50  	err := proto.Unmarshal(data, configUpdate)
    51  	if err != nil {
    52  		return nil, err
    53  	}
    54  	return configUpdate, nil
    55  }
    56  
    57  // UnmarshalConfigUpdate attempts to unmarshal bytes to a *cb.ConfigUpdate or panics
    58  func UnmarshalConfigUpdateOrPanic(data []byte) *cb.ConfigUpdate {
    59  	result, err := UnmarshalConfigUpdate(data)
    60  	if err != nil {
    61  		panic(err)
    62  	}
    63  	return result
    64  }
    65  
    66  // UnmarshalConfigUpdateEnvelope attempts to unmarshal bytes to a *cb.ConfigUpdate
    67  func UnmarshalConfigUpdateEnvelope(data []byte) (*cb.ConfigUpdateEnvelope, error) {
    68  	configUpdateEnvelope := &cb.ConfigUpdateEnvelope{}
    69  	err := proto.Unmarshal(data, configUpdateEnvelope)
    70  	if err != nil {
    71  		return nil, err
    72  	}
    73  	return configUpdateEnvelope, nil
    74  }
    75  
    76  // UnmarshalConfigUpdateEnvelope attempts to unmarshal bytes to a *cb.ConfigUpdateEnvelope or panics
    77  func UnmarshalConfigUpdateEnvelopeOrPanic(data []byte) *cb.ConfigUpdateEnvelope {
    78  	result, err := UnmarshalConfigUpdateEnvelope(data)
    79  	if err != nil {
    80  		panic(err)
    81  	}
    82  	return result
    83  }
    84  
    85  // UnmarshalConfigEnvelope attempts to unmarshal bytes to a *cb.ConfigEnvelope
    86  func UnmarshalConfigEnvelope(data []byte) (*cb.ConfigEnvelope, error) {
    87  	configEnv := &cb.ConfigEnvelope{}
    88  	err := proto.Unmarshal(data, configEnv)
    89  	if err != nil {
    90  		return nil, err
    91  	}
    92  	return configEnv, nil
    93  }
    94  
    95  // UnmarshalConfigEnvelope attempts to unmarshal bytes to a *cb.ConfigEnvelope or panics
    96  func UnmarshalConfigEnvelopeOrPanic(data []byte) *cb.ConfigEnvelope {
    97  	result, err := UnmarshalConfigEnvelope(data)
    98  	if err != nil {
    99  		panic(err)
   100  	}
   101  	return result
   102  }
   103  
   104  // ConfigEnvelopeFromBlock extract the config envelope from a config block
   105  func ConfigEnvelopeFromBlock(block *cb.Block) (*cb.ConfigEnvelope, error) {
   106  	if block.Data == nil || len(block.Data.Data) != 1 {
   107  		return nil, fmt.Errorf("Not a config block, must contain exactly one tx")
   108  	}
   109  
   110  	envelope, err := utils.UnmarshalEnvelope(block.Data.Data[0])
   111  	if err != nil {
   112  		return nil, err
   113  	}
   114  
   115  	payload, err := utils.UnmarshalPayload(envelope.Payload)
   116  	if err != nil {
   117  		return nil, err
   118  	}
   119  
   120  	return UnmarshalConfigEnvelope(payload.Data)
   121  }