github.com/yacovm/fabric@v2.0.0-alpha.0.20191128145320-c5d4087dc723+incompatible/common/configtx/util.go (about)

     1  /*
     2  Copyright IBM Corp. All Rights Reserved.
     3  
     4  SPDX-License-Identifier: Apache-2.0
     5  */
     6  
     7  package configtx
     8  
     9  import (
    10  	"github.com/golang/protobuf/proto"
    11  	cb "github.com/hyperledger/fabric-protos-go/common"
    12  	"github.com/hyperledger/fabric/protoutil"
    13  )
    14  
    15  // UnmarshalConfig attempts to unmarshal bytes to a *cb.Config
    16  func UnmarshalConfig(data []byte) (*cb.Config, error) {
    17  	config := &cb.Config{}
    18  	err := proto.Unmarshal(data, config)
    19  	if err != nil {
    20  		return nil, err
    21  	}
    22  	return config, nil
    23  }
    24  
    25  // UnmarshalConfigOrPanic attempts to unmarshal bytes to a *cb.Config or panics on error
    26  func UnmarshalConfigOrPanic(data []byte) *cb.Config {
    27  	result, err := UnmarshalConfig(data)
    28  	if err != nil {
    29  		panic(err)
    30  	}
    31  	return result
    32  }
    33  
    34  // UnmarshalConfigUpdate attempts to unmarshal bytes to a *cb.ConfigUpdate
    35  func UnmarshalConfigUpdate(data []byte) (*cb.ConfigUpdate, error) {
    36  	configUpdate := &cb.ConfigUpdate{}
    37  	err := proto.Unmarshal(data, configUpdate)
    38  	if err != nil {
    39  		return nil, err
    40  	}
    41  	return configUpdate, nil
    42  }
    43  
    44  // UnmarshalConfigUpdateOrPanic attempts to unmarshal bytes to a *cb.ConfigUpdate or panics on error
    45  func UnmarshalConfigUpdateOrPanic(data []byte) *cb.ConfigUpdate {
    46  	result, err := UnmarshalConfigUpdate(data)
    47  	if err != nil {
    48  		panic(err)
    49  	}
    50  	return result
    51  }
    52  
    53  // UnmarshalConfigUpdateEnvelope attempts to unmarshal bytes to a *cb.ConfigUpdate
    54  func UnmarshalConfigUpdateEnvelope(data []byte) (*cb.ConfigUpdateEnvelope, error) {
    55  	configUpdateEnvelope := &cb.ConfigUpdateEnvelope{}
    56  	err := proto.Unmarshal(data, configUpdateEnvelope)
    57  	if err != nil {
    58  		return nil, err
    59  	}
    60  	return configUpdateEnvelope, nil
    61  }
    62  
    63  // UnmarshalConfigUpdateEnvelopeOrPanic attempts to unmarshal bytes to a *cb.ConfigUpdateEnvelope or panics on error
    64  func UnmarshalConfigUpdateEnvelopeOrPanic(data []byte) *cb.ConfigUpdateEnvelope {
    65  	result, err := UnmarshalConfigUpdateEnvelope(data)
    66  	if err != nil {
    67  		panic(err)
    68  	}
    69  	return result
    70  }
    71  
    72  // UnmarshalConfigEnvelope attempts to unmarshal bytes to a *cb.ConfigEnvelope
    73  func UnmarshalConfigEnvelope(data []byte) (*cb.ConfigEnvelope, error) {
    74  	configEnv := &cb.ConfigEnvelope{}
    75  	err := proto.Unmarshal(data, configEnv)
    76  	if err != nil {
    77  		return nil, err
    78  	}
    79  	return configEnv, nil
    80  }
    81  
    82  // UnmarshalConfigEnvelopeOrPanic attempts to unmarshal bytes to a *cb.ConfigEnvelope or panics on error
    83  func UnmarshalConfigEnvelopeOrPanic(data []byte) *cb.ConfigEnvelope {
    84  	result, err := UnmarshalConfigEnvelope(data)
    85  	if err != nil {
    86  		panic(err)
    87  	}
    88  	return result
    89  }
    90  
    91  // UnmarshalConfigUpdateFromPayload unmarshals configuration update from given payload
    92  func UnmarshalConfigUpdateFromPayload(payload *cb.Payload) (*cb.ConfigUpdate, error) {
    93  	configEnv, err := UnmarshalConfigEnvelope(payload.Data)
    94  	if err != nil {
    95  		return nil, err
    96  	}
    97  	configUpdateEnv, err := protoutil.EnvelopeToConfigUpdate(configEnv.LastUpdate)
    98  	if err != nil {
    99  		return nil, err
   100  	}
   101  
   102  	return UnmarshalConfigUpdate(configUpdateEnv.ConfigUpdate)
   103  }