2019-07-24 05:38:16 +00:00
|
|
|
package actors
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
2019-08-09 21:41:50 +00:00
|
|
|
"fmt"
|
2019-07-24 05:38:16 +00:00
|
|
|
|
2019-09-13 14:11:40 +00:00
|
|
|
"github.com/ipfs/go-cid"
|
|
|
|
|
2019-10-18 04:47:41 +00:00
|
|
|
"github.com/filecoin-project/lotus/build"
|
|
|
|
"github.com/filecoin-project/lotus/chain/actors/aerrors"
|
|
|
|
"github.com/filecoin-project/lotus/chain/address"
|
|
|
|
"github.com/filecoin-project/lotus/chain/types"
|
2019-07-24 05:38:16 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type PaymentChannelActor struct{}
|
|
|
|
|
2019-08-07 20:16:26 +00:00
|
|
|
type PaymentInfo struct {
|
2019-09-13 14:11:40 +00:00
|
|
|
PayChActor address.Address
|
|
|
|
Payer address.Address
|
|
|
|
ChannelMessage *cid.Cid
|
2019-08-07 20:16:26 +00:00
|
|
|
|
2019-09-06 22:39:47 +00:00
|
|
|
Vouchers []*types.SignedVoucher
|
2019-08-07 20:16:26 +00:00
|
|
|
}
|
|
|
|
|
2019-07-24 05:38:16 +00:00
|
|
|
type LaneState struct {
|
|
|
|
Closed bool
|
|
|
|
Redeemed types.BigInt
|
|
|
|
Nonce uint64
|
|
|
|
}
|
|
|
|
|
|
|
|
type PaymentChannelActorState struct {
|
|
|
|
From address.Address
|
|
|
|
To address.Address
|
|
|
|
|
2019-08-09 21:41:50 +00:00
|
|
|
ToSend types.BigInt
|
2019-07-24 05:38:16 +00:00
|
|
|
|
|
|
|
ClosingAt uint64
|
|
|
|
MinCloseHeight uint64
|
|
|
|
|
2019-09-25 12:56:04 +00:00
|
|
|
// TODO: needs to be map[uint64]*laneState
|
2019-08-09 21:41:50 +00:00
|
|
|
// waiting on refmt#35 to be fixed
|
|
|
|
LaneStates map[string]*LaneState
|
2019-07-24 05:38:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (pca PaymentChannelActor) Exports() []interface{} {
|
|
|
|
return []interface{}{
|
2019-08-09 21:41:50 +00:00
|
|
|
1: pca.Constructor,
|
|
|
|
2: pca.UpdateChannelState,
|
|
|
|
3: pca.Close,
|
|
|
|
4: pca.Collect,
|
|
|
|
5: pca.GetOwner,
|
|
|
|
6: pca.GetToSend,
|
2019-07-24 05:38:16 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-09 21:41:50 +00:00
|
|
|
type pcaMethods struct {
|
|
|
|
Constructor uint64
|
|
|
|
UpdateChannelState uint64
|
|
|
|
Close uint64
|
|
|
|
Collect uint64
|
|
|
|
GetOwner uint64
|
|
|
|
GetToSend uint64
|
|
|
|
}
|
|
|
|
|
|
|
|
var PCAMethods = pcaMethods{1, 2, 3, 4, 5, 6}
|
|
|
|
|
2019-07-24 05:38:16 +00:00
|
|
|
type PCAConstructorParams struct {
|
2019-08-09 21:41:50 +00:00
|
|
|
To address.Address
|
2019-07-24 05:38:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (pca PaymentChannelActor) Constructor(act *types.Actor, vmctx types.VMContext, params *PCAConstructorParams) ([]byte, ActorError) {
|
|
|
|
var self PaymentChannelActorState
|
|
|
|
self.From = vmctx.Origin()
|
|
|
|
self.To = params.To
|
2019-08-09 21:41:50 +00:00
|
|
|
self.LaneStates = make(map[string]*LaneState)
|
2019-07-24 05:38:16 +00:00
|
|
|
|
|
|
|
storage := vmctx.Storage()
|
2019-09-10 19:58:45 +00:00
|
|
|
c, err := storage.Put(&self)
|
2019-07-24 05:38:16 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := storage.Commit(EmptyCBOR, c); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
2019-08-09 21:41:50 +00:00
|
|
|
type PCAUpdateChannelStateParams struct {
|
|
|
|
Sv types.SignedVoucher
|
2019-07-24 05:38:16 +00:00
|
|
|
Secret []byte
|
|
|
|
Proof []byte
|
|
|
|
}
|
|
|
|
|
|
|
|
func hash(b []byte) []byte {
|
|
|
|
panic("blake 2b hash pls")
|
|
|
|
}
|
|
|
|
|
2019-11-13 23:04:13 +00:00
|
|
|
type PaymentVerifyParams struct {
|
|
|
|
Extra []byte
|
|
|
|
Proof []byte
|
|
|
|
}
|
|
|
|
|
2019-08-09 21:41:50 +00:00
|
|
|
func (pca PaymentChannelActor) UpdateChannelState(act *types.Actor, vmctx types.VMContext, params *PCAUpdateChannelStateParams) ([]byte, ActorError) {
|
2019-07-24 05:38:16 +00:00
|
|
|
var self PaymentChannelActorState
|
|
|
|
oldstate := vmctx.Storage().GetHead()
|
2019-07-25 03:32:00 +00:00
|
|
|
storage := vmctx.Storage()
|
|
|
|
if err := storage.Get(oldstate, &self); err != nil {
|
2019-07-24 05:38:16 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
sv := params.Sv
|
|
|
|
|
2019-08-09 21:41:50 +00:00
|
|
|
vb, nerr := sv.SigningBytes()
|
|
|
|
if nerr != nil {
|
|
|
|
return nil, aerrors.Escalate(nerr, "failed to serialize signedvoucher")
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := vmctx.VerifySignature(sv.Signature, self.From, vb); err != nil {
|
2019-07-24 05:38:16 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if vmctx.BlockHeight() < sv.TimeLock {
|
|
|
|
return nil, aerrors.New(2, "cannot use this voucher yet!")
|
|
|
|
}
|
|
|
|
|
2019-09-10 23:03:17 +00:00
|
|
|
if len(sv.SecretPreimage) > 0 {
|
2019-07-24 05:38:16 +00:00
|
|
|
if !bytes.Equal(hash(params.Secret), sv.SecretPreimage) {
|
2019-09-09 13:55:06 +00:00
|
|
|
return nil, aerrors.New(3, "incorrect secret!")
|
2019-07-24 05:38:16 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if sv.Extra != nil {
|
2019-09-10 19:58:45 +00:00
|
|
|
encoded, err := SerializeParams(&PaymentVerifyParams{sv.Extra.Data, params.Proof})
|
2019-07-24 05:38:16 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2019-08-05 22:16:45 +00:00
|
|
|
_, err = vmctx.Send(sv.Extra.Actor, sv.Extra.Method, types.NewInt(0), encoded)
|
2019-07-24 05:38:16 +00:00
|
|
|
if err != nil {
|
2019-09-09 13:55:06 +00:00
|
|
|
return nil, aerrors.Newf(4, "spend voucher verification failed: %s", err)
|
2019-07-24 05:38:16 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-09 21:41:50 +00:00
|
|
|
ls, ok := self.LaneStates[fmt.Sprint(sv.Lane)]
|
|
|
|
if !ok {
|
|
|
|
ls = new(LaneState)
|
|
|
|
ls.Redeemed = types.NewInt(0) // TODO: kinda annoying that this doesnt default to a usable value
|
|
|
|
self.LaneStates[fmt.Sprint(sv.Lane)] = ls
|
|
|
|
}
|
2019-07-24 05:38:16 +00:00
|
|
|
if ls.Closed {
|
2019-08-05 22:16:45 +00:00
|
|
|
return nil, aerrors.New(5, "cannot redeem a voucher on a closed lane")
|
2019-07-24 05:38:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if ls.Nonce > sv.Nonce {
|
2019-08-05 22:16:45 +00:00
|
|
|
return nil, aerrors.New(6, "voucher has an outdated nonce, cannot redeem")
|
2019-07-24 05:38:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
mergeValue := types.NewInt(0)
|
|
|
|
for _, merge := range sv.Merges {
|
|
|
|
if merge.Lane == sv.Lane {
|
2019-08-05 22:16:45 +00:00
|
|
|
return nil, aerrors.New(7, "voucher cannot merge its own lane")
|
2019-07-24 05:38:16 +00:00
|
|
|
}
|
|
|
|
|
2019-08-09 21:41:50 +00:00
|
|
|
ols := self.LaneStates[fmt.Sprint(merge.Lane)]
|
2019-07-24 05:38:16 +00:00
|
|
|
|
|
|
|
if ols.Nonce >= merge.Nonce {
|
2019-08-05 22:16:45 +00:00
|
|
|
return nil, aerrors.New(8, "merge in voucher has outdated nonce, cannot redeem")
|
2019-07-24 05:38:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
mergeValue = types.BigAdd(mergeValue, ols.Redeemed)
|
|
|
|
ols.Nonce = merge.Nonce
|
|
|
|
}
|
|
|
|
|
|
|
|
ls.Nonce = sv.Nonce
|
|
|
|
balanceDelta := types.BigSub(sv.Amount, types.BigAdd(mergeValue, ls.Redeemed))
|
|
|
|
ls.Redeemed = sv.Amount
|
|
|
|
|
|
|
|
newSendBalance := types.BigAdd(self.ToSend, balanceDelta)
|
2019-09-23 17:11:44 +00:00
|
|
|
if newSendBalance.LessThan(types.NewInt(0)) {
|
2019-07-24 05:38:16 +00:00
|
|
|
// TODO: is this impossible?
|
2019-08-05 22:16:45 +00:00
|
|
|
return nil, aerrors.New(9, "voucher would leave channel balance negative")
|
2019-07-24 05:38:16 +00:00
|
|
|
}
|
|
|
|
|
2019-09-23 17:11:44 +00:00
|
|
|
if newSendBalance.GreaterThan(act.Balance) {
|
2019-08-05 22:16:45 +00:00
|
|
|
return nil, aerrors.New(10, "not enough funds in channel to cover voucher")
|
2019-07-24 05:38:16 +00:00
|
|
|
}
|
|
|
|
|
2019-08-09 21:41:50 +00:00
|
|
|
log.Info("vals: ", newSendBalance, sv.Amount, balanceDelta, mergeValue, ls.Redeemed)
|
2019-07-24 05:38:16 +00:00
|
|
|
self.ToSend = newSendBalance
|
|
|
|
|
|
|
|
if sv.MinCloseHeight != 0 {
|
2019-07-25 19:17:15 +00:00
|
|
|
if self.ClosingAt != 0 && self.ClosingAt < sv.MinCloseHeight {
|
2019-07-24 05:38:16 +00:00
|
|
|
self.ClosingAt = sv.MinCloseHeight
|
|
|
|
}
|
|
|
|
if self.MinCloseHeight < sv.MinCloseHeight {
|
|
|
|
self.MinCloseHeight = sv.MinCloseHeight
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-09-10 19:58:45 +00:00
|
|
|
ncid, err := storage.Put(&self)
|
2019-07-25 03:32:00 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if err := storage.Commit(oldstate, ncid); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
2019-08-09 21:41:50 +00:00
|
|
|
func (pca PaymentChannelActor) Close(act *types.Actor, vmctx types.VMContext, params *struct{}) ([]byte, aerrors.ActorError) {
|
2019-07-25 03:32:00 +00:00
|
|
|
var self PaymentChannelActorState
|
|
|
|
storage := vmctx.Storage()
|
|
|
|
oldstate := storage.GetHead()
|
|
|
|
if err := storage.Get(oldstate, &self); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if vmctx.Message().From != self.From && vmctx.Message().From != self.To {
|
|
|
|
return nil, aerrors.New(1, "not authorized to close channel")
|
|
|
|
}
|
|
|
|
|
|
|
|
if self.ClosingAt != 0 {
|
|
|
|
return nil, aerrors.New(2, "channel already closing")
|
|
|
|
}
|
|
|
|
|
2019-09-09 13:55:06 +00:00
|
|
|
self.ClosingAt = vmctx.BlockHeight() + build.PaymentChannelClosingDelay
|
2019-07-25 03:32:00 +00:00
|
|
|
if self.ClosingAt < self.MinCloseHeight {
|
|
|
|
self.ClosingAt = self.MinCloseHeight
|
|
|
|
}
|
|
|
|
|
2019-09-10 19:58:45 +00:00
|
|
|
ncid, err := storage.Put(&self)
|
2019-07-25 03:32:00 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if err := storage.Commit(oldstate, ncid); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
2019-08-09 21:41:50 +00:00
|
|
|
func (pca PaymentChannelActor) Collect(act *types.Actor, vmctx types.VMContext, params *struct{}) ([]byte, aerrors.ActorError) {
|
2019-07-25 03:32:00 +00:00
|
|
|
var self PaymentChannelActorState
|
|
|
|
storage := vmctx.Storage()
|
|
|
|
oldstate := storage.GetHead()
|
|
|
|
if err := storage.Get(oldstate, &self); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if self.ClosingAt == 0 {
|
|
|
|
return nil, aerrors.New(1, "payment channel not closing or closed")
|
|
|
|
}
|
|
|
|
|
|
|
|
if vmctx.BlockHeight() < self.ClosingAt {
|
|
|
|
return nil, aerrors.New(2, "payment channel not closed yet")
|
|
|
|
}
|
2019-08-09 21:41:50 +00:00
|
|
|
_, err := vmctx.Send(self.From, 0, types.BigSub(act.Balance, self.ToSend), nil)
|
2019-07-25 03:32:00 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
_, err = vmctx.Send(self.To, 0, self.ToSend, nil)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
self.ToSend = types.NewInt(0)
|
|
|
|
|
2019-09-10 19:58:45 +00:00
|
|
|
ncid, err := storage.Put(&self)
|
2019-07-25 03:32:00 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if err := storage.Commit(oldstate, ncid); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2019-07-24 05:38:16 +00:00
|
|
|
return nil, nil
|
|
|
|
}
|
2019-08-09 21:41:50 +00:00
|
|
|
|
|
|
|
func (pca PaymentChannelActor) GetOwner(act *types.Actor, vmctx types.VMContext, params *struct{}) ([]byte, aerrors.ActorError) {
|
|
|
|
var self PaymentChannelActorState
|
|
|
|
storage := vmctx.Storage()
|
|
|
|
if err := storage.Get(storage.GetHead(), &self); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return self.From.Bytes(), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (pca PaymentChannelActor) GetToSend(act *types.Actor, vmctx types.VMContext, params *struct{}) ([]byte, aerrors.ActorError) {
|
|
|
|
var self PaymentChannelActorState
|
|
|
|
storage := vmctx.Storage()
|
|
|
|
if err := storage.Get(storage.GetHead(), &self); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return self.ToSend.Bytes(), nil
|
|
|
|
}
|