hyperledger/fabric · error

failed to unmarshal ApplicationPolicy bytes

Error message

failed to unmarshal ApplicationPolicy bytes

What it means

toSignaturePolicyEnvelope unmarshals raw bytes into a peer.ApplicationPolicy protobuf. If proto.Unmarshal fails, the bytes are not a valid ApplicationPolicy message and the error wraps the underlying cause. This happens when the stored policy bytes are corrupt or are not an ApplicationPolicy at all.

Source

Thrown at core/chaincode/lifecycle/metadata_provider.go:79

		ChaincodeInfoProvider:  cip,
		LegacyMetadataProvider: lmp,
		ChannelPolicyReferenceProvider: &channelPolicyReferenceProviderImpl{
			ChannelConfigSource: ccs,
		},
	}
}

type MetadataProvider struct {
	ChaincodeInfoProvider          ChaincodeInfoProvider
	LegacyMetadataProvider         LegacyMetadataProvider
	ChannelPolicyReferenceProvider ChannelPolicyReferenceProvider
}

func (mp *MetadataProvider) toSignaturePolicyEnvelope(channelID string, policyBytes []byte) ([]byte, error) {
	p := &peer.ApplicationPolicy{}
	err := proto.Unmarshal(policyBytes, p)
	if err != nil {
		return nil, errors.Wrap(err, "failed to unmarshal ApplicationPolicy bytes")
	}

	switch policy := p.Type.(type) {
	case *peer.ApplicationPolicy_SignaturePolicy:
		return protoutil.MarshalOrPanic(policy.SignaturePolicy), nil
	case *peer.ApplicationPolicy_ChannelConfigPolicyReference:
		p, err := mp.ChannelPolicyReferenceProvider.NewPolicy(channelID, policy.ChannelConfigPolicyReference)
		if err != nil {
			return nil, errors.WithMessagef(err, "could not retrieve policy for reference '%s' on channel '%s'", policy.ChannelConfigPolicyReference, channelID)
		}

		cp, ok := p.(policies.Converter)
		if !ok {
			return nil, errors.Errorf("policy with reference '%s' on channel '%s' is not convertible to SignaturePolicyEnvelope", policy.ChannelConfigPolicyReference, channelID)
		}

		spe, err := cp.Convert()
		if err != nil {

View on GitHub (pinned to 2736b63f8f)

Solutions

  1. Inspect and repair the peer state; restore from a clean backup if policy bytes are corrupt
  2. Re-set the policy via a channel config update or re-approve the chaincode with a valid endorsement policy
  3. Align peer versions across the network so serialization expectations match
  4. Enable debug logging and check the wrapped proto error to identify the malformed source
Defensive patterns

Strategy: try-catch

Try / catch

envBytes, err := mp.toSignaturePolicyEnvelope(channelID, policyBytes)
if err != nil {
  if strings.Contains(err.Error(), "failed to unmarshal ApplicationPolicy bytes") {
    // skip this policy source; log and fall back to chaincode-level policy
  }
}

Prevention

When it happens

Trigger: Metadata (discovery) calls toSignaturePolicyEnvelope with policyBytes that fail protobuf decoding — e.g. bytes referencing a legacy policy type stored in the wrong format or truncated/corrupt state.

Common situations: Peer state corruption; Fabric version mismatch where an older policy serialization is read by newer code; manual state manipulation; a policy captured as a different protobuf type.

Understand the failure class

Background: "cannot parse invalid wire-format data", "cannot unmarshal", "failed unmarshalling": protobuf unmarshal errors explained — this error's family across 10 libraries.

Related errors


AI-assisted analysis of hyperledger/fabric@2736b63f8f (2026-09-04). Data as JSON: /api/errors/6c0b4c969dac0f7d. Report an issue: GitHub.