Block
Block, protobuf encoded
$ ipfs block get QmbJG7NjeMKifAnLzbdLbbRbDtVvK1o5Y4tENAaLzh3UDb
╗║123
║
Block service will try to get block data from blockStore --> bs.Get(cid), if cid not found, then try to get it from Exchange.Fetch (bitswap)
func getBlock(ctx context.Context, c cid.Cid, bs blockstore.Blockstore, fget func() exchange.Fetcher) (blocks.Block, error) {
err := verifcid.ValidateCid(c) // hash security
if err != nil {
return nil, err
}
block, err := bs.Get(c)
if err == nil {
return block, nil
}
if err == blockstore.ErrNotFound && fget != nil {
f := fget() // Don't load the exchange until we have to
// TODO be careful checking ErrNotFound. If the underlying
// implementation changes, this will break.
log.Debug("Blockservice: Searching bitswap")
blk, err := f.GetBlock(ctx, c)
if err != nil {
if err == blockstore.ErrNotFound {
return nil, ErrNotFound
}
return nil, err
}
log.Event(ctx, "BlockService.BlockFetched", c)
return blk, nil
}
log.Debug("Blockservice GetBlock: Not found")
if err == blockstore.ErrNotFound {
return nil, ErrNotFound
}
return nil, err
}
DAG
$ ipfs dag get QmbJG7NjeMKifAnLzbdLbbRbDtVvK1o5Y4tENAaLzh3UDb
{"data":"CAISBTEyMw0KGAU=","links":[]}
dagService invike blockService to get block, then decode the block into IPLD format.
// Get retrieves a node from the dagService, fetching the block in the BlockService
func (n *dagService) Get(ctx context.Context, c cid.Cid) (ipld.Node, error) {
if n == nil {
return nil, fmt.Errorf("dagService is nil")
}
ctx, cancel := context.WithCancel(ctx)
defer cancel()
b, err := n.Blocks.GetBlock(ctx, c)
if err != nil {
if err == bserv.ErrNotFound {
return nil, ipld.ErrNotFound
}
return nil, fmt.Errorf("failed to get block for %s: %v", c, err)
}
return ipld.Decode(b)
}
UnixFs
unixFs API will reorganize the IPLD into a file…
$ipfs cat QmbJG7NjeMKifAnLzbdLbbRbDtVvK1o5Y4tENAaLzh3UDb
123