deals: setup payment channel for deals
This commit is contained in:
+44
-11
@@ -47,11 +47,13 @@ type FullNodeAPI struct {
|
||||
}
|
||||
|
||||
func (a *FullNodeAPI) ClientStartDeal(ctx context.Context, data cid.Cid, miner address.Address, price types.BigInt, blocksDuration uint64) (*cid.Cid, error) {
|
||||
// TODO: make this a param
|
||||
self, err := a.WalletDefaultAddress(ctx)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// get miner peerID
|
||||
msg := &types.Message{
|
||||
To: miner,
|
||||
From: miner,
|
||||
@@ -67,8 +69,35 @@ func (a *FullNodeAPI) ClientStartDeal(ctx context.Context, data cid.Cid, miner a
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// setup payments
|
||||
total := types.BigMul(price, types.NewInt(blocksDuration))
|
||||
c, err := a.DealClient.Start(ctx, data, total, self, miner, pid, blocksDuration)
|
||||
|
||||
// TODO: at least ping the miner before creating paych / locking the money
|
||||
paych, paychMsg, err := a.paychCreate(ctx, self, miner, total)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
sv, err := a.PaychVoucherCreate(ctx, paych, total, 0)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
proposal := deals.ClientDealProposal{
|
||||
Data: data,
|
||||
TotalPrice: total,
|
||||
Duration: blocksDuration,
|
||||
Payment: actors.PaymentInfo{
|
||||
PayChActor: paych,
|
||||
Payer: self,
|
||||
ChannelMessage: paychMsg,
|
||||
Vouchers: []types.SignedVoucher{*sv},
|
||||
},
|
||||
MinerAddress: miner,
|
||||
ClientAddress: self,
|
||||
MinerID: pid,
|
||||
}
|
||||
|
||||
c, err := a.DealClient.Start(ctx, proposal)
|
||||
return &c, err
|
||||
}
|
||||
|
||||
@@ -420,15 +449,19 @@ func (a *FullNodeAPI) StateMinerProvingSet(ctx context.Context, addr address.Add
|
||||
}
|
||||
|
||||
func (a *FullNodeAPI) PaychCreate(ctx context.Context, from, to address.Address, amt types.BigInt) (address.Address, error) {
|
||||
act, _, err := a.paychCreate(ctx, from, to, amt)
|
||||
return act, err
|
||||
}
|
||||
|
||||
func (a *FullNodeAPI) paychCreate(ctx context.Context, from, to address.Address, amt types.BigInt) (address.Address, cid.Cid, error) {
|
||||
params, aerr := actors.SerializeParams(&actors.PCAConstructorParams{To: to})
|
||||
if aerr != nil {
|
||||
return address.Undef, aerr
|
||||
return address.Undef, cid.Undef, aerr
|
||||
}
|
||||
|
||||
nonce, err := a.MpoolGetNonce(ctx, from)
|
||||
if err != nil {
|
||||
return address.Undef, err
|
||||
return address.Undef, cid.Undef, err
|
||||
}
|
||||
|
||||
enc, err := actors.SerializeParams(&actors.ExecParams{
|
||||
@@ -449,12 +482,12 @@ func (a *FullNodeAPI) PaychCreate(ctx context.Context, from, to address.Address,
|
||||
|
||||
ser, err := msg.Serialize()
|
||||
if err != nil {
|
||||
return address.Undef, err
|
||||
return address.Undef, cid.Undef, err
|
||||
}
|
||||
|
||||
sig, err := a.WalletSign(ctx, from, ser)
|
||||
if err != nil {
|
||||
return address.Undef, err
|
||||
return address.Undef, cid.Undef, err
|
||||
}
|
||||
|
||||
smsg := &types.SignedMessage{
|
||||
@@ -463,28 +496,28 @@ func (a *FullNodeAPI) PaychCreate(ctx context.Context, from, to address.Address,
|
||||
}
|
||||
|
||||
if err := a.MpoolPush(ctx, smsg); err != nil {
|
||||
return address.Undef, err
|
||||
return address.Undef, cid.Undef, err
|
||||
}
|
||||
|
||||
mwait, err := a.ChainWaitMsg(ctx, smsg.Cid())
|
||||
if err != nil {
|
||||
return address.Undef, err
|
||||
return address.Undef, cid.Undef, err
|
||||
}
|
||||
|
||||
if mwait.Receipt.ExitCode != 0 {
|
||||
return address.Undef, fmt.Errorf("payment channel creation failed (exit code %d)", mwait.Receipt.ExitCode)
|
||||
return address.Undef, cid.Undef, fmt.Errorf("payment channel creation failed (exit code %d)", mwait.Receipt.ExitCode)
|
||||
}
|
||||
|
||||
paychaddr, err := address.NewFromBytes(mwait.Receipt.Return)
|
||||
if err != nil {
|
||||
return address.Undef, err
|
||||
return address.Undef, cid.Undef, err
|
||||
}
|
||||
|
||||
if err := a.PaychMgr.TrackOutboundChannel(ctx, paychaddr); err != nil {
|
||||
return address.Undef, err
|
||||
return address.Undef, cid.Undef, err
|
||||
}
|
||||
|
||||
return paychaddr, nil
|
||||
return paychaddr, msg.Cid(), nil
|
||||
}
|
||||
|
||||
func (a *FullNodeAPI) PaychList(ctx context.Context) ([]address.Address, error) {
|
||||
|
||||
Reference in New Issue
Block a user