diff --git a/spec/electra/beaconstate.go b/spec/electra/beaconstate.go index a9ee8af2..f7cfba04 100644 --- a/spec/electra/beaconstate.go +++ b/spec/electra/beaconstate.go @@ -60,7 +60,7 @@ type BeaconState struct { EarliestExitEpoch phase0.Epoch ConsolidationBalanceToConsume phase0.Gwei EarliestConsolidationEpoch phase0.Epoch - PendingBalanceDeposits []*PendingBalanceDeposit `ssz-max:"134217728"` + PendingDeposits []*PendingDeposit `ssz-max:"134217728"` PendingPartialWithdrawals []*PendingPartialWithdrawal `ssz-max:"134217728"` PendingConsolidations []*PendingConsolidation `ssz-max:"262144"` } diff --git a/spec/electra/beaconstate_json.go b/spec/electra/beaconstate_json.go index ffe14955..411312e7 100644 --- a/spec/electra/beaconstate_json.go +++ b/spec/electra/beaconstate_json.go @@ -65,7 +65,7 @@ type beaconStateJSON struct { EarliestExitEpoch phase0.Epoch `json:"earliest_exit_epoch"` ConsolidationBalanceToConsume phase0.Gwei `json:"consolidation_balance_to_consume"` EarliestConsolidationEpoch phase0.Epoch `json:"earliest_consolidation_epoch"` - PendingBalanceDeposits []*PendingBalanceDeposit `json:"pending_balance_deposits"` + PendingDeposits []*PendingDeposit `json:"pending_deposits"` PendingPartialWithdrawals []*PendingPartialWithdrawal `json:"pending_partial_withdrawals"` PendingConsolidations []*PendingConsolidation `json:"pending_consolidations"` } @@ -132,7 +132,7 @@ func (b *BeaconState) MarshalJSON() ([]byte, error) { EarliestExitEpoch: b.EarliestExitEpoch, ConsolidationBalanceToConsume: b.ConsolidationBalanceToConsume, EarliestConsolidationEpoch: b.EarliestConsolidationEpoch, - PendingBalanceDeposits: b.PendingBalanceDeposits, + PendingDeposits: b.PendingDeposits, PendingPartialWithdrawals: b.PendingPartialWithdrawals, PendingConsolidations: b.PendingConsolidations, }) @@ -321,12 +321,12 @@ func (b *BeaconState) UnmarshalJSON(input []byte) error { return errors.Wrap(err, "earliest_consolidation_epoch") } - if err := json.Unmarshal(raw["pending_balance_deposits"], &b.PendingBalanceDeposits); err != nil { - return errors.Wrap(err, "pending_balance_deposits") + if err := json.Unmarshal(raw["pending_deposits"], &b.PendingDeposits); err != nil { + return errors.Wrap(err, "pending_deposits") } - for i := range b.PendingBalanceDeposits { - if b.PendingBalanceDeposits[i] == nil { - return fmt.Errorf("pending balance deposits entry %d missing", i) + for i := range b.PendingDeposits { + if b.PendingDeposits[i] == nil { + return fmt.Errorf("pending deposits entry %d missing", i) } } diff --git a/spec/electra/beaconstate_ssz.go b/spec/electra/beaconstate_ssz.go index b93a61d5..a78bbcf5 100644 --- a/spec/electra/beaconstate_ssz.go +++ b/spec/electra/beaconstate_ssz.go @@ -205,7 +205,7 @@ func (b *BeaconState) MarshalSSZTo(buf []byte) (dst []byte, err error) { // Offset (34) 'PendingBalanceDeposits' dst = ssz.WriteOffset(dst, offset) - offset += len(b.PendingBalanceDeposits) * 16 + offset += len(b.PendingDeposits) * 192 // Offset (35) 'PendingPartialWithdrawals' dst = ssz.WriteOffset(dst, offset) @@ -299,12 +299,12 @@ func (b *BeaconState) MarshalSSZTo(buf []byte) (dst []byte, err error) { } // Field (34) 'PendingBalanceDeposits' - if size := len(b.PendingBalanceDeposits); size > 134217728 { + if size := len(b.PendingDeposits); size > 134217728 { err = ssz.ErrListTooBigFn("BeaconState.PendingBalanceDeposits", size, 134217728) return } - for ii := 0; ii < len(b.PendingBalanceDeposits); ii++ { - if dst, err = b.PendingBalanceDeposits[ii].MarshalSSZTo(dst); err != nil { + for ii := 0; ii < len(b.PendingDeposits); ii++ { + if dst, err = b.PendingDeposits[ii].MarshalSSZTo(dst); err != nil { return } } @@ -672,16 +672,16 @@ func (b *BeaconState) UnmarshalSSZ(buf []byte) error { // Field (34) 'PendingBalanceDeposits' { buf = tail[o34:o35] - num, err := ssz.DivideInt2(len(buf), 16, 134217728) + num, err := ssz.DivideInt2(len(buf), 192, 134217728) if err != nil { return err } - b.PendingBalanceDeposits = make([]*PendingBalanceDeposit, num) + b.PendingDeposits = make([]*PendingDeposit, num) for ii := 0; ii < num; ii++ { - if b.PendingBalanceDeposits[ii] == nil { - b.PendingBalanceDeposits[ii] = new(PendingBalanceDeposit) + if b.PendingDeposits[ii] == nil { + b.PendingDeposits[ii] = new(PendingDeposit) } - if err = b.PendingBalanceDeposits[ii].UnmarshalSSZ(buf[ii*16 : (ii+1)*16]); err != nil { + if err = b.PendingDeposits[ii].UnmarshalSSZ(buf[ii*192 : (ii+1)*192]); err != nil { return err } } @@ -760,7 +760,7 @@ func (b *BeaconState) SizeSSZ() (size int) { size += len(b.HistoricalSummaries) * 64 // Field (34) 'PendingBalanceDeposits' - size += len(b.PendingBalanceDeposits) * 16 + size += len(b.PendingDeposits) * 192 // Field (35) 'PendingPartialWithdrawals' size += len(b.PendingPartialWithdrawals) * 24 @@ -1069,12 +1069,12 @@ func (b *BeaconState) HashTreeRootWith(hh ssz.HashWalker) (err error) { // Field (34) 'PendingBalanceDeposits' { subIndx := hh.Index() - num := uint64(len(b.PendingBalanceDeposits)) + num := uint64(len(b.PendingDeposits)) if num > 134217728 { err = ssz.ErrIncorrectListSize return } - for _, elem := range b.PendingBalanceDeposits { + for _, elem := range b.PendingDeposits { if err = elem.HashTreeRootWith(hh); err != nil { return } diff --git a/spec/electra/beaconstate_yaml.go b/spec/electra/beaconstate_yaml.go index f159884a..d035470c 100644 --- a/spec/electra/beaconstate_yaml.go +++ b/spec/electra/beaconstate_yaml.go @@ -62,7 +62,7 @@ type beaconStateYAML struct { EarliestExitEpoch phase0.Epoch `yaml:"earliest_exit_epoch"` ConsolidationBalanceToConsume phase0.Gwei `yaml:"consolidation_balance_to_consume"` EarliestConsolidationEpoch phase0.Epoch `yaml:"earliest_consolidation_epoch"` - PendingBalanceDeposits []*PendingBalanceDeposit `yaml:"pending_balance_deposits"` + PendingDeposits []*PendingDeposit `yaml:"pending_deposits"` PendingPartialWithdrawals []*PendingPartialWithdrawal `yaml:"pending_partial_withdrawals"` PendingConsolidations []*PendingConsolidation `yaml:"pending_consolidations"` } @@ -104,7 +104,7 @@ func (b *BeaconState) MarshalYAML() ([]byte, error) { EarliestExitEpoch: b.EarliestExitEpoch, ConsolidationBalanceToConsume: b.ConsolidationBalanceToConsume, EarliestConsolidationEpoch: b.EarliestConsolidationEpoch, - PendingBalanceDeposits: b.PendingBalanceDeposits, + PendingDeposits: b.PendingDeposits, PendingPartialWithdrawals: b.PendingPartialWithdrawals, PendingConsolidations: b.PendingConsolidations, }, yaml.Flow(true)) diff --git a/spec/electra/consensusspec_test.go b/spec/electra/consensusspec_test.go index fbb76b68..7475c8de 100644 --- a/spec/electra/consensusspec_test.go +++ b/spec/electra/consensusspec_test.go @@ -148,7 +148,7 @@ func TestConsensusSpec(t *testing.T) { }, { name: "PendingBalanceDeposit", - s: &electra.PendingBalanceDeposit{}, + s: &electra.PendingDeposit{}, }, { name: "PendingConsolidation", diff --git a/spec/electra/generate.go b/spec/electra/generate.go index f7536d21..1b4600f4 100644 --- a/spec/electra/generate.go +++ b/spec/electra/generate.go @@ -15,6 +15,6 @@ package electra //nolint:revive // Need to `go install github.com/ferranbt/fastssz/sszgen@latest` for this to work. -//go:generate rm -f aggregateandproof_ssz.go attestation_ssz.go attesterslashing_ssz.go beaconblockbody_ssz.go beaconblock_ssz.go beaconstate_ssz.go consolidation_ssz.go consolidationrequest_ssz.go depositrequest_ssz.go withdrawalrequest_ssz.go executionrequests_ssz.go pendingbalancedeposit_ssz.go pendingconsolidation_ssz.go pendingpartialwithdrawal_ssz.go signedaggregateandproof_ssz.go signedbeaconblock_ssz.go -//go:generate sszgen --suffix=ssz --path . --include ../phase0,../altair,../bellatrix,../capella,../deneb --objs AggregateAndProof,Attestation,AttesterSlashing,BeaconBlockBody,BeaconBlock,BeaconState,Consolidation,ConsolidationRequest,DepositRequest,WithdrawalRequest,ExecutionRequests,PendingBalanceDeposit,PendingConsolidation,PendingPartialWithdrawal,SignedAggregateAndProof,SignedBeaconBlock -//go:generate goimports -w aggregateandproof_ssz.go attestation_ssz.go attesterslashing_ssz.go beaconblockbody_ssz.go beaconblock_ssz.go beaconstate_ssz.go consolidation_ssz.go consolidationrequest_ssz.go depositrequest_ssz.go withdrawalrequest_ssz.go executionrequests_ssz.go pendingbalancedeposit_ssz.go pendingconsolidation_ssz.go pendingpartialwithdrawal_ssz.go signedaggregateandproof_ssz.go signedbeaconblock_ssz.go +//go:generate rm -f aggregateandproof_ssz.go attestation_ssz.go attesterslashing_ssz.go beaconblockbody_ssz.go beaconblock_ssz.go beaconstate_ssz.go consolidation_ssz.go consolidationrequest_ssz.go depositrequest_ssz.go withdrawalrequest_ssz.go executionrequests_ssz.go pendingconsolidation_ssz.go pendingdeposit_ssz.go pendingpartialwithdrawal_ssz.go signedaggregateandproof_ssz.go signedbeaconblock_ssz.go +//go:generate sszgen --suffix=ssz --path . --include ../phase0,../altair,../bellatrix,../capella,../deneb --objs AggregateAndProof,Attestation,AttesterSlashing,BeaconBlockBody,BeaconBlock,BeaconState,Consolidation,ConsolidationRequest,DepositRequest,WithdrawalRequest,ExecutionRequests,PendingConsolidation,PendingDeposit,PendingPartialWithdrawal,SignedAggregateAndProof,SignedBeaconBlock +//go:generate goimports -w aggregateandproof_ssz.go attestation_ssz.go attesterslashing_ssz.go beaconblockbody_ssz.go beaconblock_ssz.go beaconstate_ssz.go consolidation_ssz.go consolidationrequest_ssz.go depositrequest_ssz.go withdrawalrequest_ssz.go executionrequests_ssz.go pendingconsolidation_ssz.go pendingdeposit_ssz.go pendingpartialwithdrawal_ssz.go signedaggregateandproof_ssz.go signedbeaconblock_ssz.go diff --git a/spec/electra/pendingbalancedeposit_json.go b/spec/electra/pendingbalancedeposit_json.go deleted file mode 100644 index d8304ac3..00000000 --- a/spec/electra/pendingbalancedeposit_json.go +++ /dev/null @@ -1,53 +0,0 @@ -// 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 ( - "encoding/json" - - "github.com/attestantio/go-eth2-client/codecs" - "github.com/attestantio/go-eth2-client/spec/phase0" - "github.com/pkg/errors" -) - -// pendingBalanceDepositJSON is the spec representation of the struct. -type pendingBalanceDepositJSON struct { - Index phase0.ValidatorIndex `json:"index"` - Amount phase0.Gwei `json:"amount"` -} - -// MarshalJSON implements json.Marshaler. -func (p *PendingBalanceDeposit) MarshalJSON() ([]byte, error) { - return json.Marshal(&pendingBalanceDepositJSON{ - Index: p.Index, - Amount: p.Amount, - }) -} - -// UnmarshalJSON implements json.Unmarshaler. -func (p *PendingBalanceDeposit) UnmarshalJSON(input []byte) error { - raw, err := codecs.RawJSON(&pendingBalanceDepositJSON{}, input) - if err != nil { - return err - } - - if err := p.Index.UnmarshalJSON(raw["index"]); err != nil { - return errors.Wrap(err, "index") - } - if err := p.Amount.UnmarshalJSON(raw["amount"]); err != nil { - return errors.Wrap(err, "amount") - } - - return nil -} diff --git a/spec/electra/pendingbalancedeposit_ssz.go b/spec/electra/pendingbalancedeposit_ssz.go deleted file mode 100644 index 27983f0e..00000000 --- a/spec/electra/pendingbalancedeposit_ssz.go +++ /dev/null @@ -1,74 +0,0 @@ -// Code generated by fastssz. DO NOT EDIT. -// Hash: 17d4c9180818d70e873edf284079b326d586a16686d17c7c974a8a2fd19ec3e9 -// Version: 0.1.3 -package electra - -import ( - "github.com/attestantio/go-eth2-client/spec/phase0" - ssz "github.com/ferranbt/fastssz" -) - -// MarshalSSZ ssz marshals the PendingBalanceDeposit object -func (p *PendingBalanceDeposit) MarshalSSZ() ([]byte, error) { - return ssz.MarshalSSZ(p) -} - -// MarshalSSZTo ssz marshals the PendingBalanceDeposit object to a target array -func (p *PendingBalanceDeposit) MarshalSSZTo(buf []byte) (dst []byte, err error) { - dst = buf - - // Field (0) 'Index' - dst = ssz.MarshalUint64(dst, uint64(p.Index)) - - // Field (1) 'Amount' - dst = ssz.MarshalUint64(dst, uint64(p.Amount)) - - return -} - -// UnmarshalSSZ ssz unmarshals the PendingBalanceDeposit object -func (p *PendingBalanceDeposit) UnmarshalSSZ(buf []byte) error { - var err error - size := uint64(len(buf)) - if size != 16 { - return ssz.ErrSize - } - - // Field (0) 'Index' - p.Index = phase0.ValidatorIndex(ssz.UnmarshallUint64(buf[0:8])) - - // Field (1) 'Amount' - p.Amount = phase0.Gwei(ssz.UnmarshallUint64(buf[8:16])) - - return err -} - -// SizeSSZ returns the ssz encoded size in bytes for the PendingBalanceDeposit object -func (p *PendingBalanceDeposit) SizeSSZ() (size int) { - size = 16 - return -} - -// HashTreeRoot ssz hashes the PendingBalanceDeposit object -func (p *PendingBalanceDeposit) HashTreeRoot() ([32]byte, error) { - return ssz.HashWithDefaultHasher(p) -} - -// HashTreeRootWith ssz hashes the PendingBalanceDeposit object with a hasher -func (p *PendingBalanceDeposit) HashTreeRootWith(hh ssz.HashWalker) (err error) { - indx := hh.Index() - - // Field (0) 'Index' - hh.PutUint64(uint64(p.Index)) - - // Field (1) 'Amount' - hh.PutUint64(uint64(p.Amount)) - - hh.Merkleize(indx) - return -} - -// GetTree ssz hashes the PendingBalanceDeposit object -func (p *PendingBalanceDeposit) GetTree() (*ssz.Node, error) { - return ssz.ProofTree(p) -} diff --git a/spec/electra/pendingconsolidation_ssz.go b/spec/electra/pendingconsolidation_ssz.go index cabc71d9..a5a3d0c0 100644 --- a/spec/electra/pendingconsolidation_ssz.go +++ b/spec/electra/pendingconsolidation_ssz.go @@ -1,5 +1,5 @@ // Code generated by fastssz. DO NOT EDIT. -// Hash: 17d4c9180818d70e873edf284079b326d586a16686d17c7c974a8a2fd19ec3e9 +// Hash: d32594f3eb75aa0af63aae654462a5669f04a1b77e91e06bfcbd28f8599321f7 // Version: 0.1.3 package electra diff --git a/spec/electra/pendingbalancedeposit.go b/spec/electra/pendingdeposit.go similarity index 70% rename from spec/electra/pendingbalancedeposit.go rename to spec/electra/pendingdeposit.go index 10a5f228..8d25bdab 100644 --- a/spec/electra/pendingbalancedeposit.go +++ b/spec/electra/pendingdeposit.go @@ -20,14 +20,17 @@ import ( "github.com/goccy/go-yaml" ) -// PendingBalanceDeposit represents a pending balance deposit. -type PendingBalanceDeposit struct { - Index phase0.ValidatorIndex - Amount phase0.Gwei +// PendingDeposit represents a pending balance deposit. +type PendingDeposit struct { + Pubkey phase0.BLSPubKey `ssz-size:"48"` + WithdrawalCredentials []byte `ssz-size:"32"` + Amount phase0.Gwei + Signature phase0.BLSSignature `ssz-size:"96"` + Slot phase0.Slot } // String returns a string version of the structure. -func (p *PendingBalanceDeposit) String() string { +func (p *PendingDeposit) String() string { data, err := yaml.Marshal(p) if err != nil { return fmt.Sprintf("ERR: %v", err) diff --git a/spec/electra/pendingdeposit_json.go b/spec/electra/pendingdeposit_json.go new file mode 100644 index 00000000..32b4cf13 --- /dev/null +++ b/spec/electra/pendingdeposit_json.go @@ -0,0 +1,96 @@ +// 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 ( + "encoding/hex" + "encoding/json" + "fmt" + "strings" + + "github.com/attestantio/go-eth2-client/spec/phase0" + "github.com/pkg/errors" +) + +// pendingDepositJSON is the spec representation of the struct. +type pendingDepositJSON struct { + Pubkey string `json:"pubkey"` + WithdrawalCredentials string `json:"withdrawal_credentials"` + Amount phase0.Gwei `json:"amount"` + Signature string `json:"signature"` + Slot phase0.Slot `json:"slot"` +} + +// MarshalJSON implements json.Marshaler. +func (p *PendingDeposit) MarshalJSON() ([]byte, error) { + return json.Marshal(&pendingDepositJSON{ + Pubkey: fmt.Sprintf("%#x", p.Pubkey), + WithdrawalCredentials: fmt.Sprintf("%#x", p.WithdrawalCredentials), + Amount: p.Amount, + Signature: fmt.Sprintf("%#x", p.Signature), + Slot: p.Slot, + }) +} + +// UnmarshalJSON implements json.Unmarshaler. +func (p *PendingDeposit) UnmarshalJSON(input []byte) error { + var pendingDeposit pendingDepositJSON + if err := json.Unmarshal(input, &pendingDeposit); err != nil { + return errors.Wrap(err, "invalid JSON") + } + + return p.unpack(&pendingDeposit) +} + +func (p *PendingDeposit) unpack(pendingDeposit *pendingDepositJSON) error { + if pendingDeposit.Pubkey == "" { + return errors.New("public key missing") + } + pubkey, err := hex.DecodeString(strings.TrimPrefix(pendingDeposit.Pubkey, "0x")) + if err != nil { + return errors.Wrap(err, "invalid value for public key") + } + if len(pubkey) != phase0.PublicKeyLength { + return errors.New("incorrect length for public key") + } + copy(p.Pubkey[:], pubkey) + + if pendingDeposit.WithdrawalCredentials == "" { + return errors.New("withdrawal credentials missing") + } + if p.WithdrawalCredentials, err = hex.DecodeString(strings.TrimPrefix(pendingDeposit.WithdrawalCredentials, "0x")); err != nil { + return errors.Wrap(err, "invalid value for withdrawal credentials") + } + if len(p.WithdrawalCredentials) != phase0.HashLength { + return errors.New("incorrect length for withdrawal credentials") + } + + p.Amount = pendingDeposit.Amount + + if pendingDeposit.Signature == "" { + return errors.New("signature missing") + } + signature, err := hex.DecodeString(strings.TrimPrefix(pendingDeposit.Signature, "0x")) + if err != nil { + return errors.Wrap(err, "invalid value for signature") + } + if len(signature) != phase0.SignatureLength { + return errors.New("incorrect length for signature") + } + copy(p.Signature[:], signature) + + p.Slot = pendingDeposit.Slot + + return nil +} diff --git a/spec/electra/pendingdeposit_ssz.go b/spec/electra/pendingdeposit_ssz.go new file mode 100644 index 00000000..3c9318d3 --- /dev/null +++ b/spec/electra/pendingdeposit_ssz.go @@ -0,0 +1,112 @@ +// Code generated by fastssz. DO NOT EDIT. +// Hash: d32594f3eb75aa0af63aae654462a5669f04a1b77e91e06bfcbd28f8599321f7 +// Version: 0.1.3 +package electra + +import ( + "github.com/attestantio/go-eth2-client/spec/phase0" + ssz "github.com/ferranbt/fastssz" +) + +// MarshalSSZ ssz marshals the PendingDeposit object +func (p *PendingDeposit) MarshalSSZ() ([]byte, error) { + return ssz.MarshalSSZ(p) +} + +// MarshalSSZTo ssz marshals the PendingDeposit object to a target array +func (p *PendingDeposit) MarshalSSZTo(buf []byte) (dst []byte, err error) { + dst = buf + + // Field (0) 'Pubkey' + dst = append(dst, p.Pubkey[:]...) + + // Field (1) 'WithdrawalCredentials' + if size := len(p.WithdrawalCredentials); size != 32 { + err = ssz.ErrBytesLengthFn("PendingDeposit.WithdrawalCredentials", size, 32) + return + } + dst = append(dst, p.WithdrawalCredentials...) + + // Field (2) 'Amount' + dst = ssz.MarshalUint64(dst, uint64(p.Amount)) + + // Field (3) 'Signature' + dst = append(dst, p.Signature[:]...) + + // Field (4) 'Slot' + dst = ssz.MarshalUint64(dst, uint64(p.Slot)) + + return +} + +// UnmarshalSSZ ssz unmarshals the PendingDeposit object +func (p *PendingDeposit) UnmarshalSSZ(buf []byte) error { + var err error + size := uint64(len(buf)) + if size != 192 { + return ssz.ErrSize + } + + // Field (0) 'Pubkey' + copy(p.Pubkey[:], buf[0:48]) + + // Field (1) 'WithdrawalCredentials' + if cap(p.WithdrawalCredentials) == 0 { + p.WithdrawalCredentials = make([]byte, 0, len(buf[48:80])) + } + p.WithdrawalCredentials = append(p.WithdrawalCredentials, buf[48:80]...) + + // Field (2) 'Amount' + p.Amount = phase0.Gwei(ssz.UnmarshallUint64(buf[80:88])) + + // Field (3) 'Signature' + copy(p.Signature[:], buf[88:184]) + + // Field (4) 'Slot' + p.Slot = phase0.Slot(ssz.UnmarshallUint64(buf[184:192])) + + return err +} + +// SizeSSZ returns the ssz encoded size in bytes for the PendingDeposit object +func (p *PendingDeposit) SizeSSZ() (size int) { + size = 192 + return +} + +// HashTreeRoot ssz hashes the PendingDeposit object +func (p *PendingDeposit) HashTreeRoot() ([32]byte, error) { + return ssz.HashWithDefaultHasher(p) +} + +// HashTreeRootWith ssz hashes the PendingDeposit object with a hasher +func (p *PendingDeposit) HashTreeRootWith(hh ssz.HashWalker) (err error) { + indx := hh.Index() + + // Field (0) 'Pubkey' + hh.PutBytes(p.Pubkey[:]) + + // Field (1) 'WithdrawalCredentials' + if size := len(p.WithdrawalCredentials); size != 32 { + err = ssz.ErrBytesLengthFn("PendingDeposit.WithdrawalCredentials", size, 32) + return + } + hh.PutBytes(p.WithdrawalCredentials) + + // Field (2) 'Amount' + hh.PutUint64(uint64(p.Amount)) + + // Field (3) 'Signature' + hh.PutBytes(p.Signature[:]) + + // Field (4) 'Slot' + hh.PutUint64(uint64(p.Slot)) + + hh.Merkleize(indx) + return +} + +// GetTree ssz hashes the PendingDeposit object +func (p *PendingDeposit) GetTree() (*ssz.Node, error) { + return ssz.ProofTree(p) +} diff --git a/spec/electra/pendingbalancedeposit_yaml.go b/spec/electra/pendingdeposit_yaml.go similarity index 61% rename from spec/electra/pendingbalancedeposit_yaml.go rename to spec/electra/pendingdeposit_yaml.go index 24b8f328..c17f9a23 100644 --- a/spec/electra/pendingbalancedeposit_yaml.go +++ b/spec/electra/pendingdeposit_yaml.go @@ -16,23 +16,29 @@ package electra import ( "bytes" "encoding/json" + "fmt" - "github.com/attestantio/go-eth2-client/spec/phase0" "github.com/goccy/go-yaml" "github.com/pkg/errors" ) -// pendingBalanceDepositYAML is the spec representation of the struct. -type pendingBalanceDepositYAML struct { - Index phase0.ValidatorIndex `yaml:"index"` - Amount phase0.Gwei `yaml:"amount"` +// pendingDepositYAML is the spec representation of the struct. +type pendingDepositYAML struct { + Pubkey string `json:"pubkey"` + WithdrawalCredentials string `json:"withdrawal_credentials"` + Amount uint64 `json:"amount"` + Signature string `json:"signature"` + Slot uint64 `json:"slot"` } // MarshalYAML implements yaml.Marshaler. -func (p *PendingBalanceDeposit) MarshalYAML() ([]byte, error) { - yamlBytes, err := yaml.MarshalWithOptions(&pendingBalanceDepositYAML{ - Index: p.Index, - Amount: p.Amount, +func (p *PendingDeposit) MarshalYAML() ([]byte, error) { + yamlBytes, err := yaml.MarshalWithOptions(&pendingDepositYAML{ + Pubkey: fmt.Sprintf("%#x", p.Pubkey), + WithdrawalCredentials: fmt.Sprintf("%#x", p.WithdrawalCredentials), + Amount: uint64(p.Amount), + Signature: fmt.Sprintf("%#x", p.Signature), + Slot: uint64(p.Slot), }, yaml.Flow(true)) if err != nil { return nil, err @@ -42,10 +48,10 @@ func (p *PendingBalanceDeposit) MarshalYAML() ([]byte, error) { } // UnmarshalYAML implements yaml.Unmarshaler. -func (p *PendingBalanceDeposit) UnmarshalYAML(input []byte) error { +func (p *PendingDeposit) UnmarshalYAML(input []byte) error { // This is very inefficient, but YAML is only used for spec tests so we do this // rather than maintain a custom YAML unmarshaller. - var unmarshaled pendingBalanceDepositJSON + var unmarshaled pendingDepositJSON if err := yaml.Unmarshal(input, &unmarshaled); err != nil { return errors.Wrap(err, "failed to unmarshal YAML") } diff --git a/spec/electra/pendingpartialwithdrawal_ssz.go b/spec/electra/pendingpartialwithdrawal_ssz.go index 88ced33f..00fb1334 100644 --- a/spec/electra/pendingpartialwithdrawal_ssz.go +++ b/spec/electra/pendingpartialwithdrawal_ssz.go @@ -1,5 +1,5 @@ // Code generated by fastssz. DO NOT EDIT. -// Hash: 17d4c9180818d70e873edf284079b326d586a16686d17c7c974a8a2fd19ec3e9 +// Hash: d32594f3eb75aa0af63aae654462a5669f04a1b77e91e06bfcbd28f8599321f7 // Version: 0.1.3 package electra diff --git a/spec/versionedbeaconstate.go b/spec/versionedbeaconstate.go index 0983d8a8..04315cfd 100644 --- a/spec/versionedbeaconstate.go +++ b/spec/versionedbeaconstate.go @@ -296,17 +296,17 @@ func (v *VersionedBeaconState) EarliestConsolidationEpoch() (phase0.Epoch, error } } -// PendingBalanceDeposits returns the pending balance deposits of the state. -func (v *VersionedBeaconState) PendingBalanceDeposits() ([]*electra.PendingBalanceDeposit, error) { +// PendingDeposits returns the pending deposits of the state. +func (v *VersionedBeaconState) PendingDeposits() ([]*electra.PendingDeposit, error) { switch v.Version { case DataVersionPhase0, DataVersionAltair, DataVersionBellatrix, DataVersionCapella, DataVersionDeneb: - return nil, errors.New("state does not provide pending balance deposits") + return nil, errors.New("state does not provide pending deposits") case DataVersionElectra: if v.Electra == nil { return nil, errors.New("no Electra state") } - return v.Electra.PendingBalanceDeposits, nil + return v.Electra.PendingDeposits, nil default: return nil, errors.New("unknown version") }