Skip to content

Commit

Permalink
Fix handling of aliases to unsigned integer types other than uint64. (f…
Browse files Browse the repository at this point in the history
  • Loading branch information
Un1oR committed May 30, 2024
1 parent a4db753 commit f43e88e
Show file tree
Hide file tree
Showing 6 changed files with 163 additions and 4 deletions.
18 changes: 18 additions & 0 deletions sszgen/generator/generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -1313,3 +1313,21 @@ func uintVToName(v *Value) string {
panic(fmt.Sprintf("unknown uint size, %d bytes. field name=%s", v.s, v.name))
}
}

func uintVToLowerCaseName(v *Value) string {
if v.t != TypeUint {
panic(fmt.Sprintf("type %v for %s not expected", v.t, v.name))
}
switch v.s {
case 8:
return "uint64"
case 4:
return "uint32"
case 2:
return "uint16"
case 1:
return "uint8"
default:
panic(fmt.Sprintf("unknown uint size, %d bytes. field name=%s", v.s, v.name))
}
}
4 changes: 2 additions & 2 deletions sszgen/generator/hash.go
Original file line number Diff line number Diff line change
Expand Up @@ -161,8 +161,8 @@ func (v *Value) hashTreeRoot(name string, appendBytes bool) string {

case TypeUint:
if v.ref != "" || v.obj != "" {
// alias to Uint64
name = fmt.Sprintf("uint64(%s)", name)
// alias to uint*
name = fmt.Sprintf("%s(%s)", uintVToLowerCaseName(v), name)
}
bitLen := v.fixedSize() * 8
return fmt.Sprintf("hh.PutUint%d(%s)", bitLen, name)
Expand Down
4 changes: 2 additions & 2 deletions sszgen/generator/marshal.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,8 @@ func (v *Value) marshal() string {
case TypeUint:
var name string
if v.ref != "" || v.obj != "" {
// alias to Uint64
name = fmt.Sprintf("uint64(::.%s)", v.name)
// alias to uint*
name = fmt.Sprintf("%s(::.%s)", uintVToLowerCaseName(v), v.name)
} else {
name = "::." + v.name
}
Expand Down
15 changes: 15 additions & 0 deletions sszgen/testcases/uint.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package testcases

//go:generate go run ../main.go --path uint.go

type Uint8 uint8
type Uint16 uint16
type Uint32 uint32
type Uint64 uint64

type Uints struct {
Uint8 Uint8
Uint16 Uint16
Uint32 Uint32
Uint64 Uint64
}
91 changes: 91 additions & 0 deletions sszgen/testcases/uint_encoding.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

35 changes: 35 additions & 0 deletions sszgen/testcases/uint_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package testcases

import (
"github.com/stretchr/testify/assert"
"testing"
)

func TestUint(t *testing.T) {
s := Uints{
Uint8: Uint8(123),
Uint16: Uint16(12345),
Uint32: Uint32(1234567890),
Uint64: Uint64(123456789000),
}
expectedHash := [32]byte{
0xea, 0xfc, 0xf7, 0xa2, 0x41, 0x8, 0x51, 0xa2,
0xa0, 0xb0, 0x23, 0x68, 0xff, 0x4, 0x44, 0xbd,
0x24, 0xc9, 0x9b, 0xff, 0xe7, 0x81, 0xca, 0x49,
0xb6, 0xf7, 0xd4, 0x99, 0x28, 0xf3, 0xee, 0xeb,
}

bytes, err := s.MarshalSSZ()
assert.NoError(t, err)

var s2 Uints
err = s2.UnmarshalSSZ(bytes)
assert.NoError(t, err)

assert.Equal(t, s, s2)

h, err := s.HashTreeRoot()
assert.NoError(t, err)

assert.Equal(t, h, expectedHash)
}

0 comments on commit f43e88e

Please sign in to comment.