1
- // Copyright 2019 - See NOTICE file for copyright holders.
1
+ // Copyright 2024 - See NOTICE file for copyright holders.
2
2
//
3
3
// Licensed under the Apache License, Version 2.0 (the "License");
4
4
// you may not use this file except in compliance with the License.
@@ -16,6 +16,8 @@ package channel_test
16
16
17
17
import (
18
18
"context"
19
+ "fmt"
20
+ "math"
19
21
"math/big"
20
22
"math/rand"
21
23
"testing"
@@ -194,6 +196,53 @@ func testFunderCrossOverFunding(t *testing.T, n int) {
194
196
assert .NoError (t , compareOnChainAlloc (ctx , params , agreement , alloc .Assets , & funders [0 ].ContractBackend ))
195
197
}
196
198
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
+
197
246
func TestFunder_ZeroBalance (t * testing.T ) {
198
247
t .Run ("1 Participant" , func (t * testing.T ) { testFunderZeroBalance (t , 1 ) })
199
248
t .Run ("2 Participant" , func (t * testing.T ) { testFunderZeroBalance (t , 2 ) })
0 commit comments