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"
)
// 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.
func NewFullNodeRPC(addr string, requestHeader http.Header) (api.FullNode, error) {
var res api.FullNodeStruct

View File

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

View File

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

View File

@ -69,7 +69,7 @@ type StorageDealProposal struct {
type SignedStorageDealProposal struct {
Proposal StorageDealProposal
Signature types.Signature
Signature *types.Signature
}
// response
@ -96,5 +96,5 @@ type StorageDealResponse struct {
type SignedStorageDealResponse struct {
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)
}
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) {
// Wait, this is a blocking method with no way of interrupting it?
// does it checkpoint itself?

View File

@ -73,6 +73,7 @@ const (
HandleIncomingMessagesKey
RunDealClientKey
HandleDealsKey
// daemon
ExtractApiKey
@ -219,6 +220,9 @@ func Online() Option {
ApplyIf(func(s *Settings) bool { return s.nodeType == nodeStorageMiner },
Override(new(*sectorbuilder.SectorBuilder), modules.SectorBuilder),
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 {
self, err := a.WalletDefaultAddress(ctx)
if err != nil {
return err
}
msg := &types.Message{
To: miner,
From: miner, // TODO: we need /something/ here, but this smells
From: miner,
Method: actors.MAMethods.GetPeerID,
}
@ -59,7 +64,9 @@ func (a *FullNodeAPI) ClientStartDeal(ctx context.Context, data cid.Cid, miner a
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
}

View File

@ -11,6 +11,7 @@ import (
"github.com/filecoin-project/go-lotus/api"
"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/lib/sectorbuilder"
"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
}
func HandleDeals(h host.Host, handler *deals.Handler) {
h.SetStreamHandler(deals.ProtocolID, handler.HandleStream)
}