@@ -17,6 +17,7 @@ package client_test
17
17
import (
18
18
"context"
19
19
"math/big"
20
+ "math/rand"
20
21
"testing"
21
22
"time"
22
23
@@ -26,6 +27,7 @@ import (
26
27
"perun.network/go-perun/channel"
27
28
chtest "perun.network/go-perun/channel/test"
28
29
"perun.network/go-perun/client"
30
+ ctest "perun.network/go-perun/client/test"
29
31
"perun.network/go-perun/pkg/sync"
30
32
"perun.network/go-perun/pkg/test"
31
33
"perun.network/go-perun/wire"
@@ -68,7 +70,47 @@ func TestVirtualChannelsOptimistic(t *testing.T) {
68
70
assert .NoError (t , err , "Bob: invalid final balances" )
69
71
}
70
72
73
+ func TestVirtualChannelsDispute (t * testing.T ) {
74
+ ctx , cancel := context .WithTimeout (context .Background (), testDuration )
75
+ defer cancel ()
76
+
77
+ vct := setupVirtualChannelTest (t , ctx )
78
+ assert := assert .New (t )
79
+
80
+ chs := []* client.Channel {vct .chAliceIngrid , vct .chIngridAlice , vct .chBobIngrid , vct .chIngridBob }
81
+ // Register the channels in a random order.
82
+ for _ , i := range rand .Perm (len (chs )) {
83
+ err := chs [i ].Register (ctx )
84
+ assert .NoErrorf (err , "register channel: %d" , i )
85
+ }
86
+
87
+ time .Sleep (100 * time .Millisecond ) // Sleep to ensure that registered events have been processed.
88
+
89
+ // Settle the channels in a random order.
90
+ for _ , i := range rand .Perm (len (chs )) {
91
+ err := chs [i ].Settle (ctx , false )
92
+ assert .NoErrorf (err , "settle channel: %d" , i )
93
+ }
94
+
95
+ // Test final balances.
96
+ vct .testFinalBalancesDispute (t )
97
+ }
98
+
99
+ func (vct * virtualChannelTest ) testFinalBalancesDispute (t * testing.T ) {
100
+ assert := assert .New (t )
101
+ backend , asset := vct .backend , vct .asset
102
+ got , expected := backend .GetBalance (vct .alice .Identity .Address (), asset ), vct .finalBalsAlice [0 ]
103
+ assert .Truef (got .Cmp (expected ) == 0 , "alice: wrong final balance: got %v, expected %v" , got , expected )
104
+ got , expected = backend .GetBalance (vct .bob .Identity .Address (), asset ), vct .finalBalsBob [0 ]
105
+ assert .Truef (got .Cmp (expected ) == 0 , "bob: wrong final balance: got %v, expected %v" , got , expected )
106
+ got , expected = backend .GetBalance (vct .ingrid .Identity .Address (), asset ), vct .finalBalIngrid
107
+ assert .Truef (got .Cmp (expected ) == 0 , "ingrid: wrong final balance: got %v, expected %v" , got , expected )
108
+ }
109
+
71
110
type virtualChannelTest struct {
111
+ alice * Client
112
+ bob * Client
113
+ ingrid * Client
72
114
chAliceIngrid * client.Channel
73
115
chIngridAlice * client.Channel
74
116
chBobIngrid * client.Channel
@@ -78,7 +120,10 @@ type virtualChannelTest struct {
78
120
virtualBalsUpdated []* big.Int
79
121
finalBalsAlice []* big.Int
80
122
finalBalsBob []* big.Int
123
+ finalBalIngrid * big.Int
81
124
errs chan error
125
+ backend * ctest.MockBackend
126
+ asset channel.Asset
82
127
}
83
128
84
129
func setupVirtualChannelTest (t * testing.T , ctx context.Context ) (vct virtualChannelTest ) {
@@ -87,12 +132,14 @@ func setupVirtualChannelTest(t *testing.T, ctx context.Context) (vct virtualChan
87
132
88
133
// Set test values.
89
134
asset := chtest .NewRandomAsset (rng )
135
+ vct .asset = asset
90
136
initBalsAlice := []* big.Int {big .NewInt (10 ), big .NewInt (10 )} // with Ingrid
91
137
initBalsBob := []* big.Int {big .NewInt (10 ), big .NewInt (10 )} // with Ingrid
92
138
initBalsVirtual := []* big.Int {big .NewInt (5 ), big .NewInt (5 )} // Alice proposes
93
139
vct .virtualBalsUpdated = []* big.Int {big .NewInt (2 ), big .NewInt (8 )} // Send 3.
94
140
vct .finalBalsAlice = []* big.Int {big .NewInt (7 ), big .NewInt (13 )}
95
141
vct .finalBalsBob = []* big.Int {big .NewInt (13 ), big .NewInt (7 )}
142
+ vct .finalBalIngrid = new (big.Int ).Add (vct .finalBalsAlice [1 ], vct .finalBalsBob [1 ])
96
143
vct .errs = make (chan error , 10 )
97
144
98
145
// Setup clients.
@@ -102,6 +149,8 @@ func setupVirtualChannelTest(t *testing.T, ctx context.Context) (vct virtualChan
102
149
t ,
103
150
)
104
151
alice , bob , ingrid := clients [0 ], clients [1 ], clients [2 ]
152
+ vct .alice , vct .bob , vct .ingrid = alice , bob , ingrid
153
+ vct .backend = alice .Backend // Assumes all clients have same backend.
105
154
106
155
_channelsIngrid := make (chan * client.Channel , 1 )
107
156
var openingProposalHandlerIngrid client.ProposalHandlerFunc = func (cp client.ChannelProposal , pr * client.ProposalResponder ) {
0 commit comments