102 lines
3.2 KiB
Go
102 lines
3.2 KiB
Go
package retrievaladapter
|
|
|
|
import (
|
|
"context"
|
|
"io"
|
|
|
|
"github.com/ipfs/go-cid"
|
|
logging "github.com/ipfs/go-log/v2"
|
|
|
|
"github.com/filecoin-project/lotus/api"
|
|
"github.com/filecoin-project/lotus/chain/actors/builtin/paych"
|
|
"github.com/filecoin-project/lotus/chain/types"
|
|
sectorstorage "github.com/filecoin-project/lotus/extern/sector-storage"
|
|
"github.com/filecoin-project/lotus/extern/sector-storage/storiface"
|
|
"github.com/filecoin-project/lotus/storage"
|
|
|
|
"github.com/filecoin-project/go-address"
|
|
"github.com/filecoin-project/go-fil-markets/retrievalmarket"
|
|
"github.com/filecoin-project/go-fil-markets/shared"
|
|
"github.com/filecoin-project/go-state-types/abi"
|
|
specstorage "github.com/filecoin-project/specs-storage/storage"
|
|
)
|
|
|
|
var log = logging.Logger("retrievaladapter")
|
|
|
|
type retrievalProviderNode struct {
|
|
miner *storage.Miner
|
|
sealer sectorstorage.SectorManager
|
|
full api.FullNode
|
|
}
|
|
|
|
// NewRetrievalProviderNode returns a new node adapter for a retrieval provider that talks to the
|
|
// Lotus Node
|
|
func NewRetrievalProviderNode(miner *storage.Miner, sealer sectorstorage.SectorManager, full api.FullNode) retrievalmarket.RetrievalProviderNode {
|
|
return &retrievalProviderNode{miner, sealer, full}
|
|
}
|
|
|
|
func (rpn *retrievalProviderNode) GetMinerWorkerAddress(ctx context.Context, miner address.Address, tok shared.TipSetToken) (address.Address, error) {
|
|
tsk, err := types.TipSetKeyFromBytes(tok)
|
|
if err != nil {
|
|
return address.Undef, err
|
|
}
|
|
|
|
mi, err := rpn.full.StateMinerInfo(ctx, miner, tsk)
|
|
return mi.Worker, err
|
|
}
|
|
|
|
func (rpn *retrievalProviderNode) UnsealSector(ctx context.Context, sectorID abi.SectorNumber, offset abi.UnpaddedPieceSize, length abi.UnpaddedPieceSize) (io.ReadCloser, error) {
|
|
si, err := rpn.miner.GetSectorInfo(sectorID)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
mid, err := address.IDFromAddress(rpn.miner.Address())
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
ref := specstorage.SectorRef{
|
|
ID: abi.SectorID{
|
|
Miner: abi.ActorID(mid),
|
|
Number: sectorID,
|
|
},
|
|
ProofType: si.SectorType,
|
|
}
|
|
|
|
// Set up a pipe so that data can be written from the unsealing process
|
|
// into the reader returned by this function
|
|
r, w := io.Pipe()
|
|
go func() {
|
|
var commD cid.Cid
|
|
if si.CommD != nil {
|
|
commD = *si.CommD
|
|
}
|
|
// Unseal the piece into the pipe's writer
|
|
err := rpn.sealer.ReadPiece(ctx, w, ref, storiface.UnpaddedByteIndex(offset), length, si.TicketValue, commD)
|
|
if err != nil {
|
|
log.Errorf("failed to unseal piece from sector %d: %s", sectorID, err)
|
|
}
|
|
// Close the reader with any error that was returned while reading the piece
|
|
_ = w.CloseWithError(err)
|
|
}()
|
|
|
|
return r, nil
|
|
}
|
|
|
|
func (rpn *retrievalProviderNode) SavePaymentVoucher(ctx context.Context, paymentChannel address.Address, voucher *paych.SignedVoucher, proof []byte, expectedAmount abi.TokenAmount, tok shared.TipSetToken) (abi.TokenAmount, error) {
|
|
// TODO: respect the provided TipSetToken (a serialized TipSetKey) when
|
|
// querying the chain
|
|
added, err := rpn.full.PaychVoucherAdd(ctx, paymentChannel, voucher, proof, expectedAmount)
|
|
return added, err
|
|
}
|
|
|
|
func (rpn *retrievalProviderNode) GetChainHead(ctx context.Context) (shared.TipSetToken, abi.ChainEpoch, error) {
|
|
head, err := rpn.full.ChainHead(ctx)
|
|
if err != nil {
|
|
return nil, 0, err
|
|
}
|
|
|
|
return head.Key().Bytes(), head.Height(), nil
|
|
}
|