Skip to content

Commit e9eaa5c

Browse files
committed
🐛 [channel/test] Generate modified params with correct id
Also ensure that modified states are consistent. Signed-off-by: Matthias Geihs <matthias@perun.network>
1 parent d6551f3 commit e9eaa5c

File tree

1 file changed

+27
-9
lines changed

1 file changed

+27
-9
lines changed

channel/test/channeltest.go

Lines changed: 27 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -133,42 +133,54 @@ func buildModifiedParams(p1, p2 *channel.Params, s *Setup) (ret []channel.Params
133133
// Modify complete Params
134134
{
135135
modParams := *p2
136-
ret = append(ret, modParams)
136+
ret = appendModParams(ret, modParams)
137137
}
138138
// Modify ChallengeDuration
139139
{
140140
modParams := *p1
141141
modParams.ChallengeDuration = p2.ChallengeDuration
142-
ret = append(ret, modParams)
142+
ret = appendModParams(ret, modParams)
143143
}
144144
// Modify Parts
145145
{
146146
// Modify complete Parts
147147
{
148148
modParams := *p1
149149
modParams.Parts = p2.Parts
150-
ret = append(ret, modParams)
150+
ret = appendModParams(ret, modParams)
151151
}
152152
// Modify Parts[0]
153153
{
154154
modParams := *p1
155155
modParams.Parts = make([]wallet.Address, len(p1.Parts))
156156
copy(modParams.Parts, p1.Parts)
157157
modParams.Parts[0] = s.RandomAddress()
158-
ret = append(ret, modParams)
158+
ret = appendModParams(ret, modParams)
159159
}
160160
}
161161
// Modify Nonce
162162
{
163163
modParams := *p1
164164
modParams.Nonce = p2.Nonce
165-
ret = append(ret, modParams)
165+
ret = appendModParams(ret, modParams)
166166
}
167167
}
168168

169169
return
170170
}
171171

172+
func appendModParams(a []channel.Params, modParams channel.Params) []channel.Params {
173+
p := channel.NewParamsUnsafe(
174+
modParams.ChallengeDuration,
175+
modParams.Parts,
176+
modParams.App,
177+
modParams.Nonce,
178+
modParams.LedgerChannel,
179+
modParams.VirtualChannel,
180+
)
181+
return append(a, *p)
182+
}
183+
172184
// buildModifiedStates returns a slice of States that are different from `s1` assuming that `s2` differs in
173185
// every member from `s1`.
174186
// `modifyApp` indicates whether the app should also be changed or not. In some cases (signature) it is desirable
@@ -211,8 +223,11 @@ func buildModifiedStates(s1, s2 *channel.State, modifyApp bool) (ret []channel.S
211223
{
212224
// Modify complete Assets
213225
{
226+
l1, l2 := len(s1.Allocation.Assets), len(s2.Allocation.Assets)
214227
modState := s1.Clone()
215-
modState.Allocation.Assets = s2.Allocation.Assets
228+
for i := 0; i < l1 && i < l2; i++ {
229+
modState.Allocation.Assets[i] = s2.Allocation.Assets[i]
230+
}
216231
ret = append(ret, *modState)
217232
}
218233
// Modify Assets[0]
@@ -228,8 +243,11 @@ func buildModifiedStates(s1, s2 *channel.State, modifyApp bool) (ret []channel.S
228243
{
229244
// Modify complete Balances
230245
{
246+
l1, l2 := len(s1.Allocation.Balances), len(s2.Allocation.Balances)
231247
modState := s1.Clone()
232-
modState.Allocation.Balances = s2.Allocation.Balances
248+
for i := 0; i < l1 && i < l2; i++ {
249+
modState.Allocation.Balances[i] = s2.Allocation.Balances[i]
250+
}
233251
ret = append(ret, *modState)
234252
}
235253
// Modify Balances[0]
@@ -246,14 +264,14 @@ func buildModifiedStates(s1, s2 *channel.State, modifyApp bool) (ret []channel.S
246264
}
247265
}
248266
// Modify Locked
249-
{
267+
if len(s1.Allocation.Locked) > 0 && len(s2.Allocation.Locked) > 0 {
250268
// Modify complete Locked
251269
{
252270
modState := s1.Clone()
253271
modState.Allocation.Locked = s2.Allocation.Locked
254272
ret = append(ret, *modState)
255273
}
256-
// Modify AppID
274+
// Modify Locked ID
257275
{
258276
modState := s1.Clone()
259277
modState.Allocation.Locked[0].ID = s2.Allocation.Locked[0].ID

0 commit comments

Comments
 (0)