mirror of
https://github.com/OffchainLabs/prysm.git
synced 2026-01-08 23:18:15 -05:00
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:
@@ -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 = [
|
||||
|
||||
75
consensus-types/blocks/roblock.go
Normal file
75
consensus-types/blocks/roblock.go
Normal 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)
|
||||
}
|
||||
90
consensus-types/blocks/roblock_test.go
Normal file
90
consensus-types/blocks/roblock_test.go
Normal 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(ð.SignedBeaconBlock{Block: ð.BeaconBlock{
|
||||
Body: ð.BeaconBlockBody{},
|
||||
Slot: slot,
|
||||
}})
|
||||
require.NoError(t, err)
|
||||
return ROBlock{
|
||||
ReadOnlySignedBeaconBlock: b,
|
||||
root: root,
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user