Skip to content

Commit

Permalink
Change sszutil DeepEqual to ignore unexported (#8336)
Browse files Browse the repository at this point in the history
  • Loading branch information
0xKiwi authored Jan 25, 2021
1 parent c827672 commit 8d986bd
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 1 deletion.
8 changes: 7 additions & 1 deletion shared/sszutil/deep_equal.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,13 @@ func deepValueEqual(v1, v2 reflect.Value, visited map[visit]bool, depth int) boo
return deepValueEqual(v1.Elem(), v2.Elem(), visited, depth+1)
case reflect.Struct:
for i, n := 0, v1.NumField(); i < n; i++ {
if !deepValueEqual(v1.Field(i), v2.Field(i), visited, depth+1) {
v1Field := v1.Field(i)
v2Field := v2.Field(i)
if !v1Field.CanInterface() || !v2Field.CanInterface() {
// Continue for unexported fields, since they cannot be read anyways.
continue
}
if !deepValueEqual(v1Field, v2Field, visited, depth+1) {
return false
}
}
Expand Down
13 changes: 13 additions & 0 deletions shared/sszutil/deep_equal_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,19 @@ func TestDeepEqualStructs(t *testing.T) {
assert.Equal(t, false, sszutil.DeepEqual(store1, store3))
}

func TestDeepEqualStructs_Unexported(t *testing.T) {
type Store struct {
V1 uint64
V2 []byte
ignoreMe string
}
store1 := Store{uint64(1234), nil, "hi there"}
store2 := Store{uint64(1234), []byte{}, "oh hey"}
store3 := Store{uint64(4321), []byte{}, "wow"}
assert.Equal(t, true, sszutil.DeepEqual(store1, store2))
assert.Equal(t, false, sszutil.DeepEqual(store1, store3))
}

func TestDeepEqualProto(t *testing.T) {
var fork1, fork2 pb.Fork
assert.Equal(t, true, sszutil.DeepEqual(fork1, fork2))
Expand Down

0 comments on commit 8d986bd

Please sign in to comment.