Skip to content

Commit

Permalink
refactor(fund): change to loop.
Browse files Browse the repository at this point in the history
Signed-off-by: Sophia Koehler <[email protected]>
  • Loading branch information
sophia1ch committed Apr 2, 2024
1 parent 5c1556a commit a567031
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 30 deletions.
15 changes: 15 additions & 0 deletions channel/funder.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
package channel

import (
"bytes"
"context"
"fmt"
"math/big"
Expand All @@ -41,13 +42,25 @@ import (
perror "polycry.pt/poly-go/errors"
)

// LogCapturer is a custom logger that captures log messages.
type LogCapturer struct {
capturedLogs *bytes.Buffer
}

type assetHolder struct {
*assetholder.Assetholder
*common.Address
contract *bind.BoundContract
assetIndex channel.Index
}

// NewLogCapturer creates a new LogCapturer instance.
func NewLogCapturer() *LogCapturer {
return &LogCapturer{
capturedLogs: new(bytes.Buffer),
}
}

// Funder implements the channel.Funder interface for Ethereum.
//
// In addition to the `Fund` method required by the `Funder` interface, it also
Expand Down Expand Up @@ -185,6 +198,7 @@ func (f *Funder) Fund(ctx context.Context, request channel.FundingReq) error {
if channel.IsAssetFundingError(err) && err != nil {
fundingErr, ok := err.(*channel.AssetFundingError)
if !ok {
fmt.Println(err)
return fmt.Errorf("wrong type: expected %T, got %T", &channel.AssetFundingError{}, err)
}
fundingErrs = append(fundingErrs, fundingErr)
Expand Down Expand Up @@ -410,6 +424,7 @@ loop:
select {
case rawEvent := <-deposited:
event, ok := rawEvent.Data.(*assetholder.AssetholderDeposited)
fmt.Println("Funding Event ", event)
if !ok {
log.Panic("wrong event type")
}
Expand Down
52 changes: 22 additions & 30 deletions channel/test/fund.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,38 +36,30 @@ func FundAll(ctx context.Context, funders []*channel.Funder, reqs []*pchannel.Fu
var mutex sync.Mutex

var wg sync.WaitGroup
wg.Add(2)
wg.Add(len(funders))

go func() {
defer wg.Done()
startTime := time.Now()
err := funders[egoisticIndex].Fund(ctx, *reqs[egoisticIndex])
require.NoError(t, err)
finishTime := time.Now()
require.NoError(t, err)
mutex.Lock()
finishTimes[egoisticIndex] = FunderFinishTime{
Index: egoisticIndex,
Time: finishTime.Sub(startTime),
}
mutex.Unlock()
}()
waitTime := 2 * time.Second

go func() {
defer wg.Done()
time.Sleep(2 * time.Second)
startTime := time.Now()
err := funders[1-egoisticIndex].Fund(ctx, *reqs[1-egoisticIndex])
require.NoError(t, err)
finishTime := time.Now()
require.NoError(t, err)
mutex.Lock()
finishTimes[1-egoisticIndex] = FunderFinishTime{
Index: 1 - egoisticIndex,
Time: finishTime.Sub(startTime),
}
mutex.Unlock()
}()
for i := range funders {
i := i
go func() {
defer wg.Done()
if i != egoisticIndex {
time.Sleep(waitTime)
}
startTime := time.Now()
err := funders[i].Fund(ctx, *reqs[i])
require.NoError(t, err)
finishTime := time.Now()
mutex.Lock()
finishTimes[i] = FunderFinishTime{
Index: i,
Time: finishTime.Sub(startTime),
}
mutex.Unlock()
}()

}

wg.Wait()
return finishTimes, nil
Expand Down

0 comments on commit a567031

Please sign in to comment.