Skip to content

Commit 2d9efe7

Browse files
committed
✅ [client] Test virtual channel disputes
Signed-off-by: Matthias Geihs <[email protected]>
1 parent ecc7db4 commit 2d9efe7

File tree

1 file changed

+49
-0
lines changed

1 file changed

+49
-0
lines changed

client/virtual_channel_test.go

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ package client_test
1717
import (
1818
"context"
1919
"math/big"
20+
"math/rand"
2021
"testing"
2122
"time"
2223

@@ -26,6 +27,7 @@ import (
2627
"perun.network/go-perun/channel"
2728
chtest "perun.network/go-perun/channel/test"
2829
"perun.network/go-perun/client"
30+
ctest "perun.network/go-perun/client/test"
2931
"perun.network/go-perun/pkg/sync"
3032
"perun.network/go-perun/pkg/test"
3133
"perun.network/go-perun/wire"
@@ -68,7 +70,47 @@ func TestVirtualChannelsOptimistic(t *testing.T) {
6870
assert.NoError(t, err, "Bob: invalid final balances")
6971
}
7072

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+
71110
type virtualChannelTest struct {
111+
alice *Client
112+
bob *Client
113+
ingrid *Client
72114
chAliceIngrid *client.Channel
73115
chIngridAlice *client.Channel
74116
chBobIngrid *client.Channel
@@ -78,7 +120,10 @@ type virtualChannelTest struct {
78120
virtualBalsUpdated []*big.Int
79121
finalBalsAlice []*big.Int
80122
finalBalsBob []*big.Int
123+
finalBalIngrid *big.Int
81124
errs chan error
125+
backend *ctest.MockBackend
126+
asset channel.Asset
82127
}
83128

84129
func setupVirtualChannelTest(t *testing.T, ctx context.Context) (vct virtualChannelTest) {
@@ -87,12 +132,14 @@ func setupVirtualChannelTest(t *testing.T, ctx context.Context) (vct virtualChan
87132

88133
// Set test values.
89134
asset := chtest.NewRandomAsset(rng)
135+
vct.asset = asset
90136
initBalsAlice := []*big.Int{big.NewInt(10), big.NewInt(10)} // with Ingrid
91137
initBalsBob := []*big.Int{big.NewInt(10), big.NewInt(10)} // with Ingrid
92138
initBalsVirtual := []*big.Int{big.NewInt(5), big.NewInt(5)} // Alice proposes
93139
vct.virtualBalsUpdated = []*big.Int{big.NewInt(2), big.NewInt(8)} // Send 3.
94140
vct.finalBalsAlice = []*big.Int{big.NewInt(7), big.NewInt(13)}
95141
vct.finalBalsBob = []*big.Int{big.NewInt(13), big.NewInt(7)}
142+
vct.finalBalIngrid = new(big.Int).Add(vct.finalBalsAlice[1], vct.finalBalsBob[1])
96143
vct.errs = make(chan error, 10)
97144

98145
// Setup clients.
@@ -102,6 +149,8 @@ func setupVirtualChannelTest(t *testing.T, ctx context.Context) (vct virtualChan
102149
t,
103150
)
104151
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.
105154

106155
_channelsIngrid := make(chan *client.Channel, 1)
107156
var openingProposalHandlerIngrid client.ProposalHandlerFunc = func(cp client.ChannelProposal, pr *client.ProposalResponder) {

0 commit comments

Comments
 (0)