105 lines
2.2 KiB
Go
105 lines
2.2 KiB
Go
package sectorblocks
|
|
|
|
import (
|
|
"context"
|
|
"io/ioutil"
|
|
|
|
blocks "github.com/ipfs/go-block-format"
|
|
"github.com/ipfs/go-cid"
|
|
blockstore "github.com/ipfs/go-ipfs-blockstore"
|
|
logging "github.com/ipfs/go-log"
|
|
"golang.org/x/xerrors"
|
|
)
|
|
|
|
var log = logging.Logger("sectorblocks")
|
|
|
|
type SectorBlockStore struct {
|
|
intermediate blockstore.Blockstore
|
|
sectorBlocks *SectorBlocks
|
|
|
|
approveUnseal func() error
|
|
}
|
|
|
|
func (s *SectorBlockStore) DeleteBlock(cid.Cid) error {
|
|
panic("not supported")
|
|
}
|
|
func (s *SectorBlockStore) GetSize(cid.Cid) (int, error) {
|
|
panic("not supported")
|
|
}
|
|
|
|
func (s *SectorBlockStore) Put(blocks.Block) error {
|
|
panic("not supported")
|
|
}
|
|
|
|
func (s *SectorBlockStore) PutMany([]blocks.Block) error {
|
|
panic("not supported")
|
|
}
|
|
|
|
func (s *SectorBlockStore) AllKeysChan(ctx context.Context) (<-chan cid.Cid, error) {
|
|
panic("not supported")
|
|
}
|
|
|
|
func (s *SectorBlockStore) HashOnRead(enabled bool) {
|
|
panic("not supported")
|
|
}
|
|
|
|
func (s *SectorBlockStore) Has(c cid.Cid) (bool, error) {
|
|
has, err := s.intermediate.Has(c)
|
|
if err != nil {
|
|
return false, err
|
|
}
|
|
if has {
|
|
return true, nil
|
|
}
|
|
|
|
return s.sectorBlocks.Has(c)
|
|
}
|
|
|
|
func (s *SectorBlockStore) Get(c cid.Cid) (blocks.Block, error) {
|
|
val, err := s.intermediate.Get(c)
|
|
if err == nil {
|
|
return val, nil
|
|
}
|
|
if err != blockstore.ErrNotFound {
|
|
return nil, err
|
|
}
|
|
|
|
refs, err := s.sectorBlocks.GetRefs(c)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
if len(refs) == 0 {
|
|
return nil, blockstore.ErrNotFound
|
|
}
|
|
|
|
best := refs[0] // TODO: better strategy (e.g. look for already unsealed)
|
|
|
|
si, err := s.sectorBlocks.Miner.GetSectorInfo(best.SectorID)
|
|
if err != nil {
|
|
return nil, xerrors.Errorf("getting sector info: %w", err)
|
|
}
|
|
|
|
log.Infof("reading block %s from sector %d(+%d;%d)", c, best.SectorID, best.Offset, best.Size)
|
|
|
|
r, err := s.sectorBlocks.sb.ReadPieceFromSealedSector(
|
|
best.SectorID,
|
|
best.Offset,
|
|
best.Size,
|
|
si.Ticket.TicketBytes,
|
|
si.CommD,
|
|
)
|
|
if err != nil {
|
|
return nil, xerrors.Errorf("unsealing block: %w", err)
|
|
}
|
|
defer r.Close()
|
|
|
|
data, err := ioutil.ReadAll(r)
|
|
if err != nil {
|
|
return nil, xerrors.Errorf("reading block data: %w", err)
|
|
}
|
|
|
|
return blocks.NewBlockWithCid(data, c)
|
|
}
|
|
|
|
var _ blockstore.Blockstore = &SectorBlockStore{}
|