Skip to content

Commit 2ab4349

Browse files
authored
Merge pull request #423 from perun-network/ckb_test
Extend Payment Channel Test Framework
2 parents 85d5f6d + 39732fb commit 2ab4349

File tree

9 files changed

+43
-40
lines changed

9 files changed

+43
-40
lines changed

channel/backend.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,8 +66,8 @@ func SetBackend(b Backend, id int) {
6666
// CalcID calculates the CalcID.
6767
func CalcID(p *Params) (ID, error) {
6868
var lastErr error
69-
for _, b := range backend {
70-
id, err := b.CalcID(p)
69+
for i := range p.Parts[0] {
70+
id, err := backend[i].CalcID(p)
7171
if err == nil {
7272
return id, nil
7373
}

client/appchannel_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,9 +45,9 @@ func TestProgression(t *testing.T) {
4545
execConfig := &clienttest.ProgressionExecConfig{
4646
BaseExecConfig: clienttest.MakeBaseExecConfig(
4747
[2]map[wallet.BackendID]wire.Address{wire.AddressMapfromAccountMap(setups[0].Identity), wire.AddressMapfromAccountMap(setups[1].Identity)},
48-
chtest.NewRandomAsset(rng, channel.TestBackendID),
49-
channel.TestBackendID,
50-
[2]*big.Int{big.NewInt(99), big.NewInt(1)},
48+
[]channel.Asset{chtest.NewRandomAsset(rng, channel.TestBackendID)},
49+
[]wallet.BackendID{channel.TestBackendID},
50+
[][2]*big.Int{{big.NewInt(99), big.NewInt(1)}},
5151
client.WithApp(app, channel.NewMockOp(channel.OpValid)),
5252
),
5353
}

client/client_role_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -89,9 +89,9 @@ func runAliceBobTest(ctx context.Context, t *testing.T, setup func(*rand.Rand) (
8989
cfg := &ctest.AliceBobExecConfig{
9090
BaseExecConfig: ctest.MakeBaseExecConfig(
9191
[2]map[wallet.BackendID]wire.Address{wire.AddressMapfromAccountMap(setups[0].Identity), wire.AddressMapfromAccountMap(setups[1].Identity)},
92-
chtest.NewRandomAsset(rng, channel.TestBackendID),
93-
channel.TestBackendID,
94-
[2]*big.Int{big.NewInt(100), big.NewInt(100)},
92+
[]channel.Asset{chtest.NewRandomAsset(rng, channel.TestBackendID)},
93+
[]wallet.BackendID{channel.TestBackendID},
94+
[][2]*big.Int{{big.NewInt(100), big.NewInt(100)}},
9595
app,
9696
),
9797
NumPayments: [2]int{2, 2},

client/payment_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -61,9 +61,9 @@ func TestPaymentDispute(t *testing.T) {
6161
cfg := &ctest.MalloryCarolExecConfig{
6262
BaseExecConfig: ctest.MakeBaseExecConfig(
6363
[2]map[wallet.BackendID]wire.Address{wire.AddressMapfromAccountMap(setups[mallory].Identity), wire.AddressMapfromAccountMap(setups[carol].Identity)},
64-
chtest.NewRandomAsset(rng, channel.TestBackendID),
65-
channel.TestBackendID,
66-
[2]*big.Int{big.NewInt(100), big.NewInt(1)},
64+
[]channel.Asset{chtest.NewRandomAsset(rng, channel.TestBackendID)},
65+
[]wallet.BackendID{channel.TestBackendID},
66+
[][2]*big.Int{{big.NewInt(100), big.NewInt(1)}},
6767
client.WithoutApp(),
6868
),
6969
NumPayments: [2]int{5, 0},

client/subchannel_test.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -43,9 +43,9 @@ func TestSubChannelHappy(t *testing.T) {
4343
cfg := ctest.NewSusieTimExecConfig(
4444
ctest.MakeBaseExecConfig(
4545
[2]map[wallet.BackendID]wire.Address{wire.AddressMapfromAccountMap(setups[0].Identity), wire.AddressMapfromAccountMap(setups[1].Identity)},
46-
chtest.NewRandomAsset(rng, channel.TestBackendID),
47-
channel.TestBackendID,
48-
[2]*big.Int{big.NewInt(100), big.NewInt(100)},
46+
[]channel.Asset{chtest.NewRandomAsset(rng, channel.TestBackendID)},
47+
[]wallet.BackendID{channel.TestBackendID},
48+
[][2]*big.Int{{big.NewInt(100), big.NewInt(100)}},
4949
client.WithoutApp(),
5050
),
5151
[][2]*big.Int{
@@ -79,9 +79,9 @@ func TestSubChannelDispute(t *testing.T) {
7979

8080
baseCfg := ctest.MakeBaseExecConfig(
8181
[2]map[wallet.BackendID]wire.Address{wire.AddressMapfromAccountMap(setups[0].Identity), wire.AddressMapfromAccountMap(setups[1].Identity)},
82-
chtest.NewRandomAsset(rng, channel.TestBackendID),
83-
channel.TestBackendID,
84-
[2]*big.Int{big.NewInt(100), big.NewInt(100)},
82+
[]channel.Asset{chtest.NewRandomAsset(rng, channel.TestBackendID)},
83+
[]wallet.BackendID{channel.TestBackendID},
84+
[][2]*big.Int{{big.NewInt(100), big.NewInt(100)}},
8585
client.WithoutApp(),
8686
)
8787
cfg := &ctest.DisputeSusieTimExecConfig{

client/test/channel.go

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,6 @@ import (
2121
"math/big"
2222
"time"
2323

24-
"perun.network/go-perun/wallet"
25-
2624
"perun.network/go-perun/channel"
2725
"perun.network/go-perun/client"
2826
"perun.network/go-perun/log"
@@ -85,11 +83,10 @@ func (ch *paymentChannel) openSubChannel(
8583
cfg ExecConfig,
8684
initBals []*big.Int,
8785
app client.ProposalOpts,
88-
bID wallet.BackendID,
8986
) *paymentChannel {
9087
initAlloc := channel.Allocation{
91-
Assets: []channel.Asset{cfg.Asset()},
92-
Backends: []wallet.BackendID{bID},
88+
Assets: cfg.Asset(),
89+
Backends: cfg.Backend(),
9390
Balances: [][]channel.Bal{{initBals[0], initBals[1]}},
9491
}
9592

client/test/role.go

Lines changed: 22 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -88,18 +88,18 @@ type (
8888
// ExecConfig contains additional config parameters for the tests.
8989
ExecConfig interface {
9090
Peers() [2]map[wallet.BackendID]wire.Address // must match the RoleSetup.Identity's
91-
Asset() channel.Asset // single Asset to use in this channel
92-
Backend() wallet.BackendID // backend corresponding to asset
93-
InitBals() [2]*big.Int // channel deposit of each role
91+
Asset() []channel.Asset // single Asset to use in this channel
92+
Backend() []wallet.BackendID // backend corresponding to asset
93+
InitBals() [][2]*big.Int // channel deposit of each role
9494
App() client.ProposalOpts // must be either WithApp or WithoutApp
9595
}
9696

9797
// BaseExecConfig contains base config parameters.
9898
BaseExecConfig struct {
9999
peers [2]map[wallet.BackendID]wire.Address // must match the RoleSetup.Identity's
100-
asset channel.Asset // single Asset to use in this channel
101-
backend wallet.BackendID // backend corresponding to asset
102-
initBals [2]*big.Int // channel deposit of each role
100+
asset []channel.Asset // single Asset to use in this channel
101+
backend []wallet.BackendID // backend corresponding to asset
102+
initBals [][2]*big.Int // channel deposit of each role
103103
app client.ProposalOpts // must be either WithApp or WithoutApp
104104
}
105105

@@ -188,9 +188,9 @@ func ExecuteTwoPartyTest(ctx context.Context, t *testing.T, role [2]Executer, cf
188188
// MakeBaseExecConfig creates a new BaseExecConfig.
189189
func MakeBaseExecConfig(
190190
peers [2]map[wallet.BackendID]wire.Address,
191-
asset channel.Asset,
192-
backend wallet.BackendID,
193-
initBals [2]*big.Int,
191+
asset []channel.Asset,
192+
backend []wallet.BackendID,
193+
initBals [][2]*big.Int,
194194
app client.ProposalOpts,
195195
) BaseExecConfig {
196196
return BaseExecConfig{
@@ -208,17 +208,17 @@ func (c *BaseExecConfig) Peers() [2]map[wallet.BackendID]wire.Address {
208208
}
209209

210210
// Asset returns the asset.
211-
func (c *BaseExecConfig) Asset() channel.Asset {
211+
func (c *BaseExecConfig) Asset() []channel.Asset {
212212
return c.asset
213213
}
214214

215215
// Backend returns the asset.
216-
func (c *BaseExecConfig) Backend() wallet.BackendID {
216+
func (c *BaseExecConfig) Backend() []wallet.BackendID {
217217
return c.backend
218218
}
219219

220220
// InitBals returns the initial balances.
221-
func (c *BaseExecConfig) InitBals() [2]*big.Int {
221+
func (c *BaseExecConfig) InitBals() [][2]*big.Int {
222222
return c.initBals
223223
}
224224

@@ -358,13 +358,19 @@ func (r *role) LedgerChannelProposal(rng *rand.Rand, cfg ExecConfig) *client.Led
358358
r.log.Panic("Invalid ExecConfig: App does not specify an app.")
359359
}
360360

361-
peers, asset, bals := cfg.Peers(), cfg.Asset(), cfg.InitBals()
362-
alloc := channel.NewAllocation(len(peers), []wallet.BackendID{cfg.Backend()}, asset)
363-
alloc.SetAssetBalances(asset, bals[:])
361+
peers, assets, bals, backend := cfg.Peers(), cfg.Asset(), cfg.InitBals(), cfg.Backend()
362+
alloc := channel.NewAllocation(len(peers), cfg.Backend(), assets...)
363+
for i := range assets {
364+
alloc.SetAssetBalances(assets[i], bals[i][:])
365+
}
364366

367+
peersList := make(map[wallet.BackendID]wallet.Address)
368+
for i := range backend {
369+
peersList[backend[i]] = r.setup.Wallet[backend[i]].NewRandomAccount(rng).Address()
370+
}
365371
prop, err := client.NewLedgerChannelProposal(
366372
r.challengeDuration,
367-
map[wallet.BackendID]wallet.Address{cfg.Backend(): r.setup.Wallet[cfg.Backend()].NewRandomAccount(rng).Address()},
373+
peersList,
368374
alloc,
369375
peers[:],
370376
client.WithNonceFrom(rng),

client/test/subchannel.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ func (r *Susie) exec(_cfg ExecConfig, ledgerChannel *paymentChannel) {
7676

7777
// stage 2 - open subchannels
7878
openSubChannel := func(parentChannel *paymentChannel, funds []*big.Int, app client.ProposalOpts) *paymentChannel {
79-
return parentChannel.openSubChannel(rng, cfg, funds, app, cfg.backend)
79+
return parentChannel.openSubChannel(rng, cfg, funds, app)
8080
}
8181

8282
var subChannels []*paymentChannel

client/test/subchannel_dispute.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ func (r *DisputeSusie) exec(_cfg ExecConfig, ledgerChannel *paymentChannel) {
5959
r.waitStage()
6060

6161
// Stage 2 - Open sub-channel.
62-
subChannel := ledgerChannel.openSubChannel(rng, cfg, cfg.SubChannelFunds[:], client.WithoutApp(), _cfg.Backend())
62+
subChannel := ledgerChannel.openSubChannel(rng, cfg, cfg.SubChannelFunds[:], client.WithoutApp())
6363
subReq0 := client.NewTestChannel(subChannel.Channel).AdjudicatorReq() // Store AdjudicatorReq for version 0
6464
r.waitStage()
6565

0 commit comments

Comments
 (0)