lotus/paychmgr/store_test.go

89 lines
2.3 KiB
Go
Raw Normal View History

2020-07-08 21:10:02 +00:00
package paychmgr
import (
2021-12-14 17:01:45 +00:00
"context"
2020-07-08 21:10:02 +00:00
"testing"
"github.com/filecoin-project/go-address"
tutils "github.com/filecoin-project/specs-actors/support/testing"
ds "github.com/ipfs/go-datastore"
ds_sync "github.com/ipfs/go-datastore/sync"
"github.com/stretchr/testify/require"
)
func TestStore(t *testing.T) {
2021-12-14 17:01:45 +00:00
ctx := context.Background()
2020-07-08 21:10:02 +00:00
store := NewStore(ds_sync.MutexWrap(ds.NewMapDatastore()))
2021-12-14 17:01:45 +00:00
addrs, err := store.ListChannels(ctx)
2020-07-08 21:10:02 +00:00
require.NoError(t, err)
require.Len(t, addrs, 0)
2020-07-28 23:16:47 +00:00
ch := tutils.NewIDAddr(t, 100)
2020-07-08 21:10:02 +00:00
ci := &ChannelInfo{
2020-07-28 23:16:47 +00:00
Channel: &ch,
2020-07-08 21:10:02 +00:00
Control: tutils.NewIDAddr(t, 101),
Target: tutils.NewIDAddr(t, 102),
Direction: DirOutbound,
Vouchers: []*VoucherInfo{{Voucher: nil, Proof: []byte{}}},
}
2020-07-28 23:16:47 +00:00
ch2 := tutils.NewIDAddr(t, 200)
2020-07-08 21:10:02 +00:00
ci2 := &ChannelInfo{
2020-07-28 23:16:47 +00:00
Channel: &ch2,
2020-07-08 21:10:02 +00:00
Control: tutils.NewIDAddr(t, 201),
Target: tutils.NewIDAddr(t, 202),
Direction: DirOutbound,
Vouchers: []*VoucherInfo{{Voucher: nil, Proof: []byte{}}},
}
// Track the channel
2021-12-14 17:01:45 +00:00
_, err = store.TrackChannel(ctx, ci)
2020-07-08 21:10:02 +00:00
require.NoError(t, err)
// Tracking same channel again should error
2021-12-14 17:01:45 +00:00
_, err = store.TrackChannel(ctx, ci)
2020-07-08 21:10:02 +00:00
require.Error(t, err)
// Track another channel
2021-12-14 17:01:45 +00:00
_, err = store.TrackChannel(ctx, ci2)
2020-07-08 21:10:02 +00:00
require.NoError(t, err)
// List channels should include all channels
2021-12-14 17:01:45 +00:00
addrs, err = store.ListChannels(ctx)
2020-07-08 21:10:02 +00:00
require.NoError(t, err)
require.Len(t, addrs, 2)
2020-10-06 07:43:20 +00:00
t0100, err := address.NewIDAddress(100)
require.NoError(t, err)
t0200, err := address.NewIDAddress(200)
require.NoError(t, err)
require.Contains(t, addrs, t0100)
require.Contains(t, addrs, t0200)
2020-07-08 21:10:02 +00:00
// Request vouchers for channel
2021-12-14 17:01:45 +00:00
vouchers, err := store.VouchersForPaych(ctx, *ci.Channel)
2020-07-08 21:10:02 +00:00
require.NoError(t, err)
require.Len(t, vouchers, 1)
// Requesting voucher for non-existent channel should error
2021-12-14 17:01:45 +00:00
_, err = store.VouchersForPaych(ctx, tutils.NewIDAddr(t, 300))
2020-07-08 21:10:02 +00:00
require.Equal(t, err, ErrChannelNotTracked)
// Allocate lane for channel
2021-12-14 17:01:45 +00:00
lane, err := store.AllocateLane(ctx, *ci.Channel)
2020-07-08 21:10:02 +00:00
require.NoError(t, err)
require.Equal(t, lane, uint64(0))
// Allocate next lane for channel
2021-12-14 17:01:45 +00:00
lane, err = store.AllocateLane(ctx, *ci.Channel)
2020-07-08 21:10:02 +00:00
require.NoError(t, err)
require.Equal(t, lane, uint64(1))
// Allocate next lane for non-existent channel should error
2021-12-14 17:01:45 +00:00
_, err = store.AllocateLane(ctx, tutils.NewIDAddr(t, 300))
2020-07-08 21:10:02 +00:00
require.Equal(t, err, ErrChannelNotTracked)
}