forked from hyperledger-labs/go-perun
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathallocation.go
789 lines (700 loc) · 22.1 KB
/
allocation.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
// Copyright 2025 - 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 channel
import (
"encoding"
"io"
"log"
"math/big"
"perun.network/go-perun/wallet"
"perun.network/go-perun/wire/perunio"
perunbig "polycry.pt/poly-go/math/big"
"github.com/pkg/errors"
)
// MaxNumAssets is an artificial limit on the number of serialized assets in an
// Allocation to avoid having users run out of memory when a malicious peer
// pretends to send a large number of assets.
const MaxNumAssets = 1024
// MaxNumParts is an artificial limit on the number participant assets in an
// Allocation to avoid having users run out of memory when a malicious peer
// pretends to send balances for a large number of participants.
// Keep in mind that an Allocation contains information about every
// participant's balance for every asset, i.e., there are num-assets times
// num-participants balances in an Allocation.
const MaxNumParts = 1024
// MaxNumSubAllocations is an artificial limit on the number of suballocations
// in an Allocation to avoid having users run out of memory when a malicious
// peer pretends to send a large number of suballocations.
// Keep in mind that an Allocation contains information about every
// asset for every suballocation, i.e., there are num-assets times
// num-suballocations items of information in an Allocation.
const MaxNumSubAllocations = 1024
// Allocation and associated types.
type (
// Allocation is the distribution of assets, were the channel to be finalized.
//
// Assets identify the assets held in the channel, like an address to the
// deposit holder contract for this asset.
//
// Balances holds the balance allocations to the participants.
// Its outer dimension must match the size of Assets.
// Its inner dimension must match the size of the Params.parts slice.
// All asset distributions could have been saved as a single []SubAlloc, but this
// would have saved the participants slice twice, wasting space.
//
// Locked holds the locked allocations to sub-app-channels.
Allocation struct {
// Backends is the indexes to which backend the assets belong to
Backends []wallet.BackendID
// Assets are the asset types held in this channel
Assets []Asset
// Balances is the allocation of assets to the Params.Parts
Balances
// Locked describes the funds locked in subchannels. There is one entry
// per subchannel.
Locked []SubAlloc
}
// Balances two dimensional slice of `Bal`. Is a `Summer`.
Balances [][]Bal
// SubAlloc is the allocation of assets to a single receiver channel ID.
// The size of the balances slice must be of the same size as the assets slice
// of the channel Params.
SubAlloc struct {
ID ID
Bals []Bal
IndexMap []Index // Maps participant indices of the sub-channel to participant indices of the parent channel.
}
// Bal is a single asset's balance.
Bal = *big.Int
// Asset identifies an asset. E.g., it may be the address of the multi-sig
// where all participants' assets are deposited.
// The same Asset should be shareable by multiple Allocation instances.
Asset interface {
// BinaryMarshaler marshals the blockchain specific address to
// binary format (a byte array).
encoding.BinaryMarshaler
// BinaryUnmarshaler unmarshals the blockchain specific address
// from binary format (a byte array).
encoding.BinaryUnmarshaler
// Equal returns true iff this asset is equal to the given asset.
Equal(Asset) bool
// Address returns the address in string representation.
Address() []byte
}
)
var (
_ perunio.Serializer = (*Allocation)(nil)
_ perunio.Serializer = (*Balances)(nil)
_ perunbig.Summer = (*Allocation)(nil)
_ perunbig.Summer = (*Balances)(nil)
)
// NewAllocation returns a new allocation for the given number of participants and assets.
func NewAllocation(numParts int, backends []wallet.BackendID, assets ...Asset) *Allocation {
return &Allocation{
Assets: assets,
Backends: backends,
Balances: MakeBalances(len(assets), numParts),
}
}
// AssetIndex returns the index of the asset in the allocation.
func (a *Allocation) AssetIndex(asset Asset) (Index, bool) {
for idx, _asset := range a.Assets {
if asset.Equal(_asset) {
return Index(idx), true
}
}
return 0, false
}
// SetBalance sets the balance for the given asset and participant.
func (a *Allocation) SetBalance(part Index, asset Asset, val Bal) {
assetIdx, ok := a.AssetIndex(asset)
if !ok {
log.Panicf("asset not found: %v", a)
}
a.Balances[assetIdx][part] = new(big.Int).Set(val)
}
// SetAssetBalances sets the balances for the given asset and all participants.
func (a *Allocation) SetAssetBalances(asset Asset, val []Bal) {
assetIdx, ok := a.AssetIndex(asset)
if !ok {
log.Panicf("asset not found: %v", a)
}
a.Balances[assetIdx] = CloneBals(val)
}
// Balance gets the balance for the given asset and participant.
func (a *Allocation) Balance(part Index, asset Asset) Bal {
assetIdx, ok := a.AssetIndex(asset)
if !ok {
log.Panicf("asset not found: %v", a)
}
return a.Balances[assetIdx][part]
}
// AddToBalance adds a given amount to the balance of the specified participant
// for the given asset.
func (a *Allocation) AddToBalance(part Index, asset Asset, val Bal) {
bal := a.Balance(part, asset)
bal.Add(bal, val)
}
// SubFromBalance subtracts a given amount from the balance of the specified
// participant for the given asset.
func (a *Allocation) SubFromBalance(part Index, asset Asset, val Bal) {
bal := a.Balance(part, asset)
bal.Sub(bal, val)
}
// TransferBalance transfers the given amount from one participant to the other.
func (a *Allocation) TransferBalance(from, to Index, asset Asset, val Bal) {
a.SubFromBalance(from, asset, val)
a.AddToBalance(to, asset, val)
}
// NumParts returns the number of participants of this Allocation. It returns -1 if
// there are no Balances, i.e., if the Allocation is invalid.
func (a *Allocation) NumParts() int {
if len(a.Balances) == 0 {
return -1
}
return len(a.Balances[0])
}
// Clone returns a deep copy of the Allocation object.
// If it is nil, it returns nil.
func (a Allocation) Clone() (clone Allocation) {
if a.Backends != nil {
clone.Backends = make([]wallet.BackendID, len(a.Backends))
copy(clone.Backends, a.Backends)
}
if a.Assets != nil {
clone.Assets = make([]Asset, len(a.Assets))
copy(clone.Assets, a.Assets)
}
clone.Balances = a.Balances.Clone()
if a.Locked != nil {
clone.Locked = make([]SubAlloc, len(a.Locked))
for i, sa := range a.Locked {
clone.Locked[i] = *NewSubAlloc(sa.ID, CloneBals(sa.Bals), CloneIndexMap(sa.IndexMap))
}
}
return clone
}
// MakeBalances returns a new Balances object of the specified size.
func MakeBalances(numAssets, numParticipants int) Balances {
balances := make([][]*big.Int, numAssets)
for i := range balances {
balances[i] = make([]*big.Int, numParticipants)
for j := range balances[i] {
balances[i][j] = big.NewInt(0)
}
}
return balances
}
// Clone returns a deep copy of the Balance object.
// If it is nil, it returns nil.
func (b Balances) Clone() Balances {
if b == nil {
return nil
}
clone := make([][]Bal, len(b))
for i, pa := range b {
clone[i] = CloneBals(pa)
}
return clone
}
// Equal returns whether the two balances are equal.
func (b Balances) Equal(bals Balances) bool {
return b.AssertEqual(bals) == nil
}
// AssertEqual returns an error if the two balances are not equal.
func (b Balances) AssertEqual(bals Balances) error {
if len(bals) != len(b) {
return errors.New("outer length mismatch")
}
for i := range bals {
if len(bals[i]) != len(b[i]) {
return errors.Errorf("inner length mismatch at index %d", i)
}
for j := range bals[i] {
if bals[i][j].Cmp(b[i][j]) != 0 {
return errors.Errorf("value mismatch at position [%d, %d]", i, j)
}
}
}
return nil
}
// AssertGreaterOrEqual returns whether each entry of b is greater or equal to
// the corresponding entry of bals.
func (b Balances) AssertGreaterOrEqual(bals Balances) error {
if len(bals) != len(b) {
return errors.New("outer length mismatch")
}
for i := range bals {
if len(bals[i]) != len(b[i]) {
return errors.Errorf("inner length mismatch at index %d", i)
}
for j := range bals[i] {
if b[i][j].Cmp(bals[i][j]) < 0 {
return errors.Errorf("value not greater or equal at position [%d, %d]", i, j)
}
}
}
return nil
}
// Add returns the sum b + a. It panics if the dimensions do not match.
func (b Balances) Add(a Balances) Balances {
return b.operate(
a,
func(b1, b2 Bal) Bal { return new(big.Int).Add(b1, b2) },
)
}
// Sub returns the difference b - a. It panics if the dimensions do not match.
func (b Balances) Sub(a Balances) Balances {
return b.operate(
a,
func(b1, b2 Bal) Bal { return new(big.Int).Sub(b1, b2) },
)
}
// operate returns op(b, a). It panics if the dimensions do not match.
func (b Balances) operate(a Balances, op func(Bal, Bal) Bal) Balances {
if len(a) != len(b) {
log.Panic("outer length mismatch")
}
c := make([][]Bal, len(a))
for i := range a {
if len(a[i]) != len(b[i]) {
log.Panicf("inner length mismatch at index %d", i)
}
c[i] = make([]Bal, len(a[i]))
for j := range a[i] {
c[i][j] = op(b[i][j], a[i][j])
}
}
return c
}
// Encode encodes this allocation into an io.Writer.
func (a Allocation) Encode(w io.Writer) error {
if err := a.Valid(); err != nil {
return errors.WithMessagef(
err, "invalid allocations cannot be encoded, got %v", a)
}
// encode dimensions
if err := perunio.Encode(w, Index(len(a.Assets)), Index(len(a.Balances[0])), Index(len(a.Locked))); err != nil {
return err
}
// encode assets
for i, asset := range a.Assets {
if err := perunio.Encode(w, uint32(a.Backends[i])); err != nil {
return errors.WithMessagef(err, "encoding backends %d", i)
}
if err := perunio.Encode(w, asset); err != nil {
return errors.WithMessagef(err, "encoding asset %d", i)
}
}
// encode participant allocations
if err := a.Balances.Encode(w); err != nil {
return errors.WithMessage(err, "encoding balances")
}
// encode suballocations
for i, s := range a.Locked {
if err := s.Encode(w); err != nil {
return errors.WithMessagef(
err, "encoding suballocation %d", i)
}
}
return nil
}
// Decode decodes an allocation from an io.Reader.
func (a *Allocation) Decode(r io.Reader) error {
// decode dimensions
var numAssets, numParts, numLocked Index
if err := perunio.Decode(r, &numAssets, &numParts, &numLocked); err != nil {
return errors.WithMessage(err, "decoding numAssets, numParts or numLocked")
}
if numAssets > MaxNumAssets || numParts > MaxNumParts || numLocked > MaxNumSubAllocations {
return errors.New("numAssets, numParts or numLocked too big")
}
// decode assets
a.Assets = make([]Asset, numAssets)
a.Backends = make([]wallet.BackendID, numAssets)
for i := range a.Assets {
// decode backend index
var id uint32
if err := perunio.Decode(r, &id); err != nil {
return errors.WithMessagef(err, "decoding backend index for asset %d", i)
}
a.Backends[i] = wallet.BackendID(id)
asset := NewAsset(a.Backends[i])
if err := perunio.Decode(r, asset); err != nil {
return errors.WithMessagef(err, "decoding asset %d", i)
}
a.Assets[i] = asset
}
// decode participant allocations
if err := perunio.Decode(r, &a.Balances); err != nil {
return errors.WithMessage(err, "decoding balances")
}
// decode locked allocations
a.Locked = make([]SubAlloc, numLocked)
for i := range a.Locked {
if err := a.Locked[i].Decode(r); err != nil {
return errors.WithMessagef(
err, "decoding suballocation %d", i)
}
}
return a.Valid()
}
// Decode decodes a Balances from an io.Reader.
func (b *Balances) Decode(r io.Reader) error {
var numAssets, numParts Index
if err := perunio.Decode(r, &numAssets, &numParts); err != nil {
return errors.WithMessage(err, "decoding dimensions")
}
if numAssets > MaxNumAssets {
return errors.Errorf("expected maximum number of assets %d, got %d", MaxNumAssets, numAssets)
}
if numParts > MaxNumParts {
return errors.Errorf("expected maximum number of parts %d, got %d", MaxNumParts, numParts)
}
*b = make(Balances, numAssets)
for i := range *b {
(*b)[i] = make([]Bal, numParts)
for j := range (*b)[i] {
(*b)[i][j] = new(big.Int)
if err := perunio.Decode(r, &(*b)[i][j]); err != nil {
return errors.WithMessagef(
err, "decoding balance of asset %d of participant %d", i, j)
}
}
}
return nil
}
// Encode encodes these balances into an io.Writer.
func (b Balances) Encode(w io.Writer) error {
numAssets := len(b)
numParts := 0
if numAssets > 0 {
numParts = len(b[0])
}
if numAssets > MaxNumAssets {
return errors.Errorf("expected maximum number of assets %d, got %d", MaxNumAssets, numAssets)
}
if numParts > MaxNumParts {
return errors.Errorf("expected maximum number of parts %d, got %d", MaxNumParts, numParts)
}
if err := perunio.Encode(w, Index(numAssets), Index(numParts)); err != nil {
return errors.WithMessage(err, "encoding dimensions")
}
for i := range b {
for j := range b[i] {
if err := perunio.Encode(w, b[i][j]); err != nil {
return errors.WithMessagef(
err, "encoding balance of asset %d of participant %d", i, j)
}
}
}
return nil
}
// CloneBals creates a deep copy of a balance array.
func CloneBals(orig []Bal) []Bal {
if orig == nil {
return nil
}
clone := make([]Bal, len(orig))
for i, bal := range orig {
clone[i] = new(big.Int).Set(bal)
}
return clone
}
// CloneIndexMap creates a deep copy of an index map.
func CloneIndexMap(orig []Index) (clone []Index) {
if orig == nil {
return nil
}
clone = make([]Index, len(orig))
copy(clone, orig)
return
}
// Valid checks that the asset-dimensions match and slices are not nil.
// Assets and Balances cannot be of zero length.
func (a Allocation) Valid() error {
if len(a.Assets) == 0 || len(a.Balances) == 0 {
return errors.New("assets and participant balances must not be of length zero (or nil)")
}
if len(a.Assets) > MaxNumAssets || len(a.Locked) > MaxNumSubAllocations {
return errors.New("too many assets or sub-allocations")
}
n := len(a.Assets)
if len(a.Balances) != n {
return errors.Errorf("dimension mismatch: number of Assets: %d vs Balances: %d", n, len(a.Balances))
}
numParts := len(a.Balances[0])
if numParts <= 0 || numParts > MaxNumParts {
return errors.Errorf("number of participants is zero or too large")
}
for i, asset := range a.Balances {
if len(asset) != numParts {
return errors.Errorf("%d participants for asset %d, expected %d", len(asset), i, numParts)
}
for j, bal := range asset {
if bal.Sign() == -1 {
return errors.Errorf("balance[%d][%d] is negative: got %v", i, j, bal)
}
}
}
// Locked is allowed to have zero length, in which case there's nothing locked
// and the loop is empty.
for _, l := range a.Locked {
if err := l.Valid(); err != nil {
return errors.WithMessage(err, "invalid sub-allocation")
}
if len(l.Bals) != n {
return errors.Errorf("dimension mismatch of app-channel balance vector (ID: %x): got %d, expected %d", l.ID, l.Bals, n)
}
}
return nil
}
// Sum returns the sum of each asset over all participants and locked
// allocations.
func (a Allocation) Sum() []Bal {
totals := a.Balances.Sum()
// Locked is allowed to have zero length, in which case there's nothing locked
// and the loop is empty.
for _, a := range a.Locked {
for i, bal := range a.Bals {
totals[i].Add(totals[i], bal)
}
}
return totals
}
// Sum returns the sum of each asset over all participants.
func (b Balances) Sum() []Bal {
n := len(b)
totals := make([]*big.Int, n)
for i := 0; i < n; i++ {
totals[i] = new(big.Int)
}
for i, asset := range b {
for _, bal := range asset {
totals[i].Add(totals[i], bal)
}
}
return totals
}
// NewSubAlloc creates a new sub-allocation.
func NewSubAlloc(id ID, bals []Bal, indexMap []Index) *SubAlloc {
if indexMap == nil {
indexMap = []Index{}
}
return &SubAlloc{ID: id, Bals: bals, IndexMap: indexMap}
}
// SubAlloc tries to return the sub-allocation for the given subchannel.
// The second return value indicates success.
func (a Allocation) SubAlloc(subchannel ID) (subAlloc SubAlloc, ok bool) {
for _, subAlloc = range a.Locked {
if subAlloc.ID == subchannel {
ok = true
return
}
}
ok = false
return
}
// AddSubAlloc adds the given sub-allocation.
func (a *Allocation) AddSubAlloc(subAlloc SubAlloc) {
a.Locked = append(a.Locked, subAlloc)
}
// RemoveSubAlloc removes the given sub-allocation.
func (a *Allocation) RemoveSubAlloc(subAlloc SubAlloc) error {
for i := range a.Locked {
if subAlloc.Equal(&a.Locked[i]) == nil {
// remove element at index i
a.Locked = append(a.Locked[:i], a.Locked[i+1:]...)
return nil
}
}
return errors.New("not found")
}
// Equal returns nil if the `Allocation` objects are equal and an error if they
// are not equal.
func (a *Allocation) Equal(b *Allocation) error {
if a == b {
return nil
}
// Compare Backends
if err := AssertBackendsEqual(a.Backends, b.Backends); err != nil {
return errors.WithMessage(err, "comparing backends")
}
// Compare Assets
if err := AssertAssetsEqual(a.Assets, b.Assets); err != nil {
return errors.WithMessage(err, "comparing assets")
}
// Compare Balances
if err := a.Balances.AssertEqual(b.Balances); err != nil {
return errors.WithMessage(err, "comparing balances")
}
// Compare Locked
return errors.WithMessage(SubAllocsAssertEqual(a.Locked, b.Locked), "comparing sub-allocations")
}
// AssertAssetsEqual returns an error if the given assets are not equal.
func AssertAssetsEqual(a []Asset, b []Asset) error {
if len(a) != len(b) {
return errors.New("length mismatch")
}
for i, asset := range a {
if !asset.Equal(b[i]) {
return errors.Errorf("value mismatch at index %d", i)
}
}
return nil
}
// AssertBackendsEqual returns an error if the given assets are not equal.
func AssertBackendsEqual(a []wallet.BackendID, b []wallet.BackendID) error {
if len(a) != len(b) {
return errors.New("length mismatch")
}
for i, bID := range a {
if !bID.Equal(b[i]) {
return errors.Errorf("value mismatch at index %d", i)
}
}
return nil
}
var _ perunio.Serializer = new(SubAlloc)
// Valid checks if this suballocation is valid.
func (s SubAlloc) Valid() error {
if len(s.Bals) > MaxNumAssets {
return errors.New("too many bals")
}
for j, bal := range s.Bals {
if bal.Sign() == -1 {
return errors.Errorf("suballoc[%d] of ID %d is negative: got %v", j, s.ID, bal)
}
}
return nil
}
// Encode encodes the SubAlloc s into w and returns an error if it failed.
func (s SubAlloc) Encode(w io.Writer) error {
if err := s.Valid(); err != nil {
return errors.WithMessagef(
err, "invalid sub-allocations cannot be encoded, got %v", s)
}
// encode ID and dimension
if err := perunio.Encode(w, s.ID, Index(len(s.Bals))); err != nil {
return errors.WithMessagef(
err, "encoding sub-allocation ID or dimension, id %v", s.ID)
}
// encode bals
for i, bal := range s.Bals {
if err := perunio.Encode(w, bal); err != nil {
return errors.WithMessagef(
err, "encoding balance of participant %d", i)
}
}
// Encode IndexMap.
if err := perunio.Encode(w, Index(len(s.IndexMap))); err != nil {
return errors.WithMessage(err, "encoding length of index map")
}
for i, x := range s.IndexMap {
if err := perunio.Encode(w, x); err != nil {
return errors.WithMessagef(err, "encoding index map entry %d", i)
}
}
return nil
}
// Decode decodes the SubAlloc s encoded in r and returns an error if it
// failed.
func (s *SubAlloc) Decode(r io.Reader) error {
var numAssets Index
// decode ID and dimension
if err := perunio.Decode(r, &s.ID, &numAssets); err != nil {
return errors.WithMessage(err, "decoding sub-allocation ID or dimension")
}
if numAssets > MaxNumAssets {
return errors.Errorf("numAssets too big, got: %d max: %d", numAssets, MaxNumAssets)
}
// decode bals
s.Bals = make([]Bal, numAssets)
for i := range s.Bals {
if err := perunio.Decode(r, &s.Bals[i]); err != nil {
return errors.WithMessagef(
err, "decoding participant balance %d", i)
}
}
// Decode index map.
var l Index
if err := perunio.Decode(r, &l); err != nil {
return errors.WithMessage(err, "decoding index map length")
}
s.IndexMap = make([]Index, l)
for i := range s.IndexMap {
if err := perunio.Decode(r, &s.IndexMap[i]); err != nil {
return errors.WithMessagef(err, "decoding index %d", i)
}
}
return s.Valid()
}
// Equal returns nil if the `SubAlloc` objects are equal and an error if they
// are not equal.
func (s *SubAlloc) Equal(t *SubAlloc) error {
if s == t {
return nil
}
if s.ID != t.ID {
return errors.New("different ID")
}
if !s.BalancesEqual(t.Bals) {
return errors.New("balances unequal")
}
if !s.indexMapEqual(s.IndexMap) {
return errors.New("unequal index map")
}
return nil
}
// BalancesEqual returns whether balances b equal s.Bals.
func (s *SubAlloc) BalancesEqual(b []Bal) bool {
if len(s.Bals) != len(b) {
return false
}
for i, bal := range s.Bals {
if bal.Cmp(b[i]) != 0 {
return false
}
}
return true
}
// indexMapEqual returns whether s.indexMap equals b.
func (s *SubAlloc) indexMapEqual(b []Index) bool {
if len(s.IndexMap) != len(b) {
return false
}
for i, x := range s.IndexMap {
if x != b[i] {
return false
}
}
return true
}
// SubAllocsAssertEqual asserts that the two sub-allocations are equal. If they
// are unequal, an error with additional information is thrown.
func SubAllocsAssertEqual(a []SubAlloc, b []SubAlloc) error {
if len(a) != len(b) {
return errors.New("length mismatch")
}
for i := range a {
if a[i].Equal(&b[i]) != nil {
return errors.Errorf("value mismatch at index %d", i)
}
}
return nil
}
// SubAllocsEqual returns whether the two sub-allocations are equal.
func SubAllocsEqual(a []SubAlloc, b []SubAlloc) bool {
return SubAllocsAssertEqual(a, b) == nil
}