Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Extend generic channel tests #223

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
123 changes: 123 additions & 0 deletions backend/ethereum/channel/adjudicator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ package channel_test

import (
"context"
"math/rand"
"testing"
"time"

Expand All @@ -33,6 +34,128 @@ import (
pkgtest "perun.network/go-perun/pkg/test"
)

// TestAdjudicator tests the adjudicator.
func TestAdjudicator(t *testing.T) {
ctx, cancel := context.WithTimeout(context.Background(), defaultTestTimeout)
defer cancel()

rng := pkgtest.Prng(t)
numParts := 2 + rng.Intn(maxNumParts-2)
s := test.NewSetup(t, rng, numParts, blockInterval)
a := newAdjudicatorSetup(s)
channeltest.TestAdjudicator(ctx, t, a)
}

type adjudicatorSetup struct {
setup *test.Setup
}

func newAdjudicatorSetup(setup *test.Setup) *adjudicatorSetup {
return &adjudicatorSetup{
setup: setup,
}
}

type testAdjudicator struct {
*test.SimAdjudicator
}

// Progress is a no-op because app channels are not supported yet.
ggwpez marked this conversation as resolved.
Show resolved Hide resolved
func (a *testAdjudicator) Progress(ctx context.Context, req channel.ProgressReq) error {
return nil
}

func (a *adjudicatorSetup) Adjudicator() channel.Adjudicator {
return &testAdjudicator{a.setup.Adjs[0]}
}

func (a *adjudicatorSetup) NewRegisterReq(ctx context.Context, rng *rand.Rand) (*channel.AdjudicatorReq, []channel.SignedState) {
req := a.newAdjudicatorReq(ctx, rng, channeltest.WithIsFinal(false))
return req, nil
}

func (a *adjudicatorSetup) newAdjudicatorReq(ctx context.Context, rng *rand.Rand, opts ...channeltest.RandomOpt) *channel.AdjudicatorReq {
params, state := a.newRandomParamsAndState(rng, opts...)

// Fund the channel.
ggwpez marked this conversation as resolved.
Show resolved Hide resolved
numParts := len(params.Parts)
errs := make(chan error, numParts)
for i, funder := range a.setup.Funders {
req := channel.NewFundingReq(params, state, channel.Index(i), state.Balances)
go func(funder channel.Funder, req channel.FundingReq) {
errs <- funder.Fund(ctx, req)
ggwpez marked this conversation as resolved.
Show resolved Hide resolved
}(funder, *req)
}
for range a.setup.Funders {
select {
case err := <-errs:
if err != nil {
panic(err)
}
case <-ctx.Done():
panic(ctx.Err())
}
}

tx, err := signState(a.setup.Accs, state)
if err != nil {
panic(err)
}
return &channel.AdjudicatorReq{
Acc: a.setup.Accs[0],
Idx: 0,
Params: params,
Tx: tx,
}
}

func (a *adjudicatorSetup) newRandomParamsAndState(rng *rand.Rand, opts ...channeltest.RandomOpt) (*channel.Params, *channel.State) {
_opts := a.defaultOpts()
_opts = append(_opts, opts...)
return channeltest.NewRandomParamsAndState(rng, _opts...)
}

func (a *adjudicatorSetup) defaultOpts() []channeltest.RandomOpt {
ggwpez marked this conversation as resolved.
Show resolved Hide resolved
return []channeltest.RandomOpt{
channeltest.WithChallengeDuration(100),
channeltest.WithParts(a.setup.Parts...),
channeltest.WithAssets((*ethchannel.Asset)(&a.setup.Asset)),
channeltest.WithLedgerChannel(true),
channeltest.WithVirtualChannel(false),
channeltest.WithNumLocked(0),
}
}

func (a *adjudicatorSetup) NewProgressReq(context.Context, *rand.Rand) *channel.ProgressReq {
return &channel.ProgressReq{} // Progression test is not implemented.
}

func (a *adjudicatorSetup) NewWithdrawReq(ctx context.Context, rng *rand.Rand) (*channel.AdjudicatorReq, channel.StateMap) {
adj := a.Adjudicator()
req := a.newAdjudicatorReq(ctx, rng, channeltest.WithoutApp())
subChannels := []channel.SignedState{}

if !req.Tx.IsFinal {
// Register.
err := adj.Register(ctx, *req, subChannels)
ggwpez marked this conversation as resolved.
Show resolved Hide resolved
if err != nil {
panic(err)
}

// Wait until concludable.
sub, err := adj.Subscribe(ctx, req.Params.ID())
if err != nil {
panic(err)
}
err = sub.Next().Timeout().Wait(ctx)
if err != nil {
panic(err)
}
}

return req, channeltest.MakeStateMapFromSignedStates(subChannels...)
}

const defaultTxTimeout = 2 * time.Second

func testSignState(t *testing.T, accounts []*keystore.Account, state *channel.State) channel.Transaction {
Expand Down
1 change: 1 addition & 0 deletions backend/ethereum/channel/backend.go
Original file line number Diff line number Diff line change
Expand Up @@ -263,6 +263,7 @@ func FromEthState(app channel.App, s *adjudicator.ChannelState) channel.State {
ID: s.ChannelID,
Version: s.Version,
Allocation: alloc,
App: app,
matthiasgeihs marked this conversation as resolved.
Show resolved Hide resolved
Data: data,
IsFinal: s.IsFinal,
}
Expand Down
62 changes: 62 additions & 0 deletions backend/ethereum/channel/funder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -338,6 +338,68 @@ func TestFunder_Fund_multi(t *testing.T) {
t.Run("10-party funding", func(t *testing.T) { testFunderFunding(t, 10) })
}

func TestFunder(t *testing.T) {
ctx, cancel := context.WithTimeout(context.Background(), defaultTestTimeout)
defer cancel()

rng := pkgtest.Prng(t)

numParts := 2 + rng.Intn(maxNumParts-2)
s := test.NewSetup(t, rng, numParts, blockInterval)

funders := make([]channel.Funder, numParts)
for i := range funders {
funders[i] = s.Funders[i]
}

_f := &funderSetup{
setup: s,
funders: funders,
}
channeltest.TestFunder(ctx, t, rng, _f)
}

// funderSetup represents a funderSetup test setup.
type funderSetup struct {
setup *test.Setup
funders []channel.Funder
}

const maxNumParts = 8

func (f *funderSetup) Funders() []channel.Funder {
return f.funders
}

func (f *funderSetup) NewFundingRequests(ctx context.Context, t *testing.T, rng *rand.Rand) []channel.FundingReq {
params, state := f.newRandomParamsAndState(rng)

funders := f.funders
numParts := len(funders)
requests := make([]channel.FundingReq, numParts)
ggwpez marked this conversation as resolved.
Show resolved Hide resolved
for i := range funders {
requests[i] = *channel.NewFundingReq(params, state, channel.Index(i), state.Balances)
}
return requests
}

func (f *funderSetup) newRandomParamsAndState(rng *rand.Rand, opts ...channeltest.RandomOpt) (*channel.Params, *channel.State) {
_opts := f.defaultOpts()
_opts = append(_opts, opts...)
return channeltest.NewRandomParamsAndState(rng, _opts...)
}

func (f *funderSetup) defaultOpts() []channeltest.RandomOpt {
return []channeltest.RandomOpt{
ggwpez marked this conversation as resolved.
Show resolved Hide resolved
channeltest.WithChallengeDuration(100),
channeltest.WithParts(f.setup.Parts...),
channeltest.WithAssets((*ethchannel.Asset)(&f.setup.Asset)),
channeltest.WithLedgerChannel(true),
channeltest.WithVirtualChannel(false),
channeltest.WithNumLocked(0),
}
}

func testFunderFunding(t *testing.T, n int) {
t.Parallel()
ctx, cancel := context.WithTimeout(context.Background(), defaultTxTimeout*time.Duration(n))
Expand Down
113 changes: 113 additions & 0 deletions backend/ethereum/channel/subscription_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
// Copyright 2021 - See NOTICE file for copyright holders.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package channel_test

import (
"context"
"testing"

"github.com/stretchr/testify/require"
"perun.network/go-perun/backend/ethereum/channel/test"
"perun.network/go-perun/channel"
channeltest "perun.network/go-perun/channel/test"
pkgtest "perun.network/go-perun/pkg/test"
)

func TestSubscription(t *testing.T) {
ctx, cancel := context.WithTimeout(context.Background(), defaultTestTimeout)
defer cancel()

rng := pkgtest.Prng(t)
numParts := 2 + rng.Intn(maxNumParts-2)
setup := test.NewSetup(t, rng, numParts, blockInterval)
adjSetup := newAdjudicatorSetup(setup)
adj := adjSetup.Adjudicator()

opts := []channeltest.RandomOpt{channeltest.WithoutApp(), channeltest.WithIsFinal(false)}
req, subChannels := adjSetup.newAdjudicatorReq(ctx, rng, opts...), []channel.SignedState{}
sub, err := adj.Subscribe(ctx, req.Params.ID())
require.NoError(t, err, "subscribing")

subSetup := newSubscriptionSetup(sub, adj, req, subChannels)
channeltest.TestSubscription(ctx, t, subSetup)
}

type subscriptionSetup struct {
adj channel.Adjudicator
sub *testSubscription
req *channel.AdjudicatorReq
subChannels []channel.SignedState
}

func newSubscriptionSetup(
sub channel.AdjudicatorSubscription,
adj channel.Adjudicator,
req *channel.AdjudicatorReq,
subChannels []channel.SignedState,
) *subscriptionSetup {
return &subscriptionSetup{
sub: &testSubscription{
emitProgressed: false,
req: req,
AdjudicatorSubscription: sub,
},
adj: adj,
req: req,
subChannels: subChannels,
}
}

type testSubscription struct {
channel.AdjudicatorSubscription
emitProgressed bool
req *channel.AdjudicatorReq
}

// Next emulates app channel functionality because app channels are not supported yet.
func (s *testSubscription) Next() channel.AdjudicatorEvent {
if s.emitProgressed {
ggwpez marked this conversation as resolved.
Show resolved Hide resolved
s.emitProgressed = false
return channel.NewProgressedEvent(s.req.Tx.ID, &channel.ElapsedTimeout{}, s.req.Tx.State, s.req.Idx)
}
return s.AdjudicatorSubscription.Next()
}

func (s *subscriptionSetup) Subscription() channel.AdjudicatorSubscription {
return s.sub
}

// EmitRegistered operates the adjudicator so that a registered event should be emitted.
func (s *subscriptionSetup) EmitRegistered(ctx context.Context) (channel.Params, channel.State) {
err := s.adj.Register(ctx, *s.req, s.subChannels)
if err != nil {
panic(err)
}
return *s.req.Params, *s.req.Tx.State
}

// EmitProgressed emulates app channel functionality because app channels are not supported yet.
func (s *subscriptionSetup) EmitProgressed(ctx context.Context) (channel.Params, channel.State) {
s.sub.emitProgressed = true
return *s.req.Params, *s.req.Tx.State
}

// EmitRegistered operates the adjudicator so that a concluded event should be emitted.
func (s *subscriptionSetup) EmitConcluded(ctx context.Context) channel.Params {
err := s.adj.Withdraw(ctx, *s.req, channeltest.MakeStateMapFromSignedStates(s.subChannels...))
if err != nil {
panic(err)
}
return *s.req.Params
}
7 changes: 0 additions & 7 deletions backend/sim/channel/init_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,5 @@
package channel

import (
"testing"

_ "perun.network/go-perun/backend/sim/wallet" // backend init
"perun.network/go-perun/channel"
)

func TestSetBackend(t *testing.T) {
channel.SetBackendTest(t)
}
29 changes: 0 additions & 29 deletions channel/backendtest.go

This file was deleted.

Loading