-
Notifications
You must be signed in to change notification settings - Fork 930
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'handle-missing-archived-index' of github.com:prysmaticl…
…abs/prysm into handle-missing-archived-index
- Loading branch information
Showing
7 changed files
with
146 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
package sync | ||
|
||
import ( | ||
"sort" | ||
|
||
ethpb "github.com/prysmaticlabs/ethereumapis/eth/v1alpha1" | ||
) | ||
|
||
// 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 []*ethpb.SignedBeaconBlock | ||
roots [][32]byte | ||
} | ||
|
||
func (s sortedObj) Less(i, j int) bool { | ||
return s.blks[i].Block.Slot < s.blks[j].Block.Slot | ||
} | ||
|
||
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] | ||
} | ||
|
||
func (s sortedObj) Len() int { | ||
return len(s.blks) | ||
} | ||
|
||
// sort the provided blocks and roots in ascending order. This method assumes that the size of | ||
// block slice and root slice is equal. | ||
func (r *Service) sortBlocksAndRoots(blks []*ethpb.SignedBeaconBlock, roots [][32]byte) ([]*ethpb.SignedBeaconBlock, [][32]byte) { | ||
obj := sortedObj{ | ||
blks: blks, | ||
roots: roots, | ||
} | ||
sort.Sort(obj) | ||
return obj.blks, obj.roots | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
package sync | ||
|
||
import ( | ||
"math/rand" | ||
"testing" | ||
|
||
ethpb "github.com/prysmaticlabs/ethereumapis/eth/v1alpha1" | ||
"github.com/prysmaticlabs/prysm/shared/bytesutil" | ||
) | ||
|
||
func TestSortedObj_SortBlocksRoots(t *testing.T) { | ||
source := rand.NewSource(33) | ||
randGen := rand.New(source) | ||
blks := []*ethpb.SignedBeaconBlock{} | ||
roots := [][32]byte{} | ||
randFunc := func() int64 { | ||
return randGen.Int63n(50) | ||
} | ||
|
||
for i := 0; i < 10; i++ { | ||
slot := uint64(randFunc()) | ||
newBlk := ðpb.SignedBeaconBlock{Block: ðpb.BeaconBlock{Slot: slot}} | ||
blks = append(blks, newBlk) | ||
root := bytesutil.ToBytes32(bytesutil.Bytes32(slot)) | ||
roots = append(roots, root) | ||
} | ||
|
||
r := &Service{} | ||
|
||
newBlks, newRoots := r.sortBlocksAndRoots(blks, roots) | ||
|
||
previousSlot := uint64(0) | ||
for i, b := range newBlks { | ||
if b.Block.Slot < previousSlot { | ||
t.Errorf("Block list is not sorted as %d is smaller than previousSlot %d", b.Block.Slot, previousSlot) | ||
} | ||
if bytesutil.FromBytes8(newRoots[i][:]) != b.Block.Slot { | ||
t.Errorf("root doesn't match stored slot in block: wanted %d but got %d", b.Block.Slot, bytesutil.FromBytes8(newRoots[i][:])) | ||
} | ||
previousSlot = b.Block.Slot | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters