From ce52cfac5824dc7c4589597ffa64403043717f7b Mon Sep 17 00:00:00 2001 From: avalonche Date: Sat, 3 Aug 2024 09:43:32 +1000 Subject: [PATCH] Add consolidation request util functions --- util/electra/consolidation_requests.go | 21 ++++ util/electra/consolidation_requests_ssz.go | 121 +++++++++++++++++++++ 2 files changed, 142 insertions(+) create mode 100644 util/electra/consolidation_requests.go create mode 100644 util/electra/consolidation_requests_ssz.go diff --git a/util/electra/consolidation_requests.go b/util/electra/consolidation_requests.go new file mode 100644 index 00000000..8a262615 --- /dev/null +++ b/util/electra/consolidation_requests.go @@ -0,0 +1,21 @@ +// Copyright © 2024 Attestant Limited. +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package electra + +import "github.com/attestantio/go-eth2-client/spec/electra" + +// ConsolidationRequests provides information about consolidation requests. +type ConsolidationRequests struct { + ConsolidationRequests []*electra.ConsolidationRequest `ssz-max:"1"` +} diff --git a/util/electra/consolidation_requests_ssz.go b/util/electra/consolidation_requests_ssz.go new file mode 100644 index 00000000..0cd6f8fc --- /dev/null +++ b/util/electra/consolidation_requests_ssz.go @@ -0,0 +1,121 @@ +// Code generated by fastssz. DO NOT EDIT. +// Hash: 475b7bf27ca1ddd375d02f9e72c4d80172e54374d2ff30227bfcb636c73dda43 +// Version: 0.1.3 +package electra + +import ( + "github.com/attestantio/go-eth2-client/spec/electra" + ssz "github.com/ferranbt/fastssz" +) + +// MarshalSSZ ssz marshals the ConsolidationRequests object +func (c *ConsolidationRequests) MarshalSSZ() ([]byte, error) { + return ssz.MarshalSSZ(c) +} + +// MarshalSSZTo ssz marshals the ConsolidationRequests object to a target array +func (c *ConsolidationRequests) MarshalSSZTo(buf []byte) (dst []byte, err error) { + dst = buf + offset := int(4) + + // Offset (0) 'ConsolidationRequests' + dst = ssz.WriteOffset(dst, offset) + offset += len(c.ConsolidationRequests) * 116 + + // Field (0) 'ConsolidationRequests' + if size := len(c.ConsolidationRequests); size > 1 { + err = ssz.ErrListTooBigFn("ConsolidationRequests.ConsolidationRequests", size, 1) + return + } + for ii := 0; ii < len(c.ConsolidationRequests); ii++ { + if dst, err = c.ConsolidationRequests[ii].MarshalSSZTo(dst); err != nil { + return + } + } + + return +} + +// UnmarshalSSZ ssz unmarshals the ConsolidationRequests object +func (c *ConsolidationRequests) UnmarshalSSZ(buf []byte) error { + var err error + size := uint64(len(buf)) + if size < 4 { + return ssz.ErrSize + } + + tail := buf + var o0 uint64 + + // Offset (0) 'ConsolidationRequests' + if o0 = ssz.ReadOffset(buf[0:4]); o0 > size { + return ssz.ErrOffset + } + + if o0 < 4 { + return ssz.ErrInvalidVariableOffset + } + + // Field (0) 'ConsolidationRequests' + { + buf = tail[o0:] + num, err := ssz.DivideInt2(len(buf), 116, 1) + if err != nil { + return err + } + c.ConsolidationRequests = make([]*electra.ConsolidationRequest, num) + for ii := 0; ii < num; ii++ { + if c.ConsolidationRequests[ii] == nil { + c.ConsolidationRequests[ii] = new(electra.ConsolidationRequest) + } + if err = c.ConsolidationRequests[ii].UnmarshalSSZ(buf[ii*116 : (ii+1)*116]); err != nil { + return err + } + } + } + return err +} + +// SizeSSZ returns the ssz encoded size in bytes for the ConsolidationRequests object +func (c *ConsolidationRequests) SizeSSZ() (size int) { + size = 4 + + // Field (0) 'ConsolidationRequests' + size += len(c.ConsolidationRequests) * 116 + + return +} + +// HashTreeRoot ssz hashes the ConsolidationRequests object +func (c *ConsolidationRequests) HashTreeRoot() ([32]byte, error) { + return ssz.HashWithDefaultHasher(c) +} + +// HashTreeRootWith ssz hashes the ConsolidationRequests object with a hasher +func (c *ConsolidationRequests) HashTreeRootWith(hh ssz.HashWalker) (err error) { + indx := hh.Index() + + // Field (0) 'ConsolidationRequests' + { + subIndx := hh.Index() + num := uint64(len(c.ConsolidationRequests)) + if num > 1 { + err = ssz.ErrIncorrectListSize + return + } + for _, elem := range c.ConsolidationRequests { + if err = elem.HashTreeRootWith(hh); err != nil { + return + } + } + hh.MerkleizeWithMixin(subIndx, num, 1) + } + + hh.Merkleize(indx) + return +} + +// GetTree ssz hashes the ConsolidationRequests object +func (c *ConsolidationRequests) GetTree() (*ssz.Node, error) { + return ssz.ProofTree(c) +}