Skip to content

Commit 3c47eab

Browse files
sophia1chiljabvh
andauthored
Egoistic Funding Test (#52)
* feat(multiledger): Add egoistic argument in setupClient to test correctness of egoisticPart and EgoisticChains. Signed-off-by: Sophia Koehler <[email protected]> * feat(multiledger): Add new test case for egoistic funding. Signed-off-by: Sophia Koehler <[email protected]> * 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]> * revert: Remove multiledger test. Signed-off-by: Sophia Koehler <[email protected]> refactor(fund): Add t.Helper() Signed-off-by: Sophia Koehler <[email protected]> refactor(fund): Rearrange arguments in fundAll. Signed-off-by: Sophia Koehler <[email protected]> refactor(fund): Add comment for FundAll. Signed-off-by: Sophia Koehler <[email protected]> refactor(fund): Add const for waiting time. Signed-off-by: Sophia Koehler <[email protected]> refactor(fund): Change to loop. Signed-off-by: Sophia Koehler <[email protected]> Revert "refactor(fund): change to loop." This reverts commit a567031. Signed-off-by: Sophia Koehler <[email protected]> refactor(fund): change to loop. Signed-off-by: Sophia Koehler <[email protected]> refactor(fund): remove loop, add 2 second wait. Signed-off-by: Sophia Koehler <[email protected]> refactor(funder): rearrange imports. Signed-off-by: Sophia Koehler <[email protected]> refactor(fund): Add waitgroup. Signed-off-by: Sophia Koehler <[email protected]> refactor(funder_test) refactor(fund): Add concurrent testing. Signed-off-by: Sophia Koehler <[email protected]> refactor(fund): Add comments, delete unnecessary function. Signed-off-by: Sophia Koehler <[email protected]> * chore: Update dates of copyright license Signed-off-by: Sophia Koehler <[email protected]> --------- Signed-off-by: Sophia Koehler <[email protected]> Co-authored-by: Ilja von Hoessle <[email protected]>
1 parent 06cea29 commit 3c47eab

File tree

4 files changed

+121
-4
lines changed

4 files changed

+121
-4
lines changed

channel/funder_test.go

Lines changed: 50 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright 2019 - See NOTICE file for copyright holders.
1+
// Copyright 2024 - See NOTICE file for copyright holders.
22
//
33
// Licensed under the Apache License, Version 2.0 (the "License");
44
// you may not use this file except in compliance with the License.
@@ -16,6 +16,8 @@ package channel_test
1616

