lotus/paychmgr/settle_test.go

72 lines
1.9 KiB
Go
Raw Normal View History

2022-08-29 14:25:30 +00:00
// stm: #unit
2020-07-28 23:16:47 +00:00
package paychmgr
import (
"context"
"testing"
"github.com/ipfs/go-cid"
ds "github.com/ipfs/go-datastore"
ds_sync "github.com/ipfs/go-datastore/sync"
"github.com/stretchr/testify/require"
2022-06-14 15:00:51 +00:00
"github.com/filecoin-project/go-state-types/big"
tutils "github.com/filecoin-project/specs-actors/support/testing"
2020-07-28 23:16:47 +00:00
)
func TestPaychSettle(t *testing.T) {
2021-12-14 15:56:16 +00:00
//stm: @TOKEN_PAYCH_WAIT_READY_001, @TOKEN_PAYCH_SETTLE_001, @TOKEN_PAYCH_LIST_CHANNELS_001
2020-07-28 23:16:47 +00:00
ctx := context.Background()
store := NewStore(ds_sync.MutexWrap(ds.NewMapDatastore()))
expch := tutils.NewIDAddr(t, 100)
expch2 := tutils.NewIDAddr(t, 101)
from := tutils.NewIDAddr(t, 101)
to := tutils.NewIDAddr(t, 102)
mock := newMockManagerAPI()
defer mock.close()
2020-07-28 23:16:47 +00:00
mgr, err := newManager(store, mock)
2020-07-28 23:16:47 +00:00
require.NoError(t, err)
amt := big.NewInt(10)
2022-01-06 15:04:39 +00:00
_, mcid, err := mgr.GetPaych(ctx, from, to, amt, onChainReserve)
2020-07-28 23:16:47 +00:00
require.NoError(t, err)
// Send channel create response
response := testChannelResponse(t, expch)
mock.receiveMsgResponse(mcid, response)
2020-07-28 23:16:47 +00:00
// Get the channel address
ch, err := mgr.GetPaychWaitReady(ctx, mcid)
require.NoError(t, err)
require.Equal(t, expch, ch)
// Settle the channel
_, err = mgr.Settle(ctx, ch)
require.NoError(t, err)
// Send another request for funds to the same from/to
// (should create a new channel because the previous channel
// is settling)
amt2 := big.NewInt(5)
2022-01-06 15:04:39 +00:00
_, mcid2, err := mgr.GetPaych(ctx, from, to, amt2, onChainReserve)
2020-07-28 23:16:47 +00:00
require.NoError(t, err)
require.NotEqual(t, cid.Undef, mcid2)
// Send new channel create response
response2 := testChannelResponse(t, expch2)
mock.receiveMsgResponse(mcid2, response2)
2020-07-28 23:16:47 +00:00
// Make sure the new channel is different from the old channel
ch2, err := mgr.GetPaychWaitReady(ctx, mcid2)
require.NoError(t, err)
require.NotEqual(t, ch, ch2)
// There should now be two channels
2021-12-14 17:01:45 +00:00
cis, err := mgr.ListChannels(ctx)
require.NoError(t, err)
require.Len(t, cis, 2)
2020-07-28 23:16:47 +00:00
}