Revert initsync revert (#12431)

* Revert "Revert "BeaconBlocksByRange and BlobSidecarsByRange consistency (#123… (#12426)"

This reverts commit ddc1e48e05.

* fix metrics bug, add batch.next tests

---------

Co-authored-by: Kasey Kirkham <kasey@users.noreply.github.com>
This commit is contained in:
kasey
2023-05-19 11:59:13 -05:00
committed by GitHub
parent b84dd40ba9
commit 385a317902
14 changed files with 771 additions and 449 deletions

View File

@@ -7,6 +7,7 @@ go_library(
"factory.go",
"getters.go",
"proto.go",
"roblock.go",
"setters.go",
"types.go",
],
@@ -36,6 +37,7 @@ go_test(
"factory_test.go",
"getters_test.go",
"proto_test.go",
"roblock_test.go",
],
embed = [":go_default_library"],
deps = [

View File

@@ -0,0 +1,75 @@
package blocks
import (
"bytes"
"sort"
"github.com/prysmaticlabs/prysm/v4/consensus-types/interfaces"
)
// ROBlock is a value that embeds a ReadOnlySignedBeaconBlock along with its block root ([32]byte).
// This allows the block root to be cached within a value that satisfies the ReadOnlySignedBeaconBlock interface.
// Since the root and slot for each ROBlock is known, slices can be efficiently sorted using ROBlockSlice.
type ROBlock struct {
interfaces.ReadOnlySignedBeaconBlock
root [32]byte
}
// Root returns the block hash_tree_root for the embedded ReadOnlySignedBeaconBlock.Block().
func (b ROBlock) Root() [32]byte {
return b.root
}
// NewROBlockWithRoot creates an ROBlock embedding the given block with its root. It accepts the root as parameter rather than
// computing it internally, because in some cases a block is retrieved by its root and recomputing it is a waste.
func NewROBlockWithRoot(b interfaces.ReadOnlySignedBeaconBlock, root [32]byte) (ROBlock, error) {
if err := BeaconBlockIsNil(b); err != nil {
return ROBlock{}, err
}
return ROBlock{ReadOnlySignedBeaconBlock: b, root: root}, nil
}
// NewROBlock creates a ROBlock from a ReadOnlySignedBeaconBlock. It uses the HashTreeRoot method of the given
// ReadOnlySignedBeaconBlock.Block to compute the cached root.
func NewROBlock(b interfaces.ReadOnlySignedBeaconBlock) (ROBlock, error) {
if err := BeaconBlockIsNil(b); err != nil {
return ROBlock{}, err
}
root, err := b.Block().HashTreeRoot()
if err != nil {
return ROBlock{}, err
}
return ROBlock{ReadOnlySignedBeaconBlock: b, root: root}, nil
}
// ROBlockSlice implements sort.Interface so that slices of ROBlocks can be easily sorted.
// A slice of ROBlock is sorted first by slot, with ties broken by cached block roots.
type ROBlockSlice []ROBlock
var _ sort.Interface = ROBlockSlice{}
// Less reports whether the element with index i must sort before the element with index j.
// ROBlocks are ordered first by their slot,
// with a lexicographic sort of roots breaking ties for slots with duplicate blocks.
func (s ROBlockSlice) Less(i, j int) bool {
si, sj := s[i].Block().Slot(), s[j].Block().Slot()
// lower slot wins
if si != sj {
return si < sj
}
// break slot tie lexicographically comparing roots byte for byte
ri, rj := s[i].Root(), s[j].Root()
return bytes.Compare(ri[:], rj[:]) < 0
}
// Swap swaps the elements with indexes i and j.
func (s ROBlockSlice) Swap(i, j int) {
s[i], s[j] = s[j], s[i]
}
// Len is the number of elements in the collection.
func (s ROBlockSlice) Len() int {
return len(s)
}

View File

@@ -0,0 +1,90 @@
package blocks
import (
"sort"
"testing"
"github.com/prysmaticlabs/prysm/v4/consensus-types/primitives"
"github.com/prysmaticlabs/prysm/v4/encoding/bytesutil"
eth "github.com/prysmaticlabs/prysm/v4/proto/prysm/v1alpha1"
"github.com/prysmaticlabs/prysm/v4/testing/require"
)
func TestROBlockSorting(t *testing.T) {
one := bytesutil.ToBytes32(bytesutil.PadTo([]byte{1}, 32))
two := bytesutil.ToBytes32(bytesutil.PadTo([]byte{2}, 32))
cases := []struct {
name string
ros []ROBlock
sorted []ROBlock
}{
{
name: "1 item",
ros: []ROBlock{testROBlock(t, 1, [32]byte{})},
sorted: []ROBlock{testROBlock(t, 1, [32]byte{})},
},
{
name: "2 items, sorted",
ros: []ROBlock{testROBlock(t, 1, [32]byte{}), testROBlock(t, 2, [32]byte{})},
sorted: []ROBlock{testROBlock(t, 1, [32]byte{}), testROBlock(t, 2, [32]byte{})},
},
{
name: "2 items, reversed",
ros: []ROBlock{testROBlock(t, 2, [32]byte{}), testROBlock(t, 1, [32]byte{})},
sorted: []ROBlock{testROBlock(t, 1, [32]byte{}), testROBlock(t, 2, [32]byte{})},
},
{
name: "3 items, reversed, with tie breaker",
ros: []ROBlock{
testROBlock(t, 2, two),
testROBlock(t, 2, one),
testROBlock(t, 1, [32]byte{}),
},
sorted: []ROBlock{
testROBlock(t, 1, [32]byte{}),
testROBlock(t, 2, one),
testROBlock(t, 2, two),
},
},
{
name: "5 items, reversed, with double root tie",
ros: []ROBlock{
testROBlock(t, 0, one),
testROBlock(t, 2, two),
testROBlock(t, 2, one),
testROBlock(t, 2, two),
testROBlock(t, 2, one),
testROBlock(t, 1, [32]byte{}),
},
sorted: []ROBlock{
testROBlock(t, 0, one),
testROBlock(t, 1, [32]byte{}),
testROBlock(t, 2, one),
testROBlock(t, 2, one),
testROBlock(t, 2, two),
testROBlock(t, 2, two),
},
},
}
for _, c := range cases {
t.Run(c.name, func(t *testing.T) {
sort.Sort(ROBlockSlice(c.ros))
for i := 0; i < len(c.sorted); i++ {
require.Equal(t, c.sorted[i].Block().Slot(), c.ros[i].Block().Slot())
require.Equal(t, c.sorted[i].Root(), c.ros[i].Root())
}
})
}
}
func testROBlock(t *testing.T, slot primitives.Slot, root [32]byte) ROBlock {
b, err := NewSignedBeaconBlock(&eth.SignedBeaconBlock{Block: &eth.BeaconBlock{
Body: &eth.BeaconBlockBody{},
Slot: slot,
}})
require.NoError(t, err)
return ROBlock{
ReadOnlySignedBeaconBlock: b,
root: root,
}
}