Skip to content

Commit 8f6050c

Browse files
committed
fix(channel/backend.go): Dynamic use of backends instead of hardcoded index
feat(client/): Allow multiple Assets in Test Signed-off-by: Sophia Koehler <sophia@perun.network>
1 parent 22cdc68 commit 8f6050c

File tree

9 files changed

+43
-37
lines changed

9 files changed

+43
-37
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,
@@ -81,9 +81,9 @@ func TestSubChannelDispute(t *testing.T) {
8181

8282
baseCfg := ctest.MakeBaseExecConfig(
8383
[2]map[wallet.BackendID]wire.Address{wire.AddressMapfromAccountMap(setups[0].Identity), wire.AddressMapfromAccountMap(setups[1].Identity)},
84-
chtest.NewRandomAsset(rng, channel.TestBackendID),
85-
channel.TestBackendID,
86-
[2]*big.Int{big.NewInt(100), big.NewInt(100)},
84+
[]channel.Asset{chtest.NewRandomAsset(rng, channel.TestBackendID)},
85+
[]wallet.BackendID{channel.TestBackendID},
86+
[][2]*big.Int{{big.NewInt(100), big.NewInt(100)}},
8787
client.WithoutApp(),
8888
)
8989
cfg := &ctest.DisputeSusieTimExecConfig{

client/test/channel.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -85,8 +85,8 @@ func (ch *paymentChannel) openSubChannel(
8585
bID wallet.BackendID,
8686
) *paymentChannel {
8787
initAlloc := channel.Allocation{
88-
Assets: []channel.Asset{cfg.Asset()},
89-
Backends: []wallet.BackendID{bID},
88+
Assets: cfg.Asset(),
89+
Backends: cfg.Backend(),
9090
Balances: [][]channel.Bal{{initBals[0], initBals[1]}},
9191
}
9292

client/test/role.go

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

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

@@ -185,9 +185,9 @@ func ExecuteTwoPartyTest(ctx context.Context, t *testing.T, role [2]Executer, cf
185185
// MakeBaseExecConfig creates a new BaseExecConfig.
186186
func MakeBaseExecConfig(
187187
peers [2]map[wallet.BackendID]wire.Address,
188-
asset channel.Asset,
189-
backend wallet.BackendID,
190-
initBals [2]*big.Int,
188+
asset []channel.Asset,
189+
backend []wallet.BackendID,
190+
initBals [][2]*big.Int,
191191
app client.ProposalOpts,
192192
) BaseExecConfig {
193193
return BaseExecConfig{
@@ -205,17 +205,17 @@ func (c *BaseExecConfig) Peers() [2]map[wallet.BackendID]wire.Address {
205205
}
206206

207207
// Asset returns the asset.
208-
func (c *BaseExecConfig) Asset() channel.Asset {
208+
func (c *BaseExecConfig) Asset() []channel.Asset {
209209
return c.asset
210210
}
211211

212212
// Backend returns the asset.
213-
func (c *BaseExecConfig) Backend() wallet.BackendID {
213+
func (c *BaseExecConfig) Backend() []wallet.BackendID {
214214
return c.backend
215215
}
216216

217217
// InitBals returns the initial balances.
218-
func (c *BaseExecConfig) InitBals() [2]*big.Int {
218+
func (c *BaseExecConfig) InitBals() [][2]*big.Int {
219219
return c.initBals
220220
}
221221

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

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

364+
peersList := make(map[wallet.BackendID]wallet.Address)
365+
for i := range backend {
366+
peersList[backend[i]] = r.setup.Wallet[backend[i]].NewRandomAccount(rng).Address()
367+
}
362368
prop, err := client.NewLedgerChannelProposal(
363369
r.challengeDuration,
364-
map[wallet.BackendID]wallet.Address{cfg.Backend(): r.setup.Wallet[cfg.Backend()].NewRandomAccount(rng).Address()},
370+
peersList,
365371
alloc,
366372
peers[:],
367373
client.WithNonceFrom(rng),

client/test/subchannel.go

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

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

8383
var subChannels []*paymentChannel

client/test/subchannel_dispute.go

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

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

0 commit comments

Comments
 (0)