From a310d57e7ff1e0280072edd740f886dcdc2a25a6 Mon Sep 17 00:00:00 2001 From: Matthias Geihs Date: Thu, 1 Sep 2022 09:09:48 +0200 Subject: [PATCH 1/6] Add balance reader --- client/setup_test.go | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/client/setup_test.go b/client/setup_test.go index 02c7de8..145e0af 100644 --- a/client/setup_test.go +++ b/client/setup_test.go @@ -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" @@ -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), @@ -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 +} From e65f6e79eed512916746b1c88325b79ec58d7a5a Mon Sep 17 00:00:00 2001 From: Matthias Geihs Date: Thu, 1 Sep 2022 09:09:27 +0200 Subject: [PATCH 2/6] Fund: add challenge duration timeout --- channel/pallet/funder.go | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/channel/pallet/funder.go b/channel/pallet/funder.go index db94fe1..51568c5 100644 --- a/channel/pallet/funder.go +++ b/channel/pallet/funder.go @@ -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" @@ -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 From 2d4ff5d7ac81df78e1bc3b93df7939340312a904 Mon Sep 17 00:00:00 2001 From: Matthias Geihs Date: Wed, 31 Aug 2022 16:09:00 +0200 Subject: [PATCH 3/6] Test fund recovery --- client/fund_test.go | 51 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 51 insertions(+) create mode 100644 client/fund_test.go diff --git a/client/fund_test.go b/client/fund_test.go new file mode 100644 index 0000000..7a3d6e0 --- /dev/null +++ b/client/fund_test.go @@ -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 + }, + ) +} From 7f7823442e6923e3f99e604c06795258eeea1fd5 Mon Sep 17 00:00:00 2001 From: Matthias Geihs Date: Thu, 1 Sep 2022 14:07:06 +0200 Subject: [PATCH 4/6] Improve funder test stability Give more time to complete funding. --- channel/pallet/funder_test.go | 1 + 1 file changed, 1 insertion(+) diff --git a/channel/pallet/funder_test.go b/channel/pallet/funder_test.go index be7a0fe..c861a5d 100644 --- a/channel/pallet/funder_test.go +++ b/channel/pallet/funder_test.go @@ -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. From 83046254975a8deab9f3efb969140ca2f553b26e Mon Sep 17 00:00:00 2001 From: Matthias Geihs Date: Thu, 1 Sep 2022 21:19:18 +0200 Subject: [PATCH 5/6] Remove misleading debug message --- channel/pallet/funder.go | 1 - 1 file changed, 1 deletion(-) diff --git a/channel/pallet/funder.go b/channel/pallet/funder.go index 51568c5..f758e17 100644 --- a/channel/pallet/funder.go +++ b/channel/pallet/funder.go @@ -73,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 { From 5bdf4fe60383e50c4754376eea03c2f17c11e369 Mon Sep 17 00:00:00 2001 From: Matthias Geihs Date: Thu, 1 Sep 2022 14:49:39 +0200 Subject: [PATCH 6/6] TestAppChannel: Increase timeout --- client/appchannel_test.go | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/client/appchannel_test.go b/client/appchannel_test.go index 64f457b..4f85969 100644 --- a/client/appchannel_test.go +++ b/client/appchannel_test.go @@ -18,6 +18,7 @@ import ( "context" "math/big" "testing" + "time" "perun.network/go-perun/channel" "perun.network/go-perun/client" @@ -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) }