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

View File

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