hyperledger/fabric · error

error unmarshalling KVRWSet

Error message

error unmarshalling KVRWSet

What it means

UnmarshalKVRWSet wraps proto.Unmarshal failures for kvrwset.KVRWSet bytes with this message. It fires when the bytes are not a valid KVRWSet protobuf — usually because the input was the outer TxReadWriteSet or corrupted data. Only rwsetDifference calls it, in key-value RW set comparison.

Source

Thrown at protoutil/unmarshalers.go:186

// UnmarshalChaincodeProposalPayload unmarshals bytes to a ChaincodeProposalPayload
func UnmarshalChaincodeProposalPayload(bytes []byte) (*peer.ChaincodeProposalPayload, error) {
	cpp := &peer.ChaincodeProposalPayload{}
	err := proto.Unmarshal(bytes, cpp)
	return cpp, errors.Wrap(err, "error unmarshalling ChaincodeProposalPayload")
}

// UnmarshalTxReadWriteSet unmarshals bytes to a TxReadWriteSet
func UnmarshalTxReadWriteSet(bytes []byte) (*rwset.TxReadWriteSet, error) {
	rws := &rwset.TxReadWriteSet{}
	err := proto.Unmarshal(bytes, rws)
	return rws, errors.Wrap(err, "error unmarshalling TxReadWriteSet")
}

// UnmarshalKVRWSet unmarshals bytes to a KVRWSet
func UnmarshalKVRWSet(bytes []byte) (*kvrwset.KVRWSet, error) {
	rws := &kvrwset.KVRWSet{}
	err := proto.Unmarshal(bytes, rws)
	return rws, errors.Wrap(err, "error unmarshalling KVRWSet")
}

// UnmarshalHashedRWSet unmarshals bytes to a HashedRWSet
func UnmarshalHashedRWSet(bytes []byte) (*kvrwset.HashedRWSet, error) {
	hrws := &kvrwset.HashedRWSet{}
	err := proto.Unmarshal(bytes, hrws)
	return hrws, errors.Wrap(err, "error unmarshalling HashedRWSet")
}

// UnmarshalSignaturePolicy unmarshals bytes to a SignaturePolicyEnvelope
func UnmarshalSignaturePolicy(bytes []byte) (*common.SignaturePolicyEnvelope, error) {
	sp := &common.SignaturePolicyEnvelope{}
	err := proto.Unmarshal(bytes, sp)
	return sp, errors.Wrap(err, "error unmarshalling SignaturePolicyEnvelope")
}

// UnmarshalPayloadOrPanic unmarshals bytes to a Payload structure or panics
// on error

View on GitHub (pinned to 2736b63f8f)

Solutions

  1. Confirm the input is the KVRWSet for a single namespace (TxReadWriteSet.NsRwSets[i].Rwset), not the outer message
  2. Guard against empty/nil byte slices before unmarshalling
  3. Check that both sides of the comparison serialize with the same fabric-protos version
  4. Return a descriptive comparison failure when unmarshal fails instead of letting the wrapped error propagate ambiguously

Example fix

// before
rws, err := protoutil.UnmarshalKVRWSet(txRwSetBytes) // wrong level
// after
rws, err := protoutil.UnmarshalKVRWSet(nsRwSet.Rwset)
if err != nil { return fmt.Errorf("namespace %s: %w", nsRwSet.Namespace, err) }
Defensive patterns

Strategy: validation

Validate before calling

func extractKVRWSetBytes(ns *rwset.NsReadWriteSet) ([]byte, bool) {
    if ns == nil || len(ns.Rwset) == 0 { return nil, false }
    return ns.Rwset, true
}

Type guard

func safeUnmarshalKVRWS(b []byte) (rws *kvrwset.KVRWSet, ok bool) {
    rws, err := protoutil.UnmarshalKVRWSet(b)
    return rws, err == nil && rws != nil
}

Try / catch

rws, err := protoutil.UnmarshalKVRWSet(nsRwSet.Rwset)
if err != nil {
    return fmt.Errorf("namespace %s has undecodable KVRWSet: %w", nsRwSet.Namespace, err)
}

Prevention

When it happens

Trigger: Passing TxReadWriteSet bytes instead of the per-namespace KVRWSet ( Rwset field), empty bytes, truncated results, or KVRead/KVWrite data produced by a mismatched proto version.

Common situations: RW set diff tools comparing state deltas per namespace, privdata validation utilities, or tests with manually constructed byte slices.

Related errors


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