clientstates

package
v0.1.1 Latest Latest
Warning

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

Go to latest
Published: Apr 29, 2020 License: Apache-2.0, MIT Imports: 10 Imported by: 2

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ClientEvents = fsm.Events{
	fsm.Event(rm.ClientEventOpen).
		From(rm.DealStatusNew).ToNoChange(),
	fsm.Event(rm.ClientEventPaymentChannelErrored).
		FromMany(rm.DealStatusAccepted, rm.DealStatusPaymentChannelCreating).To(rm.DealStatusFailed).
		Action(func(deal *rm.ClientDealState, err error) error {
			deal.Message = xerrors.Errorf("get or create payment channel: %w", err).Error()
			return nil
		}),
	fsm.Event(rm.ClientEventPaymentChannelCreateInitiated).
		From(rm.DealStatusAccepted).To(rm.DealStatusPaymentChannelCreating).
		Action(func(deal *rm.ClientDealState, msgCID cid.Cid) error {
			deal.WaitMsgCID = &msgCID
			return nil
		}),
	fsm.Event(rm.ClientEventPaymentChannelAddingFunds).
		FromMany(rm.DealStatusAccepted).To(rm.DealStatusPaymentChannelAddingFunds).
		Action(func(deal *rm.ClientDealState, msgCID cid.Cid, payCh address.Address) error {
			deal.WaitMsgCID = &msgCID
			deal.PaymentInfo = &rm.PaymentInfo{
				PayCh: payCh,
			}
			return nil
		}),
	fsm.Event(rm.ClientEventPaymentChannelReady).
		FromMany(rm.DealStatusPaymentChannelCreating, rm.DealStatusPaymentChannelAddingFunds).
		To(rm.DealStatusPaymentChannelReady).
		Action(func(deal *rm.ClientDealState, payCh address.Address, lane uint64) error {
			deal.PaymentInfo = &rm.PaymentInfo{
				PayCh: payCh,
				Lane:  lane,
			}
			return nil
		}),
	fsm.Event(rm.ClientEventAllocateLaneErrored).
		FromMany(rm.DealStatusPaymentChannelCreating, rm.DealStatusPaymentChannelAddingFunds).To(rm.DealStatusFailed).
		Action(func(deal *rm.ClientDealState, err error) error {
			deal.Message = xerrors.Errorf("allocating payment lane: %w", err).Error()
			return nil
		}),
	fsm.Event(rm.ClientEventPaymentChannelAddFundsErrored).
		From(rm.DealStatusPaymentChannelAddingFunds).To(rm.DealStatusFailed).
		Action(func(deal *rm.ClientDealState, err error) error {
			deal.Message = xerrors.Errorf("wait for add funds: %w", err).Error()
			return nil
		}),
	fsm.Event(rm.ClientEventWriteDealProposalErrored).
		FromAny().To(rm.DealStatusErrored).
		Action(func(deal *rm.ClientDealState, err error) error {
			deal.Message = xerrors.Errorf("proposing deal: %w", err).Error()
			return nil
		}),
	fsm.Event(rm.ClientEventReadDealResponseErrored).
		FromAny().To(rm.DealStatusErrored).
		Action(func(deal *rm.ClientDealState, err error) error {
			deal.Message = xerrors.Errorf("reading deal response: %w", err).Error()
			return nil
		}),
	fsm.Event(rm.ClientEventDealRejected).
		From(rm.DealStatusNew).To(rm.DealStatusRejected).
		Action(func(deal *rm.ClientDealState, message string) error {
			deal.Message = fmt.Sprintf("deal rejected: %s", message)
			return nil
		}),
	fsm.Event(rm.ClientEventDealNotFound).
		From(rm.DealStatusNew).To(rm.DealStatusDealNotFound).
		Action(func(deal *rm.ClientDealState, message string) error {
			deal.Message = fmt.Sprintf("deal not found: %s", message)
			return nil
		}),
	fsm.Event(rm.ClientEventDealAccepted).
		From(rm.DealStatusNew).To(rm.DealStatusAccepted),
	fsm.Event(rm.ClientEventUnknownResponseReceived).
		FromAny().To(rm.DealStatusFailed).
		Action(func(deal *rm.ClientDealState) error {
			deal.Message = "Unexpected deal response status"
			return nil
		}),
	fsm.Event(rm.ClientEventFundsExpended).
		FromMany(rm.DealStatusFundsNeeded, rm.DealStatusFundsNeededLastPayment).To(rm.DealStatusFailed).
		Action(func(deal *rm.ClientDealState, expectedTotal string, actualTotal string) error {
			deal.Message = fmt.Sprintf("not enough funds left: expected amt = %s, actual amt = %s", expectedTotal, actualTotal)
			return nil
		}),
	fsm.Event(rm.ClientEventBadPaymentRequested).
		FromMany(rm.DealStatusFundsNeeded, rm.DealStatusFundsNeededLastPayment).To(rm.DealStatusFailed).
		Action(func(deal *rm.ClientDealState, message string) error {
			deal.Message = message
			return nil
		}),
	fsm.Event(rm.ClientEventCreateVoucherFailed).
		FromMany(rm.DealStatusFundsNeeded, rm.DealStatusFundsNeededLastPayment).To(rm.DealStatusFailed).
		Action(func(deal *rm.ClientDealState, err error) error {
			deal.Message = xerrors.Errorf("creating payment voucher: %w", err).Error()
			return nil
		}),
	fsm.Event(rm.ClientEventWriteDealPaymentErrored).
		FromAny().To(rm.DealStatusErrored).
		Action(func(deal *rm.ClientDealState, err error) error {
			deal.Message = xerrors.Errorf("writing deal payment: %w", err).Error()
			return nil
		}),
	fsm.Event(rm.ClientEventPaymentSent).
		From(rm.DealStatusFundsNeeded).To(rm.DealStatusOngoing).
		From(rm.DealStatusFundsNeededLastPayment).To(rm.DealStatusFinalizing).
		Action(func(deal *rm.ClientDealState) error {

			deal.FundsSpent = big.Add(deal.FundsSpent, deal.PaymentRequested)
			bytesPaidFor := big.Div(deal.PaymentRequested, deal.PricePerByte).Uint64()
			if bytesPaidFor >= deal.CurrentInterval {
				deal.CurrentInterval += deal.DealProposal.PaymentIntervalIncrease
			}
			deal.BytesPaidFor += bytesPaidFor
			deal.PaymentRequested = abi.NewTokenAmount(0)
			return nil
		}),
	fsm.Event(rm.ClientEventConsumeBlockFailed).
		FromMany(rm.DealStatusPaymentChannelReady, rm.DealStatusOngoing).To(rm.DealStatusFailed).
		Action(func(deal *rm.ClientDealState, err error) error {
			deal.Message = xerrors.Errorf("consuming block: %w", err).Error()
			return nil
		}),
	fsm.Event(rm.ClientEventLastPaymentRequested).
		FromMany(rm.DealStatusPaymentChannelReady,
			rm.DealStatusOngoing,
			rm.DealStatusBlocksComplete).To(rm.DealStatusFundsNeededLastPayment).
		Action(recordPaymentOwed),
	fsm.Event(rm.ClientEventAllBlocksReceived).
		FromMany(rm.DealStatusPaymentChannelReady,
			rm.DealStatusOngoing,
			rm.DealStatusBlocksComplete).To(rm.DealStatusBlocksComplete).
		Action(recordProcessed),
	fsm.Event(rm.ClientEventComplete).
		FromMany(rm.DealStatusPaymentChannelReady,
			rm.DealStatusOngoing,
			rm.DealStatusBlocksComplete,
			rm.DealStatusFinalizing).To(rm.DealStatusCompleted).
		Action(recordProcessed),
	fsm.Event(rm.ClientEventEarlyTermination).
		FromMany(rm.DealStatusPaymentChannelReady, rm.DealStatusOngoing).To(rm.DealStatusFailed).
		Action(func(deal *rm.ClientDealState) error {
			deal.Message = "received complete status before all blocks received"
			return nil
		}),
	fsm.Event(rm.ClientEventPaymentRequested).
		FromMany(rm.DealStatusPaymentChannelReady, rm.DealStatusOngoing).To(rm.DealStatusFundsNeeded).
		Action(recordPaymentOwed),
	fsm.Event(rm.ClientEventBlocksReceived).
		From(rm.DealStatusPaymentChannelReady).To(rm.DealStatusOngoing).
		From(rm.DealStatusOngoing).ToNoChange().
		Action(recordProcessed),
}

