Skip to content

Commit

Permalink
Add support for uint8 en/decoding
Browse files Browse the repository at this point in the history
  • Loading branch information
gballet committed Jul 25, 2024
1 parent 3d1ff7a commit c8422a9
Show file tree
Hide file tree
Showing 5 changed files with 63 additions and 0 deletions.
10 changes: 10 additions & 0 deletions cmd/sszgen/opset.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,16 @@ func (p *parseContext) resolveBasicOpset(typ *types.Basic, tags *sizeTag) (opset
"DecodeBool({{.Codec}}, &{{.Field}})",
[]int{1},
}, nil
case types.Uint8:
if tags != nil && tags.size[0] != 1 {
return nil, fmt.Errorf("byte basic type requires ssz-size=1: have %d", tags.size[0])
}
return &opsetStatic{
"DefineUint8({{.Codec}}, &{{.Field}})",
"EncodeUint8({{.Codec}}, &{{.Field}})",
"DecodeUint8({{.Codec}}, &{{.Field}})",
[]int{1},
}, nil
case types.Uint64:
if tags != nil && tags.size[0] != 8 {
return nil, fmt.Errorf("uint64 basic type requires ssz-size=8: have %d", tags.size[0])
Expand Down
13 changes: 13 additions & 0 deletions codec.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,19 @@ func DefineBool[T ~bool](c *Codec, v *T) {
HashBool(c.has, *v)
}

// DefineUint8 defines the next field as a uint64.
func DefineUint8[T ~uint8](c *Codec, n *T) {
if c.enc != nil {
EncodeUint8(c.enc, *n)
return
}
if c.dec != nil {
DecodeUint8(c.dec, n)
return
}
HashUint8(c.has, *n)
}

// DefineUint64 defines the next field as a uint64.
func DefineUint64[T ~uint64](c *Codec, n *T) {
if c.enc != nil {
Expand Down
19 changes: 19 additions & 0 deletions decoder.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,25 @@ func DecodeBool[T ~bool](dec *Decoder, v *T) {
}
}

// DecodeUint8 parses a uint8.
func DecodeUint8[T ~uint8](dec *Decoder, n *T) {
if dec.err != nil {
return
}
if dec.inReader != nil {
_, dec.err = io.ReadFull(dec.inReader, dec.buf[:1])
*n = T(dec.buf[0])
dec.inRead += 1
} else {
if len(dec.inBuffer) < 8 {
dec.err = io.ErrUnexpectedEOF
return
}
*n = T(dec.inBuffer[0])
dec.inBuffer = dec.inBuffer[1:]
}
}

// DecodeUint64 parses a uint64.
func DecodeUint64[T ~uint64](dec *Decoder, n *T) {
if dec.err != nil {
Expand Down
14 changes: 14 additions & 0 deletions encoder.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,20 @@ func EncodeBool[T ~bool](enc *Encoder, v T) {
}
}

// EncodeUint8 serializes a uint8.
func EncodeUint8[T ~uint8](enc *Encoder, n T) {
if enc.outWriter != nil {
if enc.err != nil {
return
}
enc.buf[0] = byte(n)
_, enc.err = enc.outWriter.Write(enc.buf[:1])
} else {
enc.outBuffer[0] = byte(n)
enc.outBuffer = enc.outBuffer[1:]
}
}

// EncodeUint64 serializes a uint64.
func EncodeUint64[T ~uint64](enc *Encoder, n T) {
// Nope, dive into actual encoding
Expand Down
7 changes: 7 additions & 0 deletions hasher.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,13 @@ func HashBool[T ~bool](h *Hasher, v T) {
}
}

// HashUint8 hashes a uint8.
func HashUint8[T ~uint8](h *Hasher, n T) {
var buffer [32]byte
buffer[0] = uint8(n)
h.insertChunk(buffer, 0)
}

// HashUint64 hashes a uint64.
func HashUint64[T ~uint64](h *Hasher, n T) {
var buffer [32]byte
Expand Down

0 comments on commit c8422a9

Please sign in to comment.