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-08-07 20:16:26 +00:00
|
|
|
"github.com/ipfs/go-cid"
|
|
|
|
cbor "github.com/ipfs/go-ipld-cbor"
|
|
|
|
|
2019-07-24 05:38:16 +00:00
|
|
|
"github.com/filecoin-project/go-lotus/chain/actors/aerrors"
|
|
|
|
"github.com/filecoin-project/go-lotus/chain/address"
|
|
|
|
"github.com/filecoin-project/go-lotus/chain/types"
|
|
|
|
)
|
|
|
|
|
2019-07-25 03:32:00 +00:00
|
|
|
const ChannelClosingDelay = 6 * 60 * 2 // six hours
|
|
|
|
|
2019-07-24 05:38:16 +00:00
|
|
|
func init() {
|
|
|
|
cbor.RegisterCborType(PaymentChannelActorState{})
|
|
|
|
cbor.RegisterCborType(PCAConstructorParams{})
|
|
|
|
cbor.RegisterCborType(LaneState{})
|
2019-08-09 21:41:50 +00:00
|
|
|
cbor.RegisterCborType(PCAUpdateChannelStateParams{})
|
2019-08-07 20:16:26 +00:00
|
|
|
cbor.RegisterCborType(PaymentInfo{})
|
2019-07-24 05:38:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type PaymentChannelActor struct{}
|
|
|
|
|
2019-08-07 20:16:26 +00:00
|
|
|
type PaymentInfo struct {
|
|
|
|
PayChActor address.Address
|
|
|
|
Payer address.Address
|
|
|
|
ChannelMessage cid.Cid
|
|
|
|
|
2019-08-09 21:41:50 +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-08-09 21:41:50 +00:00
|
|
|
// TODO: needs to be map[uint64]*LaneState
|
|
|
|
// 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()
|
|
|
|
c, err := storage.Put(self)
|
|
|
|
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-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!")
|
|
|
|
}
|
|
|
|
|
|
|
|
if sv.SecretPreimage != nil {
|
|
|
|
if !bytes.Equal(hash(params.Secret), sv.SecretPreimage) {
|
|
|
|
return nil, aerrors.New(3, "Incorrect secret!")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if sv.Extra != nil {
|
2019-08-05 22:16:45 +00:00
|
|
|
encoded, err := SerializeParams([]interface{}{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-08-05 22:16:45 +00:00
|
|
|
return nil, aerrors.New(4, "spend voucher verification failed")
|
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)
|
|
|
|
if types.BigCmp(newSendBalance, types.NewInt(0)) < 0 {
|
|
|
|
// 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-08-09 21:41:50 +00:00
|
|
|
if types.BigCmp(newSendBalance, act.Balance) > 0 {
|
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-07-25 03:32:00 +00:00
|
|
|
ncid, err := storage.Put(self)
|
|
|
|
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")
|
|
|
|
}
|
|
|
|
|
|
|
|
self.ClosingAt = vmctx.BlockHeight() + ChannelClosingDelay
|
|
|
|
if self.ClosingAt < self.MinCloseHeight {
|
|
|
|
self.ClosingAt = self.MinCloseHeight
|
|
|
|
}
|
|
|
|
|
|
|
|
ncid, err := storage.Put(self)
|
|
|
|
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)
|
|
|
|
|
|
|
|
ncid, err := storage.Put(self)
|
|
|
|
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
|
|
|
|
}
|