Skip to content

Commit

Permalink
Refactor array and map validation
Browse files Browse the repository at this point in the history
  • Loading branch information
fxamacker committed Sep 24, 2023
1 parent 5af0bc4 commit a71e7f8
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 57 deletions.
36 changes: 18 additions & 18 deletions array_debug.go
Original file line number Diff line number Diff line change
Expand Up @@ -167,21 +167,21 @@ 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() {
return NewFatalError(fmt.Errorf("array address %v, got %v", address, a.Address()))
}

// 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
}
Expand Down Expand Up @@ -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
}

Expand All @@ -239,7 +239,7 @@ type arrayVerifier struct {
inlineEnabled bool
}

func (v *arrayVerifier) verifyArraySlab(
func (v *arrayVerifier) verifySlab(
slab ArraySlab,
level int,
headerFromParentSlab *ArraySlabHeader,
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand All @@ -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,
Expand Down Expand Up @@ -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
}

Expand Down Expand Up @@ -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()
Expand All @@ -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() {
Expand Down
6 changes: 3 additions & 3 deletions array_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down Expand Up @@ -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++ {
Expand All @@ -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)
}

Expand Down
66 changes: 33 additions & 33 deletions map_debug.go
Original file line number Diff line number Diff line change
Expand Up @@ -245,21 +245,21 @@ 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() {
return NewFatalError(fmt.Errorf("map address %v, got %v", address, m.Address()))
}

// 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
}
Expand Down Expand Up @@ -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
}

Expand Down Expand Up @@ -349,7 +349,7 @@ type mapVerifier struct {
inlineEnabled bool
}

func (v *mapVerifier) verifyMapSlab(
func (v *mapVerifier) verifySlab(
slab MapSlab,
level int,
headerFromParentSlab *MapSlabHeader,
Expand Down Expand Up @@ -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,
Expand All @@ -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
}

Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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
}

Expand Down Expand Up @@ -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,
Expand All @@ -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,
Expand Down Expand Up @@ -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
}

Expand Down Expand Up @@ -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,
Expand All @@ -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)
}

Expand Down Expand Up @@ -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)
}

Expand All @@ -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)
}

Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -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()
Expand All @@ -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() {
Expand Down
Loading

0 comments on commit a71e7f8

Please sign in to comment.