{"record":{"id":"7f3c92019dd7aad7","repo":"hyperledger/fabric","slug":"error-unmarshalling-metadata-at-index-s","errorCode":null,"errorMessage":"error unmarshalling metadata at index [%s]","messagePattern":"error unmarshalling metadata at index \\[(.+?)\\]","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"protoutil/blockutils.go","lineNumber":137,"sourceCode":"\t}\n\n\treturn chdr.ChannelId, nil\n}\n\n// GetMetadataFromBlock retrieves metadata at the specified index.\nfunc GetMetadataFromBlock(block *cb.Block, index cb.BlockMetadataIndex) (*cb.Metadata, error) {\n\tif block.Metadata == nil {\n\t\treturn nil, errors.New(\"no metadata in block\")\n\t}\n\n\tif len(block.Metadata.Metadata) <= int(index) {\n\t\treturn nil, errors.Errorf(\"no metadata at index [%s]\", index)\n\t}\n\n\tmd := &cb.Metadata{}\n\terr := proto.Unmarshal(block.Metadata.Metadata[index], md)\n\tif err != nil {\n\t\treturn nil, errors.Wrapf(err, \"error unmarshalling metadata at index [%s]\", index)\n\t}\n\treturn md, nil\n}\n\n// GetMetadataFromBlockOrPanic retrieves metadata at the specified index, or\n// panics on error\nfunc GetMetadataFromBlockOrPanic(block *cb.Block, index cb.BlockMetadataIndex) *cb.Metadata {\n\tmd, err := GetMetadataFromBlock(block, index)\n\tif err != nil {\n\t\tpanic(err)\n\t}\n\treturn md\n}\n\n// GetConsenterMetadataFromBlock attempts to retrieve consenter metadata from the value\n// stored in block metadata at index SIGNATURES (first field). If no consenter metadata\n// is found there, it falls back to index ORDERER (third field).\nfunc GetConsenterMetadataFromBlock(block *cb.Block) (*cb.Metadata, error) {","sourceCodeStart":119,"sourceCodeEnd":155,"githubUrl":"https://github.com/hyperledger/fabric/blob/2736b63f8fd5932511d56fe68b7039d15977f7f6/protoutil/blockutils.go#L119-L155","documentation":"The bytes at the requested metadata index exist but failed to unmarshal into cb.Metadata. The error wraps the proto error, indicating the stored bytes for that index are corrupt or not the expected message type.","triggerScenarios":"GetMetadataFromBlock's proto.Unmarshal(block.Metadata.Metadata[index], md) fails — bytes truncated, wrong message type stored, or bit-rotted data at that index.","commonSituations":"Corrupted block store files; custom block producers writing non-cb.Metadata bytes; mixing binary formats across Fabric versions; failing disks or truncated snapshots.","solutions":["Verify ledger integrity and restore the block from another orderer or backup","Log the wrapped proto error to pinpoint the corruption (wrong wire type, truncated message)","Fix any custom block-building code to proto.Marshal a cb.Metadata into the index slot","If corruption is recurring, check storage hardware and Fabric version compatibility"],"exampleFix":"// before\nraw := readRawMetadataFromStore(idx)\nblk.Metadata.Metadata[idx] = raw // arbitrary bytes\n// after\nmd := &cb.Metadata{Value: val}\nblk.Metadata.Metadata[idx] = protoMarshal(md) // guaranteed cb.Metadata wire format","handlingStrategy":"try-catch","validationCode":"raw := blk.Metadata.Metadata[idx]\nprobe := &cb.Metadata{}\nif err := proto.Unmarshal(raw, probe); err != nil {\n    return fmt.Errorf(\"pre-check: metadata at index %v is corrupt: %w\", idx, err)\n}","typeGuard":"func isParsableMetadata(raw []byte) bool {\n    md := &cb.Metadata{}\n    return proto.Unmarshal(raw, md) == nil\n}","tryCatchPattern":"md, err := protoutil.GetMetadataFromBlock(blk, idx)\nif err != nil {\n    if strings.Contains(err.Error(), \"error unmarshalling metadata\") {\n        log.Errorf(\"corrupt metadata at index %v in block %d: %v\", idx, blk.Header.Number, err)\n        return restoreBlockFromBackup(blk.Header.Number)\n    }\n    return err\n}","preventionTips":["Only write proto.Marshal(cb.Metadata) bytes into metadata slots","Verify ledger file integrity; watch for disk corruption and truncated snapshots","Avoid ad-hoc byte manipulation of blocks in tooling","Keep Fabric versions consistent so metadata wire formats match"],"tags":["block","metadata","protobuf","corruption"],"backgroundTag":"protobuf-unmarshal-failed","analyzedSha":"2736b63f8fd5932511d56fe68b7039d15977f7f6","analyzedAt":"2026-09-04T08:52:36.465Z","contentChangedAt":"2026-09-04T08:52:36.465Z","schemaVersion":2},"datasetVersion":"2026-09-08T10:18:20.063Z"}