github.com/prysmaticlabs/prysm@v1.4.4/beacon-chain/sync/utils.go (about) 1 package sync 2 3 import ( 4 "errors" 5 "sort" 6 7 "github.com/prysmaticlabs/prysm/proto/interfaces" 8 ) 9 10 // A type to represent beacon blocks and roots which have methods 11 // which satisfy the Interface in `Sort` so that this type can 12 // be sorted in ascending order. 13 type sortedObj struct { 14 blks []interfaces.SignedBeaconBlock 15 roots [][32]byte 16 } 17 18 // Less reports whether the element with index i must sort before the element with index j. 19 func (s sortedObj) Less(i, j int) bool { 20 return s.blks[i].Block().Slot() < s.blks[j].Block().Slot() 21 } 22 23 // Swap swaps the elements with indexes i and j. 24 func (s sortedObj) Swap(i, j int) { 25 s.blks[i], s.blks[j] = s.blks[j], s.blks[i] 26 s.roots[i], s.roots[j] = s.roots[j], s.roots[i] 27 } 28 29 // Len is the number of elements in the collection. 30 func (s sortedObj) Len() int { 31 return len(s.blks) 32 } 33 34 // removes duplicates from provided blocks and roots. 35 func (s *Service) dedupBlocksAndRoots(blks []interfaces.SignedBeaconBlock, roots [][32]byte) ([]interfaces.SignedBeaconBlock, [][32]byte, error) { 36 if len(blks) != len(roots) { 37 return nil, nil, errors.New("input blks and roots are diff lengths") 38 } 39 40 // Remove duplicate blocks received 41 rootMap := make(map[[32]byte]bool, len(blks)) 42 newBlks := make([]interfaces.SignedBeaconBlock, 0, len(blks)) 43 newRoots := make([][32]byte, 0, len(roots)) 44 for i, r := range roots { 45 if rootMap[r] { 46 continue 47 } 48 rootMap[r] = true 49 newRoots = append(newRoots, roots[i]) 50 newBlks = append(newBlks, blks[i]) 51 } 52 return newBlks, newRoots, nil 53 } 54 55 func (s *Service) dedupRoots(roots [][32]byte) [][32]byte { 56 newRoots := make([][32]byte, 0, len(roots)) 57 rootMap := make(map[[32]byte]bool, len(roots)) 58 for i, r := range roots { 59 if rootMap[r] { 60 continue 61 } 62 rootMap[r] = true 63 newRoots = append(newRoots, roots[i]) 64 } 65 return newRoots 66 } 67 68 // sort the provided blocks and roots in ascending order. This method assumes that the size of 69 // block slice and root slice is equal. 70 func (s *Service) sortBlocksAndRoots(blks []interfaces.SignedBeaconBlock, roots [][32]byte) ([]interfaces.SignedBeaconBlock, [][32]byte) { 71 obj := sortedObj{ 72 blks: blks, 73 roots: roots, 74 } 75 sort.Sort(obj) 76 return obj.blks, obj.roots 77 }