{"record":{"id":"89237f2c9aefa43c","repo":"hyperledger/fabric","slug":"error-marshalling-ledger-metadata","errorCode":null,"errorMessage":"error marshalling ledger metadata","messagePattern":"error marshalling ledger metadata","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"core/ledger/kvledger/kv_ledger_provider.go","lineNumber":603,"sourceCode":"func (s *idStore) upgradeFormat() error {\n\teligible, err := s.checkUpgradeEligibility()\n\tif err != nil {\n\t\treturn err\n\t}\n\tif !eligible {\n\t\treturn nil\n\t}\n\n\tlogger.Infof(\"Upgrading ledgerProvider database to the new format %s\", dataformat.CurrentFormat)\n\n\tbatch := &leveldb.Batch{}\n\tbatch.Put(formatKey, []byte(dataformat.CurrentFormat))\n\n\t// add new metadata key for each ledger (channel)\n\tmetadata, err := protoutil.Marshal(&msgs.LedgerMetadata{Status: msgs.Status_ACTIVE})\n\tif err != nil {\n\t\tlogger.Errorf(\"Error marshalling ledger metadata: %s\", err)\n\t\treturn errors.Wrapf(err, \"error marshalling ledger metadata\")\n\t}\n\titr := s.db.GetIterator(genesisBlkKeyPrefix, genesisBlkKeyStop)\n\tdefer itr.Release()\n\tfor itr.Error() == nil && itr.Next() {\n\t\tid := ledgerIDFromGenesisBlockKey(itr.Key())\n\t\tbatch.Put(metadataKey(id), metadata)\n\t}\n\tif err = itr.Error(); err != nil {\n\t\tlogger.Errorf(\"Error while upgrading idStore format: %s\", err)\n\t\treturn errors.Wrapf(err, \"error while upgrading idStore format\")\n\t}\n\n\treturn s.db.WriteBatch(batch, true)\n}\n\nfunc (s *idStore) createLedgerID(ledgerID string, metadata *msgs.LedgerMetadata) error {\n\tm, err := s.getLedgerMetadata(ledgerID)\n\tif err != nil {","sourceCodeStart":585,"sourceCodeEnd":621,"githubUrl":"https://github.com/hyperledger/fabric/blob/2736b63f8fd5932511d56fe68b7039d15977f7f6/core/ledger/kvledger/kv_ledger_provider.go#L585-L621","documentation":"In upgradeFormat (run by UpgradeIDStoreFormat/UpgradeDBs), the code marshals a new LedgerMetadata{Status: ACTIVE} proto to write a metadata key for every existing ledger. protoutil.Marshal of this simple in-memory proto essentially only fails on internal proto serialization errors, so this failure signals an environment/library-level problem rather than bad user input; the upgrade transaction is aborted and the error is wrapped with this message.","triggerScenarios":"Calling UpgradeIDStoreFormat (or UpgradeDBs) against a pre-v2.0 idStore when protoutil.Marshal(&msgs.LedgerMetadata{Status: msgs.Status_ACTIVE}) returns an error — e.g. a broken/incorrectly generated protobuf runtime, or reflection/registry failures from a mismatched protobuf dependency version.","commonSituations":"See trigger scenarios.","solutions":["Rebuild the upgrade tool/peer against the official Fabric module versions so generated msgs protos and google.golang.org/protobuf are consistent (go mod tidy, clear vendor dir).","Retry the upgrade after rebuilding; the input is static so transient failures indicate environment, not data.","If it persists, capture the underlying proto error from the peer log ('Error marshalling ledger metadata: %s') and file/check the Fabric issue tracker for your versions."],"exampleFix":"// before: mismatched protobuf deps after custom vendoring\nvendor/github.com/golang/protobuf  (v1.3.x)\nvendor/google.golang.org/protobuf  (v1.28.x)\n\n// after: align modules and rebuild\ngo mod tidy\nrm -rf vendor && go mod vendor\ngo build -o peer ./cmd/peer","handlingStrategy":"try-catch","validationCode":"// Before running the upgrade, verify the protobuf runtime marshals the metadata message\nfunc verifyProtoMarshal() error {\n\tb, err := protoutil.Marshal(&msgs.LedgerMetadata{Status: msgs.Status_ACTIVE})\n\tif err != nil {\n\t\treturn fmt.Errorf(\"protobuf runtime broken: %w\", err)\n\t}\n\tif len(b) == 0 {\n\t\treturn errors.New(\"unexpected empty marshal output\")\n\t}\n\treturn nil\n}","typeGuard":null,"tryCatchPattern":"if err := kvledger.UpgradeIDStoreFormat(dbPath); err != nil {\n\tif strings.Contains(err.Error(), \"error marshalling ledger metadata\") {\n\t\tlog.Fatalf(\"protobuf marshal failed during upgrade; rebuild binaries with aligned protobuf deps: %v\", err)\n\t}\n\treturn err\n}","preventionTips":["Build peers and upgrade tools from official Fabric modules without protobuf version overrides (no replace directives on google.golang.org/protobuf).","Run go mod tidy / go mod verify in CI to catch dependency drift.","Back up ledgersData before running any format upgrade so a failed upgrade is recoverable.","Test the upgrade on a staging copy of production data first."],"tags":["protobuf","marshal","upgrade","idstore","go"],"backgroundTag":"proto-marshal-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"}