2019-08-07 18:57:48 +00:00
|
|
|
package deals
|
|
|
|
|
|
|
|
import (
|
2019-08-12 23:54:53 +00:00
|
|
|
"context"
|
2019-08-07 18:57:48 +00:00
|
|
|
"runtime"
|
|
|
|
|
2019-10-18 04:47:41 +00:00
|
|
|
"github.com/filecoin-project/lotus/api"
|
2019-09-13 21:00:36 +00:00
|
|
|
|
2019-10-18 04:47:41 +00:00
|
|
|
"github.com/filecoin-project/lotus/chain/actors"
|
|
|
|
"github.com/filecoin-project/lotus/chain/address"
|
|
|
|
"github.com/filecoin-project/lotus/chain/types"
|
|
|
|
"github.com/filecoin-project/lotus/lib/cborrpc"
|
2019-08-07 18:57:48 +00:00
|
|
|
|
|
|
|
"github.com/ipfs/go-cid"
|
2019-08-12 23:54:53 +00:00
|
|
|
cbor "github.com/ipfs/go-ipld-cbor"
|
|
|
|
inet "github.com/libp2p/go-libp2p-core/network"
|
2019-08-07 18:57:48 +00:00
|
|
|
"golang.org/x/xerrors"
|
|
|
|
)
|
|
|
|
|
2019-10-21 18:12:11 +00:00
|
|
|
func (p *Provider) failDeal(id cid.Cid, cerr error) {
|
|
|
|
if err := p.deals.End(id); err != nil {
|
2019-08-07 18:57:48 +00:00
|
|
|
log.Warnf("deals.End: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if cerr == nil {
|
|
|
|
_, f, l, _ := runtime.Caller(1)
|
|
|
|
cerr = xerrors.Errorf("unknown error (fail called at %s:%d)", f, l)
|
|
|
|
}
|
|
|
|
|
2019-08-07 19:48:53 +00:00
|
|
|
log.Errorf("deal %s failed: %s", id, cerr)
|
2019-08-07 18:57:48 +00:00
|
|
|
|
2019-10-22 10:20:43 +00:00
|
|
|
err := p.sendSignedResponse(Response{
|
2019-09-10 14:13:24 +00:00
|
|
|
State: api.DealFailed,
|
2019-08-07 18:57:48 +00:00
|
|
|
Message: cerr.Error(),
|
|
|
|
Proposal: id,
|
|
|
|
})
|
|
|
|
|
2019-10-21 18:12:11 +00:00
|
|
|
s, ok := p.conns[id]
|
2019-08-07 18:57:48 +00:00
|
|
|
if ok {
|
2019-09-10 12:35:43 +00:00
|
|
|
_ = s.Reset()
|
2019-10-21 18:12:11 +00:00
|
|
|
delete(p.conns, id)
|
2019-08-07 18:57:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if err != nil {
|
2019-08-08 17:16:41 +00:00
|
|
|
log.Warnf("notifying client about deal failure: %s", err)
|
2019-08-07 18:57:48 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-10-21 18:12:11 +00:00
|
|
|
func (p *Provider) readProposal(s inet.Stream) (proposal actors.StorageDealProposal, err error) {
|
2019-08-07 18:57:48 +00:00
|
|
|
if err := cborrpc.ReadCborRPC(s, &proposal); err != nil {
|
|
|
|
log.Errorw("failed to read proposal message", "error", err)
|
2019-10-21 18:12:11 +00:00
|
|
|
return proposal, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := proposal.Verify(); err != nil {
|
|
|
|
return proposal, xerrors.Errorf("verifying StorageDealProposal: %w", err)
|
2019-08-07 18:57:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: Validate proposal maybe
|
|
|
|
// (and signature, obviously)
|
|
|
|
|
2019-10-21 18:12:11 +00:00
|
|
|
if proposal.Provider != p.actor {
|
|
|
|
log.Errorf("proposal with wrong ProviderAddress: %s", proposal.Provider)
|
|
|
|
return proposal, err
|
2019-08-07 18:57:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-10-22 10:20:43 +00:00
|
|
|
func (p *Provider) sendSignedResponse(resp Response) error {
|
2019-10-21 18:12:11 +00:00
|
|
|
s, ok := p.conns[resp.Proposal]
|
2019-08-07 18:57:48 +00:00
|
|
|
if !ok {
|
|
|
|
return xerrors.New("couldn't send response: not connected")
|
|
|
|
}
|
|
|
|
|
|
|
|
msg, err := cbor.DumpObject(&resp)
|
|
|
|
if err != nil {
|
|
|
|
return xerrors.Errorf("serializing response: %w", err)
|
|
|
|
}
|
|
|
|
|
2019-10-21 18:12:11 +00:00
|
|
|
worker, err := p.getWorker(p.actor)
|
2019-08-12 23:54:53 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
2019-08-07 18:57:48 +00:00
|
|
|
}
|
|
|
|
|
2019-10-21 18:12:11 +00:00
|
|
|
sig, err := p.full.WalletSign(context.TODO(), worker, msg)
|
2019-08-07 18:57:48 +00:00
|
|
|
if err != nil {
|
|
|
|
return xerrors.Errorf("failed to sign response message: %w", err)
|
|
|
|
}
|
|
|
|
|
2019-10-22 10:20:43 +00:00
|
|
|
signedResponse := SignedResponse{
|
2019-08-07 18:57:48 +00:00
|
|
|
Response: resp,
|
|
|
|
Signature: sig,
|
|
|
|
}
|
|
|
|
|
|
|
|
err = cborrpc.WriteCborRPC(s, signedResponse)
|
|
|
|
if err != nil {
|
|
|
|
// Assume client disconnected
|
|
|
|
s.Close()
|
2019-10-21 18:12:11 +00:00
|
|
|
delete(p.conns, resp.Proposal)
|
2019-08-07 18:57:48 +00:00
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|
2019-09-13 21:00:36 +00:00
|
|
|
|
2019-10-21 18:12:11 +00:00
|
|
|
func (p *Provider) getWorker(miner address.Address) (address.Address, error) {
|
2019-09-13 21:00:36 +00:00
|
|
|
getworker := &types.Message{
|
|
|
|
To: miner,
|
|
|
|
From: miner,
|
|
|
|
Method: actors.MAMethods.GetWorkerAddr,
|
|
|
|
}
|
2019-10-21 18:12:11 +00:00
|
|
|
r, err := p.full.StateCall(context.TODO(), getworker, nil)
|
2019-09-13 21:00:36 +00:00
|
|
|
if err != nil {
|
|
|
|
return address.Undef, xerrors.Errorf("getting worker address: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if r.ExitCode != 0 {
|
|
|
|
return address.Undef, xerrors.Errorf("getWorker call failed: %d", r.ExitCode)
|
|
|
|
}
|
|
|
|
|
|
|
|
return address.NewFromBytes(r.Return)
|
|
|
|
}
|