From a71e7f842afd53c7ec6cf4739db8b3c6d69a8d15 Mon Sep 17 00:00:00 2001 From: Faye Amacker <33205765+fxamacker@users.noreply.github.com> Date: Sun, 24 Sep 2023 15:46:38 -0500 Subject: [PATCH] Refactor array and map validation --- array_debug.go | 36 +++++++++++++-------------- array_test.go | 6 ++--- map_debug.go | 66 +++++++++++++++++++++++++------------------------- map_test.go | 6 ++--- 4 files changed, 57 insertions(+), 57 deletions(-) diff --git a/array_debug.go b/array_debug.go index 81d6fa67..090dc226 100644 --- a/array_debug.go +++ b/array_debug.go @@ -167,7 +167,7 @@ func DumpArraySlabs(a *Array) ([]string, error) { type TypeInfoComparator func(TypeInfo, TypeInfo) bool -func ValidArray(a *Array, address Address, typeInfo TypeInfo, tic TypeInfoComparator, hip HashInputProvider, inlineEnabled bool) error { +func VerifyArray(a *Array, address Address, typeInfo TypeInfo, tic TypeInfoComparator, hip HashInputProvider, inlineEnabled bool) error { // Verify array address (independent of array inlined status) if address != a.Address() { @@ -175,13 +175,13 @@ func ValidArray(a *Array, address Address, typeInfo TypeInfo, tic TypeInfoCompar } // Verify array value ID (independent of array inlined status) - err := validArrayValueID(a) + err := verifyArrayValueID(a) if err != nil { return err } // Verify array slab ID (dependent of array inlined status) - err = validArraySlabID(a) + err = verifyArraySlabID(a) if err != nil { return err } @@ -211,9 +211,9 @@ func ValidArray(a *Array, address Address, typeInfo TypeInfo, tic TypeInfoCompar } // Verify array slabs - computedCount, dataSlabIDs, nextDataSlabIDs, err := v.verifyArraySlab(a.root, 0, nil, []SlabID{}, []SlabID{}) + computedCount, dataSlabIDs, nextDataSlabIDs, err := v.verifySlab(a.root, 0, nil, []SlabID{}, []SlabID{}) if err != nil { - // Don't need to wrap error as external error because err is already categorized by validArraySlab(). + // Don't need to wrap error as external error because err is already categorized by verifySlab(). return err } @@ -239,7 +239,7 @@ type arrayVerifier struct { inlineEnabled bool } -func (v *arrayVerifier) verifyArraySlab( +func (v *arrayVerifier) verifySlab( slab ArraySlab, level int, headerFromParentSlab *ArraySlabHeader, @@ -298,17 +298,17 @@ func (v *arrayVerifier) verifyArraySlab( switch slab := slab.(type) { case *ArrayDataSlab: - return v.verifyArrayDataSlab(slab, level, dataSlabIDs, nextDataSlabIDs) + return v.verifyDataSlab(slab, level, dataSlabIDs, nextDataSlabIDs) case *ArrayMetaDataSlab: - return v.verifyArrayMetaDataSlab(slab, level, dataSlabIDs, nextDataSlabIDs) + return v.verifyMetaDataSlab(slab, level, dataSlabIDs, nextDataSlabIDs) default: return 0, nil, nil, NewFatalError(fmt.Errorf("ArraySlab is either *ArrayDataSlab or *ArrayMetaDataSlab, got %T", slab)) } } -func (v *arrayVerifier) verifyArrayDataSlab( +func (v *arrayVerifier) verifyDataSlab( dataSlab *ArrayDataSlab, level int, dataSlabIDs []SlabID, @@ -389,9 +389,9 @@ func (v *arrayVerifier) verifyArrayDataSlab( } // Verify element - err = ValidValue(value, v.address, nil, v.tic, v.hip, v.inlineEnabled) + err = verifyValue(value, v.address, nil, v.tic, v.hip, v.inlineEnabled) if err != nil { - // Don't need to wrap error as external error because err is already categorized by ValidValue(). + // Don't need to wrap error as external error because err is already categorized by verifyValue(). return 0, nil, nil, fmt.Errorf( "data slab %s element %q isn't valid: %w", id, e, err, @@ -402,7 +402,7 @@ func (v *arrayVerifier) verifyArrayDataSlab( return dataSlab.header.count, dataSlabIDs, nextDataSlabIDs, nil } -func (v *arrayVerifier) verifyArrayMetaDataSlab( +func (v *arrayVerifier) verifyMetaDataSlab( metaSlab *ArrayMetaDataSlab, level int, dataSlabIDs []SlabID, @@ -454,9 +454,9 @@ func (v *arrayVerifier) verifyArrayMetaDataSlab( // Verify child slabs var count uint32 count, dataSlabIDs, nextDataSlabIDs, err = - v.verifyArraySlab(childSlab, level+1, &h, dataSlabIDs, nextDataSlabIDs) + v.verifySlab(childSlab, level+1, &h, dataSlabIDs, nextDataSlabIDs) if err != nil { - // Don't need to wrap error as external error because err is already categorized by validArraySlab(). + // Don't need to wrap error as external error because err is already categorized by verifySlab(). return 0, nil, nil, err } @@ -945,9 +945,9 @@ func getSlabIDFromStorable(storable Storable, ids []SlabID) []SlabID { return ids } -// validArrayValueID verifies array ValueID is always the same as +// verifyArrayValueID verifies array ValueID is always the same as // root slab's SlabID indepedent of array's inlined status. -func validArrayValueID(a *Array) error { +func verifyArrayValueID(a *Array) error { rootSlabID := a.root.Header().slabID vid := a.ValueID() @@ -973,9 +973,9 @@ func validArrayValueID(a *Array) error { return nil } -// validArraySlabID verifies array SlabID is either empty for inlined array, or +// verifyArraySlabID verifies array SlabID is either empty for inlined array, or // same as root slab's SlabID for not-inlined array. -func validArraySlabID(a *Array) error { +func verifyArraySlabID(a *Array) error { sid := a.SlabID() if a.Inlined() { diff --git a/array_test.go b/array_test.go index 0345f521..f14b79cc 100644 --- a/array_test.go +++ b/array_test.go @@ -109,7 +109,7 @@ func _verifyArray( require.Equal(t, len(values), i) // Verify in-memory slabs - err = ValidArray(array, address, typeInfo, typeInfoComparator, hashInputProvider, inlineEnabled) + err = VerifyArray(array, address, typeInfo, typeInfoComparator, hashInputProvider, inlineEnabled) if err != nil { PrintArray(array) } @@ -4663,7 +4663,7 @@ func TestSlabSizeWhenResettingMutableStorable(t *testing.T) { expectedArrayRootDataSlabSize := arrayRootDataSlabPrefixSize + initialStorableSize*arraySize require.Equal(t, uint32(expectedArrayRootDataSlabSize), array.root.ByteSize()) - err = ValidArray(array, address, typeInfo, typeInfoComparator, hashInputProvider, true) + err = VerifyArray(array, address, typeInfo, typeInfoComparator, hashInputProvider, true) require.NoError(t, err) for i := uint64(0); i < arraySize; i++ { @@ -4680,7 +4680,7 @@ func TestSlabSizeWhenResettingMutableStorable(t *testing.T) { expectedArrayRootDataSlabSize = arrayRootDataSlabPrefixSize + mutatedStorableSize*arraySize require.Equal(t, uint32(expectedArrayRootDataSlabSize), array.root.ByteSize()) - err = ValidArray(array, address, typeInfo, typeInfoComparator, hashInputProvider, true) + err = VerifyArray(array, address, typeInfo, typeInfoComparator, hashInputProvider, true) require.NoError(t, err) } diff --git a/map_debug.go b/map_debug.go index 8f94f0d3..cec40668 100644 --- a/map_debug.go +++ b/map_debug.go @@ -245,7 +245,7 @@ func DumpMapSlabs(m *OrderedMap) ([]string, error) { return dumps, nil } -func ValidMap(m *OrderedMap, address Address, typeInfo TypeInfo, tic TypeInfoComparator, hip HashInputProvider, inlineEnabled bool) error { +func VerifyMap(m *OrderedMap, address Address, typeInfo TypeInfo, tic TypeInfoComparator, hip HashInputProvider, inlineEnabled bool) error { // Verify map address if address != m.Address() { @@ -253,13 +253,13 @@ func ValidMap(m *OrderedMap, address Address, typeInfo TypeInfo, tic TypeInfoCom } // Verify map value ID - err := validMapValueID(m) + err := verifyMapValueID(m) if err != nil { return err } // Verify map slab ID - err = validMapSlabID(m) + err = verifyMapSlabID(m) if err != nil { return err } @@ -295,10 +295,10 @@ func ValidMap(m *OrderedMap, address Address, typeInfo TypeInfo, tic TypeInfoCom inlineEnabled: inlineEnabled, } - computedCount, dataSlabIDs, nextDataSlabIDs, firstKeys, err := v.verifyMapSlab( + computedCount, dataSlabIDs, nextDataSlabIDs, firstKeys, err := v.verifySlab( m.root, 0, nil, []SlabID{}, []SlabID{}, []Digest{}) if err != nil { - // Don't need to wrap error as external error because err is already categorized by validMapSlab(). + // Don't need to wrap error as external error because err is already categorized by verifySlab(). return err } @@ -349,7 +349,7 @@ type mapVerifier struct { inlineEnabled bool } -func (v *mapVerifier) verifyMapSlab( +func (v *mapVerifier) verifySlab( slab MapSlab, level int, headerFromParentSlab *MapSlabHeader, @@ -412,17 +412,17 @@ func (v *mapVerifier) verifyMapSlab( switch slab := slab.(type) { case *MapDataSlab: - return v.verifyMapDataSlab(slab, level, dataSlabIDs, nextDataSlabIDs, firstKeys) + return v.verifyDataSlab(slab, level, dataSlabIDs, nextDataSlabIDs, firstKeys) case *MapMetaDataSlab: - return v.verifyMapMetaDataSlab(slab, level, dataSlabIDs, nextDataSlabIDs, firstKeys) + return v.verifyMetaDataSlab(slab, level, dataSlabIDs, nextDataSlabIDs, firstKeys) default: return 0, nil, nil, nil, NewFatalError(fmt.Errorf("MapSlab is either *MapDataSlab or *MapMetaDataSlab, got %T", slab)) } } -func (v *mapVerifier) verifyMapDataSlab( +func (v *mapVerifier) verifyDataSlab( dataSlab *MapDataSlab, level int, dataSlabIDs []SlabID, @@ -442,9 +442,9 @@ func (v *mapVerifier) verifyMapDataSlab( } // Verify data slab's elements - elementCount, elementSize, err := v.verifyMapElements(id, dataSlab.elements, 0, nil) + elementCount, elementSize, err := v.verifyElements(id, dataSlab.elements, 0, nil) if err != nil { - // Don't need to wrap error as external error because err is already categorized by validMapElements(). + // Don't need to wrap error as external error because err is already categorized by verifyElements(). return 0, nil, nil, nil, err } @@ -501,7 +501,7 @@ func (v *mapVerifier) verifyMapDataSlab( return elementCount, dataSlabIDs, nextDataSlabIDs, firstKeys, nil } -func (v *mapVerifier) verifyMapMetaDataSlab( +func (v *mapVerifier) verifyMetaDataSlab( metaSlab *MapMetaDataSlab, level int, dataSlabIDs []SlabID, @@ -546,9 +546,9 @@ func (v *mapVerifier) verifyMapMetaDataSlab( // Verify child slabs count := uint64(0) count, dataSlabIDs, nextDataSlabIDs, firstKeys, err = - v.verifyMapSlab(childSlab, level+1, &h, dataSlabIDs, nextDataSlabIDs, firstKeys) + v.verifySlab(childSlab, level+1, &h, dataSlabIDs, nextDataSlabIDs, firstKeys) if err != nil { - // Don't need to wrap error as external error because err is already categorized by validMapSlab(). + // Don't need to wrap error as external error because err is already categorized by verifySlab(). return 0, nil, nil, nil, err } @@ -594,7 +594,7 @@ func (v *mapVerifier) verifyMapMetaDataSlab( return elementCount, dataSlabIDs, nextDataSlabIDs, firstKeys, nil } -func (v *mapVerifier) verifyMapElements( +func (v *mapVerifier) verifyElements( id SlabID, elements elements, digestLevel uint, @@ -607,15 +607,15 @@ func (v *mapVerifier) verifyMapElements( switch elems := elements.(type) { case *hkeyElements: - return v.verifyMapHkeyElements(id, elems, digestLevel, hkeyPrefixes) + return v.verifyHkeyElements(id, elems, digestLevel, hkeyPrefixes) case *singleElements: - return v.verifyMapSingleElements(id, elems, digestLevel, hkeyPrefixes) + return v.verifySingleElements(id, elems, digestLevel, hkeyPrefixes) default: return 0, 0, NewFatalError(fmt.Errorf("slab %d has unknown elements type %T at digest level %d", id, elements, digestLevel)) } } -func (v *mapVerifier) verifyMapHkeyElements( +func (v *mapVerifier) verifyHkeyElements( id SlabID, elements *hkeyElements, digestLevel uint, @@ -686,9 +686,9 @@ func (v *mapVerifier) verifyMapHkeyElements( copy(hkeys, hkeyPrefixes) hkeys[len(hkeys)-1] = elements.hkeys[i] - count, size, err := v.verifyMapElements(id, ge, digestLevel+1, hkeys) + count, size, err := v.verifyElements(id, ge, digestLevel+1, hkeys) if err != nil { - // Don't need to wrap error as external error because err is already categorized by validMapElement(). + // Don't need to wrap error as external error because err is already categorized by verifyElements(). return 0, 0, err } @@ -746,7 +746,7 @@ func (v *mapVerifier) verifyMapHkeyElements( return elementCount, elementSize, nil } -func (v *mapVerifier) verifyMapSingleElements( +func (v *mapVerifier) verifySingleElements( id SlabID, elements *singleElements, digestLevel uint, @@ -771,7 +771,7 @@ func (v *mapVerifier) verifyMapSingleElements( // Verify element computedSize, maxDigestLevel, err := v.verifySingleElement(e, hkeyPrefixes) if err != nil { - // Don't need to wrap error as external error because err is already categorized by validSingleElement(). + // Don't need to wrap error as external error because err is already categorized by verifySingleElement(). return 0, 0, fmt.Errorf("data slab %d: %w", id, err) } @@ -834,9 +834,9 @@ func (v *mapVerifier) verifySingleElement( return 0, 0, wrapErrorfAsExternalErrorIfNeeded(err, fmt.Sprintf("element %s key can't be converted to value", e)) } - err = ValidValue(kv, v.address, nil, v.tic, v.hip, v.inlineEnabled) + err = verifyValue(kv, v.address, nil, v.tic, v.hip, v.inlineEnabled) if err != nil { - // Don't need to wrap error as external error because err is already categorized by ValidValue(). + // Don't need to wrap error as external error because err is already categorized by verifyValue(). return 0, 0, fmt.Errorf("element %s key isn't valid: %w", e, err) } @@ -847,9 +847,9 @@ func (v *mapVerifier) verifySingleElement( return 0, 0, wrapErrorfAsExternalErrorIfNeeded(err, fmt.Sprintf("element %s value can't be converted to value", e)) } - err = ValidValue(vv, v.address, nil, v.tic, v.hip, v.inlineEnabled) + err = verifyValue(vv, v.address, nil, v.tic, v.hip, v.inlineEnabled) if err != nil { - // Don't need to wrap error as external error because err is already categorized by ValidValue(). + // Don't need to wrap error as external error because err is already categorized by verifyValue(). return 0, 0, fmt.Errorf("element %s value isn't valid: %w", e, err) } @@ -889,12 +889,12 @@ func (v *mapVerifier) verifySingleElement( return computedSize, digest.Levels(), nil } -func ValidValue(value Value, address Address, typeInfo TypeInfo, tic TypeInfoComparator, hip HashInputProvider, inlineEnabled bool) error { +func verifyValue(value Value, address Address, typeInfo TypeInfo, tic TypeInfoComparator, hip HashInputProvider, inlineEnabled bool) error { switch v := value.(type) { case *Array: - return ValidArray(v, address, typeInfo, tic, hip, inlineEnabled) + return VerifyArray(v, address, typeInfo, tic, hip, inlineEnabled) case *OrderedMap: - return ValidMap(v, address, typeInfo, tic, hip, inlineEnabled) + return VerifyMap(v, address, typeInfo, tic, hip, inlineEnabled) } return nil } @@ -1508,9 +1508,9 @@ func mapExtraDataEqual(expected, actual *MapExtraData) error { return nil } -// validMapValueID verifies map ValueID is always the same as +// verifyMapValueID verifies map ValueID is always the same as // root slab's SlabID indepedent of map's inlined status. -func validMapValueID(m *OrderedMap) error { +func verifyMapValueID(m *OrderedMap) error { rootSlabID := m.root.Header().slabID vid := m.ValueID() @@ -1536,9 +1536,9 @@ func validMapValueID(m *OrderedMap) error { return nil } -// validMapSlabID verifies map SlabID is either empty for inlined map, or +// verifyMapSlabID verifies map SlabID is either empty for inlined map, or // same as root slab's SlabID for not-inlined map. -func validMapSlabID(m *OrderedMap) error { +func verifyMapSlabID(m *OrderedMap) error { sid := m.SlabID() if m.Inlined() { diff --git a/map_test.go b/map_test.go index ed102c61..05ca3b03 100644 --- a/map_test.go +++ b/map_test.go @@ -182,7 +182,7 @@ func _verifyMap( } // Verify in-memory slabs - err = ValidMap(m, address, typeInfo, typeInfoComparator, hashInputProvider, inlineEnabled) + err = VerifyMap(m, address, typeInfo, typeInfoComparator, hashInputProvider, inlineEnabled) if err != nil { PrintMap(m) } @@ -10733,7 +10733,7 @@ func TestSlabSizeWhenResettingMutableStorableInMap(t *testing.T) { expectedMapRootDataSlabSize := mapRootDataSlabPrefixSize + hkeyElementsPrefixSize + expectedElementSize*mapSize require.Equal(t, expectedMapRootDataSlabSize, m.root.ByteSize()) - err = ValidMap(m, address, typeInfo, typeInfoComparator, hashInputProvider, true) + err = VerifyMap(m, address, typeInfo, typeInfoComparator, hashInputProvider, true) require.NoError(t, err) // Reset mutable values after changing its storable size @@ -10751,7 +10751,7 @@ func TestSlabSizeWhenResettingMutableStorableInMap(t *testing.T) { expectedMapRootDataSlabSize = mapRootDataSlabPrefixSize + hkeyElementsPrefixSize + expectedElementSize*mapSize require.Equal(t, expectedMapRootDataSlabSize, m.root.ByteSize()) - err = ValidMap(m, address, typeInfo, typeInfoComparator, hashInputProvider, true) + err = VerifyMap(m, address, typeInfo, typeInfoComparator, hashInputProvider, true) require.NoError(t, err) }