mirror of
https://github.com/OffchainLabs/prysm.git
synced 2026-01-09 13:28:01 -05:00
ssz: Move stateutil.SliceRoot to ssz package (#14123)
This commit is contained in:
@@ -16,7 +16,6 @@ go_library(
|
||||
"pending_consolidations_root.go",
|
||||
"pending_partial_withdrawals_root.go",
|
||||
"reference.go",
|
||||
"slice_root.go",
|
||||
"sync_committee.root.go",
|
||||
"trie_helpers.go",
|
||||
"unrealized_justification.go",
|
||||
|
||||
@@ -2,9 +2,10 @@ package stateutil
|
||||
|
||||
import (
|
||||
fieldparams "github.com/prysmaticlabs/prysm/v5/config/fieldparams"
|
||||
"github.com/prysmaticlabs/prysm/v5/encoding/ssz"
|
||||
ethpb "github.com/prysmaticlabs/prysm/v5/proto/prysm/v1alpha1"
|
||||
)
|
||||
|
||||
func HistoricalSummariesRoot(summaries []*ethpb.HistoricalSummary) ([32]byte, error) {
|
||||
return SliceRoot(summaries, fieldparams.HistoricalRootsLength)
|
||||
return ssz.SliceRoot(summaries, fieldparams.HistoricalRootsLength)
|
||||
}
|
||||
|
||||
@@ -2,9 +2,10 @@ package stateutil
|
||||
|
||||
import (
|
||||
fieldparams "github.com/prysmaticlabs/prysm/v5/config/fieldparams"
|
||||
"github.com/prysmaticlabs/prysm/v5/encoding/ssz"
|
||||
ethpb "github.com/prysmaticlabs/prysm/v5/proto/prysm/v1alpha1"
|
||||
)
|
||||
|
||||
func PendingBalanceDepositsRoot(slice []*ethpb.PendingBalanceDeposit) ([32]byte, error) {
|
||||
return SliceRoot(slice, fieldparams.PendingBalanceDepositsLimit)
|
||||
return ssz.SliceRoot(slice, fieldparams.PendingBalanceDepositsLimit)
|
||||
}
|
||||
|
||||
@@ -2,9 +2,10 @@ package stateutil
|
||||
|
||||
import (
|
||||
fieldparams "github.com/prysmaticlabs/prysm/v5/config/fieldparams"
|
||||
"github.com/prysmaticlabs/prysm/v5/encoding/ssz"
|
||||
ethpb "github.com/prysmaticlabs/prysm/v5/proto/prysm/v1alpha1"
|
||||
)
|
||||
|
||||
func PendingConsolidationsRoot(slice []*ethpb.PendingConsolidation) ([32]byte, error) {
|
||||
return SliceRoot(slice, fieldparams.PendingConsolidationsLimit)
|
||||
return ssz.SliceRoot(slice, fieldparams.PendingConsolidationsLimit)
|
||||
}
|
||||
|
||||
@@ -2,9 +2,10 @@ package stateutil
|
||||
|
||||
import (
|
||||
fieldparams "github.com/prysmaticlabs/prysm/v5/config/fieldparams"
|
||||
"github.com/prysmaticlabs/prysm/v5/encoding/ssz"
|
||||
ethpb "github.com/prysmaticlabs/prysm/v5/proto/prysm/v1alpha1"
|
||||
)
|
||||
|
||||
func PendingPartialWithdrawalsRoot(slice []*ethpb.PendingPartialWithdrawal) ([32]byte, error) {
|
||||
return SliceRoot(slice, fieldparams.PendingPartialWithdrawalsLimit)
|
||||
return ssz.SliceRoot(slice, fieldparams.PendingPartialWithdrawalsLimit)
|
||||
}
|
||||
|
||||
@@ -1,41 +0,0 @@
|
||||
package stateutil
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/binary"
|
||||
"fmt"
|
||||
|
||||
"github.com/pkg/errors"
|
||||
"github.com/prysmaticlabs/prysm/v5/encoding/ssz"
|
||||
)
|
||||
|
||||
// SliceRoot computes the root of a slice of hashable objects.
|
||||
func SliceRoot[T ssz.Hashable](slice []T, limit uint64) ([32]byte, error) {
|
||||
max := limit
|
||||
if uint64(len(slice)) > max {
|
||||
return [32]byte{}, fmt.Errorf("slice exceeds max length %d", max)
|
||||
}
|
||||
|
||||
roots := make([][32]byte, len(slice))
|
||||
for i := 0; i < len(slice); i++ {
|
||||
r, err := slice[i].HashTreeRoot()
|
||||
if err != nil {
|
||||
return [32]byte{}, errors.Wrap(err, "could not merkleize object")
|
||||
}
|
||||
roots[i] = r
|
||||
}
|
||||
|
||||
sliceRoot, err := ssz.BitwiseMerkleize(roots, uint64(len(roots)), limit)
|
||||
if err != nil {
|
||||
return [32]byte{}, errors.Wrap(err, "could not slice merkleization")
|
||||
}
|
||||
sliceLenBuf := new(bytes.Buffer)
|
||||
if err := binary.Write(sliceLenBuf, binary.LittleEndian, uint64(len(slice))); err != nil {
|
||||
return [32]byte{}, errors.Wrap(err, "could not marshal slice length")
|
||||
}
|
||||
// We need to mix in the length of the slice.
|
||||
sliceLenRoot := make([]byte, 32)
|
||||
copy(sliceLenRoot, sliceLenBuf.Bytes())
|
||||
res := ssz.MixInLength(sliceRoot, sliceLenRoot)
|
||||
return res, nil
|
||||
}
|
||||
Reference in New Issue
Block a user