-
Notifications
You must be signed in to change notification settings - Fork 222
Expand file tree
/
Copy pathprofiles.go
More file actions
63 lines (52 loc) · 1.43 KB
/
Copy pathprofiles.go
File metadata and controls
63 lines (52 loc) · 1.43 KB
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
package iblockproc
import (
"io"
"math/big"
"github.com/Fantom-foundation/lachesis-base/inter/idx"
"github.com/Fantom-foundation/lachesis-base/inter/pos"
"github.com/ethereum/go-ethereum/rlp"
"github.com/Fantom-foundation/go-opera/inter/drivertype"
)
type ValidatorProfiles map[idx.ValidatorID]drivertype.Validator
func (vv ValidatorProfiles) Copy() ValidatorProfiles {
cp := make(ValidatorProfiles, len(vv))
for k, v := range vv {
cpv := v
cpv.Weight = new(big.Int).Set(cpv.Weight)
cpv.PubKey = cpv.PubKey.Copy()
cp[k] = cpv
}
return cp
}
func (vv ValidatorProfiles) SortedArray() []drivertype.ValidatorAndID {
builder := pos.NewBigBuilder()
for id, profile := range vv {
builder.Set(id, profile.Weight)
}
validators := builder.Build()
sortedIds := validators.SortedIDs()
arr := make([]drivertype.ValidatorAndID, validators.Len())
for i, id := range sortedIds {
arr[i] = drivertype.ValidatorAndID{
ValidatorID: id,
Validator: vv[id],
}
}
return arr
}
// EncodeRLP is for RLP serialization.
func (vv ValidatorProfiles) EncodeRLP(w io.Writer) error {
return rlp.Encode(w, vv.SortedArray())
}
// DecodeRLP is for RLP deserialization.
func (vv *ValidatorProfiles) DecodeRLP(s *rlp.Stream) error {
var arr []drivertype.ValidatorAndID
if err := s.Decode(&arr); err != nil {
return err
}
*vv = make(ValidatorProfiles, len(arr))
for _, it := range arr {
(*vv)[it.ValidatorID] = it.Validator
}
return nil
}