hyperledger/fabric · error

no such block hash [ ] in index

Error message

no such block hash [%x] in index

What it means

Redundant duplicate slot kept intentionally: same error as errorIndex 280 — 'no such block hash [%x] in index' from blockIndex.getBlockLocByHash when the index DB has no entry for constructBlockHashKey(blockHash). It signals a lookup miss in the block index, not a DB failure.

Solutions

  1. Confirm the hash belongs to this ledger by retrieving the block by number and comparing hashes
  2. Decode hex to raw bytes before lookup
  3. Re-sync/rebuild the block index if entries are missing
  4. Check index configuration includes block-hash indexing

Example fix

// before
blk, err := store.RetrieveBlockByHash([]byte(hexHash))
// after
raw, _ := hex.DecodeString(hexHash)
blk, err := store.RetrieveBlockByHash(raw)
Defensive patterns

Strategy: validation

Validate before calling

func blockHashIndexed(ledger lgr.PeerLedger, hash []byte) (bool, error) {
    bc, err := ledger.GetBlockchainInfo()
    if err != nil { return false, err }
    blk, err := ledger.GetBlockByNumber(bc.Height - 1)
    if err != nil { return false, err }
    if len(blk.Data.Hash) == 0 { return false, errors.New("index may not maintain block hashes") }
    _ = hash // compare against known block hashes or verify via header before lookup
    return true, nil
}

Type guard

func is32ByteHash(b []byte) bool { return len(b) == 32 }

Try / catch

blk, err := store.RetrieveBlockByHash(hash)
if err != nil {
    if strings.Contains(err.Error(), "no such block hash") {
        return nil, ErrBlockNotIndexed // handle as not-found, not fatal
    }
    return err
}

Prevention

When it happens

Trigger: retrieveBlockByHash is called with a hash absent from the index: wrong channel, undecoded hex string passed as bytes, or the index lacks the hash entry (index not synced, rebuilt, or partially lost).

Common situations: Cross-channel hash lookups, string-vs-bytes hash handling mistakes, peers with index DBs wiped or recreated without reindexing, snapshots restored without the index.

Related errors


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

Appendix: source

Thrown at common/ledger/blkstorage/blockindex.go:163

	}
	return nil
}

func (index *blockIndex) isAttributeIndexed(attribute IndexableAttr) bool {
	_, ok := index.indexItemsMap[attribute]
	return ok
}

func (index *blockIndex) getBlockLocByHash(blockHash []byte) (*fileLocPointer, error) {
	if !index.isAttributeIndexed(IndexableAttrBlockHash) {
		return nil, errors.New("block hashes not maintained in index")
	}
	b, err := index.db.Get(constructBlockHashKey(blockHash))
	if err != nil {
		return nil, err
	}
	if b == nil {
		return nil, errors.Errorf("no such block hash [%x] in index", blockHash)
	}
	blkLoc := &fileLocPointer{}
	if err := blkLoc.unmarshal(b); err != nil {
		return nil, err
	}
	return blkLoc, nil
}

func (index *blockIndex) getBlockLocByBlockNum(blockNum uint64) (*fileLocPointer, error) {
	if !index.isAttributeIndexed(IndexableAttrBlockNum) {
		return nil, errors.New("block numbers not maintained in index")
	}
	b, err := index.db.Get(constructBlockNumKey(blockNum))
	if err != nil {
		return nil, err
	}
	if b == nil {
		return nil, errors.Errorf("no such block number [%d] in index", blockNum)

View on GitHub (pinned to 2736b63f8f)