deals: Sending initial proposal works

This commit is contained in:
Łukasz Magiera 2019-08-02 18:25:10 +02:00 committed by whyrusleeping
parent 322031d8e4
commit b65041cac1
8 changed files with 71 additions and 21 deletions

View File

@ -7,6 +7,17 @@ import (
"github.com/filecoin-project/go-lotus/lib/jsonrpc" "github.com/filecoin-project/go-lotus/lib/jsonrpc"
) )
// NewCommonRPC creates a new http jsonrpc client.
func NewCommonRPC(addr string, requestHeader http.Header) (api.Common, error) {
var res api.CommonStruct
_, err := jsonrpc.NewMergeClient(addr, "Filecoin",
[]interface{}{
&res.Internal,
}, requestHeader)
return &res, err
}
// NewFullNodeRPC creates a new http jsonrpc client. // NewFullNodeRPC creates a new http jsonrpc client.
func NewFullNodeRPC(addr string, requestHeader http.Header) (api.FullNode, error) { func NewFullNodeRPC(addr string, requestHeader http.Header) (api.FullNode, error) {
var res api.FullNodeStruct var res api.FullNodeStruct

View File

@ -6,7 +6,9 @@ import (
"os" "os"
"sync/atomic" "sync/atomic"
sectorbuilder "github.com/filecoin-project/go-sectorbuilder"
"github.com/ipfs/go-cid" "github.com/ipfs/go-cid"
cbor "github.com/ipfs/go-ipld-cbor"
logging "github.com/ipfs/go-log" logging "github.com/ipfs/go-log"
"github.com/libp2p/go-libp2p-core/host" "github.com/libp2p/go-libp2p-core/host"
"github.com/libp2p/go-libp2p-core/peer" "github.com/libp2p/go-libp2p-core/peer"
@ -14,8 +16,8 @@ import (
"github.com/filecoin-project/go-lotus/chain/address" "github.com/filecoin-project/go-lotus/chain/address"
"github.com/filecoin-project/go-lotus/chain/store" "github.com/filecoin-project/go-lotus/chain/store"
"github.com/filecoin-project/go-lotus/chain/types" "github.com/filecoin-project/go-lotus/chain/types"
"github.com/filecoin-project/go-lotus/chain/wallet"
"github.com/filecoin-project/go-lotus/lib/cborrpc" "github.com/filecoin-project/go-lotus/lib/cborrpc"
"github.com/filecoin-project/go-lotus/lib/sectorbuilder"
) )
var log = logging.Logger("deals") var log = logging.Logger("deals")
@ -36,8 +38,8 @@ type Deal struct {
type Client struct { type Client struct {
cs *store.ChainStore cs *store.ChainStore
sb *sectorbuilder.SectorBuilder
h host.Host h host.Host
w *wallet.Wallet
next uint64 next uint64
deals map[uint64]Deal deals map[uint64]Deal
@ -48,9 +50,11 @@ type Client struct {
stopped chan struct{} stopped chan struct{}
} }
func NewClient(cs *store.ChainStore) *Client { func NewClient(cs *store.ChainStore, h host.Host, w *wallet.Wallet) *Client {
c := &Client{ c := &Client{
cs: cs, cs: cs,
h: h,
w: w,
deals: map[uint64]Deal{}, deals: map[uint64]Deal{},
@ -95,7 +99,7 @@ func (c *Client) Start(ctx context.Context, data cid.Cid, totalPrice types.BigIn
if err := f.Close(); err != nil { if err := f.Close(); err != nil {
return 0, err return 0, err
} }
commP, err := c.sb.GeneratePieceCommitment(f.Name(), 6) commP, err := sectorbuilder.GeneratePieceCommitment(f.Name(), 6)
if err != nil { if err != nil {
return 0, err return 0, err
} }
@ -103,6 +107,8 @@ func (c *Client) Start(ctx context.Context, data cid.Cid, totalPrice types.BigIn
return 0, err return 0, err
} }
dummyCid, _ := cid.Parse("bafkqaaa")
// TODO: use data // TODO: use data
proposal := StorageDealProposal{ proposal := StorageDealProposal{
PieceRef: "bafkqabtimvwgy3yk", // identity 'hello\n' PieceRef: "bafkqabtimvwgy3yk", // identity 'hello\n'
@ -111,7 +117,13 @@ func (c *Client) Start(ctx context.Context, data cid.Cid, totalPrice types.BigIn
Size: 6, Size: 6,
TotalPrice: totalPrice, TotalPrice: totalPrice,
Duration: blocksDuration, Duration: blocksDuration,
Payment: nil, // TODO Payment: PaymentInfo{
PayChActor: address.Address{},
Payer: address.Address{},
Channel: 0,
ChannelMessage: dummyCid,
Vouchers: nil,
},
MinerAddress: miner, MinerAddress: miner,
ClientAddress: from, ClientAddress: from,
} }
@ -124,21 +136,34 @@ func (c *Client) Start(ctx context.Context, data cid.Cid, totalPrice types.BigIn
log.Info("Sending deal proposal") log.Info("Sending deal proposal")
msg, err := cbor.DumpObject(proposal)
if err != nil {
return 0, err
}
sig, err := c.w.Sign(from, msg)
if err != nil {
return 0, err
}
signedProposal := &SignedStorageDealProposal{ signedProposal := &SignedStorageDealProposal{
Proposal: proposal, Proposal: proposal,
Signature: nil, // TODO: SIGN! Signature: sig,
} }
if err := cborrpc.WriteCborRPC(s, signedProposal); err != nil { if err := cborrpc.WriteCborRPC(s, signedProposal); err != nil {
return 0, err return 0, err
} }
log.Info("Reading response")
var resp SignedStorageDealResponse var resp SignedStorageDealResponse
if err := cborrpc.ReadCborRPC(s, &resp); err != nil { if err := cborrpc.ReadCborRPC(s, &resp); err != nil {
log.Errorw("failed to read StorageDealResponse message", "error", err) log.Errorw("failed to read StorageDealResponse message", "error", err)
return 0, err return 0, err
} }
log.Info("Registering deal")
id := atomic.AddUint64(&c.next, 1) id := atomic.AddUint64(&c.next, 1)
deal := Deal{ deal := Deal{
ID: id, ID: id,

View File

@ -15,6 +15,8 @@ func NewHandler() *Handler {
func (h *Handler) HandleStream(s inet.Stream) { func (h *Handler) HandleStream(s inet.Stream) {
defer s.Close() defer s.Close()
log.Info("Handling storage deal proposal!")
var proposal SignedStorageDealProposal var proposal SignedStorageDealProposal
if err := cborrpc.ReadCborRPC(s, &proposal); err != nil { if err := cborrpc.ReadCborRPC(s, &proposal); err != nil {
log.Errorw("failed to read proposal message", "error", err) log.Errorw("failed to read proposal message", "error", err)
@ -27,11 +29,11 @@ func (h *Handler) HandleStream(s inet.Stream) {
response := StorageDealResponse{ response := StorageDealResponse{
State: Accepted, State: Accepted,
Message: "", Message: "",
Proposal: nil, // TODO //Proposal: , // TODO
} }
signedResponse := &SignedStorageDealResponse{ signedResponse := &SignedStorageDealResponse{
Response: response, Response: response,
Signature: nil, // TODO //Signature: sig, // TODO
} }
if err := cborrpc.WriteCborRPC(s, signedResponse); err != nil { if err := cborrpc.WriteCborRPC(s, signedResponse); err != nil {
log.Errorw("failed to write deal response", "error", err) log.Errorw("failed to write deal response", "error", err)

View File

@ -69,7 +69,7 @@ type StorageDealProposal struct {
type SignedStorageDealProposal struct { type SignedStorageDealProposal struct {
Proposal StorageDealProposal Proposal StorageDealProposal
Signature types.Signature Signature *types.Signature
} }
// response // response
@ -96,5 +96,5 @@ type StorageDealResponse struct {
type SignedStorageDealResponse struct { type SignedStorageDealResponse struct {
Response StorageDealResponse Response StorageDealResponse
Signature types.Signature Signature *types.Signature
} }

View File

@ -88,10 +88,6 @@ func (sb *SectorBuilder) GetAllStagedSectors() ([]StagedSectorMetadata, error) {
return sectorbuilder.GetAllStagedSectors(sb.handle) return sectorbuilder.GetAllStagedSectors(sb.handle)
} }
func (sb *SectorBuilder) GeneratePieceCommitment(piecePath string, pieceSize uint64) ([CommLen]byte, error) {
return sectorbuilder.GeneratePieceCommitment(piecePath, pieceSize)
}
func (sb *SectorBuilder) GeneratePoSt(sortedCommRs [][CommLen]byte, challengeSeed [CommLen]byte) ([][]byte, []uint64, error) { func (sb *SectorBuilder) GeneratePoSt(sortedCommRs [][CommLen]byte, challengeSeed [CommLen]byte) ([][]byte, []uint64, error) {
// Wait, this is a blocking method with no way of interrupting it? // Wait, this is a blocking method with no way of interrupting it?
// does it checkpoint itself? // does it checkpoint itself?

View File

@ -73,6 +73,7 @@ const (
HandleIncomingMessagesKey HandleIncomingMessagesKey
RunDealClientKey RunDealClientKey
HandleDealsKey
// daemon // daemon
ExtractApiKey ExtractApiKey
@ -219,6 +220,9 @@ func Online() Option {
ApplyIf(func(s *Settings) bool { return s.nodeType == nodeStorageMiner }, ApplyIf(func(s *Settings) bool { return s.nodeType == nodeStorageMiner },
Override(new(*sectorbuilder.SectorBuilder), modules.SectorBuilder), Override(new(*sectorbuilder.SectorBuilder), modules.SectorBuilder),
Override(new(*storage.Miner), modules.StorageMiner), Override(new(*storage.Miner), modules.StorageMiner),
Override(new(*deals.Handler), deals.NewHandler),
Override(HandleDealsKey, modules.HandleDeals),
), ),
) )
} }

View File

@ -44,9 +44,14 @@ type FullNodeAPI struct {
} }
func (a *FullNodeAPI) ClientStartDeal(ctx context.Context, data cid.Cid, miner address.Address, blocksDuration uint64) error { func (a *FullNodeAPI) ClientStartDeal(ctx context.Context, data cid.Cid, miner address.Address, blocksDuration uint64) error {
self, err := a.WalletDefaultAddress(ctx)
if err != nil {
return err
}
msg := &types.Message{ msg := &types.Message{
To: miner, To: miner,
From: miner, // TODO: we need /something/ here, but this smells From: miner,
Method: actors.MAMethods.GetPeerID, Method: actors.MAMethods.GetPeerID,
} }
@ -59,7 +64,9 @@ func (a *FullNodeAPI) ClientStartDeal(ctx context.Context, data cid.Cid, miner a
return err return err
} }
_, err = a.DealClient.Start(ctx, data, miner, pid, blocksDuration) price := types.NewInt(10 * blocksDuration) // TODO: allow to actually specify this
_, err = a.DealClient.Start(ctx, data, price, self, miner, pid, blocksDuration)
return err return err
} }

View File

@ -11,6 +11,7 @@ import (
"github.com/filecoin-project/go-lotus/api" "github.com/filecoin-project/go-lotus/api"
"github.com/filecoin-project/go-lotus/chain/address" "github.com/filecoin-project/go-lotus/chain/address"
"github.com/filecoin-project/go-lotus/chain/deals"
"github.com/filecoin-project/go-lotus/chain/wallet" "github.com/filecoin-project/go-lotus/chain/wallet"
"github.com/filecoin-project/go-lotus/lib/sectorbuilder" "github.com/filecoin-project/go-lotus/lib/sectorbuilder"
"github.com/filecoin-project/go-lotus/node/modules/dtypes" "github.com/filecoin-project/go-lotus/node/modules/dtypes"
@ -94,3 +95,7 @@ func StorageMiner(mctx helpers.MetricsCtx, lc fx.Lifecycle, api api.FullNode, h
return sm, nil return sm, nil
} }
func HandleDeals(h host.Host, handler *deals.Handler) {
h.SetStreamHandler(deals.ProtocolID, handler.HandleStream)
}