From a302a9e6a56c62b4eb537441da263960c7494f81 Mon Sep 17 00:00:00 2001 From: hannahhoward Date: Wed, 2 Feb 2022 17:00:45 -0800 Subject: [PATCH] feat(clientstates): handle payment chanel ready to go handle GetOrCreatePaymentChannel returns cid.Undef as payment channel being ready to use --- retrievalmarket/impl/clientstates/client_fsm.go | 3 +-- retrievalmarket/impl/clientstates/client_states.go | 4 ++++ .../impl/clientstates/client_states_test.go | 14 ++++++++++++++ 3 files changed, 19 insertions(+), 2 deletions(-) diff --git a/retrievalmarket/impl/clientstates/client_fsm.go b/retrievalmarket/impl/clientstates/client_fsm.go index 1b460cba0..1eb1ba9de 100644 --- a/retrievalmarket/impl/clientstates/client_fsm.go +++ b/retrievalmarket/impl/clientstates/client_fsm.go @@ -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 diff --git a/retrievalmarket/impl/clientstates/client_states.go b/retrievalmarket/impl/clientstates/client_states.go index 070ef3c83..4d55d7f5f 100644 --- a/retrievalmarket/impl/clientstates/client_states.go +++ b/retrievalmarket/impl/clientstates/client_states.go @@ -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" @@ -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) } diff --git a/retrievalmarket/impl/clientstates/client_states_test.go b/retrievalmarket/impl/clientstates/client_states_test.go index abdbc9736..028513c52 100644 --- a/retrievalmarket/impl/clientstates/client_states_test.go +++ b/retrievalmarket/impl/clientstates/client_states_test.go @@ -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" @@ -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{