Merge pull request #4597 from filecoin-project/fix/paych-mgr-add-funds-race
fix race in paych manager add funds
This commit is contained in:
commit
43b492204c
@ -663,6 +663,7 @@ func TestPaychGetMergeAddFunds(t *testing.T) {
|
|||||||
defer addFundsSent.Done()
|
defer addFundsSent.Done()
|
||||||
|
|
||||||
// Request add funds - should block until create channel has completed
|
// Request add funds - should block until create channel has completed
|
||||||
|
var err error
|
||||||
addFundsCh1, addFundsMcid1, err = mgr.GetPaych(ctx, from, to, addFundsAmt1)
|
addFundsCh1, addFundsMcid1, err = mgr.GetPaych(ctx, from, to, addFundsAmt1)
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
}()
|
}()
|
||||||
@ -671,6 +672,7 @@ func TestPaychGetMergeAddFunds(t *testing.T) {
|
|||||||
defer addFundsSent.Done()
|
defer addFundsSent.Done()
|
||||||
|
|
||||||
// Request add funds again - should merge with waiting add funds request
|
// Request add funds again - should merge with waiting add funds request
|
||||||
|
var err error
|
||||||
addFundsCh2, addFundsMcid2, err = mgr.GetPaych(ctx, from, to, addFundsAmt2)
|
addFundsCh2, addFundsMcid2, err = mgr.GetPaych(ctx, from, to, addFundsAmt2)
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
}()
|
}()
|
||||||
@ -766,6 +768,7 @@ func TestPaychGetMergeAddFundsCtxCancelOne(t *testing.T) {
|
|||||||
defer addFundsSent.Done()
|
defer addFundsSent.Done()
|
||||||
|
|
||||||
// Request add funds again - should merge with waiting add funds request
|
// Request add funds again - should merge with waiting add funds request
|
||||||
|
var err error
|
||||||
addFundsCh2, addFundsMcid2, err = mgr.GetPaych(ctx, from, to, addFundsAmt2)
|
addFundsCh2, addFundsMcid2, err = mgr.GetPaych(ctx, from, to, addFundsAmt2)
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
}()
|
}()
|
||||||
@ -861,7 +864,6 @@ func TestPaychGetMergeAddFundsCtxCancelAll(t *testing.T) {
|
|||||||
|
|
||||||
// Request add funds again - should merge with waiting add funds request
|
// Request add funds again - should merge with waiting add funds request
|
||||||
_, _, addFundsErr2 = mgr.GetPaych(addFundsCtx2, from, to, big.NewInt(3))
|
_, _, addFundsErr2 = mgr.GetPaych(addFundsCtx2, from, to, big.NewInt(3))
|
||||||
require.NoError(t, err)
|
|
||||||
}()
|
}()
|
||||||
// Wait for add funds requests to be queued up
|
// Wait for add funds requests to be queued up
|
||||||
waitForQueueSize(t, mgr, from, to, 2)
|
waitForQueueSize(t, mgr, from, to, 2)
|
||||||
@ -950,6 +952,7 @@ func TestPaychAvailableFunds(t *testing.T) {
|
|||||||
defer addFundsSent.Done()
|
defer addFundsSent.Done()
|
||||||
|
|
||||||
// Request add funds - should block until create channel has completed
|
// Request add funds - should block until create channel has completed
|
||||||
|
var err error
|
||||||
_, addFundsMcid, err = mgr.GetPaych(ctx, from, to, addFundsAmt)
|
_, addFundsMcid, err = mgr.GetPaych(ctx, from, to, addFundsAmt)
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
}()
|
}()
|
||||||
|
@ -101,10 +101,13 @@ type mergedFundsReq struct {
|
|||||||
|
|
||||||
func newMergedFundsReq(reqs []*fundsReq) *mergedFundsReq {
|
func newMergedFundsReq(reqs []*fundsReq) *mergedFundsReq {
|
||||||
ctx, cancel := context.WithCancel(context.Background())
|
ctx, cancel := context.WithCancel(context.Background())
|
||||||
|
|
||||||
|
rqs := make([]*fundsReq, len(reqs))
|
||||||
|
copy(rqs, reqs)
|
||||||
m := &mergedFundsReq{
|
m := &mergedFundsReq{
|
||||||
ctx: ctx,
|
ctx: ctx,
|
||||||
cancel: cancel,
|
cancel: cancel,
|
||||||
reqs: reqs,
|
reqs: rqs,
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, r := range m.reqs {
|
for _, r := range m.reqs {
|
||||||
@ -201,7 +204,7 @@ func (ca *channelAccessor) processQueue(channelID string) (*api.ChannelAvailable
|
|||||||
// Merge all pending requests into one.
|
// Merge all pending requests into one.
|
||||||
// For example if there are pending requests for 3, 2, 4 then
|
// For example if there are pending requests for 3, 2, 4 then
|
||||||
// amt = 3 + 2 + 4 = 9
|
// amt = 3 + 2 + 4 = 9
|
||||||
merged := newMergedFundsReq(ca.fundsReqQueue[:])
|
merged := newMergedFundsReq(ca.fundsReqQueue)
|
||||||
amt := merged.sum()
|
amt := merged.sum()
|
||||||
if amt.IsZero() {
|
if amt.IsZero() {
|
||||||
// Note: The amount can be zero if requests are cancelled as we're
|
// Note: The amount can be zero if requests are cancelled as we're
|
||||||
|
Loading…
Reference in New Issue
Block a user