2020-05-18 17:58:20 +00:00
|
|
|
package sync
|
|
|
|
|
|
|
|
import (
|
2020-08-16 23:48:39 +00:00
|
|
|
"errors"
|
2020-05-18 17:58:20 +00:00
|
|
|
"sort"
|
|
|
|
|
2022-08-16 12:20:13 +00:00
|
|
|
"github.com/prysmaticlabs/prysm/v3/consensus-types/interfaces"
|
2020-05-18 17:58:20 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// A type to represent beacon blocks and roots which have methods
|
|
|
|
// which satisfy the Interface in `Sort` so that this type can
|
|
|
|
// be sorted in ascending order.
|
|
|
|
type sortedObj struct {
|
2022-05-02 18:32:37 +00:00
|
|
|
blks []interfaces.SignedBeaconBlock
|
2020-05-18 17:58:20 +00:00
|
|
|
roots [][32]byte
|
|
|
|
}
|
|
|
|
|
2021-04-23 12:06:05 +00:00
|
|
|
// Less reports whether the element with index i must sort before the element with index j.
|
2020-05-18 17:58:20 +00:00
|
|
|
func (s sortedObj) Less(i, j int) bool {
|
2021-05-26 16:19:54 +00:00
|
|
|
return s.blks[i].Block().Slot() < s.blks[j].Block().Slot()
|
2020-05-18 17:58:20 +00:00
|
|
|
}
|
|
|
|
|
2021-04-23 12:06:05 +00:00
|
|
|
// Swap swaps the elements with indexes i and j.
|
2020-05-18 17:58:20 +00:00
|
|
|
func (s sortedObj) Swap(i, j int) {
|
|
|
|
s.blks[i], s.blks[j] = s.blks[j], s.blks[i]
|
|
|
|
s.roots[i], s.roots[j] = s.roots[j], s.roots[i]
|
|
|
|
}
|
|
|
|
|
2021-04-23 12:06:05 +00:00
|
|
|
// Len is the number of elements in the collection.
|
2020-05-18 17:58:20 +00:00
|
|
|
func (s sortedObj) Len() int {
|
|
|
|
return len(s.blks)
|
|
|
|
}
|
|
|
|
|
2020-06-12 13:50:07 +00:00
|
|
|
// removes duplicates from provided blocks and roots.
|
2022-05-02 18:32:37 +00:00
|
|
|
func (_ *Service) dedupBlocksAndRoots(blks []interfaces.SignedBeaconBlock, roots [][32]byte) ([]interfaces.SignedBeaconBlock, [][32]byte, error) {
|
2020-08-16 23:48:39 +00:00
|
|
|
if len(blks) != len(roots) {
|
|
|
|
return nil, nil, errors.New("input blks and roots are diff lengths")
|
|
|
|
}
|
|
|
|
|
2020-06-12 13:50:07 +00:00
|
|
|
// Remove duplicate blocks received
|
2020-07-09 15:50:58 +00:00
|
|
|
rootMap := make(map[[32]byte]bool, len(blks))
|
2022-05-02 18:32:37 +00:00
|
|
|
newBlks := make([]interfaces.SignedBeaconBlock, 0, len(blks))
|
2020-06-12 13:50:07 +00:00
|
|
|
newRoots := make([][32]byte, 0, len(roots))
|
|
|
|
for i, r := range roots {
|
|
|
|
if rootMap[r] {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
rootMap[r] = true
|
|
|
|
newRoots = append(newRoots, roots[i])
|
|
|
|
newBlks = append(newBlks, blks[i])
|
|
|
|
}
|
2020-08-16 23:48:39 +00:00
|
|
|
return newBlks, newRoots, nil
|
2020-06-12 13:50:07 +00:00
|
|
|
}
|
|
|
|
|
2021-12-07 17:52:39 +00:00
|
|
|
func (_ *Service) dedupRoots(roots [][32]byte) [][32]byte {
|
2020-08-20 04:50:14 +00:00
|
|
|
newRoots := make([][32]byte, 0, len(roots))
|
|
|
|
rootMap := make(map[[32]byte]bool, len(roots))
|
|
|
|
for i, r := range roots {
|
|
|
|
if rootMap[r] {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
rootMap[r] = true
|
|
|
|
newRoots = append(newRoots, roots[i])
|
|
|
|
}
|
|
|
|
return newRoots
|
|
|
|
}
|
|
|
|
|
2020-05-18 17:58:20 +00:00
|
|
|
// sort the provided blocks and roots in ascending order. This method assumes that the size of
|
|
|
|
// block slice and root slice is equal.
|
2022-05-02 18:32:37 +00:00
|
|
|
func (_ *Service) sortBlocksAndRoots(blks []interfaces.SignedBeaconBlock, roots [][32]byte) ([]interfaces.SignedBeaconBlock, [][32]byte) {
|
2020-05-18 17:58:20 +00:00
|
|
|
obj := sortedObj{
|
|
|
|
blks: blks,
|
|
|
|
roots: roots,
|
|
|
|
}
|
|
|
|
sort.Sort(obj)
|
|
|
|
return obj.blks, obj.roots
|
|
|
|
}
|