Skip to content

Commit

Permalink
feat(ssz): Remove error from HashTreeRoot() (#1830)
Browse files Browse the repository at this point in the history
  • Loading branch information
itsdevbear authored Jul 31, 2024
1 parent 78f08cc commit 90772c9
Show file tree
Hide file tree
Showing 74 changed files with 247 additions and 515 deletions.
12 changes: 2 additions & 10 deletions mod/beacon/blockchain/execution_engine.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,16 +71,8 @@ func (s *Service[
return
}

prevBlockRoot, err := blk.HashTreeRoot()
if err != nil {
s.logger.Error(
"failed to get block root in non-optimistic payload",
"error", err,
)
return
}

if _, err = s.lb.RequestPayloadAsync(
prevBlockRoot := blk.HashTreeRoot()
if _, err := s.lb.RequestPayloadAsync(
ctx,
stCopy,
blk.GetSlot()+1,
Expand Down
34 changes: 8 additions & 26 deletions mod/beacon/blockchain/payload.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ import (
"context"
"time"

"github.com/berachain/beacon-kit/mod/primitives/pkg/common"
"github.com/berachain/beacon-kit/mod/primitives/pkg/math"
)

Expand Down Expand Up @@ -87,10 +86,8 @@ func (s *Service[
st BeaconStateT,
) error {
var (
prevStateRoot common.Root
prevBlockRoot common.Root
lph ExecutionPayloadHeaderT
slot math.Slot
lph ExecutionPayloadHeaderT
slot math.Slot
)

s.logger.Info("Rebuilding payload for rejected block ⏳ ")
Expand All @@ -109,16 +106,8 @@ func (s *Service[
return err
}

prevStateRoot, err = st.HashTreeRoot()
if err != nil {
return err
}

latestHeader.SetStateRoot(prevStateRoot)
prevBlockRoot, err = latestHeader.HashTreeRoot()
if err != nil {
return err
}
// Set the previous state root on the header.
latestHeader.SetStateRoot(st.HashTreeRoot())

// We need to get the *last* finalized execution payload, thus
// the BeaconState that was passed in must be `unmodified`.
Expand All @@ -140,7 +129,7 @@ func (s *Service[
uint64((lph.GetTimestamp()+1)),
),
// We set the parent root to the previous block root.
prevBlockRoot,
latestHeader.HashTreeRoot(),
// We set the head of our chain to the previous finalized block.
lph.GetBlockHash(),
// We can say that the payload from the previous block is *finalized*,
Expand Down Expand Up @@ -190,23 +179,16 @@ func (s *Service[
"next_slot", slot.Base10(),
)

// We know that this block was properly formed so we can
// calculate the block hash easily.
blkRoot, err := blk.HashTreeRoot()
if err != nil {
return err
}

// We process the slot to update any RANDAO values.
if _, err = s.sp.ProcessSlots(
if _, err := s.sp.ProcessSlots(
st, slot,
); err != nil {
return err
}

// We then trigger a request for the next payload.
payload := blk.GetBody().GetExecutionPayload()
if _, err = s.lb.RequestPayloadAsync(
if _, err := s.lb.RequestPayloadAsync(
ctx, st,
slot,
// TODO: this is hood as fuck.
Expand All @@ -217,7 +199,7 @@ func (s *Service[
),
// The previous block root is simply the root of the block we just
// processed.
blkRoot,
blk.HashTreeRoot(),
// We set the head of our chain to the block we just processed.
payload.GetBlockHash(),
// We can say that the payload from the previous block is *finalized*,
Expand Down
2 changes: 1 addition & 1 deletion mod/beacon/blockchain/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ type ReadOnlyBeaconState[
// GetSlot retrieves the current slot of the beacon state.
GetSlot() (math.Slot, error)
// HashTreeRoot returns the hash tree root of the beacon state.
HashTreeRoot() (common.Root, error)
HashTreeRoot() common.Root
}

// StateProcessor defines the interface for processing various state transitions
Expand Down
8 changes: 2 additions & 6 deletions mod/beacon/validator/block_builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -188,18 +188,14 @@ func (s *Service[
return crypto.BLSSignature{}, err
}

signingRoot, err := forkData.New(
signingRoot := forkData.New(
version.FromUint32[common.Version](
s.chainSpec.ActiveForkVersionForEpoch(epoch),
), genesisValidatorsRoot,
).ComputeRandaoSigningRoot(
s.chainSpec.DomainTypeRandao(),
epoch,
)

if err != nil {
return crypto.BLSSignature{}, err
}
return s.signer.Sign(signingRoot[:])
}

Expand Down Expand Up @@ -383,5 +379,5 @@ func (s *Service[
return common.Root{}, err
}

return st.HashTreeRoot()
return st.HashTreeRoot(), nil
}
4 changes: 2 additions & 2 deletions mod/beacon/validator/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ type BeaconState[ExecutionPayloadHeaderT any] interface {
// GetSlot returns the current slot of the beacon state.
GetSlot() (math.Slot, error)
// HashTreeRoot returns the hash tree root of the beacon state.
HashTreeRoot() (common.Root, error)
HashTreeRoot() common.Root
// ValidatorIndexByPubkey returns the validator index by public key.
ValidatorIndexByPubkey(crypto.BLSPubkey) (math.ValidatorIndex, error)
// GetEth1DepositIndex returns the latest deposit index from the beacon
Expand Down Expand Up @@ -185,7 +185,7 @@ type ForkData[T any] interface {
ComputeRandaoSigningRoot(
common.DomainType,
math.Epoch,
) (common.Root, error)
) common.Root
}

// PayloadBuilder represents a service that is responsible for
Expand Down
4 changes: 2 additions & 2 deletions mod/consensus-types/pkg/types/attestation_data.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,8 +87,8 @@ func (a *AttestationData) DefineSSZ(codec *ssz.Codec) {
}

// HashTreeRoot computes the SSZ hash tree root of the AttestationData object.
func (a *AttestationData) HashTreeRoot() (common.Root, error) {
return ssz.HashSequential(a), nil
func (a *AttestationData) HashTreeRoot() common.Root {
return ssz.HashSequential(a)
}

// MarshalSSZ marshals the AttestationData object to SSZ format.
Expand Down
3 changes: 1 addition & 2 deletions mod/consensus-types/pkg/types/attestation_data_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,8 +108,7 @@ func TestAttestationData_GetTree(t *testing.T) {
require.NoError(t, err)
require.NotNil(t, tree)

expectedRoot, err := data.HashTreeRoot()
require.NoError(t, err)
expectedRoot := data.HashTreeRoot()

// Compare the tree root with the expected root
actualRoot := tree.Hash()
Expand Down
11 changes: 3 additions & 8 deletions mod/consensus-types/pkg/types/block.go
Original file line number Diff line number Diff line change
Expand Up @@ -135,8 +135,8 @@ func (b *BeaconBlock) UnmarshalSSZ(buf []byte) error {
}

// HashTreeRoot computes the Merkleization of the BeaconBlock object.
func (b *BeaconBlock) HashTreeRoot() (common.Root, error) {
return ssz.HashConcurrent(b), nil
func (b *BeaconBlock) HashTreeRoot() common.Root {
return ssz.HashConcurrent(b)
}

/* -------------------------------------------------------------------------- */
Expand Down Expand Up @@ -226,16 +226,11 @@ func (b *BeaconBlock) GetBody() *BeaconBlockBody {

// GetHeader builds a BeaconBlockHeader from the BeaconBlock.
func (b *BeaconBlock) GetHeader() *BeaconBlockHeader {
bodyRoot, err := b.GetBody().HashTreeRoot()
if err != nil {
return nil
}

return &BeaconBlockHeader{
Slot: b.Slot,
ProposerIndex: b.ProposerIndex,
ParentBlockRoot: b.ParentRoot,
StateRoot: b.StateRoot,
BodyRoot: bodyRoot,
BodyRoot: b.GetBody().HashTreeRoot(),
}
}
3 changes: 1 addition & 2 deletions mod/consensus-types/pkg/types/block_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -139,8 +139,7 @@ func TestBeaconBlock_MarshalUnmarshalSSZ(t *testing.T) {

func TestBeaconBlock_HashTreeRoot(t *testing.T) {
block := generateValidBeaconBlock()
hashRoot, err := block.HashTreeRoot()
require.NoError(t, err)
hashRoot := block.HashTreeRoot()
require.NotNil(t, hashRoot)
}

Expand Down
30 changes: 9 additions & 21 deletions mod/consensus-types/pkg/types/body.go
Original file line number Diff line number Diff line change
Expand Up @@ -147,8 +147,8 @@ func (b *BeaconBlockBody) UnmarshalSSZ(buf []byte) error {
}

// HashTreeRoot returns the SSZ hash tree root of the BeaconBlockBody.
func (b *BeaconBlockBody) HashTreeRoot() (common.Root, error) {
return ssz.HashConcurrent(b), nil
func (b *BeaconBlockBody) HashTreeRoot() common.Root {
return ssz.HashConcurrent(b)
}

/* -------------------------------------------------------------------------- */
Expand Down Expand Up @@ -291,26 +291,14 @@ func (b *BeaconBlockBody) SetSlashingInfo(_ []*SlashingInfo) {
}

// GetTopLevelRoots returns the top-level roots of the BeaconBlockBody.
func (b *BeaconBlockBody) GetTopLevelRoots() ([][32]byte, error) {
var (
err error
layer = make([][32]byte, BodyLengthDeneb)
)

layer[0], err = b.GetRandaoReveal().HashTreeRoot()
if err != nil {
return nil, err
}

layer[1], err = b.Eth1Data.HashTreeRoot()
if err != nil {
return nil, err
func (b *BeaconBlockBody) GetTopLevelRoots() []common.Root {
return []common.Root{
b.GetRandaoReveal().HashTreeRoot(),
b.Eth1Data.HashTreeRoot(),
b.GetGraffiti().HashTreeRoot(),
Deposits(b.GetDeposits()).HashTreeRoot(),
b.GetExecutionPayload().HashTreeRoot(),
}

layer[2] = b.GetGraffiti()
layer[3] = Deposits(b.GetDeposits()).HashTreeRoot()
layer[4], err = b.GetExecutionPayload().HashTreeRoot()
return layer, err
}

// Length returns the number of fields in the BeaconBlockBody struct.
Expand Down
3 changes: 1 addition & 2 deletions mod/consensus-types/pkg/types/body_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -147,8 +147,7 @@ func TestBeaconBlockBody_MarshalSSZTo(t *testing.T) {

func TestBeaconBlockBody_GetTopLevelRoots(t *testing.T) {
body := generateBeaconBlockBody()
roots, err := body.GetTopLevelRoots()
require.NoError(t, err)
roots := body.GetTopLevelRoots()
require.NotNil(t, roots)
}

Expand Down
4 changes: 2 additions & 2 deletions mod/consensus-types/pkg/types/deposit.go
Original file line number Diff line number Diff line change
Expand Up @@ -137,8 +137,8 @@ func (d *Deposit) SizeSSZ() uint32 {
}

// HashTreeRoot computes the Merkleization of the Deposit object.
func (d *Deposit) HashTreeRoot() (common.Root, error) {
return ssz.HashSequential(d), nil
func (d *Deposit) HashTreeRoot() common.Root {
return ssz.HashSequential(d)
}

/* -------------------------------------------------------------------------- */
Expand Down
31 changes: 7 additions & 24 deletions mod/consensus-types/pkg/types/deposit_message.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,22 +51,13 @@ func CreateAndSignDepositMessage(
credentials WithdrawalCredentials,
amount math.Gwei,
) (*DepositMessage, crypto.BLSSignature, error) {
domain, err := forkData.ComputeDomain(domainType)
if err != nil {
return nil, crypto.BLSSignature{}, err
}

domain := forkData.ComputeDomain(domainType)
depositMessage := &DepositMessage{
Pubkey: signer.PublicKey(),
Credentials: credentials,
Amount: amount,
}

signingRoot, err := ComputeSigningRoot(depositMessage, domain)
if err != nil {
return nil, crypto.BLSSignature{}, err
}

signingRoot := ComputeSigningRoot(depositMessage, domain)
signature, err := signer.Sign(signingRoot[:])
if err != nil {
return nil, crypto.BLSSignature{}, err
Expand Down Expand Up @@ -107,8 +98,8 @@ func (dm *DepositMessage) DefineSSZ(codec *ssz.Codec) {
}

// HashTreeRoot computes the SSZ hash tree root of the DepositMessage object.
func (dm *DepositMessage) HashTreeRoot() (common.Root, error) {
return ssz.HashSequential(dm), nil
func (dm *DepositMessage) HashTreeRoot() common.Root {
return ssz.HashSequential(dm)
}

// MarshalSSZTo marshals the DepositMessage object to SSZ format into the
Expand Down Expand Up @@ -139,17 +130,9 @@ func (dm *DepositMessage) VerifyCreateValidator(
pubkey crypto.BLSPubkey, message []byte, signature crypto.BLSSignature,
) error,
) error {
domain, err := forkData.ComputeDomain(domainType)
if err != nil {
return err
}

signingRoot, err := ComputeSigningRoot(dm, domain)
if err != nil {
return err
}

if err = signatureVerificationFn(
signingRoot := ComputeSigningRoot(
dm, forkData.ComputeDomain(domainType))
if err := signatureVerificationFn(
dm.Pubkey, signingRoot[:], signature,
); err != nil {
return errors.Join(err, ErrDepositMessage)
Expand Down
6 changes: 3 additions & 3 deletions mod/consensus-types/pkg/types/deposit_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,9 +86,9 @@ func TestDeposit_MarshalSSZTo(t *testing.T) {

func TestDeposit_HashTreeRoot(t *testing.T) {
deposit := generateValidDeposit()

_, err := deposit.HashTreeRoot()
require.NoError(t, err)
require.NotPanics(t, func() {
_ = deposit.HashTreeRoot()
})
}

func TestDeposit_SizeSSZ(t *testing.T) {
Expand Down
4 changes: 2 additions & 2 deletions mod/consensus-types/pkg/types/eth1data.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,8 +87,8 @@ func (e *Eth1Data) DefineSSZ(codec *ssz.Codec) {
}

// HashTreeRoot computes the SSZ hash tree root of the Eth1Data object.
func (e *Eth1Data) HashTreeRoot() (common.Root, error) {
return ssz.HashSequential(e), nil
func (e *Eth1Data) HashTreeRoot() common.Root {
return ssz.HashSequential(e)
}

// MarshalSSZ marshals the Eth1Data object to SSZ format.
Expand Down
5 changes: 3 additions & 2 deletions mod/consensus-types/pkg/types/eth1data_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,8 +78,9 @@ func TestEth1Data_HashTreeRoot(t *testing.T) {
BlockHash: gethprimitives.ExecutionHash{},
}

_, err := eth1Data.HashTreeRoot()
require.NoError(t, err)
require.NotPanics(t, func() {
_ = eth1Data.HashTreeRoot()
})
}

func TestEth1Data_GetTree(t *testing.T) {
Expand Down
4 changes: 2 additions & 2 deletions mod/consensus-types/pkg/types/fork.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,8 +100,8 @@ func (f *Fork) UnmarshalSSZ(buf []byte) error {
}

// HashTreeRoot computes the SSZ hash tree root of the Fork object.
func (f *Fork) HashTreeRoot() (common.Root, error) {
return ssz.HashSequential(f), nil
func (f *Fork) HashTreeRoot() common.Root {
return ssz.HashSequential(f)
}

/* -------------------------------------------------------------------------- */
Expand Down
Loading

0 comments on commit 90772c9

Please sign in to comment.