Skip to content
This repository was archived by the owner on Apr 8, 2025. It is now read-only.

Simplify ssz encodings #14

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 3 additions & 7 deletions committee_index.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,17 +35,13 @@ func (c *CommitteeIndex) UnmarshalSSZ(buf []byte) error {

// MarshalSSZTo marshals committee index with the provided byte slice.
func (c *CommitteeIndex) MarshalSSZTo(dst []byte) ([]byte, error) {
marshalled, err := c.MarshalSSZ()
if err != nil {
return nil, err
}
return append(dst, marshalled...), nil
dst = fssz.MarshalUint64(dst, uint64(*c))
return dst, nil
}

// MarshalSSZ marshals committee index into a serialized object.
func (c *CommitteeIndex) MarshalSSZ() ([]byte, error) {
marshalled := fssz.MarshalUint64([]byte{}, uint64(*c))
return marshalled, nil
return fssz.MarshalSSZ(c)
}

// SizeSSZ returns the size of the serialized object.
Expand Down
9 changes: 3 additions & 6 deletions domain.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,16 +39,13 @@ func (e *Domain) UnmarshalSSZ(buf []byte) error {

// MarshalSSZTo marshals Domain with the provided byte slice.
func (e *Domain) MarshalSSZTo(dst []byte) ([]byte, error) {
marshalled, err := e.MarshalSSZ()
if err != nil {
return nil, err
}
return append(dst, marshalled...), nil
dst = append(dst, (*e)[:]...)
return dst, nil
}

// MarshalSSZ marshals Domain into a serialized object.
func (e *Domain) MarshalSSZ() ([]byte, error) {
return *e, nil
return fssz.MarshalSSZ(e)
}

// SizeSSZ returns the size of the serialized object.
Expand Down
10 changes: 3 additions & 7 deletions epoch.go
Original file line number Diff line number Diff line change
Expand Up @@ -132,17 +132,13 @@ func (e *Epoch) UnmarshalSSZ(buf []byte) error {

// MarshalSSZTo marshals epoch with the provided byte slice.
func (e *Epoch) MarshalSSZTo(dst []byte) ([]byte, error) {
marshalled, err := e.MarshalSSZ()
if err != nil {
return nil, err
}
return append(dst, marshalled...), nil
dst = fssz.MarshalUint64(dst, uint64(*e))
return dst, nil
}

// MarshalSSZ marshals epoch into a serialized object.
func (e *Epoch) MarshalSSZ() ([]byte, error) {
marshalled := fssz.MarshalUint64([]byte{}, uint64(*e))
return marshalled, nil
return fssz.MarshalSSZ(e)
}

// SizeSSZ returns the size of the serialized object.
Expand Down
10 changes: 3 additions & 7 deletions slot.go
Original file line number Diff line number Diff line change
Expand Up @@ -180,17 +180,13 @@ func (s *Slot) UnmarshalSSZ(buf []byte) error {

// MarshalSSZTo marshals slot with the provided byte slice.
func (s *Slot) MarshalSSZTo(dst []byte) ([]byte, error) {
marshalled, err := s.MarshalSSZ()
if err != nil {
return nil, err
}
return append(dst, marshalled...), nil
dst = fssz.MarshalUint64(dst, uint64(*s))
return dst, nil
}

// MarshalSSZ marshals slot into a serialized object.
func (s *Slot) MarshalSSZ() ([]byte, error) {
marshalled := fssz.MarshalUint64([]byte{}, uint64(*s))
return marshalled, nil
return fssz.MarshalSSZ(s)
}

// SizeSSZ returns the size of the serialized object.
Expand Down
19 changes: 4 additions & 15 deletions sszuint64.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package types

import (
"encoding/binary"
"fmt"

fssz "github.com/ferranbt/fastssz"
Expand All @@ -21,17 +20,13 @@ func (s *SSZUint64) SizeSSZ() int {

// MarshalSSZTo marshals the uint64 with the provided byte slice.
func (s *SSZUint64) MarshalSSZTo(dst []byte) ([]byte, error) {
marshalled, err := s.MarshalSSZ()
if err != nil {
return nil, err
}
return append(dst, marshalled...), nil
dst = fssz.MarshalUint64(dst, uint64(*s))
return dst, nil
}

// MarshalSSZ marshals uin64 into a serialized object.
func (s *SSZUint64) MarshalSSZ() ([]byte, error) {
marshalled := fssz.MarshalUint64([]byte{}, uint64(*s))
return marshalled, nil
return fssz.MarshalSSZ(s)
}

// UnmarshalSSZ deserializes the provided bytes buffer into the uint64 object.
Expand All @@ -45,17 +40,11 @@ func (s *SSZUint64) UnmarshalSSZ(buf []byte) error {

// HashTreeRoot returns calculated hash root.
func (s *SSZUint64) HashTreeRoot() ([32]byte, error) {
buf := make([]byte, 8)
binary.LittleEndian.PutUint64(buf, uint64(*s))
var root [32]byte
copy(root[:], buf)
return root, nil
return fssz.HashWithDefaultHasher(s)
}

// HashWithDefaultHasher hashes a HashRoot object with a Hasher from the default HasherPool.
func (s *SSZUint64) HashTreeRootWith(hh *fssz.Hasher) error {
indx := hh.Index()
hh.PutUint64(uint64(*s))
hh.Merkleize(indx)
return nil
}
10 changes: 3 additions & 7 deletions validator.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,17 +61,13 @@ func (v *ValidatorIndex) UnmarshalSSZ(buf []byte) error {

// MarshalSSZTo marshals validator index with the provided byte slice.
func (v *ValidatorIndex) MarshalSSZTo(dst []byte) ([]byte, error) {
marshalled, err := v.MarshalSSZ()
if err != nil {
return nil, err
}
return append(dst, marshalled...), nil
dst = fssz.MarshalUint64(dst, uint64(*v))
return dst, nil
}

// MarshalSSZ marshals validator index into a serialized object.
func (v *ValidatorIndex) MarshalSSZ() ([]byte, error) {
marshalled := fssz.MarshalUint64([]byte{}, uint64(*v))
return marshalled, nil
return fssz.MarshalSSZ(v)
}

// SizeSSZ returns the size of the serialized object.
Expand Down