2019-12-10 04:19:59 +00:00
|
|
|
package retrievaladapter
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2020-01-24 20:19:52 +00:00
|
|
|
"io"
|
2019-12-10 04:19:59 +00:00
|
|
|
|
|
|
|
"github.com/filecoin-project/go-address"
|
2020-01-10 17:13:12 +00:00
|
|
|
"github.com/filecoin-project/go-fil-markets/retrievalmarket"
|
2020-03-18 17:51:25 +00:00
|
|
|
"github.com/filecoin-project/go-fil-markets/shared"
|
2020-02-09 06:06:32 +00:00
|
|
|
"github.com/filecoin-project/specs-actors/actors/abi"
|
2020-02-21 17:26:44 +00:00
|
|
|
"github.com/filecoin-project/specs-actors/actors/builtin/paych"
|
2020-02-09 06:06:32 +00:00
|
|
|
|
2019-11-04 19:57:54 +00:00
|
|
|
"github.com/filecoin-project/lotus/api"
|
2020-02-29 03:23:55 +00:00
|
|
|
"github.com/filecoin-project/lotus/chain/types"
|
2020-01-24 20:19:52 +00:00
|
|
|
"github.com/filecoin-project/lotus/storage"
|
2020-03-27 23:00:21 +00:00
|
|
|
"github.com/filecoin-project/sector-storage"
|
|
|
|
"github.com/filecoin-project/sector-storage/ffiwrapper"
|
2019-12-10 04:19:59 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type retrievalProviderNode struct {
|
2020-03-03 22:19:22 +00:00
|
|
|
miner *storage.Miner
|
2020-03-23 11:40:02 +00:00
|
|
|
sealer sectorstorage.SectorManager
|
2020-03-03 22:19:22 +00:00
|
|
|
full api.FullNode
|
2019-12-10 04:19:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewRetrievalProviderNode returns a new node adapter for a retrieval provider that talks to the
|
|
|
|
// Lotus Node
|
2020-03-23 11:40:02 +00:00
|
|
|
func NewRetrievalProviderNode(miner *storage.Miner, sealer sectorstorage.SectorManager, full api.FullNode) retrievalmarket.RetrievalProviderNode {
|
2020-03-03 22:19:22 +00:00
|
|
|
return &retrievalProviderNode{miner, sealer, full}
|
2019-12-10 04:19:59 +00:00
|
|
|
}
|
|
|
|
|
2020-03-18 17:51:25 +00:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
addr, err := rpn.full.StateMinerWorker(ctx, miner, tsk)
|
2020-02-29 03:23:55 +00:00
|
|
|
return addr, err
|
|
|
|
}
|
|
|
|
|
2020-02-21 17:26:44 +00:00
|
|
|
func (rpn *retrievalProviderNode) UnsealSector(ctx context.Context, sectorID uint64, offset uint64, length uint64) (io.ReadCloser, error) {
|
|
|
|
si, err := rpn.miner.GetSectorInfo(abi.SectorNumber(sectorID))
|
2019-12-17 03:17:46 +00:00
|
|
|
if err != nil {
|
2020-01-24 20:19:52 +00:00
|
|
|
return nil, err
|
2019-12-17 03:17:46 +00:00
|
|
|
}
|
2020-03-17 20:19:52 +00:00
|
|
|
|
|
|
|
mid, err := address.IDFromAddress(rpn.miner.Address())
|
|
|
|
if err != nil {
|
2020-03-22 21:39:06 +00:00
|
|
|
return nil, err
|
2020-03-17 20:19:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
sid := abi.SectorID{
|
2020-03-18 01:08:11 +00:00
|
|
|
Miner: abi.ActorID(mid),
|
2020-03-17 20:19:52 +00:00
|
|
|
Number: abi.SectorNumber(sectorID),
|
|
|
|
}
|
2020-03-26 02:50:56 +00:00
|
|
|
return rpn.sealer.ReadPieceFromSealedSector(ctx, sid, ffiwrapper.UnpaddedByteIndex(offset), abi.UnpaddedPieceSize(length), si.Ticket.Value, *si.CommD)
|
2019-12-17 03:17:46 +00:00
|
|
|
}
|
|
|
|
|
2020-03-18 17:51:25 +00:00
|
|
|
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
|
2020-02-12 22:32:26 +00:00
|
|
|
added, err := rpn.full.PaychVoucherAdd(ctx, paymentChannel, voucher, proof, expectedAmount)
|
|
|
|
return added, err
|
2019-12-10 04:19:59 +00:00
|
|
|
}
|
2020-03-18 17:51:25 +00:00
|
|
|
|
|
|
|
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
|
|
|
|
}
|