payments

package
v0.1.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jan 11, 2022 License: BSD-3-Clause Imports: 34 Imported by: 0

Documentation

Index

Constants

View Source
const (
	DirInbound  = 1
	DirOutbound = 2
)

Channel directions

Variables

View Source
var ErrChannelNotTracked = fmt.Errorf("channel not tracked")

ErrChannelNotTracked is returned when we cannot find a channel in our store

Functions

func SerializeParams

func SerializeParams(i cbg.CBORMarshaler) ([]byte, error)

Types

type AvailableFunds

type AvailableFunds struct {
	// Channel is the address of the channel
	Channel *address.Address
	// From is the from address of the channel (channel creator)
	From address.Address
	// To is the to address of the channel
	To address.Address
	// ConfirmedAmt is the amount of funds that have been confirmed on-chain
	// for the channel
	ConfirmedAmt filecoin.BigInt
	// PendingAmt is the amount of funds that are pending confirmation on-chain
	PendingAmt filecoin.BigInt
	// PendingWaitSentinel can be used with PaychGetWaitReady to wait for
	// confirmation of pending funds
	PendingWaitSentinel *cid.Cid
	// QueuedAmt is the amount that is queued up behind a pending request
	QueuedAmt filecoin.BigInt
	// VoucherRedeemedAmt is the amount that is redeemed by vouchers on-chain
	// and in the local datastore
	VoucherRedeemedAmt filecoin.BigInt
}

AvailableFunds describes the current state of a channel on chain

type ChannelInfo

type ChannelInfo struct {
	// ChannelID is a uuid set at channel creation
	ChannelID string
	// Channel address - may be nil if the channel hasn't been created yet
	Channel *address.Address
	// Control is the address of the local node
	Control address.Address
	// Target is the address of the remote node (on the other end of the channel)
	Target address.Address
	// Direction indicates if the channel is inbound (Control is the "to" address)
	// or outbound (Control is the "from" address)
	Direction uint64
	// Vouchers is a list of all vouchers sent on the channel
	Vouchers []*VoucherInfo
	// NextLane is the number of the next lane that should be used when the
	// client requests a new lane (eg to create a voucher for a new deal)
	NextLane uint64
	// Amount added to the channel.
	// Note: This amount is only used by GetPaych to keep track of how much
	// has locally been added to the channel. It should reflect the channel's
	// Balance on chain as long as all operations occur on the same datastore.
	Amount fil.BigInt
	// PendingAmount is the amount that we're awaiting confirmation of
	PendingAmount fil.BigInt
	// CreateMsg is the CID of a pending create message (while waiting for confirmation)
	CreateMsg *cid.Cid
	// AddFundsMsg is the CID of a pending add funds message (while waiting for confirmation)
	AddFundsMsg *cid.Cid
	// Settling indicates whether the channel has entered into the settling state
	Settling bool
	// SettlingAt is the height at which the channel can be 'collected'
	SettlingAt abi.ChainEpoch
}

ChannelInfo keeps track of information about a channel

func (*ChannelInfo) MarshalCBOR

func (t *ChannelInfo) MarshalCBOR(w io.Writer) error

func (*ChannelInfo) UnmarshalCBOR

func (t *ChannelInfo) UnmarshalCBOR(r io.Reader) error

type ChannelResponse

type ChannelResponse struct {
	Channel      address.Address
	WaitSentinel cid.Cid
}

ChannelResponse is the result of calling GetChannel

type ChannelState

type ChannelState interface {
	cbortypes.Marshaler
	// Channel owner, who has funded the actor
	From() (address.Address, error)
	// Recipient of payouts from channel
	To() (address.Address, error)

	// Height at which the channel can be `Collected`
	SettlingAt() (abi.ChainEpoch, error)

	// Amount successfully redeemed through the payment channel, paid out on `Collect()`
	ToSend() (abi.TokenAmount, error)

	// Get total number of lanes
	LaneCount() (uint64, error)

	// Iterate lane states
	ForEachLaneState(cb func(idx uint64, dl LaneState) error) error
}

