Skip to content

Commit

Permalink
Merge pull request #19 from perun-network/test-funder-fail
Browse files Browse the repository at this point in the history
Test funder fail
  • Loading branch information
matthiasgeihs authored Sep 1, 2022
2 parents a8df3ae + 5bdf4fe commit 9ec28fc
Show file tree
Hide file tree
Showing 5 changed files with 86 additions and 3 deletions.
6 changes: 4 additions & 2 deletions channel/pallet/funder.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ package pallet

import (
"context"
"time"

"github.com/centrifuge/go-substrate-rpc-client/v3/types"
"github.com/perun-network/perun-polkadot-backend/channel"
Expand Down Expand Up @@ -58,7 +59,9 @@ func (f *Funder) Fund(ctx context.Context, req pchannel.FundingReq) error {
}

// Wait for all Deposited events.
return f.waitForFundings(ctx, sub, req)
timeoutCtx, cancel := context.WithTimeout(ctx, time.Duration(req.Params.ChallengeDuration)*time.Second)
defer cancel()
return f.waitForFundings(timeoutCtx, sub, req)
}

// waitForFundings blocks until either; all fundings of the request were received or
Expand All @@ -70,7 +73,6 @@ func (f *Funder) waitForFundings(ctx context.Context, sub *EventSub, req pchanne
return err
}
f.Log().Tracef("Waiting for funding from %d peers", len(fids))
defer f.Log().Debug("All peers funded")

for len(fids) != 0 {
select {
Expand Down
1 change: 1 addition & 0 deletions channel/pallet/funder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ func TestFunder_FundMultiple(t *testing.T) {
func TestFunder_Timeout(t *testing.T) {
s := test.NewSetup(t)
params, state := s.NewRandomParamAndState()
params.ChallengeDuration = 10
dSetup := chtest.NewDepositSetup(params, state)

// Bob did not fund and times out.
Expand Down
3 changes: 2 additions & 1 deletion client/appchannel_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (
"context"
"math/big"
"testing"
"time"

"perun.network/go-perun/channel"
"perun.network/go-perun/client"
Expand Down Expand Up @@ -54,7 +55,7 @@ func TestAppChannel(t *testing.T) {
),
}

ctx, cancel := context.WithTimeout(context.Background(), TestTimeoutBlocks*s.BlockTime)
ctx, cancel := context.WithTimeout(context.Background(), 60*time.Second)
defer cancel()
clienttest.ExecuteTwoPartyTest(ctx, t, roles, execConfig)
}
51 changes: 51 additions & 0 deletions client/fund_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
// Copyright 2022 - 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 client_test

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

dotchannel "github.com/perun-network/perun-polkadot-backend/channel"
"github.com/perun-network/perun-polkadot-backend/channel/pallet/test"
"perun.network/go-perun/channel"
ctest "perun.network/go-perun/client/test"
pkgtest "polycry.pt/poly-go/test"
)

func TestFundRecovery(t *testing.T) {
ctx, cancel := context.WithTimeout(context.Background(), 60*time.Second)
defer cancel()

ctest.TestFundRecovery(
ctx,
t,
ctest.FundSetup{
ChallengeDuration: 1,
FridaInitBal: big.NewInt(100000000000000),
FredInitBal: big.NewInt(50000000000000),
BalanceDelta: big.NewInt(1000000000),
},
func(r *rand.Rand) ([2]ctest.RoleSetup, channel.Asset) {
rng := pkgtest.Prng(t)
s := test.NewSetup(t)
roles := makeRoleSetups(rng, s, [2]string{"Frida", "Fred"})
return roles, dotchannel.Asset
},
)
}
28 changes: 28 additions & 0 deletions client/setup_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,10 @@ import (
"time"

"github.com/perun-network/perun-polkadot-backend/channel/pallet/test"
"github.com/perun-network/perun-polkadot-backend/pkg/substrate"
"github.com/perun-network/perun-polkadot-backend/wallet"
sr25519test "github.com/perun-network/perun-polkadot-backend/wallet/sr25519/test"
perunchannel "perun.network/go-perun/channel"
clienttest "perun.network/go-perun/client/test"
"perun.network/go-perun/watcher/local"
"perun.network/go-perun/wire"
Expand All @@ -33,6 +36,7 @@ func makeRoleSetups(rng *rand.Rand, s *test.Setup, names [2]string) (setup [2]cl
if err != nil {
panic(err)
}
acc := wallet.AsAddr(s.Accs[i].Acc.Address())
setup[i] = clienttest.RoleSetup{
Name: names[i],
Identity: netwire.NewRandomAccount(rng),
Expand All @@ -43,7 +47,31 @@ func makeRoleSetups(rng *rand.Rand, s *test.Setup, names [2]string) (setup [2]cl
Timeout: TestTimeoutBlocks * time.Second,
ChallengeDuration: 5, // 5 sec timeout
Watcher: watcher,
BalanceReader: NewBalanceReader(s.API, acc),
}
}
return
}

// BalanceReader is a balance reader used for testing. It is associated with a
// given account.
type BalanceReader struct {
chain *substrate.API
acc wallet.Address
}

func NewBalanceReader(chain *substrate.API, acc wallet.Address) *BalanceReader {
return &BalanceReader{
chain: chain,
acc: acc,
}
}

// Balance returns the asset balance of the associated account.
func (br *BalanceReader) Balance(_ perunchannel.Asset) perunchannel.Bal {
accInfo, err := br.chain.AccountInfo(br.acc.AccountID())
if err != nil {
panic(err)
}
return accInfo.Free.Int
}

0 comments on commit 9ec28fc

Please sign in to comment.