From 8f6050cb4409c34fc4e1e2e07869cd59b0b6e676 Mon Sep 17 00:00:00 2001 From: Sophia Koehler Date: Wed, 21 May 2025 11:29:16 +0200 Subject: [PATCH 1/2] fix(channel/backend.go): Dynamic use of backends instead of hardcoded index feat(client/): Allow multiple Assets in Test Signed-off-by: Sophia Koehler --- channel/backend.go | 4 ++-- client/appchannel_test.go | 6 ++--- client/client_role_test.go | 6 ++--- client/payment_test.go | 6 ++--- client/subchannel_test.go | 12 +++++----- client/test/channel.go | 4 ++-- client/test/role.go | 38 ++++++++++++++++++------------- client/test/subchannel.go | 2 +- client/test/subchannel_dispute.go | 2 +- 9 files changed, 43 insertions(+), 37 deletions(-) diff --git a/channel/backend.go b/channel/backend.go index 7ca3633b..189e2999 100644 --- a/channel/backend.go +++ b/channel/backend.go @@ -66,8 +66,8 @@ func SetBackend(b Backend, id int) { // CalcID calculates the CalcID. func CalcID(p *Params) (ID, error) { var lastErr error - for _, b := range backend { - id, err := b.CalcID(p) + for i := range p.Parts[0] { + id, err := backend[i].CalcID(p) if err == nil { return id, nil } diff --git a/client/appchannel_test.go b/client/appchannel_test.go index e21127b6..d5e97ed8 100644 --- a/client/appchannel_test.go +++ b/client/appchannel_test.go @@ -45,9 +45,9 @@ func TestProgression(t *testing.T) { execConfig := &clienttest.ProgressionExecConfig{ BaseExecConfig: clienttest.MakeBaseExecConfig( [2]map[wallet.BackendID]wire.Address{wire.AddressMapfromAccountMap(setups[0].Identity), wire.AddressMapfromAccountMap(setups[1].Identity)}, - chtest.NewRandomAsset(rng, channel.TestBackendID), - channel.TestBackendID, - [2]*big.Int{big.NewInt(99), big.NewInt(1)}, + []channel.Asset{chtest.NewRandomAsset(rng, channel.TestBackendID)}, + []wallet.BackendID{channel.TestBackendID}, + [][2]*big.Int{{big.NewInt(99), big.NewInt(1)}}, client.WithApp(app, channel.NewMockOp(channel.OpValid)), ), } diff --git a/client/client_role_test.go b/client/client_role_test.go index 0de26193..980b4144 100644 --- a/client/client_role_test.go +++ b/client/client_role_test.go @@ -89,9 +89,9 @@ func runAliceBobTest(ctx context.Context, t *testing.T, setup func(*rand.Rand) ( cfg := &ctest.AliceBobExecConfig{ BaseExecConfig: ctest.MakeBaseExecConfig( [2]map[wallet.BackendID]wire.Address{wire.AddressMapfromAccountMap(setups[0].Identity), wire.AddressMapfromAccountMap(setups[1].Identity)}, - chtest.NewRandomAsset(rng, channel.TestBackendID), - channel.TestBackendID, - [2]*big.Int{big.NewInt(100), big.NewInt(100)}, + []channel.Asset{chtest.NewRandomAsset(rng, channel.TestBackendID)}, + []wallet.BackendID{channel.TestBackendID}, + [][2]*big.Int{{big.NewInt(100), big.NewInt(100)}}, app, ), NumPayments: [2]int{2, 2}, diff --git a/client/payment_test.go b/client/payment_test.go index 5865eec4..c5d0d6a4 100644 --- a/client/payment_test.go +++ b/client/payment_test.go @@ -61,9 +61,9 @@ func TestPaymentDispute(t *testing.T) { cfg := &ctest.MalloryCarolExecConfig{ BaseExecConfig: ctest.MakeBaseExecConfig( [2]map[wallet.BackendID]wire.Address{wire.AddressMapfromAccountMap(setups[mallory].Identity), wire.AddressMapfromAccountMap(setups[carol].Identity)}, - chtest.NewRandomAsset(rng, channel.TestBackendID), - channel.TestBackendID, - [2]*big.Int{big.NewInt(100), big.NewInt(1)}, + []channel.Asset{chtest.NewRandomAsset(rng, channel.TestBackendID)}, + []wallet.BackendID{channel.TestBackendID}, + [][2]*big.Int{{big.NewInt(100), big.NewInt(1)}}, client.WithoutApp(), ), NumPayments: [2]int{5, 0}, diff --git a/client/subchannel_test.go b/client/subchannel_test.go index 50b4c3a1..a4bbc053 100644 --- a/client/subchannel_test.go +++ b/client/subchannel_test.go @@ -43,9 +43,9 @@ func TestSubChannelHappy(t *testing.T) { cfg := ctest.NewSusieTimExecConfig( ctest.MakeBaseExecConfig( [2]map[wallet.BackendID]wire.Address{wire.AddressMapfromAccountMap(setups[0].Identity), wire.AddressMapfromAccountMap(setups[1].Identity)}, - chtest.NewRandomAsset(rng, channel.TestBackendID), - channel.TestBackendID, - [2]*big.Int{big.NewInt(100), big.NewInt(100)}, + []channel.Asset{chtest.NewRandomAsset(rng, channel.TestBackendID)}, + []wallet.BackendID{channel.TestBackendID}, + [][2]*big.Int{{big.NewInt(100), big.NewInt(100)}}, client.WithoutApp(), ), 2, @@ -81,9 +81,9 @@ func TestSubChannelDispute(t *testing.T) { baseCfg := ctest.MakeBaseExecConfig( [2]map[wallet.BackendID]wire.Address{wire.AddressMapfromAccountMap(setups[0].Identity), wire.AddressMapfromAccountMap(setups[1].Identity)}, - chtest.NewRandomAsset(rng, channel.TestBackendID), - channel.TestBackendID, - [2]*big.Int{big.NewInt(100), big.NewInt(100)}, + []channel.Asset{chtest.NewRandomAsset(rng, channel.TestBackendID)}, + []wallet.BackendID{channel.TestBackendID}, + [][2]*big.Int{{big.NewInt(100), big.NewInt(100)}}, client.WithoutApp(), ) cfg := &ctest.DisputeSusieTimExecConfig{ diff --git a/client/test/channel.go b/client/test/channel.go index 28b7c6c9..ee1b0209 100644 --- a/client/test/channel.go +++ b/client/test/channel.go @@ -85,8 +85,8 @@ func (ch *paymentChannel) openSubChannel( bID wallet.BackendID, ) *paymentChannel { initAlloc := channel.Allocation{ - Assets: []channel.Asset{cfg.Asset()}, - Backends: []wallet.BackendID{bID}, + Assets: cfg.Asset(), + Backends: cfg.Backend(), Balances: [][]channel.Bal{{initBals[0], initBals[1]}}, } diff --git a/client/test/role.go b/client/test/role.go index 9f74a92c..c4af9cef 100644 --- a/client/test/role.go +++ b/client/test/role.go @@ -86,18 +86,18 @@ type ( // ExecConfig contains additional config parameters for the tests. ExecConfig interface { Peers() [2]map[wallet.BackendID]wire.Address // must match the RoleSetup.Identity's - Asset() channel.Asset // single Asset to use in this channel - Backend() wallet.BackendID // backend corresponding to asset - InitBals() [2]*big.Int // channel deposit of each role + Asset() []channel.Asset // single Asset to use in this channel + Backend() []wallet.BackendID // backend corresponding to asset + InitBals() [][2]*big.Int // channel deposit of each role App() client.ProposalOpts // must be either WithApp or WithoutApp } // BaseExecConfig contains base config parameters. BaseExecConfig struct { peers [2]map[wallet.BackendID]wire.Address // must match the RoleSetup.Identity's - asset channel.Asset // single Asset to use in this channel - backend wallet.BackendID // backend corresponding to asset - initBals [2]*big.Int // channel deposit of each role + asset []channel.Asset // single Asset to use in this channel + backend []wallet.BackendID // backend corresponding to asset + initBals [][2]*big.Int // channel deposit of each role app client.ProposalOpts // must be either WithApp or WithoutApp } @@ -185,9 +185,9 @@ func ExecuteTwoPartyTest(ctx context.Context, t *testing.T, role [2]Executer, cf // MakeBaseExecConfig creates a new BaseExecConfig. func MakeBaseExecConfig( peers [2]map[wallet.BackendID]wire.Address, - asset channel.Asset, - backend wallet.BackendID, - initBals [2]*big.Int, + asset []channel.Asset, + backend []wallet.BackendID, + initBals [][2]*big.Int, app client.ProposalOpts, ) BaseExecConfig { return BaseExecConfig{ @@ -205,17 +205,17 @@ func (c *BaseExecConfig) Peers() [2]map[wallet.BackendID]wire.Address { } // Asset returns the asset. -func (c *BaseExecConfig) Asset() channel.Asset { +func (c *BaseExecConfig) Asset() []channel.Asset { return c.asset } // Backend returns the asset. -func (c *BaseExecConfig) Backend() wallet.BackendID { +func (c *BaseExecConfig) Backend() []wallet.BackendID { return c.backend } // InitBals returns the initial balances. -func (c *BaseExecConfig) InitBals() [2]*big.Int { +func (c *BaseExecConfig) InitBals() [][2]*big.Int { return c.initBals } @@ -355,13 +355,19 @@ func (r *role) LedgerChannelProposal(rng *rand.Rand, cfg ExecConfig) *client.Led r.log.Panic("Invalid ExecConfig: App does not specify an app.") } - peers, asset, bals := cfg.Peers(), cfg.Asset(), cfg.InitBals() - alloc := channel.NewAllocation(len(peers), []wallet.BackendID{cfg.Backend()}, asset) - alloc.SetAssetBalances(asset, bals[:]) + peers, assets, bals, backend := cfg.Peers(), cfg.Asset(), cfg.InitBals(), cfg.Backend() + alloc := channel.NewAllocation(len(peers), cfg.Backend(), assets...) + for i := range assets { + alloc.SetAssetBalances(assets[i], bals[i][:]) + } + peersList := make(map[wallet.BackendID]wallet.Address) + for i := range backend { + peersList[backend[i]] = r.setup.Wallet[backend[i]].NewRandomAccount(rng).Address() + } prop, err := client.NewLedgerChannelProposal( r.challengeDuration, - map[wallet.BackendID]wallet.Address{cfg.Backend(): r.setup.Wallet[cfg.Backend()].NewRandomAccount(rng).Address()}, + peersList, alloc, peers[:], client.WithNonceFrom(rng), diff --git a/client/test/subchannel.go b/client/test/subchannel.go index 782489a4..cedf209c 100644 --- a/client/test/subchannel.go +++ b/client/test/subchannel.go @@ -77,7 +77,7 @@ func (r *Susie) exec(_cfg ExecConfig, ledgerChannel *paymentChannel) { // stage 2 - open subchannels openSubChannel := func(parentChannel *paymentChannel, funds []*big.Int, app client.ProposalOpts) *paymentChannel { - return parentChannel.openSubChannel(rng, cfg, funds, app, cfg.backend) + return parentChannel.openSubChannel(rng, cfg, funds, app, cfg.backend[0]) } var subChannels []*paymentChannel diff --git a/client/test/subchannel_dispute.go b/client/test/subchannel_dispute.go index 45692d66..e897b94b 100644 --- a/client/test/subchannel_dispute.go +++ b/client/test/subchannel_dispute.go @@ -58,7 +58,7 @@ func (r *DisputeSusie) exec(_cfg ExecConfig, ledgerChannel *paymentChannel) { r.waitStage() // Stage 2 - Open sub-channel. - subChannel := ledgerChannel.openSubChannel(rng, cfg, cfg.SubChannelFunds[:], client.WithoutApp(), _cfg.Backend()) + subChannel := ledgerChannel.openSubChannel(rng, cfg, cfg.SubChannelFunds[:], client.WithoutApp(), _cfg.Backend()[0]) subReq0 := client.NewTestChannel(subChannel.Channel).AdjudicatorReq() // Store AdjudicatorReq for version 0 r.waitStage() From 93a45591b742ee5cd60fd7fdcb18d6076ade2935 Mon Sep 17 00:00:00 2001 From: Sophia Koehler Date: Wed, 28 May 2025 11:29:38 +0200 Subject: [PATCH 2/2] fix(client/test): Remove unused argument in openSubChannel Signed-off-by: Sophia Koehler --- client/test/channel.go | 3 --- client/test/subchannel.go | 2 +- client/test/subchannel_dispute.go | 2 +- 3 files changed, 2 insertions(+), 5 deletions(-) diff --git a/client/test/channel.go b/client/test/channel.go index ee1b0209..264d4bba 100644 --- a/client/test/channel.go +++ b/client/test/channel.go @@ -21,8 +21,6 @@ import ( "math/big" "time" - "perun.network/go-perun/wallet" - "perun.network/go-perun/channel" "perun.network/go-perun/client" "perun.network/go-perun/log" @@ -82,7 +80,6 @@ func (ch *paymentChannel) openSubChannel( cfg ExecConfig, initBals []*big.Int, app client.ProposalOpts, - bID wallet.BackendID, ) *paymentChannel { initAlloc := channel.Allocation{ Assets: cfg.Asset(), diff --git a/client/test/subchannel.go b/client/test/subchannel.go index cedf209c..0d0c1489 100644 --- a/client/test/subchannel.go +++ b/client/test/subchannel.go @@ -77,7 +77,7 @@ func (r *Susie) exec(_cfg ExecConfig, ledgerChannel *paymentChannel) { // stage 2 - open subchannels openSubChannel := func(parentChannel *paymentChannel, funds []*big.Int, app client.ProposalOpts) *paymentChannel { - return parentChannel.openSubChannel(rng, cfg, funds, app, cfg.backend[0]) + return parentChannel.openSubChannel(rng, cfg, funds, app) } var subChannels []*paymentChannel diff --git a/client/test/subchannel_dispute.go b/client/test/subchannel_dispute.go index e897b94b..2b1b9795 100644 --- a/client/test/subchannel_dispute.go +++ b/client/test/subchannel_dispute.go @@ -58,7 +58,7 @@ func (r *DisputeSusie) exec(_cfg ExecConfig, ledgerChannel *paymentChannel) { r.waitStage() // Stage 2 - Open sub-channel. - subChannel := ledgerChannel.openSubChannel(rng, cfg, cfg.SubChannelFunds[:], client.WithoutApp(), _cfg.Backend()[0]) + subChannel := ledgerChannel.openSubChannel(rng, cfg, cfg.SubChannelFunds[:], client.WithoutApp()) subReq0 := client.NewTestChannel(subChannel.Channel).AdjudicatorReq() // Store AdjudicatorReq for version 0 r.waitStage()