hyperledger/fabric · error

error marshaling Envelope: proto: Marshal called with nil

Error message

error marshaling Envelope: proto: Marshal called with nil

What it means

GetBytesEnvelope returns 'error marshaling Envelope: proto: Marshal called with nil' when the *common.Envelope argument is nil. An Envelope wraps the serialized Payload plus a signature; a nil Envelope cannot be marshaled by protobuf, so the helper returns this explicit error.

Source

Thrown at protoutil/proputils.go:219

		return nil, errors.New("error marshaling Transaction: proto: Marshal called with nil")
	}
	bytes, err := proto.Marshal(tx)
	return bytes, errors.Wrap(err, "error unmarshalling Transaction")
}

// GetBytesPayload get the bytes of Payload from the message
func GetBytesPayload(payl *common.Payload) ([]byte, error) {
	if payl == nil {
		return nil, errors.New("error marshaling Payload: proto: Marshal called with nil")
	}
	bytes, err := proto.Marshal(payl)
	return bytes, errors.Wrap(err, "error marshaling Payload")
}

// GetBytesEnvelope get the bytes of Envelope from the message
func GetBytesEnvelope(env *common.Envelope) ([]byte, error) {
	if env == nil {
		return nil, errors.New("error marshaling Envelope: proto: Marshal called with nil")
	}
	bytes, err := proto.Marshal(env)
	return bytes, errors.Wrap(err, "error marshaling Envelope")
}

// GetActionFromEnvelope extracts a ChaincodeAction message from a
// serialized Envelope
// TODO: fix function name as per FAB-11831
func GetActionFromEnvelope(envBytes []byte) (*peer.ChaincodeAction, error) {
	env, err := GetEnvelopeFromBlock(envBytes)
	if err != nil {
		return nil, err
	}
	return GetActionFromEnvelopeMsg(env)
}

func GetActionFromEnvelopeMsg(env *common.Envelope) (*peer.ChaincodeAction, error) {
	payl, err := UnmarshalPayload(env.Payload)

View on GitHub (pinned to 2736b63f8f)

Solutions

  1. Nil-check the Envelope before marshaling
  2. Verify CreateSignedTx returned a non-nil, signed envelope and handle its error before calling GetBytesEnvelope
  3. Ensure a valid signer (bccsp/crypto provider) is configured so the signing step completes

Example fix

// before
envBytes, err := protoutil.GetBytesEnvelope(env)
// after
if env == nil || env.Payload == nil {
	return nil, errors.New("envelope or payload is nil")
}
envBytes, err := protoutil.GetBytesEnvelope(env)
Defensive patterns

Strategy: validation

Validate before calling

func validEnvelope(env *common.Envelope) error {
	if env == nil {
		return errors.New("Envelope is nil")
	}
	if len(env.Payload) == 0 {
		return errors.New("Envelope has empty payload")
	}
	return nil
}

Type guard

func isSignedEnvelope(env *common.Envelope) bool {
	return env != nil && len(env.Payload) > 0 && len(env.Signature) > 0
}

Try / catch

envBytes, err := protoutil.GetBytesEnvelope(env)
if err != nil {
	return nil, fmt.Errorf("envelope serialization failed: %w", err)
}

Prevention

When it happens

Trigger: Calling protoutil.GetBytesEnvelope(nil), or test/SDK callers (TestInvoke, TestValidateDeployOK, TestRWSetTooBig, etc.) passing an envelope that was never created because proposal signing or CreateSignedTx failed earlier.

Common situations: End-to-end chaincode invoke/deploy test scaffolding where envelope construction is skipped or fails (no signer configured, signing error ignored) and the nil envelope flows into serialization.

Related errors


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