Skip to content

Commit

Permalink
ssz: Move stateutil.SliceRoot to ssz package (#14123)
Browse files Browse the repository at this point in the history
  • Loading branch information
prestonvanloon authored Jun 20, 2024
1 parent df3a9f2 commit 305d585
Show file tree
Hide file tree
Showing 8 changed files with 15 additions and 51 deletions.
1 change: 0 additions & 1 deletion beacon-chain/state/stateutil/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
3 changes: 2 additions & 1 deletion beacon-chain/state/stateutil/historical_summaries_root.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
3 changes: 2 additions & 1 deletion beacon-chain/state/stateutil/pending_consolidations_root.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Original file line number Diff line number Diff line change
Expand Up @@ -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 change: 1 addition & 0 deletions encoding/ssz/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ go_library(
"helpers.go",
"htrutils.go",
"merkleize.go",
"slice_root.go",
],
importpath = "github.com/prysmaticlabs/prysm/v5/encoding/ssz",
visibility = ["//visibility:public"],
Expand Down
42 changes: 2 additions & 40 deletions encoding/ssz/htrutils.go
Original file line number Diff line number Diff line change
Expand Up @@ -144,51 +144,13 @@ func WithdrawalSliceRoot(withdrawals []*enginev1.Withdrawal, limit uint64) ([32]
// DepositRequestsSliceRoot computes the HTR of a slice of deposit receipts.
// The limit parameter is used as input to the bitwise merkleization algorithm.
func DepositRequestsSliceRoot(depositRequests []*enginev1.DepositRequest, limit uint64) ([32]byte, error) {
roots := make([][32]byte, len(depositRequests))
for i := 0; i < len(depositRequests); i++ {
r, err := depositRequests[i].HashTreeRoot()
if err != nil {
return [32]byte{}, err
}
roots[i] = r
}

bytesRoot, err := BitwiseMerkleize(roots, uint64(len(roots)), limit)
if err != nil {
return [32]byte{}, errors.Wrap(err, "could not compute merkleization")
}
bytesRootBuf := new(bytes.Buffer)
if err := binary.Write(bytesRootBuf, binary.LittleEndian, uint64(len(depositRequests))); err != nil {
return [32]byte{}, errors.Wrap(err, "could not marshal length")
}
bytesRootBufRoot := make([]byte, 32)
copy(bytesRootBufRoot, bytesRootBuf.Bytes())
return MixInLength(bytesRoot, bytesRootBufRoot), nil
return SliceRoot(depositRequests, limit)
}

// WithdrawalRequestsSliceRoot computes the HTR of a slice of withdrawal requests from the EL.
// The limit parameter is used as input to the bitwise merkleization algorithm.
func WithdrawalRequestsSliceRoot(withdrawalRequests []*enginev1.WithdrawalRequest, limit uint64) ([32]byte, error) {
roots := make([][32]byte, len(withdrawalRequests))
for i := 0; i < len(withdrawalRequests); i++ {
r, err := withdrawalRequests[i].HashTreeRoot()
if err != nil {
return [32]byte{}, err
}
roots[i] = r
}

bytesRoot, err := BitwiseMerkleize(roots, uint64(len(roots)), limit)
if err != nil {
return [32]byte{}, errors.Wrap(err, "could not compute merkleization")
}
bytesRootBuf := new(bytes.Buffer)
if err := binary.Write(bytesRootBuf, binary.LittleEndian, uint64(len(withdrawalRequests))); err != nil {
return [32]byte{}, errors.Wrap(err, "could not marshal length")
}
bytesRootBufRoot := make([]byte, 32)
copy(bytesRootBufRoot, bytesRootBuf.Bytes())
return MixInLength(bytesRoot, bytesRootBufRoot), nil
return SliceRoot(withdrawalRequests, limit)
}

// ByteSliceRoot is a helper func to merkleize an arbitrary List[Byte, N]
Expand Down
Original file line number Diff line number Diff line change
@@ -1,16 +1,15 @@
package stateutil
package ssz

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) {
func SliceRoot[T 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)
Expand All @@ -25,7 +24,7 @@ func SliceRoot[T ssz.Hashable](slice []T, limit uint64) ([32]byte, error) {
roots[i] = r
}

sliceRoot, err := ssz.BitwiseMerkleize(roots, uint64(len(roots)), limit)
sliceRoot, err := BitwiseMerkleize(roots, uint64(len(roots)), limit)
if err != nil {
return [32]byte{}, errors.Wrap(err, "could not slice merkleization")
}
Expand All @@ -36,6 +35,5 @@ func SliceRoot[T ssz.Hashable](slice []T, limit uint64) ([32]byte, error) {
// 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
return MixInLength(sliceRoot, sliceLenRoot), nil
}

0 comments on commit 305d585

Please sign in to comment.