{"record":{"id":"04b8a9683cd79a80","repo":"hyperledger/fabric","slug":"could-not-retrieve-ledger-for-channel-s","errorCode":null,"errorMessage":"Could not retrieve ledger for channel %s","messagePattern":"Could not retrieve ledger for channel (.+?)","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"core/scc/lscc/lscc.go","lineNumber":170,"sourceCode":"\t// BCCSP instance\n\tBCCSP bccsp.BCCSP\n\n\tPackageCache PackageCache\n}\n\n// PeerShim adapts the peer instance for use with LSCC by providing methods\n// previously provided by the scc provider.  If the lscc code weren't all getting\n// deleted soon, it would probably be worth rewriting it to use these APIs directly\n// rather that go through this shim, but it will be gone soon.\ntype PeerShim struct {\n\tPeer *peer.Peer\n}\n\n// GetQueryExecutorForLedger returns a query executor for the specified channel\nfunc (p *PeerShim) GetQueryExecutorForLedger(cid string) (ledger.QueryExecutor, error) {\n\tl := p.Peer.GetLedger(cid)\n\tif l == nil {\n\t\treturn nil, fmt.Errorf(\"Could not retrieve ledger for channel %s\", cid)\n\t}\n\n\treturn l.NewQueryExecutor()\n}\n\n// GetApplicationConfig returns the configtxapplication.SharedConfig for the channel\n// and whether the Application config exists\nfunc (p *PeerShim) GetApplicationConfig(cid string) (channelconfig.Application, bool) {\n\treturn p.Peer.GetApplicationConfig(cid)\n}\n\n// Returns the policy manager associated to the passed channel\n// and whether the policy manager exists\nfunc (p *PeerShim) PolicyManager(channelID string) (policies.Manager, bool) {\n\tm := p.Peer.GetPolicyManager(channelID)\n\treturn m, m != nil\n}\n","sourceCodeStart":152,"sourceCodeEnd":188,"githubUrl":"https://github.com/hyperledger/fabric/blob/2736b63f8fd5932511d56fe68b7039d15977f7f6/core/scc/lscc/lscc.go#L152-L188","documentation":"PeerShim.GetQueryExecutorForLedger returns this error when p.Peer.GetLedger(cid) returns nil, i.e. the peer has no ledger for the requested channel ID. lscc (and anything using this shim) cannot obtain a query executor for a channel the peer is not joined to or that does not exist.","triggerScenarios":"Calling GetQueryExecutorForLedger with a channel ID that the peer has never joined, a misspelled channel name, or a channel whose ledger was not yet created/opened on this peer.","commonSituations":"Invoking lifecycle/lscc queries against a channel the peer hasn't joined; typos in channel ID in CLI commands or SDK calls; querying a channel before JoinChain completes on this peer.","solutions":["Verify the channel ID is correct and that the peer has joined it (peer channel list)","Join the peer to the channel before issuing queries that need its ledger","Check for case/whitespace typos in the cid string","Confirm the peer's ledger data directory is intact and the channel ledger was not deleted"],"exampleFix":"// before\nqe, err := peerShim.GetQueryExecutorForLedger(\"mychan\")\n// after\njoined, _ := peerShim.Peer.GetChannels()\nif !containsChannel(joined, \"mychannel\") { return fmt.Errorf(\"peer not joined to mychannel\") }\nqe, err := peerShim.GetQueryExecutorForLedger(\"mychannel\")","handlingStrategy":"validation","validationCode":"// Go: confirm the peer has the channel's ledger before requesting a query executor\nchannels, err := peer.GetChannels()\nif err != nil { return err }\nfound := false\nfor _, ch := range channels {\n    if ch.Name == cid { found = true; break }\n}\nif !found { return fmt.Errorf(\"peer has no ledger for channel %s\", cid) }","typeGuard":null,"tryCatchPattern":"// Go\nqe, err := peerShim.GetQueryExecutorForLedger(cid)\nif err != nil {\n    if strings.Contains(err.Error(), \"Could not retrieve ledger\") {\n        return fmt.Errorf(\"peer is not joined to channel %q (or ledger missing): %w\", cid, err)\n    }\n    return err\n}","preventionTips":["Join the peer to the channel before issuing channel-scoped queries","Verify channel IDs with `peer channel list` to rule out typos","Confirm the channel's genesis/join completed before querying","Check the peer's ledger directory if a previously joined channel's ledger disappeared"],"tags":["hyperledger-fabric","ledger","channel","peer"],"backgroundTag":"channel-not-found","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"}