@@ -16,6 +16,7 @@ package channel_test
16
16
17
17
import (
18
18
"context"
19
+ "math/rand"
19
20
"testing"
20
21
"time"
21
22
@@ -33,6 +34,128 @@ import (
33
34
pkgtest "perun.network/go-perun/pkg/test"
34
35
)
35
36
37
+ // TestAdjudicator tests the adjudicator.
38
+ func TestAdjudicator (t * testing.T ) {
39
+ ctx , cancel := context .WithTimeout (context .Background (), defaultTestTimeout )
40
+ defer cancel ()
41
+
42
+ rng := pkgtest .Prng (t )
43
+ numParts := 2 + rng .Intn (maxNumParts - 2 )
44
+ s := test .NewSetup (t , rng , numParts , blockInterval )
45
+ a := newAdjudicatorSetup (s )
46
+ channeltest .TestAdjudicator (ctx , t , a )
47
+ }
48
+
49
+ type adjudicatorSetup struct {
50
+ setup * test.Setup
51
+ }
52
+
53
+ func newAdjudicatorSetup (setup * test.Setup ) * adjudicatorSetup {
54
+ return & adjudicatorSetup {
55
+ setup : setup ,
56
+ }
57
+ }
58
+
59
+ type testAdjudicator struct {
60
+ * test.SimAdjudicator
61
+ }
62
+
63
+ // Progress is a no-op because app channels are not supported yet.
64
+ func (a * testAdjudicator ) Progress (ctx context.Context , req channel.ProgressReq ) error {
65
+ return nil
66
+ }
67
+
68
+ func (a * adjudicatorSetup ) Adjudicator () channel.Adjudicator {
69
+ return & testAdjudicator {a .setup .Adjs [0 ]}
70
+ }
71
+
72
+ func (a * adjudicatorSetup ) NewRegisterReq (ctx context.Context , rng * rand.Rand ) (* channel.AdjudicatorReq , []channel.SignedState ) {
73
+ req := a .newAdjudicatorReq (ctx , rng , channeltest .WithIsFinal (false ))
74
+ return req , nil
75
+ }
76
+
77
+ func (a * adjudicatorSetup ) newAdjudicatorReq (ctx context.Context , rng * rand.Rand , opts ... channeltest.RandomOpt ) * channel.AdjudicatorReq {
78
+ params , state := a .newRandomParamsAndState (rng , opts ... )
79
+
80
+ // Fund the channel.
81
+ numParts := len (params .Parts )
82
+ errs := make (chan error , numParts )
83
+ for i , funder := range a .setup .Funders {
84
+ req := channel .NewFundingReq (params , state , channel .Index (i ), state .Balances )
85
+ go func (funder channel.Funder , req channel.FundingReq ) {
86
+ errs <- funder .Fund (ctx , req )
87
+ }(funder , * req )
88
+ }
89
+ for range a .setup .Funders {
90
+ select {
91
+ case err := <- errs :
92
+ if err != nil {
93
+ panic (err )
94
+ }
95
+ case <- ctx .Done ():
96
+ panic (ctx .Err ())
97
+ }
98
+ }
99
+
100
+ tx , err := signState (a .setup .Accs , state )
101
+ if err != nil {
102
+ panic (err )
103
+ }
104
+ return & channel.AdjudicatorReq {
105
+ Acc : a .setup .Accs [0 ],
106
+ Idx : 0 ,
107
+ Params : params ,
108
+ Tx : tx ,
109
+ }
110
+ }
111
+
112
+ func (a * adjudicatorSetup ) newRandomParamsAndState (rng * rand.Rand , opts ... channeltest.RandomOpt ) (* channel.Params , * channel.State ) {
113
+ _opts := a .defaultOpts ()
114
+ _opts = append (_opts , opts ... )
115
+ return channeltest .NewRandomParamsAndState (rng , _opts ... )
116
+ }
117
+
118
+ func (a * adjudicatorSetup ) defaultOpts () []channeltest.RandomOpt {
119
+ return []channeltest.RandomOpt {
120
+ channeltest .WithChallengeDuration (100 ),
121
+ channeltest .WithParts (a .setup .Parts ... ),
122
+ channeltest .WithAssets ((* ethchannel .Asset )(& a .setup .Asset )),
123
+ channeltest .WithLedgerChannel (true ),
124
+ channeltest .WithVirtualChannel (false ),
125
+ channeltest .WithNumLocked (0 ),
126
+ }
127
+ }
128
+
129
+ func (a * adjudicatorSetup ) NewProgressReq (context.Context , * rand.Rand ) * channel.ProgressReq {
130
+ return & channel.ProgressReq {} // Progression test is not implemented.
131
+ }
132
+
133
+ func (a * adjudicatorSetup ) NewWithdrawReq (ctx context.Context , rng * rand.Rand ) (* channel.AdjudicatorReq , channel.StateMap ) {
134
+ adj := a .Adjudicator ()
135
+ req := a .newAdjudicatorReq (ctx , rng , channeltest .WithoutApp ())
136
+ subChannels := []channel.SignedState {}
137
+
138
+ if ! req .Tx .IsFinal {
139
+ // Register.
140
+ err := adj .Register (ctx , * req , subChannels )
141
+ if err != nil {
142
+ panic (err )
143
+ }
144
+
145
+ // Wait until concludable.
146
+ sub , err := adj .Subscribe (ctx , req .Params .ID ())
147
+ if err != nil {
148
+ panic (err )
149
+ }
150
+ err = sub .Next ().Timeout ().Wait (ctx )
151
+ if err != nil {
152
+ panic (err )
153
+ }
154
+ }
155
+
156
+ return req , channeltest .MakeStateMapFromSignedStates (subChannels ... )
157
+ }
158
+
36
159
const defaultTxTimeout = 2 * time .Second
37
160
38
161
func testSignState (t * testing.T , accounts []* keystore.Account , state * channel.State ) channel.Transaction {
0 commit comments