hyperledger/fabric · error

Could not retrieve ledger for channel %s

Error message

Could not retrieve ledger for channel %s

What it means

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.

Source

Thrown at core/scc/lscc/lscc.go:170

	// BCCSP instance
	BCCSP bccsp.BCCSP

	PackageCache PackageCache
}

// PeerShim adapts the peer instance for use with LSCC by providing methods
// previously provided by the scc provider.  If the lscc code weren't all getting
// deleted soon, it would probably be worth rewriting it to use these APIs directly
// rather that go through this shim, but it will be gone soon.
type PeerShim struct {
	Peer *peer.Peer
}

// GetQueryExecutorForLedger returns a query executor for the specified channel
func (p *PeerShim) GetQueryExecutorForLedger(cid string) (ledger.QueryExecutor, error) {
	l := p.Peer.GetLedger(cid)
	if l == nil {
		return nil, fmt.Errorf("Could not retrieve ledger for channel %s", cid)
	}

	return l.NewQueryExecutor()
}

// GetApplicationConfig returns the configtxapplication.SharedConfig for the channel
// and whether the Application config exists
func (p *PeerShim) GetApplicationConfig(cid string) (channelconfig.Application, bool) {
	return p.Peer.GetApplicationConfig(cid)
}

// Returns the policy manager associated to the passed channel
// and whether the policy manager exists
func (p *PeerShim) PolicyManager(channelID string) (policies.Manager, bool) {
	m := p.Peer.GetPolicyManager(channelID)
	return m, m != nil
}

View on GitHub (pinned to 2736b63f8f)

Solutions

  1. Verify the channel ID is correct and that the peer has joined it (peer channel list)
  2. Join the peer to the channel before issuing queries that need its ledger
  3. Check for case/whitespace typos in the cid string
  4. Confirm the peer's ledger data directory is intact and the channel ledger was not deleted

Example fix

// before
qe, err := peerShim.GetQueryExecutorForLedger("mychan")
// after
joined, _ := peerShim.Peer.GetChannels()
if !containsChannel(joined, "mychannel") { return fmt.Errorf("peer not joined to mychannel") }
qe, err := peerShim.GetQueryExecutorForLedger("mychannel")
Defensive patterns

Strategy: validation

Validate before calling

// Go: confirm the peer has the channel's ledger before requesting a query executor
channels, err := peer.GetChannels()
if err != nil { return err }
found := false
for _, ch := range channels {
    if ch.Name == cid { found = true; break }
}
if !found { return fmt.Errorf("peer has no ledger for channel %s", cid) }

Try / catch

// Go
qe, err := peerShim.GetQueryExecutorForLedger(cid)
if err != nil {
    if strings.Contains(err.Error(), "Could not retrieve ledger") {
        return fmt.Errorf("peer is not joined to channel %q (or ledger missing): %w", cid, err)
    }
    return err
}

Prevention

When it happens

Trigger: 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.

Common situations: 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.

Related errors


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