Skip to content

Commit

Permalink
Add intermediary structure for withdraws encoding
Browse files Browse the repository at this point in the history
  • Loading branch information
lukanus committed Mar 15, 2023
1 parent 3e4970f commit 5084896
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 9 deletions.
4 changes: 2 additions & 2 deletions pkg/beacon/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -300,8 +300,8 @@ func (m *Manager) updatedExpectedWithdrawals(slot structs.Slot, state State, cli
}
return
}

root, err := withdrawals.Data.Withdrawals.HashTreeRoot()
hW := structs.HashWithdrawals{Withdrawals: withdrawals.Data.Withdrawals}
root, err := hW.HashTreeRoot()
if err != nil {
logger.WithError(err).Warn("failed to compute withdrawals root")
return
Expand Down
3 changes: 2 additions & 1 deletion pkg/relay/submit.go
Original file line number Diff line number Diff line change
Expand Up @@ -284,7 +284,8 @@ func verifyWithdrawals(state State, submitBlockRequest structs.SubmitBlockReques
}

// get latest withdrawals and verify the roots match
withdrawalsRoot, err := withdrawals.HashTreeRoot()
hW := structs.HashWithdrawals{Withdrawals: withdrawals}
withdrawalsRoot, err := hW.HashTreeRoot()
if err != nil {
return root, retried, fmt.Errorf("failed to compute withdrawals root: %w", err)
}
Expand Down
3 changes: 2 additions & 1 deletion pkg/structs/forks/capella/capella.go
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,8 @@ func PayloadToPayloadHeader(p *ExecutionPayload) (*ExecutionPayloadHeader, error
var withdrawalsRoot [32]byte
w := p.EpWithdrawals
if w != nil {
withdrawalsRoot, err = p.EpWithdrawals.HashTreeRoot()
hW := structs.HashWithdrawals{Withdrawals: p.EpWithdrawals}
withdrawalsRoot, err = hW.HashTreeRoot()
if err != nil {
return nil, err
}
Expand Down
25 changes: 20 additions & 5 deletions pkg/structs/withdrawals.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,24 +8,39 @@ import (
// Withdrawal provides information about a withdrawal.
type Withdrawals []*Withdrawal

// Withdrawal provides information about a withdrawal.
type HashWithdrawals struct {
Withdrawals Withdrawals `ssz-max:"16"`
}

// SizeSSZ returns the ssz encoded size in bytes for the Withdrawals object
func (w *HashWithdrawals) SizeSSZ() (size int) {
size = 4

// Field (0) 'Withdrawals'
size += len(w.Withdrawals) * 44

return
}

// HashTreeRoot ssz hashes the Withdrawals object
func (w Withdrawals) HashTreeRoot() ([32]byte, error) {
func (w *HashWithdrawals) HashTreeRoot() ([32]byte, error) {
return ssz.HashWithDefaultHasher(w)
}

// HashTreeRootWith ssz hashes the Withdrawals object with a hasher
func (w Withdrawals) HashTreeRootWith(hh ssz.HashWalker) (err error) {
func (w *HashWithdrawals) HashTreeRootWith(hh ssz.HashWalker) (err error) {
indx := hh.Index()

// Field (0) 'Withdrawals'
{
subIndx := hh.Index()
num := uint64(len(w))
num := uint64(len(w.Withdrawals))
if num > 16 {
err = ssz.ErrIncorrectListSize
return
}
for _, elem := range w {
for _, elem := range w.Withdrawals {
if err = elem.HashTreeRootWith(hh); err != nil {
return
}
Expand All @@ -38,7 +53,7 @@ func (w Withdrawals) HashTreeRootWith(hh ssz.HashWalker) (err error) {
}

// GetTree ssz hashes the Withdrawals object
func (w Withdrawals) GetTree() (*ssz.Node, error) {
func (w *HashWithdrawals) GetTree() (*ssz.Node, error) {
return ssz.ProofTree(w)
}

Expand Down

0 comments on commit 5084896

Please sign in to comment.