hyperledger/fabric · error

could not retrieve state for chaincode %s

Error message

could not retrieve state for chaincode %s

What it means

ChaincodeEndorsementInfo wraps ledger errors with this message when qe.GetState("lscc", chaincodeName) itself fails (a state-retrieval error, not a missing key). It distinguishes 'could not read the ledger' from 'chaincode not found' — the wrapped err carries the storage-level cause.

Source

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

	// peer on the other hand might have created a deploy
	// transaction that attempts to bypass the instantiation
	// policy. This check is there to ensure that this will not
	// happen, i.e. that the peer will refuse to invoke the
	// chaincode under these conditions. More info on
	// https://jira.hyperledger.org/browse/FAB-3156
	if fsData.InstantiationPolicy != nil {
		if !bytes.Equal(fsData.InstantiationPolicy, cd.InstantiationPolicy) {
			return fmt.Errorf("Instantiation policy mismatch for cc %s", cd.ChaincodeID())
		}
	}

	return nil
}

func (lscc *SCC) ChaincodeEndorsementInfo(channelID, chaincodeName string, qe ledger.SimpleQueryExecutor) (*lifecycle.ChaincodeEndorsementInfo, error) {
	chaincodeDataBytes, err := qe.GetState("lscc", chaincodeName)
	if err != nil {
		return nil, errors.Wrapf(err, "could not retrieve state for chaincode %s", chaincodeName)
	}

	if chaincodeDataBytes == nil {
		return nil, errors.Errorf("chaincode %s not found", chaincodeName)
	}

	chaincodeData := &ccprovider.ChaincodeData{}
	err = proto.Unmarshal(chaincodeDataBytes, chaincodeData)
	if err != nil {
		return nil, errors.Wrapf(err, "chaincode %s has bad definition", chaincodeName)
	}

	ls := &LegacySecurity{
		Support:      lscc.Support,
		PackageCache: &lscc.PackageCache,
	}

	err = ls.SecurityCheckLegacyChaincode(chaincodeData)

View on GitHub (pinned to 2736b63f8f)

Solutions

  1. Inspect the wrapped error for the state-DB failure cause
  2. Check the peer logs and state database (LevelDB/CouchDB) health
  3. Retry once the ledger/state DB is healthy — this is typically a transient or environmental failure
  4. If the DB is corrupt, rebuild the state database from the blockchain
Defensive patterns

Strategy: retry

Validate before calling

// Go: probe state availability first
if _, err := qe.GetState("lscc", chaincodeName); err != nil {
    // state DB currently failing; defer the call until healthy
    return fmt.Errorf("lscc state unavailable: %w", err)
}

Try / catch

// Go
info, err := lscc.ChaincodeEndorsementInfo(channelID, name, qe)
if err != nil && strings.Contains(err.Error(), "could not retrieve state for chaincode") {
    // transient ledger failure: back off and retry
    time.Sleep(retryDelay)
    info, err = lscc.ChaincodeEndorsementInfo(channelID, name, qe)
}

Prevention

When it happens

Trigger: Calling ChaincodeEndorsementInfo when the underlying query executor's GetState returns a non-nil error — e.g. ledger/database I/O failure, iterator problems, or transient state-DB issues while reading the lscc namespace.

Common situations: CouchDB/LevelDB connectivity problems; state database corruption; querying during ledger maintenance or while the state DB is being rebuilt.

Related errors


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