ClientEvents are the events that can happen in a retrieval client

ClientStateEntryFuncs are the handlers for different states in a retrieval client

Functions

func Finalize

func Finalize(ctx fsm.Context, environment ClientDealEnvironment, deal rm.ClientDealState) error

Finalize completes a deal

func ProcessNextResponse

func ProcessNextResponse(ctx fsm.Context, environment ClientDealEnvironment, deal rm.ClientDealState) error

ProcessNextResponse reads and processes the next response from the provider

func ProcessPaymentRequested

func ProcessPaymentRequested(ctx fsm.Context, environment ClientDealEnvironment, deal rm.ClientDealState) error

ProcessPaymentRequested processes a request for payment from the provider

func ProposeDeal

func ProposeDeal(ctx fsm.Context, environment ClientDealEnvironment, deal rm.ClientDealState) error

ProposeDeal sends the proposal to the other party

func SetupPaymentChannelStart

func SetupPaymentChannelStart(ctx fsm.Context, environment ClientDealEnvironment, deal rm.ClientDealState) error

SetupPaymentChannelStart initiates setting up a payment channel for a deal

func WaitForPaymentChannelAddFunds

func WaitForPaymentChannelAddFunds(ctx fsm.Context, environment ClientDealEnvironment, deal rm.ClientDealState) error

WaitForPaymentChannelAddFunds waits for funds to be added to an existing payment channel, then signals that payment channel is ready again

func WaitForPaymentChannelCreate

func WaitForPaymentChannelCreate(ctx fsm.Context, environment ClientDealEnvironment, deal rm.ClientDealState) error

WaitForPaymentChannelCreate waits for payment channel creation to be posted on chain,

allocates a lane for vouchers, then signals that the payment channel is ready

Types

type ClientDealEnvironment

type ClientDealEnvironment interface {
	Node() rm.RetrievalClientNode
	DealStream(id rm.DealID) rmnet.RetrievalDealStream
	ConsumeBlock(context.Context, rm.DealID, rm.Block) (uint64, bool, error)
}

ClientDealEnvironment is a bridge to the environment a client deal is executing in

Jump to

Keyboard shortcuts

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