Skip to content

Commit

Permalink
Check outcome in virtual channel dispute test
Browse files Browse the repository at this point in the history
  • Loading branch information
matthiasgeihs committed Jul 8, 2021
1 parent a0ee7ba commit 72687d8
Showing 1 changed file with 28 additions and 0 deletions.
28 changes: 28 additions & 0 deletions client/virtual_channel_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import (
"perun.network/go-perun/channel"
chtest "perun.network/go-perun/channel/test"
"perun.network/go-perun/client"
ctest "perun.network/go-perun/client/test"
"perun.network/go-perun/pkg/sync"
"perun.network/go-perun/pkg/test"
"perun.network/go-perun/wire"
Expand Down Expand Up @@ -91,6 +92,9 @@ func TestVirtualChannelsDispute(t *testing.T) {
// Ingrid registers and settles channel with Bob.
err = vct.registerAndSettle(ctx, vct.chIngridBob)
assert.NoError(err, "ingrid-bob: register and settle")

// Test final balances.
vct.testFinalBalancesDispute(t)
}

func TestVirtualChannelsDisputeHubFirst(t *testing.T) {
Expand Down Expand Up @@ -134,9 +138,26 @@ func TestVirtualChannelsDisputeHubFirst(t *testing.T) {
// Bob settles channel with Ingrid.
err = vct.chBobIngrid.Settle(ctx, false)
assert.NoError(err, "bob-ingrid: settle")

// Test final balances.
vct.testFinalBalancesDispute(t)
}

func (vct *virtualChannelTest) testFinalBalancesDispute(t *testing.T) {
assert := assert.New(t)
backend, asset := vct.backend, vct.asset
got, expected := backend.GetBalance(vct.alice.Identity.Address(), asset), vct.finalBalsAlice[0]
assert.Truef(got.Cmp(expected) == 0, "alice: wrong final balance: got %v, expected %v", got, expected)
got, expected = backend.GetBalance(vct.bob.Identity.Address(), asset), vct.finalBalsBob[0]
assert.Truef(got.Cmp(expected) == 0, "bob: wrong final balance: got %v, expected %v", got, expected)
got, expected = backend.GetBalance(vct.ingrid.Identity.Address(), asset), vct.finalBalIngrid
assert.Truef(got.Cmp(expected) == 0, "ingrid: wrong final balance: got %v, expected %v", got, expected)
}

type virtualChannelTest struct {
alice *Client
bob *Client
ingrid *Client
chAliceIngrid *client.Channel
chIngridAlice *client.Channel
chBobIngrid *client.Channel
Expand All @@ -146,7 +167,10 @@ type virtualChannelTest struct {
virtualBalsUpdated []*big.Int
finalBalsAlice []*big.Int
finalBalsBob []*big.Int
finalBalIngrid *big.Int
errs chan error
backend *ctest.MockBackend
asset channel.Asset
}

func setupVirtualChannelTest(t *testing.T, ctx context.Context) (vct virtualChannelTest) {
Expand All @@ -155,12 +179,14 @@ func setupVirtualChannelTest(t *testing.T, ctx context.Context) (vct virtualChan

// Set test values.
asset := chtest.NewRandomAsset(rng)
vct.asset = asset
initBalsAlice := []*big.Int{big.NewInt(10), big.NewInt(10)} // with Ingrid
initBalsBob := []*big.Int{big.NewInt(10), big.NewInt(10)} // with Ingrid
initBalsVirtual := []*big.Int{big.NewInt(5), big.NewInt(5)} // Alice proposes
vct.virtualBalsUpdated = []*big.Int{big.NewInt(2), big.NewInt(8)} // Send 3.
vct.finalBalsAlice = []*big.Int{big.NewInt(7), big.NewInt(13)}
vct.finalBalsBob = []*big.Int{big.NewInt(13), big.NewInt(7)}
vct.finalBalIngrid = new(big.Int).Add(vct.finalBalsAlice[1], vct.finalBalsBob[1])
vct.errs = make(chan error, 10)

// Setup clients.
Expand All @@ -170,6 +196,8 @@ func setupVirtualChannelTest(t *testing.T, ctx context.Context) (vct virtualChan
t,
)
alice, bob, ingrid := clients[0], clients[1], clients[2]
vct.alice, vct.bob, vct.ingrid = alice, bob, ingrid
vct.backend = alice.Backend // Assumes all clients have same backend.

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

0 comments on commit 72687d8

Please sign in to comment.