2019-08-02 14:09:54 +00:00
|
|
|
package deals
|
|
|
|
|
|
|
|
import (
|
2019-08-06 22:04:21 +00:00
|
|
|
"context"
|
2019-08-06 23:08:34 +00:00
|
|
|
"github.com/filecoin-project/go-lotus/chain/address"
|
2019-08-06 22:04:21 +00:00
|
|
|
"github.com/filecoin-project/go-lotus/lib/sectorbuilder"
|
|
|
|
"github.com/ipfs/go-cid"
|
|
|
|
"github.com/ipfs/go-datastore"
|
|
|
|
"github.com/ipfs/go-datastore/namespace"
|
|
|
|
files "github.com/ipfs/go-ipfs-files"
|
|
|
|
"github.com/ipfs/go-merkledag"
|
|
|
|
unixfile "github.com/ipfs/go-unixfs/file"
|
|
|
|
"math"
|
|
|
|
|
2019-08-06 12:31:13 +00:00
|
|
|
"github.com/filecoin-project/go-lotus/chain/wallet"
|
2019-08-02 14:09:54 +00:00
|
|
|
"github.com/filecoin-project/go-lotus/lib/cborrpc"
|
2019-08-06 22:04:21 +00:00
|
|
|
"github.com/filecoin-project/go-lotus/node/modules/dtypes"
|
2019-08-06 12:31:13 +00:00
|
|
|
|
|
|
|
cbor "github.com/ipfs/go-ipld-cbor"
|
2019-08-02 14:09:54 +00:00
|
|
|
inet "github.com/libp2p/go-libp2p-core/network"
|
2019-08-06 22:04:21 +00:00
|
|
|
"github.com/libp2p/go-libp2p-core/peer"
|
2019-08-02 14:09:54 +00:00
|
|
|
)
|
|
|
|
|
2019-08-06 23:08:34 +00:00
|
|
|
func init() {
|
|
|
|
cbor.RegisterCborType(MinerDeal{})
|
|
|
|
}
|
|
|
|
|
2019-08-06 22:04:21 +00:00
|
|
|
type MinerDeal struct {
|
|
|
|
Client peer.ID
|
|
|
|
Proposal StorageDealProposal
|
|
|
|
ProposalCid cid.Cid
|
|
|
|
State DealState
|
|
|
|
|
|
|
|
Ref cid.Cid
|
|
|
|
}
|
|
|
|
|
2019-08-02 14:09:54 +00:00
|
|
|
type Handler struct {
|
2019-08-06 22:04:21 +00:00
|
|
|
w *wallet.Wallet
|
|
|
|
sb *sectorbuilder.SectorBuilder
|
|
|
|
|
|
|
|
// TODO: Use a custom protocol or graphsync in the future
|
|
|
|
// TODO: GC
|
|
|
|
dag dtypes.StagingDAG
|
|
|
|
|
|
|
|
deals StateStore
|
|
|
|
|
|
|
|
incoming chan MinerDeal
|
|
|
|
|
2019-08-06 23:08:34 +00:00
|
|
|
actor address.Address
|
|
|
|
|
2019-08-06 22:04:21 +00:00
|
|
|
stop chan struct{}
|
|
|
|
stopped chan struct{}
|
2019-08-02 14:09:54 +00:00
|
|
|
}
|
|
|
|
|
2019-08-06 23:08:34 +00:00
|
|
|
func NewHandler(w *wallet.Wallet, ds dtypes.MetadataDS, sb *sectorbuilder.SectorBuilder, dag dtypes.StagingDAG) (*Handler, error) {
|
|
|
|
addr, err := ds.Get(datastore.NewKey("miner-address"))
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
minerAddress, err := address.NewFromBytes(addr)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2019-08-06 12:31:13 +00:00
|
|
|
return &Handler{
|
2019-08-06 22:04:21 +00:00
|
|
|
w: w,
|
2019-08-06 23:08:34 +00:00
|
|
|
sb: sb,
|
2019-08-06 22:04:21 +00:00
|
|
|
dag: dag,
|
|
|
|
|
2019-08-06 23:08:34 +00:00
|
|
|
incoming: make(chan MinerDeal),
|
|
|
|
|
|
|
|
actor: minerAddress,
|
|
|
|
|
2019-08-06 22:04:21 +00:00
|
|
|
deals: StateStore{ds: namespace.Wrap(ds, datastore.NewKey("/deals/client"))},
|
2019-08-06 23:08:34 +00:00
|
|
|
}, nil
|
2019-08-02 14:09:54 +00:00
|
|
|
}
|
|
|
|
|
2019-08-06 22:04:21 +00:00
|
|
|
func (h *Handler) Run(ctx context.Context) {
|
|
|
|
go func() {
|
2019-08-06 23:08:34 +00:00
|
|
|
defer log.Error("quitting deal handler loop")
|
2019-08-06 22:04:21 +00:00
|
|
|
defer close(h.stopped)
|
|
|
|
fetched := make(chan cid.Cid)
|
|
|
|
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case deal := <-h.incoming:
|
|
|
|
log.Info("incoming deal")
|
|
|
|
|
|
|
|
if err := h.deals.Begin(deal.ProposalCid, deal); err != nil {
|
|
|
|
// TODO: This can happen when client re-sends proposal
|
|
|
|
log.Errorf("deal tracking failed: %s", err)
|
2019-08-06 23:08:34 +00:00
|
|
|
continue
|
2019-08-06 22:04:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
go func(id cid.Cid) {
|
2019-08-06 23:17:41 +00:00
|
|
|
log.Info("fetching data for a deal")
|
2019-08-06 22:04:21 +00:00
|
|
|
err := merkledag.FetchGraph(ctx, deal.Ref, h.dag)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
select {
|
|
|
|
case fetched <- id:
|
|
|
|
case <-h.stop:
|
|
|
|
}
|
2019-08-06 23:17:41 +00:00
|
|
|
log.Info("Fetched!")
|
2019-08-06 22:04:21 +00:00
|
|
|
}(deal.ProposalCid)
|
|
|
|
case id := <-fetched:
|
|
|
|
// TODO: send response if client still there
|
|
|
|
// TODO: staging
|
|
|
|
|
|
|
|
// TODO: async
|
|
|
|
log.Info("sealing deal")
|
|
|
|
|
|
|
|
var deal MinerDeal
|
|
|
|
err := h.deals.MutateMiner(id, func(in MinerDeal) (MinerDeal, error) {
|
|
|
|
in.State = Sealing
|
|
|
|
deal = in
|
|
|
|
return in, nil
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
// TODO: fail deal
|
|
|
|
log.Errorf("deal tracking failed (set sealing): %s", err)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
root, err := h.dag.Get(ctx, deal.Ref)
|
|
|
|
if err != nil {
|
|
|
|
// TODO: fail deal
|
|
|
|
log.Errorf("failed to get file root for deal: %s", err)
|
2019-08-06 23:08:34 +00:00
|
|
|
continue
|
2019-08-06 22:04:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: abstract this away into ReadSizeCloser + implement different modes
|
|
|
|
n, err := unixfile.NewUnixfsFile(ctx, h.dag, root)
|
|
|
|
if err != nil {
|
|
|
|
// TODO: fail deal
|
|
|
|
log.Errorf("cannot open unixfs file: %s", err)
|
2019-08-06 23:08:34 +00:00
|
|
|
continue
|
2019-08-06 22:04:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
f, ok := n.(files.File)
|
|
|
|
if !ok {
|
|
|
|
// TODO: we probably got directory, how should we handle this in unixfs mode?
|
|
|
|
log.Errorf("unsupported unixfs type")
|
|
|
|
// TODO: fail deal
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
size, err := f.Size()
|
|
|
|
if err != nil {
|
|
|
|
log.Errorf("failed to get file size: %s", err)
|
|
|
|
// TODO: fail deal
|
2019-08-06 23:08:34 +00:00
|
|
|
continue
|
2019-08-06 22:04:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: can we use pipes?
|
|
|
|
sectorID, err := h.sb.AddPiece(ctx, deal.Proposal.PieceRef, uint64(size), f)
|
|
|
|
if err != nil {
|
|
|
|
// TODO: fail deal
|
|
|
|
log.Errorf("AddPiece failed: %s", err)
|
2019-08-06 23:08:34 +00:00
|
|
|
continue
|
2019-08-06 22:04:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
log.Warnf("New Sector: %d", sectorID)
|
|
|
|
|
|
|
|
// TODO: update state, tell client
|
|
|
|
case <-h.stop:
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
}
|
|
|
|
|
2019-08-02 14:09:54 +00:00
|
|
|
func (h *Handler) HandleStream(s inet.Stream) {
|
|
|
|
defer s.Close()
|
|
|
|
|
2019-08-02 16:25:10 +00:00
|
|
|
log.Info("Handling storage deal proposal!")
|
|
|
|
|
2019-08-02 14:09:54 +00:00
|
|
|
var proposal SignedStorageDealProposal
|
|
|
|
if err := cborrpc.ReadCborRPC(s, &proposal); err != nil {
|
|
|
|
log.Errorw("failed to read proposal message", "error", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: Validate proposal maybe
|
|
|
|
// (and signature, obviously)
|
|
|
|
|
2019-08-06 23:08:34 +00:00
|
|
|
if proposal.Proposal.MinerAddress != h.actor {
|
|
|
|
log.Errorf("proposal with wrong MinerAddress: %s", proposal.Proposal.MinerAddress)
|
|
|
|
// TODO: send error
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-08-06 22:04:21 +00:00
|
|
|
switch proposal.Proposal.SerializationMode {
|
|
|
|
//case SerializationRaw:
|
|
|
|
//case SerializationIPLD:
|
|
|
|
case SerializationUnixFs:
|
|
|
|
default:
|
|
|
|
log.Errorf("deal proposal with unsupported serialization: %s", proposal.Proposal.SerializationMode)
|
|
|
|
// TODO: send error
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-08-06 12:31:13 +00:00
|
|
|
// TODO: Review: Not signed?
|
|
|
|
proposalNd, err := cbor.WrapObject(proposal.Proposal, math.MaxUint64, -1)
|
|
|
|
if err != nil {
|
2019-08-06 23:08:34 +00:00
|
|
|
log.Error(err)
|
2019-08-06 12:31:13 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-08-02 14:09:54 +00:00
|
|
|
response := StorageDealResponse{
|
2019-08-06 22:04:21 +00:00
|
|
|
State: Accepted,
|
|
|
|
Message: "",
|
2019-08-06 12:31:13 +00:00
|
|
|
Proposal: proposalNd.Cid(),
|
2019-08-02 14:09:54 +00:00
|
|
|
}
|
2019-08-06 12:31:13 +00:00
|
|
|
|
|
|
|
msg, err := cbor.DumpObject(response)
|
|
|
|
if err != nil {
|
|
|
|
log.Errorw("failed to serialize response message", "error", err)
|
|
|
|
return
|
|
|
|
}
|
2019-08-06 23:08:34 +00:00
|
|
|
|
|
|
|
def, err := h.w.ListAddrs()
|
|
|
|
if err != nil {
|
|
|
|
log.Error(err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if len(def) != 1 {
|
|
|
|
// NOTE: If this ever happens for a good reason, implement this with GetWorker on the miner actor
|
|
|
|
// TODO: implement with GetWorker on the miner actor
|
|
|
|
log.Errorf("Expected only 1 address in wallet, got %d", len(def))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
sig, err := h.w.Sign(def[0], msg)
|
2019-08-06 12:31:13 +00:00
|
|
|
if err != nil {
|
|
|
|
log.Errorw("failed to sign response message", "error", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-08-06 23:08:34 +00:00
|
|
|
log.Info("accepting deal")
|
|
|
|
|
2019-08-02 14:09:54 +00:00
|
|
|
signedResponse := &SignedStorageDealResponse{
|
2019-08-06 22:04:21 +00:00
|
|
|
Response: response,
|
2019-08-06 12:31:13 +00:00
|
|
|
Signature: sig,
|
2019-08-02 14:09:54 +00:00
|
|
|
}
|
|
|
|
if err := cborrpc.WriteCborRPC(s, signedResponse); err != nil {
|
|
|
|
log.Errorw("failed to write deal response", "error", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-08-06 22:04:21 +00:00
|
|
|
ref, err := cid.Parse(proposal.Proposal.PieceRef)
|
|
|
|
if err != nil {
|
2019-08-06 23:08:34 +00:00
|
|
|
log.Error(err)
|
2019-08-06 22:04:21 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-08-06 23:08:34 +00:00
|
|
|
log.Info("processing deal")
|
|
|
|
|
2019-08-06 22:04:21 +00:00
|
|
|
h.incoming <- MinerDeal{
|
|
|
|
Client: s.Conn().RemotePeer(),
|
|
|
|
Proposal: proposal.Proposal,
|
|
|
|
ProposalCid: proposalNd.Cid(),
|
|
|
|
State: Accepted,
|
|
|
|
|
|
|
|
Ref: ref,
|
|
|
|
}
|
2019-08-02 14:09:54 +00:00
|
|
|
}
|
2019-08-06 23:08:34 +00:00
|
|
|
|
|
|
|
func (h *Handler) Stop() {
|
|
|
|
close(h.stop)
|
|
|
|
<-h.stopped
|
|
|
|
}
|