diff --git a/shared/sszutil/deep_equal.go b/shared/sszutil/deep_equal.go index 9d37e26eba47..2bcf45e19eef 100644 --- a/shared/sszutil/deep_equal.go +++ b/shared/sszutil/deep_equal.go @@ -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 } } diff --git a/shared/sszutil/deep_equal_test.go b/shared/sszutil/deep_equal_test.go index 49e1dd2ac9cc..6551c83fc7a5 100644 --- a/shared/sszutil/deep_equal_test.go +++ b/shared/sszutil/deep_equal_test.go @@ -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))