2020-01-10 18:21:46 +00:00
|
|
|
package storageadapter
|
2019-11-04 19:57:54 +00:00
|
|
|
|
|
|
|
// this file implements storagemarket.StorageProviderNode
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"context"
|
|
|
|
|
|
|
|
"github.com/ipfs/go-cid"
|
|
|
|
logging "github.com/ipfs/go-log"
|
|
|
|
unixfile "github.com/ipfs/go-unixfs/file"
|
|
|
|
"golang.org/x/xerrors"
|
|
|
|
|
|
|
|
"github.com/filecoin-project/go-address"
|
2019-12-17 10:46:39 +00:00
|
|
|
"github.com/filecoin-project/go-fil-markets/shared/tokenamount"
|
|
|
|
sharedtypes "github.com/filecoin-project/go-fil-markets/shared/types"
|
|
|
|
"github.com/filecoin-project/go-fil-markets/storagemarket"
|
2019-11-04 19:57:54 +00:00
|
|
|
"github.com/filecoin-project/lotus/api"
|
|
|
|
"github.com/filecoin-project/lotus/chain/actors"
|
|
|
|
"github.com/filecoin-project/lotus/chain/types"
|
|
|
|
"github.com/filecoin-project/lotus/lib/padreader"
|
2020-01-10 18:21:46 +00:00
|
|
|
"github.com/filecoin-project/lotus/markets/utils"
|
2019-11-04 19:57:54 +00:00
|
|
|
"github.com/filecoin-project/lotus/node/modules/dtypes"
|
|
|
|
"github.com/filecoin-project/lotus/storage/sectorblocks"
|
|
|
|
)
|
|
|
|
|
|
|
|
var log = logging.Logger("provideradapter")
|
|
|
|
|
|
|
|
type ProviderNodeAdapter struct {
|
|
|
|
api.FullNode
|
|
|
|
|
|
|
|
// this goes away with the data transfer module
|
|
|
|
dag dtypes.StagingDAG
|
|
|
|
|
|
|
|
secb *sectorblocks.SectorBlocks
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewProviderNodeAdapter(dag dtypes.StagingDAG, secb *sectorblocks.SectorBlocks, full api.FullNode) storagemarket.StorageProviderNode {
|
|
|
|
return &ProviderNodeAdapter{
|
|
|
|
FullNode: full,
|
|
|
|
dag: dag,
|
|
|
|
secb: secb,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (n *ProviderNodeAdapter) PublishDeals(ctx context.Context, deal storagemarket.MinerDeal) (storagemarket.DealID, cid.Cid, error) {
|
|
|
|
log.Info("publishing deal")
|
|
|
|
|
2020-02-11 23:29:45 +00:00
|
|
|
worker, err := n.StateMinerWorker(ctx, deal.Proposal.Provider, types.EmptyTSK)
|
2019-11-04 19:57:54 +00:00
|
|
|
if err != nil {
|
|
|
|
return 0, cid.Undef, err
|
|
|
|
}
|
|
|
|
|
2020-01-10 18:21:46 +00:00
|
|
|
localProposal, err := utils.FromSharedStorageDealProposal(&deal.Proposal)
|
2019-12-17 10:46:39 +00:00
|
|
|
if err != nil {
|
|
|
|
return 0, cid.Undef, err
|
|
|
|
}
|
2019-11-04 19:57:54 +00:00
|
|
|
params, err := actors.SerializeParams(&actors.PublishStorageDealsParams{
|
2019-12-17 10:46:39 +00:00
|
|
|
Deals: []actors.StorageDealProposal{*localProposal},
|
2019-11-04 19:57:54 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return 0, cid.Undef, xerrors.Errorf("serializing PublishStorageDeals params failed: ", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: We may want this to happen after fetching data
|
|
|
|
smsg, err := n.MpoolPushMessage(ctx, &types.Message{
|
|
|
|
To: actors.StorageMarketAddress,
|
|
|
|
From: worker,
|
|
|
|
Value: types.NewInt(0),
|
|
|
|
GasPrice: types.NewInt(0),
|
|
|
|
GasLimit: types.NewInt(1000000),
|
|
|
|
Method: actors.SMAMethods.PublishStorageDeals,
|
|
|
|
Params: params,
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return 0, cid.Undef, err
|
|
|
|
}
|
|
|
|
r, err := n.StateWaitMsg(ctx, smsg.Cid())
|
|
|
|
if err != nil {
|
|
|
|
return 0, cid.Undef, err
|
|
|
|
}
|
|
|
|
if r.Receipt.ExitCode != 0 {
|
|
|
|
return 0, cid.Undef, xerrors.Errorf("publishing deal failed: exit %d", r.Receipt.ExitCode)
|
|
|
|
}
|
|
|
|
var resp actors.PublishStorageDealResponse
|
|
|
|
if err := resp.UnmarshalCBOR(bytes.NewReader(r.Receipt.Return)); err != nil {
|
|
|
|
return 0, cid.Undef, err
|
|
|
|
}
|
|
|
|
if len(resp.DealIDs) != 1 {
|
|
|
|
return 0, cid.Undef, xerrors.Errorf("got unexpected number of DealIDs from")
|
|
|
|
}
|
|
|
|
|
|
|
|
return storagemarket.DealID(resp.DealIDs[0]), smsg.Cid(), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (n *ProviderNodeAdapter) OnDealComplete(ctx context.Context, deal storagemarket.MinerDeal, piecePath string) (uint64, error) {
|
|
|
|
root, err := n.dag.Get(ctx, deal.Ref)
|
|
|
|
if err != nil {
|
|
|
|
return 0, xerrors.Errorf("failed to get file root for deal: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: abstract this away into ReadSizeCloser + implement different modes
|
|
|
|
node, err := unixfile.NewUnixfsFile(ctx, n.dag, root)
|
|
|
|
if err != nil {
|
|
|
|
return 0, xerrors.Errorf("cannot open unixfs file: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
uf, ok := node.(sectorblocks.UnixfsReader)
|
|
|
|
if !ok {
|
|
|
|
// we probably got directory, unsupported for now
|
|
|
|
return 0, xerrors.Errorf("unsupported unixfs file type")
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: uf.Size() is user input, not trusted
|
|
|
|
// This won't be useful / here after we migrate to putting CARs into sectors
|
|
|
|
size, err := uf.Size()
|
|
|
|
if err != nil {
|
|
|
|
return 0, xerrors.Errorf("getting unixfs file size: %w", err)
|
|
|
|
}
|
|
|
|
if padreader.PaddedSize(uint64(size)) != deal.Proposal.PieceSize {
|
|
|
|
return 0, xerrors.Errorf("deal.Proposal.PieceSize didn't match padded unixfs file size")
|
|
|
|
}
|
|
|
|
|
|
|
|
sectorID, err := n.secb.AddUnixfsPiece(ctx, uf, deal.DealID)
|
|
|
|
if err != nil {
|
|
|
|
return 0, xerrors.Errorf("AddPiece failed: %s", err)
|
|
|
|
}
|
|
|
|
log.Warnf("New Sector: %d (deal %d)", sectorID, deal.DealID)
|
|
|
|
|
|
|
|
return sectorID, nil
|
|
|
|
}
|
|
|
|
|
2019-12-17 10:46:39 +00:00
|
|
|
func (n *ProviderNodeAdapter) ListProviderDeals(ctx context.Context, addr address.Address) ([]storagemarket.StorageDeal, error) {
|
2020-02-11 23:29:45 +00:00
|
|
|
allDeals, err := n.StateMarketDeals(ctx, types.EmptyTSK)
|
2019-11-04 19:57:54 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2019-12-17 10:46:39 +00:00
|
|
|
var out []storagemarket.StorageDeal
|
2019-11-04 19:57:54 +00:00
|
|
|
|
|
|
|
for _, deal := range allDeals {
|
2020-01-10 18:21:46 +00:00
|
|
|
sharedDeal := utils.FromOnChainDeal(deal)
|
2019-12-17 10:46:39 +00:00
|
|
|
if sharedDeal.Provider == addr {
|
|
|
|
out = append(out, sharedDeal)
|
2019-11-04 19:57:54 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return out, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (n *ProviderNodeAdapter) GetMinerWorker(ctx context.Context, miner address.Address) (address.Address, error) {
|
2020-02-11 23:29:45 +00:00
|
|
|
addr, err := n.StateMinerWorker(ctx, miner, types.EmptyTSK)
|
2020-01-10 18:01:48 +00:00
|
|
|
return addr, err
|
2019-11-04 19:57:54 +00:00
|
|
|
}
|
|
|
|
|
2019-12-17 10:46:39 +00:00
|
|
|
func (n *ProviderNodeAdapter) SignBytes(ctx context.Context, signer address.Address, b []byte) (*sharedtypes.Signature, error) {
|
2020-01-10 18:01:48 +00:00
|
|
|
localSignature, err := n.WalletSign(ctx, signer, b)
|
2019-12-17 10:46:39 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2020-01-10 18:21:46 +00:00
|
|
|
return utils.ToSharedSignature(localSignature)
|
2019-11-04 19:57:54 +00:00
|
|
|
}
|
|
|
|
|
2019-12-17 10:46:39 +00:00
|
|
|
func (n *ProviderNodeAdapter) EnsureFunds(ctx context.Context, addr address.Address, amt tokenamount.TokenAmount) error {
|
2020-01-10 18:21:46 +00:00
|
|
|
return n.MarketEnsureAvailable(ctx, addr, utils.FromSharedTokenAmount(amt))
|
2019-11-04 19:57:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (n *ProviderNodeAdapter) MostRecentStateId(ctx context.Context) (storagemarket.StateKey, error) {
|
|
|
|
return n.ChainHead(ctx)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Adds funds with the StorageMinerActor for a storage participant. Used by both providers and clients.
|
2019-12-17 10:46:39 +00:00
|
|
|
func (n *ProviderNodeAdapter) AddFunds(ctx context.Context, addr address.Address, amount tokenamount.TokenAmount) error {
|
2019-11-04 19:57:54 +00:00
|
|
|
// (Provider Node API)
|
|
|
|
smsg, err := n.MpoolPushMessage(ctx, &types.Message{
|
|
|
|
To: actors.StorageMarketAddress,
|
2020-01-10 18:01:48 +00:00
|
|
|
From: addr,
|
2020-01-10 18:21:46 +00:00
|
|
|
Value: utils.FromSharedTokenAmount(amount),
|
2019-11-04 19:57:54 +00:00
|
|
|
GasPrice: types.NewInt(0),
|
|
|
|
GasLimit: types.NewInt(1000000),
|
|
|
|
Method: actors.SMAMethods.AddBalance,
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
r, err := n.StateWaitMsg(ctx, smsg.Cid())
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if r.Receipt.ExitCode != 0 {
|
|
|
|
return xerrors.Errorf("adding funds to storage miner market actor failed: exit %d", r.Receipt.ExitCode)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (n *ProviderNodeAdapter) GetBalance(ctx context.Context, addr address.Address) (storagemarket.Balance, error) {
|
2020-02-11 23:29:45 +00:00
|
|
|
bal, err := n.StateMarketBalance(ctx, addr, types.EmptyTSK)
|
2019-11-04 19:57:54 +00:00
|
|
|
if err != nil {
|
|
|
|
return storagemarket.Balance{}, err
|
|
|
|
}
|
|
|
|
|
2020-01-10 18:21:46 +00:00
|
|
|
return utils.ToSharedBalance(bal), nil
|
2019-11-04 19:57:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var _ storagemarket.StorageProviderNode = &ProviderNodeAdapter{}
|