Skip to content

Commit 4cb6cbc

Browse files
Merge pull request #310 from hyperledger-labs/fix-funder-timeout-tests
Fix funder timeout tests
2 parents f661f51 + fac15d8 commit 4cb6cbc

File tree

2 files changed

+23
-13
lines changed

2 files changed

+23
-13
lines changed

backend/ethereum/channel/funder.go

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -336,23 +336,33 @@ func (f *Funder) waitForFundingConfirmation(ctx context.Context, request channel
336336
log.Debugf("peer[%d]: got: %v, remaining for [%d,%d] = %v. N: %d", request.Idx, event.Amount, asset.assetIndex, idx, amount, N)
337337

338338
case <-ctx.Done():
339-
var indices []channel.Index
340-
for k, bals := range agreement {
341-
if bals.Sign() == 1 {
342-
indices = append(indices, channel.Index(k))
343-
}
344-
}
345-
if len(indices) != 0 {
346-
return &channel.AssetFundingError{Asset: asset.assetIndex, TimedOutPeers: indices}
347-
}
348-
return nil
339+
return fundingTimeoutError(agreement, asset)
349340
case err := <-subErr:
341+
// Resolve race between ctx and subErr, as ctx fires both events.
342+
select {
343+
case <-ctx.Done():
344+
return fundingTimeoutError(agreement, asset)
345+
default:
346+
}
350347
return err
351348
}
352349
}
353350
return nil
354351
}
355352

353+
func fundingTimeoutError(agreement []channel.Bal, asset assetHolder) error {
354+
var indices []channel.Index
355+
for k, bals := range agreement {
356+
if bals.Sign() == 1 {
357+
indices = append(indices, channel.Index(k))
358+
}
359+
}
360+
if len(indices) != 0 {
361+
return &channel.AssetFundingError{Asset: asset.assetIndex, TimedOutPeers: indices}
362+
}
363+
return nil
364+
}
365+
356366
func partIdx(partID [32]byte, fundingIDs [][32]byte) int {
357367
for i, id := range fundingIDs {
358368
if id == partID {

backend/ethereum/channel/funder_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -298,7 +298,7 @@ func testFundingTimeout(t *testing.T, faultyPeer, n int) {
298298
for i, funder := range funders {
299299
sleepTime := time.Millisecond * time.Duration(rng.Int63n(10)+1)
300300
i, funder := i, funder
301-
go ct.StageN("funding loop", n, func(rt pkgtest.ConcT) {
301+
go ct.StageN("funding loop", n, func(t pkgtest.ConcT) {
302302
// Faulty peer does not fund the channel.
303303
if i == faultyPeer {
304304
return
@@ -307,13 +307,13 @@ func testFundingTimeout(t *testing.T, faultyPeer, n int) {
307307
req := channel.NewFundingReq(params, &channel.State{Allocation: *alloc}, channel.Index(i), alloc.Balances)
308308
err := funder.Fund(ctx, *req)
309309
require.Error(t, err)
310-
require.True(rt, channel.IsFundingTimeoutError(err), "funder should return FundingTimeoutError")
310+
require.True(t, channel.IsFundingTimeoutError(err), "funder should return FundingTimeoutError")
311311
pErr := errors.Cause(err).(channel.FundingTimeoutError) // unwrap error
312312
// Check that `faultyPeer` is reported as faulty.
313313
require.Len(t, pErr.Errors, len(alloc.Assets))
314314
for _, e := range pErr.Errors {
315315
require.Len(t, e.TimedOutPeers, 1)
316-
assert.Equal(t, channel.Index(faultyPeer), e.TimedOutPeers[0], "Peer should be detected as erroneous")
316+
require.Equal(t, channel.Index(faultyPeer), e.TimedOutPeers[0], "Peer should be detected as erroneous")
317317
}
318318
outer:
319319
for a := 0; a < len(alloc.Assets); a++ {

0 commit comments

Comments
 (0)