ChannelState is an abstract version of payment channel state that works across versions. TODO: we need to handle future actor version upgrades

type ErrInsufficientFunds

type ErrInsufficientFunds struct {
	// contains filtered or unexported fields
}

ErrInsufficientFunds indicates that there are not enough funds in the channel to create a voucher

func (*ErrInsufficientFunds) Error

func (e *ErrInsufficientFunds) Error() string

func (*ErrInsufficientFunds) Shortfall

func (e *ErrInsufficientFunds) Shortfall() filecoin.BigInt

Shortfall is how much FIL the channel is missing

type FilObjectStore

type FilObjectStore struct {
	// contains filtered or unexported fields
}

FilObjectStore fetches object from the Filecoin API if it is absent from the blockstore

func NewFilObjectStore

func NewFilObjectStore(api filecoin.API, bs blockstore.Blockstore) *FilObjectStore

NewFilObjectStore creates a new instance of FilObjectStore

func (*FilObjectStore) Get

func (fos *FilObjectStore) Get(ctx context.Context, c cid.Cid, out interface{}) error

Get fetches the object from filecoin API if absent then proxies the method to the underlying reads and unmarshals the content at `c` into `out`.

func (*FilObjectStore) Put

func (fos *FilObjectStore) Put(ctx context.Context, v interface{}) (cid.Cid, error)

Put just forwards the method

type LaneState

type LaneState interface {
	Redeemed() (big.Int, error)
	Nonce() (uint64, error)
}

LaneState is an abstract copy of the state of a single lane not to be mixed up with the original paych.LaneState struct

type Manager

type Manager interface {
	GetChannel(ctx context.Context, from, to address.Address, amt filecoin.BigInt) (*ChannelResponse, error)
	TrackChannel(ctx context.Context, chAddr address.Address) (*ChannelInfo, error)
	WaitForChannel(context.Context, cid.Cid) (address.Address, error)
	ListChannels() ([]address.Address, error)
	ListVouchers(context.Context, address.Address) ([]*VoucherInfo, error)
	GetChannelInfo(address.Address) (*ChannelInfo, error)
	CreateVoucher(context.Context, address.Address, filecoin.BigInt, uint64) (*VoucherCreateResult, error)
	AllocateLane(context.Context, address.Address) (uint64, error)
	AddVoucherInbound(context.Context, address.Address, *paych.SignedVoucher, []byte, filecoin.BigInt) (filecoin.BigInt, error)
	ChannelAvailableFunds(address.Address) (*AvailableFunds, error)
	SubmitAllVouchers(context.Context, address.Address) error
	SubmitVoucherForLane(context.Context, address.Address, uint64) error
	Settle(context.Context, address.Address) (abi.ChainEpoch, error)
	Collect(context.Context, address.Address) error
	StartAutoCollect(context.Context) error
}

Manager is the interface required to handle payments for the pop exchange

type MessageBuilder

type MessageBuilder interface {
	Create(to address.Address, initialAmount abi.TokenAmount) (*fil.Message, error)
	Update(paych address.Address, voucher *paych.SignedVoucher, secret []byte) (*fil.Message, error)
	Settle(paych address.Address) (*fil.Message, error)
	Collect(paych address.Address) (*fil.Message, error)
}

MessageBuilder abstracts methods to create new payment channel messages

type MsgInfo

type MsgInfo struct {
	// ChannelID links the message to a channel
	ChannelID string
	// MsgCid is the CID of the message
	MsgCid cid.Cid
	// Received indicates whether a response has been received
	Received bool
	// Err is the error received in the response
	Err string
}

MsgInfo stores information about a create channel / add funds message that has been sent

func (*MsgInfo) MarshalCBOR

func (t *MsgInfo) MarshalCBOR(w io.Writer) error

func (*MsgInfo) UnmarshalCBOR

func (t *MsgInfo) UnmarshalCBOR(r io.Reader) error

type Payments

type Payments struct {
	// contains filtered or unexported fields
}