1717
import (
1818
"context"
19+
"fmt"
20+
"math"
1921
"math/big"
2022
"math/rand"
2123
"testing"
@@ -194,6 +196,53 @@ func testFunderCrossOverFunding(t *testing.T, n int) {
194196
assert.NoError(t, compareOnChainAlloc(ctx, params, agreement, alloc.Assets, &funders[0].ContractBackend))
195197
}
196198

199+
func TestEgoisticParticipantFunding(t *testing.T) {
200+
// Peers will randomly fund for each other.
201+
for i := 0; i < 30; i++ {
202+
name := fmt.Sprintf("Egoistic Funding %v", i)
203+
t.Run(name, func(t *testing.T) { testEgoisticParticipantFunding(t) })
204+
}
205+
}
206+
207+
func testEgoisticParticipantFunding(t *testing.T) {
208+
t.Helper()
209+
t.Parallel()
210+
n := 2
211+
rng := pkgtest.Prng(t, n)
212+
EgoisticTxTimeout := 20 * time.Second
213+
ctx, cancel := context.WithTimeout(context.Background(), EgoisticTxTimeout*time.Duration(n))
214+
defer cancel()
215+
_, funders, params, alloc := newNFunders(ctx, t, rng, n)
216+
egoisticIndex := rng.Intn(2)
217+
balances := [][]*big.Int{
218+
{big.NewInt(1), big.NewInt(0)},
219+
{big.NewInt(0), big.NewInt(2)},
220+
}
221+
allocation := channel.Allocation{Assets: alloc.Assets, Balances: balances, Locked: alloc.Locked}
222+
egoisticParts := make([]bool, n)
223+
funders[egoisticIndex].SetEgoisticPart(channel.Index(egoisticIndex), n)
224+
egoisticParts[egoisticIndex] = true
225+
t.Logf("Egoistic Participants: %v", egoisticParts)
226+
agreement := allocation.Balances
227+
require.Equal(t, agreement.Sum(), allocation.Balances.Sum())
228+
229+
fundingRequests := make([]*channel.FundingReq, n)
230+
for i := range funders {
231+
req := channel.NewFundingReq(params, &channel.State{Allocation: allocation}, channel.Index(i), agreement)
232+
fundingRequests[i] = req
233+
}
234+
finishTimes, err := test.FundAll(ctx, t, funders, fundingRequests, egoisticIndex)
235+
require.True(t, len(finishTimes) == n, "Length of indexes must be n")
236+
require.NoError(t, err)
237+
238+
t.Logf("finishTimes: %v", finishTimes)
239+
// Check if finish time of egoistic funder is larger than finish time of non-egoistic funder.
240+
correct := finishTimes[egoisticIndex].Time > finishTimes[int(math.Abs(float64(1-egoisticIndex)))].Time
241+
// Use require.True to compare the finish times
242+
require.True(t, correct, "Non-egoistic funders finish earlier than egoistic funders")
243+
assert.NoError(t, compareOnChainAlloc(ctx, params, agreement, allocation.Assets, &funders[0].ContractBackend))
244+
}
245+
197246
func TestFunder_ZeroBalance(t *testing.T) {
198247
t.Run("1 Participant", func(t *testing.T) { testFunderZeroBalance(t, 1) })
199248
t.Run("2 Participant", func(t *testing.T) { testFunderZeroBalance(t, 2) })

channel/test/fund.go

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
// Copyright 2024 - See NOTICE file for copyright holders.
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
package test
16+
17+
import (
18+
"context"
19+
"sync"
20+
"testing"
21+
"time"
22+
23+
"github.com/perun-network/perun-eth-backend/channel"
24+
"github.com/stretchr/testify/require"
25+
pchannel "perun.network/go-perun/channel"
26+
)
27+
28+
// waitTime is the time to wait before funding a channel when the funder is not egoistic.
29+
const waitTime = 2 * time.Second
30+
31+
// FunderFinishTime is the returned type of FundAll which includes the process time of each funder.
32+
type FunderFinishTime struct {
33+
Index int
34+
Time time.Duration
35+
}
36+
37+
// FundAll calls fund for all funders simultaneously.
38+
func FundAll(ctx context.Context, t *testing.T, funders []*channel.Funder, reqs []*pchannel.FundingReq, egoisticIndex int) ([]FunderFinishTime, error) {
39+
t.Helper()
40+
finishTimes := make([]FunderFinishTime, len(funders))
41+
var mutex sync.Mutex
42+
43+
var wg sync.WaitGroup
44+
wg.Add(len(funders))
45+
46+
for i := range funders {
47+
i := i
48+
go func() {
49+
defer wg.Done()
50+
if i != egoisticIndex {
51+
time.Sleep(waitTime)
52+
}
53+
startTime := time.Now()
54+
err := funders[i].Fund(ctx, *reqs[i])
55+
require.NoError(t, err)
56+
finishTime := time.Now()
57+
mutex.Lock()
58+
finishTimes[i] = FunderFinishTime{
59+
Index: i,
60+
Time: finishTime.Sub(startTime),
61+
}
62+
mutex.Unlock()
63+
}()
64+
}
65+
66+
wg.Wait()
67+
return finishTimes, nil
68+
}

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ require (
99
github.com/sirupsen/logrus v1.8.1
1010
github.com/stretchr/testify v1.7.0
1111
golang.org/x/sync v0.0.0-20210220032951-036812b2e83c
12-
perun.network/go-perun v0.11.1-0.20240312125059-3811fdb76baf
12+
perun.network/go-perun v0.11.1-0.20240326094100-011cfdf0ea51
1313
polycry.pt/poly-go v0.0.0-20220301085937-fb9d71b45a37
1414
)
1515

go.sum

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -645,8 +645,8 @@ honnef.co/go/tools v0.0.0-20190418001031-e561f6794a2a/go.mod h1:rf3lG4BRIbNafJWh
645645
honnef.co/go/tools v0.0.0-20190523083050-ea95bdfd59fc/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4=
646646
honnef.co/go/tools v0.0.1-2019.2.3/go.mod h1:a3bituU0lyd329TUQxRnasdCoJDkEUEAqEt0JzvZhAg=
647647
honnef.co/go/tools v0.1.3/go.mod h1:NgwopIslSNH47DimFoV78dnkksY2EFtX0ajyb3K/las=
648-
perun.network/go-perun v0.11.1-0.20240312125059-3811fdb76baf h1:eTmgN9p2Uo48c+8Jiya67wnRakZEBoXlG3fWOSXlPw0=
649-
perun.network/go-perun v0.11.1-0.20240312125059-3811fdb76baf/go.mod h1:VfCebjZTnFrQRcjbkK1s1l+H71MKW7jodpFQBdP7tRQ=
648+
perun.network/go-perun v0.11.1-0.20240326094100-011cfdf0ea51 h1:fIM4nKc6OKkBX/gJrCTMMFmxeoFTE1DAerndDW/njVc=
649+
perun.network/go-perun v0.11.1-0.20240326094100-011cfdf0ea51/go.mod h1:VfCebjZTnFrQRcjbkK1s1l+H71MKW7jodpFQBdP7tRQ=
650650
polycry.pt/poly-go v0.0.0-20220301085937-fb9d71b45a37 h1:iA5GzEa/hHfVlQpimEjPV09NATwHXxSjWNB0VVodtew=
651651
polycry.pt/poly-go v0.0.0-20220301085937-fb9d71b45a37/go.mod h1:XUBrNtqgEhN3EEOP/5gh7IBd3xVHKidCjXDZfl9+kMU=
652652
rsc.io/binaryregexp v0.2.0/go.mod h1:qTv7/COck+e2FymRvadv62gMdZztPaShugOCi3I+8D8=

0 commit comments

Comments
 (0)