hyperledger/fabric · critical

no namespace or key is found for namespace %s and key %s wit

Error message

no namespace or key is found for namespace %s and key %s with decoded blockNum %d and tranNum %d

What it means

The history query scanner looked up the statedb value for a historical (namespace, key, blockNum, tranNum) recorded in the history DB, and got no result. This signals inconsistency between historydb and statedb — the history index references a version that no longer exists in state.

Source

Thrown at core/ledger/kvledger/history/query_executer.go:84

	}
	logger.Debugf("Found history record for namespace:%s key:%s at blockNumTranNum %v:%v\n",
		scanner.namespace, scanner.key, blockNum, tranNum)

	// Get the transaction from block storage that is associated with this history record
	tranEnvelope, err := scanner.blockStore.RetrieveTxByBlockNumTranNum(blockNum, tranNum)
	if err != nil {
		return nil, err
	}

	// Get the txid, key write value, timestamp, and delete indicator associated with this transaction
	queryResult, err := getKeyModificationFromTran(tranEnvelope, scanner.namespace, scanner.key)
	if err != nil {
		return nil, err
	}
	if queryResult == nil {
		// should not happen, but make sure there is inconsistency between historydb and statedb
		logger.Errorf("No namespace or key is found for namespace %s and key %s with decoded blockNum %d and tranNum %d", scanner.namespace, scanner.key, blockNum, tranNum)
		return nil, errors.Errorf("no namespace or key is found for namespace %s and key %s with decoded blockNum %d and tranNum %d", scanner.namespace, scanner.key, blockNum, tranNum)
	}
	logger.Debugf("Found historic key value for namespace:%s key:%s from transaction %s",
		scanner.namespace, scanner.key, queryResult.(*queryresult.KeyModification).TxId)
	return queryResult, nil
}

func (scanner *historyScanner) Close() {
	scanner.dbItr.Release()
}

// getKeyModificationFromTran inspects a transaction for writes to a given key
func getKeyModificationFromTran(tranEnvelope *common.Envelope, namespace string, key string) (commonledger.QueryResult, error) {
	logger.Debugf("Entering getKeyModificationFromTran %s:%s", namespace, key)

	// extract action from the envelope
	payload, err := protoutil.UnmarshalPayload(tranEnvelope.Payload)
	if err != nil {
		return nil, err

View on GitHub (pinned to 2736b63f8f)

Solutions

  1. Rebuild both state and history DBs together (peer node rebuild-dbs) so they share a consistent version baseline
  2. Restore the peer's ledger data from snapshot or resync from the blockchain to repair the diverged DBs
  3. Check for failed/partial writes (disk issues) that dropped the state entry while keeping history
  4. Identify the mismatched key and confirm via block replay whether the state entry was lost

Example fix

# before
peer node rebuild-dbs  # only state rebuilt, history stale
# after
# rebuild state AND history together, or wipe both:
rm -rf ledgersData/stateLeveldb ledgersData/historyLeveldb
peer node start  # both DBs replay from blockchain consistently
Defensive patterns

Strategy: try-catch

Validate before calling

// before iterating history, verify the key exists in state at all
_, err := statedb.GetState(namespace, key)
if err != nil {
    return fmt.Errorf("state unavailable for history query: %w", err)
}

Type guard

func hasStateEntry(qe queryExecuter, ns, key string) bool {
    v, err := qe.getState(ns, key)
    return err == nil && v != nil
}

Try / catch

for scanner.Next() {
    result, err := scanner.Next()
    if err != nil {
        if strings.Contains(err.Error(), "no namespace or key is found") {
            // history/state mismatch: stop iterating, trigger ledger rebuild
            return nil, ErrHistoryStateInconsistent
        }
        return err
    }
}

Prevention

When it happens

Trigger: Next() on a QueryResultsIterator fetches the value at a recorded block/tran number via queryExecuter and the state DB returns nil — history entries pointing at pruned/rebuilt state, or DBs out of sync.

Common situations: State DB rebuilt (e.g. peer node rebuild-dbs) while the history DB kept old entries; LevelDB/CouchDB corruption or partial deletes; history db and state db diverged after failed writes or manual data surgery.

Related errors


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