Skip to content

Commit

Permalink
feat(clientstates): handle payment chanel ready to go (#677)
Browse files Browse the repository at this point in the history
handle GetOrCreatePaymentChannel returns cid.Undef as payment channel being ready to use
  • Loading branch information
hannahhoward authored Feb 16, 2022
1 parent c03723a commit 5e87024
Show file tree
Hide file tree
Showing 6 changed files with 23 additions and 5 deletions.
1 change: 1 addition & 0 deletions docs/retrievalclient.mmd
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ stateDiagram-v2
DealStatusCheckFunds --> DealStatusPaymentChannelAddingFunds : ClientEventPaymentChannelAddingFunds
DealStatusPaymentChannelCreating --> DealStatusPaymentChannelAllocatingLane : ClientEventPaymentChannelReady
DealStatusPaymentChannelAddingFunds --> DealStatusOngoing : ClientEventPaymentChannelReady
DealStatusAccepted --> DealStatusPaymentChannelAllocatingLane : ClientEventPaymentChannelReady
DealStatusCheckFunds --> DealStatusOngoing : ClientEventPaymentChannelReady
DealStatusPaymentChannelAddingInitialFunds --> DealStatusPaymentChannelAllocatingLane : ClientEventPaymentChannelReady
DealStatusPaymentChannelAllocatingLane --> DealStatusFailing : ClientEventAllocateLaneErrored
Expand Down
Binary file modified docs/retrievalclient.mmd.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
6 changes: 3 additions & 3 deletions docs/retrievalclient.mmd.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
3 changes: 1 addition & 2 deletions retrievalmarket/impl/clientstates/client_fsm.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,8 +119,7 @@ var ClientEvents = fsm.Events{
// created for an earlier deal but the initial funding for this deal
// was being added, then we still need to allocate a payment channel
// lane
From(rm.DealStatusPaymentChannelCreating).To(rm.DealStatusPaymentChannelAllocatingLane).
From(rm.DealStatusPaymentChannelAddingInitialFunds).To(rm.DealStatusPaymentChannelAllocatingLane).
FromMany(rm.DealStatusPaymentChannelCreating, rm.DealStatusPaymentChannelAddingInitialFunds, rm.DealStatusAccepted).To(rm.DealStatusPaymentChannelAllocatingLane).
// If the payment channel ran out of funds and needed to be topped up,
// then the payment channel lane already exists so just move straight
// to the ongoing state
Expand Down
4 changes: 4 additions & 0 deletions retrievalmarket/impl/clientstates/client_states.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package clientstates
import (
"context"

"github.com/ipfs/go-cid"
logging "github.com/ipfs/go-log/v2"
peer "github.com/libp2p/go-libp2p-core/peer"

Expand Down Expand Up @@ -59,6 +60,9 @@ func SetupPaymentChannelStart(ctx fsm.Context, environment ClientDealEnvironment
return ctx.Trigger(rm.ClientEventPaymentChannelCreateInitiated, msgCID)
}

if msgCID == cid.Undef {
return ctx.Trigger(rm.ClientEventPaymentChannelReady, paych)
}
return ctx.Trigger(rm.ClientEventPaymentChannelAddingFunds, msgCID, paych)
}

Expand Down
14 changes: 14 additions & 0 deletions retrievalmarket/impl/clientstates/client_states_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"math/rand"
"testing"

"github.com/ipfs/go-cid"
peer "github.com/libp2p/go-libp2p-core/peer"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
Expand Down Expand Up @@ -141,6 +142,19 @@ func TestSetupPaymentChannel(t *testing.T) {
require.Equal(t, expectedPayCh, dealState.PaymentInfo.PayCh)
})

t.Run("payment channel fully ready", func(t *testing.T) {
envParams := testnodes.TestRetrievalClientNodeParams{
AddFundsOnly: true,
PayCh: expectedPayCh,
AddFundsCID: cid.Undef,
}
dealState := makeDealState(retrievalmarket.DealStatusAccepted)
runSetupPaymentChannel(t, envParams, dealState)
require.Empty(t, dealState.Message)
require.Equal(t, dealState.Status, retrievalmarket.DealStatusPaymentChannelAllocatingLane)
require.Equal(t, expectedPayCh, dealState.PaymentInfo.PayCh)
})

t.Run("when create payment channel fails", func(t *testing.T) {
dealState := makeDealState(retrievalmarket.DealStatusAccepted)
envParams := testnodes.TestRetrievalClientNodeParams{
Expand Down

0 comments on commit 5e87024

Please sign in to comment.