diff --git a/Makefile b/Makefile index 6c4bcd3..04c3c0a 100644 --- a/Makefile +++ b/Makefile @@ -30,7 +30,7 @@ lint: generate-ssz: rm -f types/builder_encoding.go types/signing_encoding.go types/common_encoding.go - sszgen --path types --include ../go-ethereum/common/hexutil --objs Eth1Data,BeaconBlockHeader,SignedBeaconBlockHeader,ProposerSlashing,Checkpoint,AttestationData,IndexedAttestation,AttesterSlashing,Attestation,Deposit,VoluntaryExit,SyncAggregate,ExecutionPayloadHeader,VersionedExecutionPayloadHeader,BlindedBeaconBlockBody,BlindedBeaconBlock,RegisterValidatorRequestMessage,BuilderBid,SignedBuilderBid,SigningData,ForkData,Transactions,BidTraceMessage,BuilderSubmitBlockRequestMessage,BuilderSubmitBlockResponseMessage + sszgen --path types --include ../go-ethereum/common/hexutil --objs Eth1Data,BeaconBlockHeader,SignedBeaconBlockHeader,ProposerSlashing,Checkpoint,AttestationData,IndexedAttestation,AttesterSlashing,Attestation,Deposit,VoluntaryExit,SyncAggregate,ExecutionPayloadHeader,VersionedExecutionPayloadHeader,BlindedBeaconBlockBody,BlindedBeaconBlock,SignedBlindedBeaconBlock,RegisterValidatorRequestMessage,BuilderBid,SignedBuilderBid,SigningData,ForkData,Transactions,BidTraceMessage,BuilderSubmitBlockRequestMessage,BuilderSubmitBlockResponseMessage cover: go test -coverprofile=/tmp/go-sim-lb.cover.tmp ./... diff --git a/types/builder_encoding.go b/types/builder_encoding.go index 11829e9..19ae624 100644 --- a/types/builder_encoding.go +++ b/types/builder_encoding.go @@ -1,5 +1,5 @@ // Code generated by fastssz. DO NOT EDIT. -// Hash: da350027c880b999c04fba4bb596db37092c2806c2dbf24a308a2d6cc0933581 +// Hash: d1fe35ae1ab44365856903184e6aa983590a55d0579fe5f7ddcfc880405068c4 package types import ( @@ -2422,6 +2422,109 @@ func (s *SignedBuilderBid) GetTree() (*ssz.Node, error) { return ssz.ProofTree(s) } +// MarshalSSZ ssz marshals the SignedBlindedBeaconBlock object +func (s *SignedBlindedBeaconBlock) MarshalSSZ() ([]byte, error) { + return ssz.MarshalSSZ(s) +} + +// MarshalSSZTo ssz marshals the SignedBlindedBeaconBlock object to a target array +func (s *SignedBlindedBeaconBlock) MarshalSSZTo(buf []byte) (dst []byte, err error) { + dst = buf + offset := int(100) + + // Offset (0) 'Message' + dst = ssz.WriteOffset(dst, offset) + if s.Message == nil { + s.Message = new(BlindedBeaconBlock) + } + offset += s.Message.SizeSSZ() + + // Field (1) 'Signature' + dst = append(dst, s.Signature[:]...) + + // Field (0) 'Message' + if dst, err = s.Message.MarshalSSZTo(dst); err != nil { + return + } + + return +} + +// UnmarshalSSZ ssz unmarshals the SignedBlindedBeaconBlock object +func (s *SignedBlindedBeaconBlock) UnmarshalSSZ(buf []byte) error { + var err error + size := uint64(len(buf)) + if size < 100 { + return ssz.ErrSize + } + + tail := buf + var o0 uint64 + + // Offset (0) 'Message' + if o0 = ssz.ReadOffset(buf[0:4]); o0 > size { + return ssz.ErrOffset + } + + if o0 < 100 { + return ssz.ErrInvalidVariableOffset + } + + // Field (1) 'Signature' + copy(s.Signature[:], buf[4:100]) + + // Field (0) 'Message' + { + buf = tail[o0:] + if s.Message == nil { + s.Message = new(BlindedBeaconBlock) + } + if err = s.Message.UnmarshalSSZ(buf); err != nil { + return err + } + } + return err +} + +// SizeSSZ returns the ssz encoded size in bytes for the SignedBlindedBeaconBlock object +func (s *SignedBlindedBeaconBlock) SizeSSZ() (size int) { + size = 100 + + // Field (0) 'Message' + if s.Message == nil { + s.Message = new(BlindedBeaconBlock) + } + size += s.Message.SizeSSZ() + + return +} + +// HashTreeRoot ssz hashes the SignedBlindedBeaconBlock object +func (s *SignedBlindedBeaconBlock) HashTreeRoot() ([32]byte, error) { + return ssz.HashWithDefaultHasher(s) +} + +// HashTreeRootWith ssz hashes the SignedBlindedBeaconBlock object with a hasher +func (s *SignedBlindedBeaconBlock) HashTreeRootWith(hh ssz.HashWalker) (err error) { + indx := hh.Index() + + // Field (0) 'Message' + if err = s.Message.HashTreeRootWith(hh); err != nil { + return + } + + // Field (1) 'Signature' + hh.PutBytes(s.Signature[:]) + + hh.Merkleize(indx) + return +} + +// GetTree ssz hashes the SignedBlindedBeaconBlock object +func (s *SignedBlindedBeaconBlock) GetTree() (*ssz.Node, error) { + return ssz.ProofTree(s) +} + // MarshalSSZ ssz marshals the Transactions object func (t *Transactions) MarshalSSZ() ([]byte, error) { return ssz.MarshalSSZ(t) diff --git a/types/encoding_bench_test.go b/types/encoding_bench_test.go new file mode 100644 index 0000000..af173ec --- /dev/null +++ b/types/encoding_bench_test.go @@ -0,0 +1,52 @@ +package types + +import ( + "encoding/json" + "os" + "testing" + + "github.com/stretchr/testify/require" +) + +func BenchmarkJSONvsSSZEncoding(b *testing.B) { + var err error + jsonFile, err := os.Open("../testdata/signed-blinded-beacon-block-with-deposit.json") + require.NoError(b, err) + defer jsonFile.Close() + signedBlindedBeaconBlock := new(SignedBlindedBeaconBlock) + require.NoError(b, DecodeJSON(jsonFile, &signedBlindedBeaconBlock)) + signedBlindedBeaconBlockJSONBytes, err := json.Marshal(signedBlindedBeaconBlock) + require.NoError(b, err) + signedBlindedBeaconBlockSSZBytes, err := signedBlindedBeaconBlock.MarshalSSZ() + require.NoError(b, err) + + b.Run("Encode JSON", func(b *testing.B) { + for n := 0; n < b.N; n++ { + _, err = json.Marshal(signedBlindedBeaconBlock) + require.NoError(b, err) + } + }) + + b.Run("Encode SSZ", func(b *testing.B) { + for n := 0; n < b.N; n++ { + _, err = signedBlindedBeaconBlock.MarshalSSZ() + require.NoError(b, err) + } + }) + + b.Run("Decode JSON", func(b *testing.B) { + _blindedBeaconBlock := new(SignedBlindedBeaconBlock) + for n := 0; n < b.N; n++ { + err = json.Unmarshal(signedBlindedBeaconBlockJSONBytes, _blindedBeaconBlock) + require.NoError(b, err) + } + }) + + b.Run("Decode SSZ", func(b *testing.B) { + _blindedBeaconBlock := new(SignedBlindedBeaconBlock) + for n := 0; n < b.N; n++ { + err = _blindedBeaconBlock.UnmarshalSSZ(signedBlindedBeaconBlockSSZBytes) + require.NoError(b, err) + } + }) +}