Payments is our full payment system, it manages payment channels, stores vouchers and interacts with the filecoin chain to send transactions

func New

func New(ctx context.Context, api filecoin.API, w wallet.Driver, ds datastore.Batching, bs blockstore.Blockstore) *Payments

New creates a new instance of payments manager

func (*Payments) AddVoucherInbound

func (p *Payments) AddVoucherInbound(ctx context.Context, chAddr address.Address, sv *paych.SignedVoucher, proof []byte, minDelta filecoin.BigInt) (filecoin.BigInt, error)

AddVoucherInbound adds a voucher for an inbound channel. If the channel is not in the store, fetches the channel from state (and checks that the channel To address is owned by the wallet).

func (*Payments) AllocateLane

func (p *Payments) AllocateLane(ctx context.Context, chAddr address.Address) (uint64, error)

AllocateLane creates a new lane for a given channel

func (*Payments) ChannelAvailableFunds

func (p *Payments) ChannelAvailableFunds(chAddr address.Address) (*AvailableFunds, error)

ChannelAvailableFunds returns the amount a channel can still spend

func (*Payments) CheckVoucherSpendable

func (p *Payments) CheckVoucherSpendable(ctx context.Context, addr address.Address, sv *paych.SignedVoucher, secret []byte, proof []byte) (bool, error)

CheckVoucherSpendable checks if the given voucher is currently spendable

func (*Payments) Collect

func (p *Payments) Collect(ctx context.Context, addr address.Address) error

Collect a given channel

func (*Payments) CreateVoucher

func (p *Payments) CreateVoucher(ctx context.Context, chAddr address.Address, amt filecoin.BigInt, lane uint64) (*VoucherCreateResult, error)

CreateVoucher creates a voucher for a given payment channel

func (*Payments) GetChannel

func (p *Payments) GetChannel(ctx context.Context, from, to address.Address, amt filecoin.BigInt) (*ChannelResponse, error)

GetChannel adds fund to a new channel in a given direction, if one already exists it will update it it does not wait for the message to be confirmed on chain

func (*Payments) GetChannelInfo

func (p *Payments) GetChannelInfo(chAddr address.Address) (*ChannelInfo, error)

GetChannelInfo from the store

func (*Payments) ListChannels

func (p *Payments) ListChannels() ([]address.Address, error)

ListChannels we have in the store

func (*Payments) ListVouchers

func (p *Payments) ListVouchers(ctx context.Context, chAddr address.Address) ([]*VoucherInfo, error)

ListVouchers we have created so far

func (*Payments) Settle

func (p *Payments) Settle(ctx context.Context, addr address.Address) (abi.ChainEpoch, error)

Settle a given channel then save and return the time when it can be collected

func (*Payments) StartAutoCollect

func (p *Payments) StartAutoCollect(ctx context.Context) error

StartAutoCollect is a routine that ticks every epoch and tries to collect settling payment channels called usually at startup

func (*Payments) SubmitAllVouchers

func (p *Payments) SubmitAllVouchers(ctx context.Context, addr address.Address) error

SubmitAllVouchers picks the best vouchers and submits them (should be submitted as one) it blocks until done

func (*Payments) SubmitVoucher

func (p *Payments) SubmitVoucher(ctx context.Context, addr address.Address, sv *paych.SignedVoucher, secret []byte, proof []byte) (cid.Cid, error)

SubmitVoucher gets a channel from the store and submits a new voucher to the chain

func (*Payments) SubmitVoucherForLane

func (p *Payments) SubmitVoucherForLane(ctx context.Context, addr address.Address, lane uint64) error

SubmitVoucherForLane picks the best voucher for a given lane and submits it. It blocks until done.

func (*Payments) TrackChannel

func (p *Payments) TrackChannel(ctx context.Context, addr address.Address) (*ChannelInfo, error)

TrackChannel adds a channel to the store loading its state from the chain. TODO: outbound

func (*Payments) WaitForChannel

func (p *Payments) WaitForChannel(ctx context.Context, mcid cid.Cid) (address.Address, error)

