fix: lane allocation

This commit is contained in:
Dirk McCormick 2020-07-09 17:20:17 -04:00
parent 770ee8b54d
commit 1cdb008bd5
2 changed files with 19 additions and 26 deletions

View File

@ -4,7 +4,6 @@ import (
"bytes" "bytes"
"context" "context"
"fmt" "fmt"
"math"
"github.com/filecoin-project/lotus/api" "github.com/filecoin-project/lotus/api"
@ -82,14 +81,18 @@ func newManager(sm StateManagerApi, pchstore *Store) *Manager {
} }
} }
func maxLaneFromState(st *paych.State) (uint64, error) { func nextLaneFromState(st *paych.State) uint64 {
maxLane := uint64(math.MaxInt64) if len(st.LaneStates) == 0 {
return 0
}
maxLane := st.LaneStates[0].ID
for _, state := range st.LaneStates { for _, state := range st.LaneStates {
if (state.ID)+1 > maxLane+1 { if state.ID > maxLane {
maxLane = state.ID maxLane = state.ID
} }
} }
return maxLane, nil return maxLane + 1
} }
func (pm *Manager) TrackInboundChannel(ctx context.Context, ch address.Address) error { func (pm *Manager) TrackInboundChannel(ctx context.Context, ch address.Address) error {
@ -110,18 +113,13 @@ func (pm *Manager) TrackInboundChannel(ctx context.Context, ch address.Address)
} }
to := account.Address to := account.Address
maxLane, err := maxLaneFromState(st)
if err != nil {
return err
}
return pm.store.TrackChannel(&ChannelInfo{ return pm.store.TrackChannel(&ChannelInfo{
Channel: ch, Channel: ch,
Control: to, Control: to,
Target: from, Target: from,
Direction: DirInbound, Direction: DirInbound,
NextLane: maxLane + 1, NextLane: nextLaneFromState(st),
}) })
} }
@ -131,11 +129,6 @@ func (pm *Manager) loadOutboundChannelInfo(ctx context.Context, ch address.Addre
return nil, err return nil, err
} }
maxLane, err := maxLaneFromState(st)
if err != nil {
return nil, err
}
var account account.State var account account.State
_, err = pm.sm.LoadActorState(ctx, st.From, &account, nil) _, err = pm.sm.LoadActorState(ctx, st.From, &account, nil)
if err != nil { if err != nil {
@ -154,7 +147,7 @@ func (pm *Manager) loadOutboundChannelInfo(ctx context.Context, ch address.Addre
Target: to, Target: to,
Direction: DirOutbound, Direction: DirOutbound,
NextLane: maxLane + 1, NextLane: nextLaneFromState(st),
}, nil }, nil
} }
@ -365,7 +358,7 @@ func (pm *Manager) AddVoucher(ctx context.Context, ch address.Address, sv *paych
Proof: proof, Proof: proof,
}) })
if ci.NextLane <= (sv.Lane) { if ci.NextLane <= sv.Lane {
ci.NextLane = sv.Lane + 1 ci.NextLane = sv.Lane + 1
} }

View File

@ -366,7 +366,7 @@ func TestAddVoucherNextLane(t *testing.T) {
minDelta := big.NewInt(0) minDelta := big.NewInt(0)
voucherAmount := big.NewInt(2) voucherAmount := big.NewInt(2)
// Add a voucher in lane: 2 // Add a voucher in lane 2
nonce := uint64(1) nonce := uint64(1)
voucherLane := uint64(2) voucherLane := uint64(2)
sv := testCreateVoucher(t, voucherLane, nonce, voucherAmount, fromKeyPrivate) sv := testCreateVoucher(t, voucherLane, nonce, voucherAmount, fromKeyPrivate)
@ -377,8 +377,7 @@ func TestAddVoucherNextLane(t *testing.T) {
require.NoError(t, err) require.NoError(t, err)
require.EqualValues(t, ci.NextLane, 3) require.EqualValues(t, ci.NextLane, 3)
// Add a voucher in lane: 1 // Add a voucher in lane 1
nonce++
voucherLane = uint64(1) voucherLane = uint64(1)
sv = testCreateVoucher(t, voucherLane, nonce, voucherAmount, fromKeyPrivate) sv = testCreateVoucher(t, voucherLane, nonce, voucherAmount, fromKeyPrivate)
_, err = mgr.AddVoucher(ctx, ch, sv, nil, minDelta) _, err = mgr.AddVoucher(ctx, ch, sv, nil, minDelta)
@ -388,9 +387,8 @@ func TestAddVoucherNextLane(t *testing.T) {
require.NoError(t, err) require.NoError(t, err)
require.EqualValues(t, ci.NextLane, 3) require.EqualValues(t, ci.NextLane, 3)
// Add a voucher in lane: 5 // Add a voucher in lane 5
nonce++ voucherLane = uint64(5)
voucherLane = uint64(1)
sv = testCreateVoucher(t, voucherLane, nonce, voucherAmount, fromKeyPrivate) sv = testCreateVoucher(t, voucherLane, nonce, voucherAmount, fromKeyPrivate)
_, err = mgr.AddVoucher(ctx, ch, sv, nil, minDelta) _, err = mgr.AddVoucher(ctx, ch, sv, nil, minDelta)
require.NoError(t, err) require.NoError(t, err)
@ -453,13 +451,15 @@ func TestAllocateLane(t *testing.T) {
// Set up a manager with a single payment channel // Set up a manager with a single payment channel
mgr, ch, _ := testSetupMgrWithChannel(t, ctx) mgr, ch, _ := testSetupMgrWithChannel(t, ctx)
// First lane should be 0
lane, err := mgr.AllocateLane(ch) lane, err := mgr.AllocateLane(ch)
require.NoError(t, err) require.NoError(t, err)
require.EqualValues(t, lane, 1) require.EqualValues(t, lane, 0)
// Next lane should be 1
lane, err = mgr.AllocateLane(ch) lane, err = mgr.AllocateLane(ch)
require.NoError(t, err) require.NoError(t, err)
require.EqualValues(t, lane, 2) require.EqualValues(t, lane, 1)
} }
func TestNextNonceForLane(t *testing.T) { func TestNextNonceForLane(t *testing.T) {