mirror of
https://gitlab.com/pulsechaincom/prysm-pulse.git
synced 2024-12-25 12:57:18 +00:00
caf9bdbc6f
* commit initial work * checkpoint current work * gaz * checkpoint * req/resp changes * initial-sync * finally works * fix error * fix bugs * fix issue * fix issues * fix refs * tests * more text fixes * more text fixes * more text fixes * fix tests * fix tests * tests * finally fix builds * finally * comments * fix fuzz * share common library * fix * fix * add in more defensive nil checks * add in more defensive nil checks * imports * Apply suggestions from code review Co-authored-by: terence tsao <terence@prysmaticlabs.com> * Apply suggestions from code review Co-authored-by: terence tsao <terence@prysmaticlabs.com> * Update shared/interfaces/block_interface.go Co-authored-by: terence tsao <terence@prysmaticlabs.com> * Update shared/interfaces/block_wrapper.go Co-authored-by: terence tsao <terence@prysmaticlabs.com> * Update shared/interfaces/block_interface.go Co-authored-by: terence tsao <terence@prysmaticlabs.com> * imports * fix bad changes * fix * terence's review * terence's review * fmt * Update beacon-chain/rpc/beacon/blocks.go Co-authored-by: Radosław Kapka <rkapka@wp.pl> * fix tests * fix * fix all tests Co-authored-by: terence tsao <terence@prysmaticlabs.com> Co-authored-by: Radosław Kapka <rkapka@wp.pl>
78 lines
2.1 KiB
Go
78 lines
2.1 KiB
Go
package sync
|
|
|
|
import (
|
|
"errors"
|
|
"sort"
|
|
|
|
"github.com/prysmaticlabs/prysm/shared/interfaces"
|
|
)
|
|
|
|
// 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 {
|
|
blks []interfaces.SignedBeaconBlock
|
|
roots [][32]byte
|
|
}
|
|
|
|
// Less reports whether the element with index i must sort before the element with index j.
|
|
func (s sortedObj) Less(i, j int) bool {
|
|
return s.blks[i].Block().Slot() < s.blks[j].Block().Slot()
|
|
}
|
|
|
|
// Swap swaps the elements with indexes i and j.
|
|
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]
|
|
}
|
|
|
|
// Len is the number of elements in the collection.
|
|
func (s sortedObj) Len() int {
|
|
return len(s.blks)
|
|
}
|
|
|
|
// removes duplicates from provided blocks and roots.
|
|
func (s *Service) dedupBlocksAndRoots(blks []interfaces.SignedBeaconBlock, roots [][32]byte) ([]interfaces.SignedBeaconBlock, [][32]byte, error) {
|
|
if len(blks) != len(roots) {
|
|
return nil, nil, errors.New("input blks and roots are diff lengths")
|
|
}
|
|
|
|
// Remove duplicate blocks received
|
|
rootMap := make(map[[32]byte]bool, len(blks))
|
|
newBlks := make([]interfaces.SignedBeaconBlock, 0, len(blks))
|
|
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])
|
|
}
|
|
return newBlks, newRoots, nil
|
|
}
|
|
|
|
func (s *Service) dedupRoots(roots [][32]byte) [][32]byte {
|
|
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
|
|
}
|
|
|
|
// sort the provided blocks and roots in ascending order. This method assumes that the size of
|
|
// block slice and root slice is equal.
|
|
func (s *Service) sortBlocksAndRoots(blks []interfaces.SignedBeaconBlock, roots [][32]byte) ([]interfaces.SignedBeaconBlock, [][32]byte) {
|
|
obj := sortedObj{
|
|
blks: blks,
|
|
roots: roots,
|
|
}
|
|
sort.Sort(obj)
|
|
return obj.blks, obj.roots
|
|
}
|