iotaledger / hive.go

A Go library containing data structures, various utils and abstractions which are used by both GoShimmer and Hornet.
Apache License 2.0
61 stars 24 forks source link

Fix Must Occur serix validation #614

Closed PhilippGackstatter closed 11 months ago

PhilippGackstatter commented 11 months ago

Description of change

Fixes/Implements Must Occur serix validation for arrays.

This functionality is implemented in ReadSliceOfObjects, which is never called except in tests.

The way it's implemented trades readability and deduplication over performance. It would have been possible to implement the check on the bytes directly, which would have been more efficient than iterating separately over the slice and over the reflection types. However, obtaining the correct type denotation for the already-serialized objects is not trivial, but needed in order to read the correct number of bytes (uint8 vs uint32 for the two possible type denotations). It would have likely also resulted in an implementation that would be integrated in the existing code, which makes it less readable and reusable.

The way it is implemented now is self-contained in a function and simply called four times from encoding/decoding and map encoding/decoding.

Type of change

Choose a type of change, and delete any options that are not relevant.

How the change has been tested

Tests were added.

Existing test frameworks like serializeTest and deSerializeTest were not suitable for reuse, so new ones were added.