-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(fund): Add fundAll function that funds all funders and returns a…
… list of times, each funder took to finish. feat(TestEgoisticParticipantFunding): Add Test that sets one participant as egoistic, funds and checks if egoistic funder funds last. Signed-off-by: Sophia Koehler <[email protected]> refactor(multiledger): Add comment, rename function. Signed-off-by: Sophia Koehler <[email protected]> refactor(multiledger): Add var for participants length. Signed-off-by: Sophia Koehler <[email protected]>
- Loading branch information
Showing
3 changed files
with
137 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
package test | ||
|
||
import ( | ||
"context" | ||
"github.com/perun-network/perun-eth-backend/channel" | ||
pchannel "perun.network/go-perun/channel" | ||
pkgerrors "polycry.pt/poly-go/errors" | ||
"sync" | ||
"time" | ||
) | ||
|
||
type FunderFinishTime struct { | ||
Index int // Funder ID | ||
Time time.Duration // Time when Fund method returned | ||
} | ||
|
||
func FundAll(ctx context.Context, funders []*channel.Funder, reqs []*pchannel.FundingReq) ([]FunderFinishTime, error) { | ||
g := pkgerrors.NewGatherer() | ||
finishTimes := make([]FunderFinishTime, len(funders)) | ||
var wg sync.WaitGroup | ||
var mutex sync.Mutex | ||
|
||
wg.Add(len(funders)) | ||
for i := range funders { | ||
i := i | ||
g.Go(func() error { | ||
defer wg.Done() | ||
startTime := time.Now() | ||
err := funders[i].Fund(ctx, *reqs[i]) | ||
finishTime := time.Now() | ||
mutex.Lock() | ||
finishTimes[i] = FunderFinishTime{ | ||
Index: i, | ||
Time: finishTime.Sub(startTime), | ||
} | ||
mutex.Unlock() | ||
return err | ||
}) | ||
} | ||
wg.Wait() | ||
|
||
return finishTimes, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters