Skip to content

Commit

Permalink
feat(clientstates): handle payment chanel ready to go
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 committed Feb 3, 2022
1 parent c03723a commit a302a9e
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 2 deletions.
3 changes: 1 addition & 2 deletions retrievalmarket/impl/clientstates/client_fsm.go
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
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
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 a302a9e

Please sign in to comment.