From 9b3337c99d279c26ae55f5cd2fa9ce32eb3feba1 Mon Sep 17 00:00:00 2001 From: Justin Traglia Date: Mon, 6 May 2024 07:53:43 -0500 Subject: [PATCH 1/6] Fix attestation aggregation bits error message --- spec/phase0/attestation.go | 2 +- spec/phase0/attestation_test.go | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/spec/phase0/attestation.go b/spec/phase0/attestation.go index e87cdf38..e9c5715c 100644 --- a/spec/phase0/attestation.go +++ b/spec/phase0/attestation.go @@ -72,7 +72,7 @@ func (a *Attestation) unpack(attestationJSON *attestationJSON) error { return errors.New("aggregation bits missing") } if a.AggregationBits, err = hex.DecodeString(strings.TrimPrefix(attestationJSON.AggregationBits, "0x")); err != nil { - return errors.Wrap(err, "invalid value for beacon block root") + return errors.Wrap(err, "invalid value for aggregation bits") } a.Data = attestationJSON.Data if a.Data == nil { diff --git a/spec/phase0/attestation_test.go b/spec/phase0/attestation_test.go index cdfd6926..162c2bde 100644 --- a/spec/phase0/attestation_test.go +++ b/spec/phase0/attestation_test.go @@ -52,7 +52,7 @@ func TestAttestationJSON(t *testing.T) { { name: "AggregationBitsInvalid", input: []byte(`{"aggregation_bits":"invalid","data":{"slot":"100","index":"1","beacon_block_root":"0x000102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f","source":{"epoch":"1","root":"0x202122232425262728292a2b2c2d2e2f303132333435363738393a3b3c3d3e3f"},"target":{"epoch":"2","root":"0x404142434445464748494a4b4c4d4e4f505152535455565758595a5b5c5d5e5f"}},"signature":"0x606162636465666768696a6b6c6d6e6f707172737475767778797a7b7c7d7e7f808182838485868788898a8b8c8d8e8f909192939495969798999a9b9c9d9e9fa0a1a2a3a4a5a6a7a8a9aaabacadaeafb0b1b2b3b4b5b6b7b8b9babbbcbdbebf"}`), - err: "invalid value for beacon block root: encoding/hex: invalid byte: U+0069 'i'", + err: "invalid value for aggregation bits: encoding/hex: invalid byte: U+0069 'i'", }, { name: "DataMissing", From 6e270315f815b9ce59528855a48d202f0c0bdec8 Mon Sep 17 00:00:00 2001 From: Justin Traglia Date: Mon, 6 May 2024 07:56:00 -0500 Subject: [PATCH 2/6] Remove deleted files from api/v1/deneb/generate.go --- api/v1/deneb/generate.go | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/api/v1/deneb/generate.go b/api/v1/deneb/generate.go index 2546891f..159f43a3 100644 --- a/api/v1/deneb/generate.go +++ b/api/v1/deneb/generate.go @@ -14,6 +14,7 @@ package deneb // Need to `go install github.com/ferranbt/fastssz/sszgen@latest` for this to work. -//go:generate rm -f blindedbeaconblock_ssz.go blindedbeaconblockbody_ssz.go blindedblobsidecar_ssz.go blindedblockcontents_ssz.go blockcontents_ssz.go signedblindedbeaconblock_ssz.go signedblindedblockcontents_ssz.go signedblockcontents_ssz.go -//go:generate sszgen --include ../../../spec/phase0,../../../spec/altair,../../../spec/bellatrix,../../../spec/capella,../../../spec/deneb -path . --suffix ssz -objs BlindedBeaconBlock,BlindedBeaconBlockBody,BlindedBlobSidecar,BlindedBlockContents,BlockContents,SignedBlindedBeaconBlock,SignedBlindedBlockContents,SignedBlockContents -//go:generate goimports -w blindedbeaconblock_ssz.go blindedbeaconblockbody_ssz.go blindedblobsidecar_ssz.go blindedblockcontents_ssz.go blockcontents_ssz.go signedblindedbeaconblock_ssz.go signedblindedblockcontents_ssz.go,signedblockcontents_ssz.go +//go:generate rm -f blindedbeaconblock_ssz.go blindedbeaconblockbody_ssz.go blockcontents_ssz.go signedblindedbeaconblock_ssz.go signedblockcontents_ssz.go +//go:generate sszgen --include ../../../spec/phase0,../../../spec/altair,../../../spec/bellatrix,../../../spec/capella,../../../spec/deneb -path . --suffix ssz -objs BlindedBeaconBlock,BlindedBeaconBlockBody,BlockContents,SignedBlindedBeaconBlock,SignedBlockContents +//go:generate goimports -w blindedbeaconblock_ssz.go blindedbeaconblockbody_ssz.go blockcontents_ssz.go signedblindedbeaconblock_ssz.go signedblockcontents_ssz.go + From 092f304f507f2ef1d080a47df778224bdae64902 Mon Sep 17 00:00:00 2001 From: Justin Traglia Date: Mon, 6 May 2024 07:57:42 -0500 Subject: [PATCH 3/6] Remove extra newline --- api/v1/deneb/generate.go | 1 - 1 file changed, 1 deletion(-) diff --git a/api/v1/deneb/generate.go b/api/v1/deneb/generate.go index 159f43a3..d5e1a9f7 100644 --- a/api/v1/deneb/generate.go +++ b/api/v1/deneb/generate.go @@ -17,4 +17,3 @@ package deneb //go:generate rm -f blindedbeaconblock_ssz.go blindedbeaconblockbody_ssz.go blockcontents_ssz.go signedblindedbeaconblock_ssz.go signedblockcontents_ssz.go //go:generate sszgen --include ../../../spec/phase0,../../../spec/altair,../../../spec/bellatrix,../../../spec/capella,../../../spec/deneb -path . --suffix ssz -objs BlindedBeaconBlock,BlindedBeaconBlockBody,BlockContents,SignedBlindedBeaconBlock,SignedBlockContents //go:generate goimports -w blindedbeaconblock_ssz.go blindedbeaconblockbody_ssz.go blockcontents_ssz.go signedblindedbeaconblock_ssz.go signedblockcontents_ssz.go - From 95dbd10f7090cf4bf5b7c990b1c5ed2df49f272c Mon Sep 17 00:00:00 2001 From: Justin Traglia Date: Mon, 6 May 2024 08:38:49 -0500 Subject: [PATCH 4/6] Add Marshalling functions for Epoch --- spec/phase0/types.go | 35 +++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) diff --git a/spec/phase0/types.go b/spec/phase0/types.go index c2646d6f..1b7d6d57 100644 --- a/spec/phase0/types.go +++ b/spec/phase0/types.go @@ -13,9 +13,44 @@ package phase0 +import ( + "bytes" + "fmt" + "strconv" + + "github.com/pkg/errors" +) + // Epoch is an epoch number. type Epoch uint64 +// UnmarshalJSON implements json.Unmarshaler. +func (e *Epoch) UnmarshalJSON(input []byte) error { + if len(input) == 0 { + return errors.New("input missing") + } + + if !bytes.HasPrefix(input, []byte{'"'}) { + return errors.New("invalid prefix") + } + if !bytes.HasSuffix(input, []byte{'"'}) { + return errors.New("invalid suffix") + } + + val, err := strconv.ParseUint(string(input[1:len(input)-1]), 10, 64) + if err != nil { + return errors.Wrapf(err, "invalid value %s", string(input[1:len(input)-1])) + } + *e = Epoch(val) + + return nil +} + +// MarshalJSON implements json.Marshaler. +func (e *Epoch) MarshalJSON() ([]byte, error) { + return []byte(fmt.Sprintf(`"%d"`, e)), nil +} + // CommitteeIndex is a committee index at a slot. type CommitteeIndex uint64 From 2fc06f1a68d32bbf5049a3f214494015c9bed424 Mon Sep 17 00:00:00 2001 From: Jim McDonald Date: Mon, 6 May 2024 15:22:58 +0100 Subject: [PATCH 5/6] Update types.go Additional unmarshal check. --- spec/phase0/types.go | 3 +++ 1 file changed, 3 insertions(+) diff --git a/spec/phase0/types.go b/spec/phase0/types.go index 1b7d6d57..d8ec2ed4 100644 --- a/spec/phase0/types.go +++ b/spec/phase0/types.go @@ -29,6 +29,9 @@ func (e *Epoch) UnmarshalJSON(input []byte) error { if len(input) == 0 { return errors.New("input missing") } + if len(input) < 3 { + return errors.New("input malformed") + } if !bytes.HasPrefix(input, []byte{'"'}) { return errors.New("invalid prefix") From ed02a7fb4e86ec2a545859af3e04a43910e6abde Mon Sep 17 00:00:00 2001 From: Jim McDonald Date: Mon, 6 May 2024 15:24:23 +0100 Subject: [PATCH 6/6] Update types.go Marshal directly for type. --- spec/phase0/types.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/spec/phase0/types.go b/spec/phase0/types.go index d8ec2ed4..74954a66 100644 --- a/spec/phase0/types.go +++ b/spec/phase0/types.go @@ -50,7 +50,7 @@ func (e *Epoch) UnmarshalJSON(input []byte) error { } // MarshalJSON implements json.Marshaler. -func (e *Epoch) MarshalJSON() ([]byte, error) { +func (e Epoch) MarshalJSON() ([]byte, error) { return []byte(fmt.Sprintf(`"%d"`, e)), nil }