Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make Domain a fixed-size array #13

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
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
51 changes: 34 additions & 17 deletions domain.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,52 +6,69 @@ import (
fssz "github.com/ferranbt/fastssz"
)

var _ fssz.HashRoot = (Domain)([]byte{})
var _ fssz.HashRoot = (Domain)([32]byte{})
var _ fssz.Marshaler = (*Domain)(nil)
var _ fssz.Unmarshaler = (*Domain)(nil)

// Domain represents a 32 bytes domain object in Ethereum beacon chain consensus.
type Domain []byte
type Domain [32]byte

// Size of the array.
func (d *Domain) Size() int {
return len(d)
}

// MarshalTo serializes the array to the buffer.
func (d *Domain) MarshalTo(data []byte) (int, error) {
return copy(data[:d.Size()], d[:]), nil
}

// Unmarshal deserializes into the provided array type.
func (d *Domain) Unmarshal(data []byte) error {
copy(d[:], data)
return nil
}

// HashTreeRoot returns calculated hash root.
func (e Domain) HashTreeRoot() ([32]byte, error) {
return fssz.HashWithDefaultHasher(e)
func (d Domain) HashTreeRoot() ([32]byte, error) {
return fssz.HashWithDefaultHasher(d)
}

// HashTreeRootWith hashes a Domain object with a Hasher from the default HasherPool.
func (e Domain) HashTreeRootWith(hh *fssz.Hasher) error {
hh.PutBytes(e[:])
func (d Domain) HashTreeRootWith(hh *fssz.Hasher) error {
hh.PutBytes(d[:])
return nil
}

// UnmarshalSSZ deserializes the provided bytes buffer into the Domain object.
func (e *Domain) UnmarshalSSZ(buf []byte) error {
if len(buf) != e.SizeSSZ() {
return fmt.Errorf("expected buffer of length %d received %d", e.SizeSSZ(), len(buf))
func (d *Domain) UnmarshalSSZ(buf []byte) error {
if len(buf) != d.SizeSSZ() {
return fmt.Errorf("expected buffer of length %d received %d", d.SizeSSZ(), len(buf))
}

var b [32]byte
item := Domain(b[:])
copy(item, buf)
*e = item
item := Domain(b)
copy(item[:], buf)
*d = item
return nil
}

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

// MarshalSSZ marshals Domain into a serialized object.
func (e *Domain) MarshalSSZ() ([]byte, error) {
return *e, nil
func (d *Domain) MarshalSSZ() ([]byte, error) {
b := [32]byte(*d)
return b[:], nil
}

// SizeSSZ returns the size of the serialized object.
func (e *Domain) SizeSSZ() int {
func (d *Domain) SizeSSZ() int {
return 32
}