WaitForChannel to be ready and return the address on chain

type Store

type Store struct {
	// contains filtered or unexported fields
}

Store is a datastore for persisting payment channels

func NewStore

func NewStore(ds datastore.Batching) *Store

NewStore for payments

func (*Store) AllocateLane

func (s *Store) AllocateLane(ch address.Address) (uint64, error)

AllocateLane allocates a new lane for the given channel

func (*Store) ByAddress

func (s *Store) ByAddress(addr address.Address) (*ChannelInfo, error)

ByAddress gets the channel that matches the given address

func (*Store) ByChannelID

func (s *Store) ByChannelID(channelID string) (*ChannelInfo, error)

ByChannelID gets channel info by channel ID

func (*Store) ByMessageCid

func (s *Store) ByMessageCid(mcid cid.Cid) (*ChannelInfo, error)

ByMessageCid gets the channel associated with a message

func (*Store) CreateChannel

func (s *Store) CreateChannel(from, to address.Address, mcid cid.Cid, amt fil.BigInt) (*ChannelInfo, error)

CreateChannel and put it in our store

func (*Store) GetMessage

func (s *Store) GetMessage(mcid cid.Cid) (*MsgInfo, error)

GetMessage gets the message info for a given message CID

func (*Store) ListChannels

func (s *Store) ListChannels() ([]address.Address, error)

ListChannels returns the addresses of all channels that have been created

func (*Store) ListSettlingChannels

func (s *Store) ListSettlingChannels() ([]ChannelInfo, error)

ListSettlingChannels returns the addresses of all channels that need to be collected

func (*Store) MarkVoucherSubmitted

func (s *Store) MarkVoucherSubmitted(ci *ChannelInfo, sv *paych.SignedVoucher) error

MarkVoucherSubmitted sets the Submitted field to true on VoucherInfo and puts it in the store

func (*Store) OutboundActiveByFromTo

func (s *Store) OutboundActiveByFromTo(from address.Address, to address.Address) (*ChannelInfo, error)

OutboundActiveByFromTo looks for outbound channels that have not been settled, with the given from / to addresses

func (*Store) RemoveChannel

func (s *Store) RemoveChannel(channelID string) error

RemoveChannel removes the channel with the given channel ID

func (*Store) SaveMessageResult

func (s *Store) SaveMessageResult(mcid cid.Cid, msgErr error) error

SaveMessageResult is called when the result of a message is received

func (*Store) SaveNewMessage

func (s *Store) SaveNewMessage(channelID string, mcid cid.Cid) error

SaveNewMessage is called when a message is sent

func (*Store) SetChannelSettlingAt

func (s *Store) SetChannelSettlingAt(ci *ChannelInfo, ep abi.ChainEpoch) error

SetChannelSettlingAt sets the epoch value at which it can be collected

func (*Store) TrackChannel

func (s *Store) TrackChannel(ci *ChannelInfo) (*ChannelInfo, error)

TrackChannel stores a channel, returning an error if the channel was already being tracked

func (*Store) VouchersForPaych

func (s *Store) VouchersForPaych(ch address.Address) ([]*VoucherInfo, error)

VouchersForPaych gets the vouchers for the given channel

type VoucherCreateResult

type VoucherCreateResult struct {
	// Voucher that was created, or nil if there was an error or if there
	// were insufficient funds in the channel
	Voucher *paych.SignedVoucher
	// Shortfall is the additional amount that would be needed in the channel
	// in order to be able to create the voucher
	Shortfall filecoin.BigInt
}

VoucherCreateResult is the response to createVoucher method

type VoucherInfo

type VoucherInfo struct {
	Voucher   *paych.SignedVoucher
	Proof     []byte
	Submitted bool
}

VoucherInfo points to a voucher and its metadata

func (*VoucherInfo) MarshalCBOR

func (t *VoucherInfo) MarshalCBOR(w io.Writer) error

func (*VoucherInfo) UnmarshalCBOR

func (t *VoucherInfo) UnmarshalCBOR(r io.Reader) error

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL