Skip to content

MarshalSSZTo and other incompatibilities with fastssz #170

@jshufro

Description

@jshufro

Fast ssz now expects HashTreeRootWith(hh ssz.HashWalker) (err error) in its HashTreeRoot interface. I believe the implementation is as simple as:

func (u *Uint256) HashTreeRootWith(hh ssz.HashWalker) (err error) {
        bytes := make([]byte, 32)
        bytes, err = u.MarshalSSZTo(bytes)
        if err != nil {
                return
        }

        hh.AppendBytes32(bytes)
        return
}

but I have been known to make mistakes.

Further, fastssz's MarshalSSZTo(buf []byte) ([]byte, error) interface function expects the implementation to append to the end of buf, not to overwrite it. Unfortunately, this library overwrites instead, and a wrapper is needed (one is needed anyway for sszgen to work):

type Uint256 struct {
        repr *uint256.Int `ssz:"-"`
}
[...]
func (u *Uint256) MarshalSSZTo(buf []byte) ([]byte, error) {
        bytes, err := u.repr.MarshalSSZ()
        if err != nil {
                return nil, err
        }
        return append(buf, bytes...), nil
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions