Skip to content

Commit

Permalink
Check composite field count during validation
Browse files Browse the repository at this point in the history
This is a temporary (and inefficient) workaround to check number
of fields in Cadence CompositeValue.

In the future, CompositeValue.FieldCount() will be added to
Cadence, so that function can be used to improve this workaround.
  • Loading branch information
fxamacker committed Jan 9, 2024
1 parent 706b5a5 commit d56df32
Showing 1 changed file with 18 additions and 0 deletions.
18 changes: 18 additions & 0 deletions cmd/util/ledger/migrations/cadence_value_validation.go
Original file line number Diff line number Diff line change
Expand Up @@ -273,6 +273,7 @@ func cadenceCompositeValueEqual(
}

var err *validationError
vFieldNames := make([]string, 0, 10) // v's field names
v.ForEachField(nopMemoryGauge, func(fieldName string, fieldValue interpreter.Value) bool {
otherFieldValue := otherComposite.GetField(otherInterpreter, interpreter.EmptyLocationRange, fieldName)

Expand All @@ -282,9 +283,26 @@ func cadenceCompositeValueEqual(
return false
}

vFieldNames = append(vFieldNames, fieldName)
return true
})

// TODO: Use CompositeValue.FieldCount() from Cadence after it is merged and available.
otherFieldNames := make([]string, 0, len(vFieldNames)) // otherComposite's field names
otherComposite.ForEachField(nopMemoryGauge, func(fieldName string, _ interpreter.Value) bool {
otherFieldNames = append(otherFieldNames, fieldName)
return true
})

if len(vFieldNames) != len(otherFieldNames) {
return newValidationErrorf(
"composite %s fields differ: %v != %v",
v.TypeID(),
vFieldNames,
otherFieldNames,
)
}

return err
}

Expand Down

0 comments on commit d56df32

Please sign in to comment.