lotus/chain/deals/client.go

197 lines
4.2 KiB
Go
Raw Normal View History

2019-08-01 17:12:41 +00:00
package deals
import (
"context"
2019-08-02 14:09:54 +00:00
"io/ioutil"
2019-08-06 22:04:21 +00:00
"math"
2019-08-02 14:09:54 +00:00
"os"
2019-08-01 17:12:41 +00:00
2019-08-02 16:25:10 +00:00
sectorbuilder "github.com/filecoin-project/go-sectorbuilder"
2019-08-01 17:12:41 +00:00
"github.com/ipfs/go-cid"
2019-08-06 22:04:21 +00:00
"github.com/ipfs/go-datastore"
"github.com/ipfs/go-datastore/namespace"
2019-08-02 16:25:10 +00:00
cbor "github.com/ipfs/go-ipld-cbor"
2019-08-01 17:12:41 +00:00
logging "github.com/ipfs/go-log"
2019-08-02 14:09:54 +00:00
"github.com/libp2p/go-libp2p-core/host"
2019-08-01 17:12:41 +00:00
"github.com/libp2p/go-libp2p-core/peer"
2019-08-06 22:04:21 +00:00
"golang.org/x/xerrors"
2019-08-01 17:12:41 +00:00
"github.com/filecoin-project/go-lotus/chain/address"
"github.com/filecoin-project/go-lotus/chain/store"
"github.com/filecoin-project/go-lotus/chain/types"
2019-08-02 16:25:10 +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-01 17:12:41 +00:00
)
var log = logging.Logger("deals")
2019-08-02 14:09:54 +00:00
const ProtocolID = "/fil/storage/mk/1.0.0"
2019-08-01 17:12:41 +00:00
type DealStatus int
const (
DealResolvingMiner = DealStatus(iota)
)
2019-08-06 22:04:21 +00:00
type ClientDeal struct {
ProposalCid cid.Cid
Status DealStatus
Miner peer.ID
2019-08-01 17:12:41 +00:00
}
type Client struct {
cs *store.ChainStore
2019-08-02 14:09:54 +00:00
h host.Host
2019-08-02 16:25:10 +00:00
w *wallet.Wallet
2019-08-01 17:12:41 +00:00
2019-08-06 22:04:21 +00:00
deals StateStore
2019-08-01 17:12:41 +00:00
2019-08-06 22:04:21 +00:00
incoming chan ClientDeal
2019-08-01 17:12:41 +00:00
stop chan struct{}
stopped chan struct{}
}
2019-08-06 22:04:21 +00:00
func NewClient(cs *store.ChainStore, h host.Host, w *wallet.Wallet, ds dtypes.MetadataDS) *Client {
2019-08-01 17:12:41 +00:00
c := &Client{
cs: cs,
2019-08-02 16:25:10 +00:00
h: h,
w: w,
2019-08-01 17:12:41 +00:00
2019-08-06 22:04:21 +00:00
deals: StateStore{ds: namespace.Wrap(ds, datastore.NewKey("/deals/client"))},
2019-08-01 17:12:41 +00:00
2019-08-06 22:04:21 +00:00
incoming: make(chan ClientDeal, 16),
2019-08-01 17:12:41 +00:00
stop: make(chan struct{}),
stopped: make(chan struct{}),
}
return c
}
func (c *Client) Run() {
go func() {
defer close(c.stopped)
for {
select {
case deal := <-c.incoming:
log.Info("incoming deal")
2019-08-02 14:09:54 +00:00
// TODO: track in datastore
2019-08-06 22:04:21 +00:00
if err := c.deals.Begin(deal.ProposalCid, deal); err != nil {
log.Errorf("deal state begin failed: %s", err)
continue
}
2019-08-01 17:12:41 +00:00
case <-c.stop:
return
}
}
}()
}
2019-08-06 22:04:21 +00:00
func (c *Client) Start(ctx context.Context, data cid.Cid, totalPrice types.BigInt, from address.Address, miner address.Address, minerID peer.ID, blocksDuration uint64) (cid.Cid, error) {
2019-08-02 14:09:54 +00:00
// TODO: Eww
f, err := ioutil.TempFile(os.TempDir(), "commP-temp-")
2019-08-01 17:12:41 +00:00
if err != nil {
2019-08-06 22:04:21 +00:00
return cid.Undef, err
2019-08-01 17:12:41 +00:00
}
2019-08-02 14:09:54 +00:00
_, err = f.Write([]byte("hello\n"))
2019-08-01 17:12:41 +00:00
if err != nil {
2019-08-06 22:04:21 +00:00
return cid.Undef, err
2019-08-02 14:09:54 +00:00
}
if err := f.Close(); err != nil {
2019-08-06 22:04:21 +00:00
return cid.Undef, err
2019-08-02 14:09:54 +00:00
}
2019-08-02 16:25:10 +00:00
commP, err := sectorbuilder.GeneratePieceCommitment(f.Name(), 6)
2019-08-02 14:09:54 +00:00
if err != nil {
2019-08-06 22:04:21 +00:00
return cid.Undef, err
2019-08-02 14:09:54 +00:00
}
if err := os.Remove(f.Name()); err != nil {
2019-08-06 22:04:21 +00:00
return cid.Undef, err
2019-08-01 17:12:41 +00:00
}
2019-08-02 16:25:10 +00:00
dummyCid, _ := cid.Parse("bafkqaaa")
2019-08-02 14:09:54 +00:00
// TODO: use data
proposal := StorageDealProposal{
PieceRef: "bafkqabtimvwgy3yk", // identity 'hello\n'
SerializationMode: SerializationRaw,
CommP: commP[:],
Size: 6,
TotalPrice: totalPrice,
Duration: blocksDuration,
2019-08-02 16:25:10 +00:00
Payment: PaymentInfo{
PayChActor: address.Address{},
Payer: address.Address{},
ChannelMessage: dummyCid,
Vouchers: nil,
},
MinerAddress: miner,
ClientAddress: from,
2019-08-01 17:12:41 +00:00
}
2019-08-02 14:09:54 +00:00
s, err := c.h.NewStream(ctx, minerID, ProtocolID)
2019-08-01 17:12:41 +00:00
if err != nil {
2019-08-06 22:04:21 +00:00
return cid.Undef, err
2019-08-01 17:12:41 +00:00
}
2019-08-02 14:09:54 +00:00
defer s.Close() // TODO: not too soon?
log.Info("Sending deal proposal")
2019-08-02 16:25:10 +00:00
msg, err := cbor.DumpObject(proposal)
if err != nil {
2019-08-06 22:04:21 +00:00
return cid.Undef, err
2019-08-02 16:25:10 +00:00
}
sig, err := c.w.Sign(from, msg)
if err != nil {
2019-08-06 22:04:21 +00:00
return cid.Undef, err
2019-08-02 16:25:10 +00:00
}
2019-08-02 14:09:54 +00:00
signedProposal := &SignedStorageDealProposal{
Proposal: proposal,
2019-08-02 16:25:10 +00:00
Signature: sig,
2019-08-01 17:12:41 +00:00
}
2019-08-02 14:09:54 +00:00
if err := cborrpc.WriteCborRPC(s, signedProposal); err != nil {
2019-08-06 22:04:21 +00:00
return cid.Undef, err
2019-08-01 17:12:41 +00:00
}
2019-08-02 16:25:10 +00:00
log.Info("Reading response")
2019-08-02 14:09:54 +00:00
var resp SignedStorageDealResponse
if err := cborrpc.ReadCborRPC(s, &resp); err != nil {
log.Errorw("failed to read StorageDealResponse message", "error", err)
2019-08-06 22:04:21 +00:00
return cid.Undef, err
}
// TODO: verify signature
if resp.Response.State != Accepted {
return cid.Undef, xerrors.Errorf("Deal wasn't accepted (State=%d)", resp.Response.State)
2019-08-02 14:09:54 +00:00
}
2019-08-01 17:12:41 +00:00
2019-08-02 16:25:10 +00:00
log.Info("Registering deal")
2019-08-06 22:04:21 +00:00
proposalNd, err := cbor.WrapObject(proposal, math.MaxUint64, -1)
if err != nil {
return cid.Undef, err
}
deal := ClientDeal{
ProposalCid: proposalNd.Cid(),
Status: DealResolvingMiner,
Miner: minerID,
2019-08-01 17:12:41 +00:00
}
c.incoming <- deal
2019-08-06 22:04:21 +00:00
return proposalNd.Cid(), nil
2019-08-01 17:12:41 +00:00
}
func (c *Client) Stop() {
close(c.stop)
<-c.stopped
}