-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmain_test.go
236 lines (205 loc) · 4.99 KB
/
main_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
package main_test
import (
"context"
"fmt"
"math/big"
"sync"
"testing"
"github.com/perun-network/perun-credential-payment/app"
"github.com/perun-network/perun-credential-payment/client"
"github.com/perun-network/perun-credential-payment/client/connection"
"github.com/perun-network/perun-credential-payment/test"
"github.com/stretchr/testify/require"
)
func TestCredentialSwap(t *testing.T) {
t.Run("Honest holder", func(t *testing.T) {
runCredentialSwapTest(t, true)
})
t.Run("Dishonest holder", func(t *testing.T) {
runCredentialSwapTest(t, false)
})
}
func runCredentialSwapTest(t *testing.T, honestHolder bool) {
require := require.New(t)
ctx, cancel := context.WithCancel(context.Background())
t.Cleanup(cancel)
// Setup test environment.
env := test.Setup(t)
env.LogAccountBalances()
wg, errs := sync.WaitGroup{}, make(chan error)
wg.Add(2)
holder, issuer := env.Holder, env.Issuer
doc := []byte("Perun/Bosch: SSI Credential Payment")
balance := test.EthToWei(big.NewFloat(5))
price := test.EthToWei(big.NewFloat(1))
// Run credential holder.
go func() {
err := runCredentialHolder(
ctx,
holder,
issuer,
balance,
doc,
price,
honestHolder,
)
if err != nil {
errs <- fmt.Errorf("running credential holder: %w", err)
return
}
wg.Done()
}()
// Run credential issuer.
go func() {
err := runCredentialIssuer(
ctx,
issuer,
holder,
doc,
price,
)
if err != nil {
errs <- fmt.Errorf("running credential issuer: %w", err)
return
}
wg.Done()
}()
// Await result.
done := make(chan struct{})
go func() {
wg.Wait()
done <- struct{}{}
}()
err := func() error {
select {
case <-done:
return nil
case err := <-errs:
return err
case <-ctx.Done():
return ctx.Err()
}
}()
require.NoError(err)
env.LogAccountBalances()
}
func runCredentialHolder(
ctx context.Context,
holder *client.Client,
issuer *client.Client,
balance *big.Int,
doc []byte,
price *big.Int,
honest bool,
) error {
// Connect.
conn, err := holder.Connect(ctx, issuer.PerunAddress(), balance)
if err != nil {
return fmt.Errorf("proposing connection: %w", err)
}
// Buy credential.
{
// Request credential.
asyncCred, err := conn.RequestCredential(ctx, doc, price, issuer.Address())
if err != nil {
return fmt.Errorf("requesting credential: %w", err)
}
// Wait for the transaction issueing the credential.
resp, err := asyncCred.Await(ctx)
if err != nil {
return fmt.Errorf("awaiting credential: %w", err)
}
cred := app.Credential{
Document: doc,
Signature: resp.Signature,
}
holder.Logf("Obtained credential: %v", cred.String())
// The issuer is waiting for us to complete the transaction.
// If we are honest, we accept. If we are dishonest, we reject.
if honest {
err := resp.Accept(ctx)
if err != nil {
return fmt.Errorf("accepting transaction: %w", err)
}
} else {
err := resp.Reject(ctx, "Won't pay!")
if err != nil {
return fmt.Errorf("rejecting transaction: %w", err)
}
// We wait for the dispute to be resolved.
err = conn.WaitConcludadable(ctx)
if err != nil {
return fmt.Errorf("waiting for dispute resolution: %w", err)
}
}
}
// Close connection.
err = conn.Close(ctx)
if err != nil {
return fmt.Errorf("closing connection: %w", err)
}
return nil
}
func runCredentialIssuer(
ctx context.Context,
issuer *client.Client,
holder *client.Client,
doc []byte,
price *big.Int,
) error {
// Connect.
conn, err := func() (*connection.Connection, error) {
// Read next connection request.
req, err := issuer.NextConnectionRequest(ctx)
if err != nil {
return nil, fmt.Errorf("awaiting next connection request: %w", err)
}
// Check peer.
if !req.Peer().Equals(holder.PerunAddress()) {
return nil, fmt.Errorf("wrong peer: expected %v, got %v", holder, req.Peer())
}
// Accept.
conn, err := req.Accept(ctx)
if err != nil {
return nil, fmt.Errorf("accepting connection request: %w", err)
}
return conn, nil
}()
if err != nil {
return fmt.Errorf("connecting: %w", err)
}
// Issue credential.
err = func() error {
// Read next credential request.
req, err := conn.NextCredentialRequest(ctx)
if err != nil {
return fmt.Errorf("awaiting next credential request: %w", err)
}
// Check document and price.
if err := req.CheckDoc(doc); err != nil {
return fmt.Errorf("checking document: %w", err)
} else if err := req.CheckPrice(price); err != nil {
return fmt.Errorf("checking price: %w", err)
}
// Issue credential.
err = req.IssueCredential(ctx, issuer.Account())
if err != nil {
return fmt.Errorf("issueing credential: %w", err)
}
return nil
}()
if err != nil {
return fmt.Errorf("issueing credential: %w", err)
}
// Wait until channel is concludable.
err = conn.WaitConcludadable(ctx)
if err != nil {
return fmt.Errorf("waiting for channel finalization: %w", err)
}
// Close connection.
err = conn.Close(ctx)
if err != nil {
return fmt.Errorf("closing connection: %w", err)
}
return nil
}