lotus/paychmgr/store_test.go

86 lines
2.2 KiB
Go
Raw Normal View History

2020-07-08 21:10:02 +00:00
package paychmgr
import (
"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) {
store := NewStore(ds_sync.MutexWrap(ds.NewMapDatastore()))
addrs, err := store.ListChannels()
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
_, err = store.TrackChannel(ci)
2020-07-08 21:10:02 +00:00
require.NoError(t, err)
// Tracking same channel again should error
_, err = store.TrackChannel(ci)
2020-07-08 21:10:02 +00:00
require.Error(t, err)
// Track another channel
_, err = store.TrackChannel(ci2)
2020-07-08 21:10:02 +00:00
require.NoError(t, err)
// List channels should include all channels
addrs, err = store.ListChannels()
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
2020-07-28 23:16:47 +00:00
vouchers, err := store.VouchersForPaych(*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
2020-07-09 22:49:43 +00:00
_, err = store.VouchersForPaych(tutils.NewIDAddr(t, 300))
2020-07-08 21:10:02 +00:00
require.Equal(t, err, ErrChannelNotTracked)
// Allocate lane for channel
2020-07-28 23:16:47 +00:00
lane, err := store.AllocateLane(*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
2020-07-28 23:16:47 +00:00
lane, err = store.AllocateLane(*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
2020-07-09 22:49:43 +00:00
_, err = store.AllocateLane(tutils.NewIDAddr(t, 300))
2020-07-08 21:10:02 +00:00
require.Equal(t, err, ErrChannelNotTracked)
}