2019-08-09 21:42:56 +00:00
|
|
|
package paych
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
|
|
|
|
|
|
|
"github.com/filecoin-project/go-lotus/chain/actors"
|
|
|
|
"github.com/filecoin-project/go-lotus/chain/address"
|
|
|
|
"github.com/filecoin-project/go-lotus/chain/state"
|
|
|
|
"github.com/filecoin-project/go-lotus/chain/store"
|
|
|
|
"github.com/filecoin-project/go-lotus/chain/types"
|
2019-08-13 04:27:54 +00:00
|
|
|
"github.com/filecoin-project/go-lotus/chain/vm"
|
2019-08-09 21:42:56 +00:00
|
|
|
|
|
|
|
hamt "github.com/ipfs/go-hamt-ipld"
|
|
|
|
)
|
|
|
|
|
2019-08-12 17:09:56 +00:00
|
|
|
type Manager struct {
|
2019-08-09 21:42:56 +00:00
|
|
|
chain *store.ChainStore
|
2019-08-12 17:09:56 +00:00
|
|
|
store *Store
|
2019-08-09 21:42:56 +00:00
|
|
|
}
|
|
|
|
|
2019-08-12 18:30:20 +00:00
|
|
|
func NewManager(chain *store.ChainStore, pchstore *Store) *Manager {
|
2019-08-12 17:09:56 +00:00
|
|
|
return &Manager{
|
2019-08-09 21:42:56 +00:00
|
|
|
chain: chain,
|
|
|
|
store: pchstore,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-12 17:09:56 +00:00
|
|
|
func (pm *Manager) TrackInboundChannel(ctx context.Context, ch address.Address) error {
|
|
|
|
_, st, err := pm.loadPaychState(ctx, ch)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return pm.store.TrackChannel(&ChannelInfo{
|
|
|
|
Channel: ch,
|
|
|
|
Direction: DirInbound,
|
|
|
|
ControlAddr: st.To,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func (pm *Manager) TrackOutboundChannel(ctx context.Context, ch address.Address) error {
|
|
|
|
_, st, err := pm.loadPaychState(ctx, ch)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return pm.store.TrackChannel(&ChannelInfo{
|
|
|
|
Channel: ch,
|
|
|
|
Direction: DirOutbound,
|
|
|
|
ControlAddr: st.From,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func (pm *Manager) ListChannels() ([]address.Address, error) {
|
|
|
|
return pm.store.ListChannels()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (pm *Manager) GetChannelInfo(addr address.Address) (*ChannelInfo, error) {
|
|
|
|
return pm.store.getChannelInfo(addr)
|
2019-08-09 21:42:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// checks if the given voucher is valid (is or could become spendable at some point)
|
2019-08-12 17:09:56 +00:00
|
|
|
func (pm *Manager) CheckVoucherValid(ctx context.Context, ch address.Address, sv *types.SignedVoucher) error {
|
2019-08-09 21:42:56 +00:00
|
|
|
act, pca, err := pm.loadPaychState(ctx, ch)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// verify signature
|
|
|
|
vb, err := sv.SigningBytes()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2019-08-13 18:47:40 +00:00
|
|
|
|
|
|
|
// TODO: technically, either party may create and sign a voucher.
|
|
|
|
// However, for now, we only accept them from the channel creator.
|
|
|
|
// More complex handling logic can be added later
|
2019-08-09 21:42:56 +00:00
|
|
|
if err := sv.Signature.Verify(pca.From, vb); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
sendAmount := sv.Amount
|
|
|
|
|
|
|
|
// now check the lane state
|
2019-08-13 18:47:40 +00:00
|
|
|
// TODO: should check against vouchers in our local store too
|
|
|
|
// there might be something conflicting
|
2019-08-09 21:42:56 +00:00
|
|
|
ls, ok := pca.LaneStates[fmt.Sprint(sv.Lane)]
|
|
|
|
if !ok {
|
|
|
|
} else {
|
|
|
|
if ls.Closed {
|
|
|
|
return fmt.Errorf("voucher is on a closed lane")
|
|
|
|
}
|
|
|
|
if ls.Nonce >= sv.Nonce {
|
|
|
|
return fmt.Errorf("nonce too low")
|
|
|
|
}
|
|
|
|
|
|
|
|
sendAmount = types.BigSub(sv.Amount, ls.Redeemed)
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: also account for vouchers on other lanes we've received
|
|
|
|
newTotal := types.BigAdd(sendAmount, pca.ToSend)
|
|
|
|
if types.BigCmp(act.Balance, newTotal) < 0 {
|
|
|
|
return fmt.Errorf("not enough funds in channel to cover voucher")
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(sv.Merges) != 0 {
|
|
|
|
return fmt.Errorf("dont currently support paych lane merges")
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// checks if the given voucher is currently spendable
|
2019-08-12 17:09:56 +00:00
|
|
|
func (pm *Manager) CheckVoucherSpendable(ctx context.Context, ch address.Address, sv *types.SignedVoucher, secret []byte, proof []byte) (bool, error) {
|
2019-08-09 21:42:56 +00:00
|
|
|
owner, err := pm.getPaychOwner(ctx, ch)
|
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
|
|
|
|
enc, err := actors.SerializeParams(&actors.PCAUpdateChannelStateParams{
|
|
|
|
Sv: *sv,
|
|
|
|
Secret: secret,
|
|
|
|
Proof: proof,
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
|
2019-08-13 04:27:54 +00:00
|
|
|
ret, err := vm.Call(ctx, pm.chain, &types.Message{
|
2019-08-09 21:42:56 +00:00
|
|
|
From: owner,
|
|
|
|
To: ch,
|
|
|
|
Method: actors.PCAMethods.UpdateChannelState,
|
|
|
|
Params: enc,
|
|
|
|
}, nil)
|
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if ret.ExitCode != 0 {
|
|
|
|
return false, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return true, nil
|
|
|
|
}
|
|
|
|
|
2019-08-12 17:09:56 +00:00
|
|
|
func (pm *Manager) loadPaychState(ctx context.Context, ch address.Address) (*types.Actor, *actors.PaymentChannelActorState, error) {
|
2019-08-09 21:42:56 +00:00
|
|
|
st, err := pm.chain.TipSetState(pm.chain.GetHeaviestTipSet().Cids())
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
cst := hamt.CSTFromBstore(pm.chain.Blockstore())
|
|
|
|
tree, err := state.LoadStateTree(cst, st)
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
act, err := tree.GetActor(ch)
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
var pcast actors.PaymentChannelActorState
|
|
|
|
if err := cst.Get(ctx, act.Head, &pcast); err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return act, &pcast, nil
|
|
|
|
}
|
|
|
|
|
2019-08-12 17:09:56 +00:00
|
|
|
func (pm *Manager) getPaychOwner(ctx context.Context, ch address.Address) (address.Address, error) {
|
2019-08-13 04:27:54 +00:00
|
|
|
ret, err := vm.Call(ctx, pm.chain, &types.Message{
|
2019-08-09 21:42:56 +00:00
|
|
|
From: ch,
|
|
|
|
To: ch,
|
|
|
|
Method: actors.PCAMethods.GetOwner,
|
|
|
|
}, nil)
|
|
|
|
if err != nil {
|
|
|
|
return address.Undef, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if ret.ExitCode != 0 {
|
|
|
|
return address.Undef, fmt.Errorf("failed to get payment channel owner (exit code %d)", ret.ExitCode)
|
|
|
|
}
|
|
|
|
|
|
|
|
return address.NewFromBytes(ret.Return)
|
|
|
|
}
|
2019-08-12 17:09:56 +00:00
|
|
|
|
|
|
|
func (pm *Manager) AddVoucher(ctx context.Context, ch address.Address, sv *types.SignedVoucher) error {
|
|
|
|
if err := pm.CheckVoucherValid(ctx, ch, sv); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return pm.store.AddVoucher(ch, sv)
|
|
|
|
}
|
2019-08-12 19:51:01 +00:00
|
|
|
|
|
|
|
func (pm *Manager) ListVouchers(ctx context.Context, ch address.Address) ([]*types.SignedVoucher, error) {
|
|
|
|
// TODO: just having a passthrough method like this feels odd. Seems like
|
|
|
|
// there should be some filtering we're doing here
|
|
|
|
return pm.store.VouchersForPaych(ch)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (pm *Manager) NextNonceForLane(ctx context.Context, ch address.Address, lane uint64) (uint64, error) {
|
|
|
|
vouchers, err := pm.store.VouchersForPaych(ch)
|
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
|
|
|
|
var maxnonce uint64
|
|
|
|
for _, v := range vouchers {
|
|
|
|
if v.Lane == lane {
|
|
|
|
if v.Nonce > maxnonce {
|
|
|
|
maxnonce = v.Nonce
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-13 04:27:54 +00:00
|
|
|
return maxnonce + 1, nil
|
2019-08-12 19:51:01 +00:00
|
|
|
}
|