getBlock method

  1. @override
Future<GetBlockResponse> getBlock(
  1. String cid
)

Returns a Future that resolves to a GetBlockResponse for the given cid.

Implementation

@override
/// Returns a [Future] that resolves to a [GetBlockResponse] for the given [cid].
Future<GetBlockResponse> getBlock(String cid) async {
  try {
    // Check the bounded in-memory cache first
    final cachedBlock = _blockCache.get(cid);
    if (cachedBlock != null) {
      return BlockResponseFactory.successGet(cachedBlock.toProto());
    }

    // Load from disk on cache miss
    final blockPath = p.join(path, cid);
    if (await getPlatform().exists(blockPath)) {
      final data = await getPlatform().readBytes(blockPath);
      if (data != null) {
        // The filename is the CID — no need to rehash the content.
        final block = Block(cid: CID.decode(cid), data: data);
        _blockCache.put(cid, block);
        return BlockResponseFactory.successGet(block.toProto());
      }
    }

    _logger.debug('Block not found: $cid');
    return BlockResponseFactory.notFound();
  } catch (e) {
    _logger.error('Failed to get block $cid', e);
    return BlockResponseFactory.notFound();
  }
}