@@ -28,7 +28,10 @@ type CreateFunc func() interface{}
2828type HandleFunc func () error
2929type IterationFunc func () (CheckFunc , CreateFunc , HandleFunc )
3030
31- // IterateKeysInPrefixRange will iterate over all keys with prefixes in the range [startPrefix, endPrefix] (both inclusive)
31+ // IterateKeysInPrefixRange will iterate over all entries in the database, where the key starts with a prefixes in
32+ // the range [startPrefix, endPrefix] (both inclusive). We require that startPrefix <= endPrefix (otherwise this
33+ // function errors). On every such key, the `check` function is called. If `check` errors, iteration is aborted.
34+ // No errors expected during normal operations.
3235func IterateKeysInPrefixRange (startPrefix []byte , endPrefix []byte , check func (key []byte ) error ) func (storage.Reader ) error {
3336 return Iterate (startPrefix , endPrefix , func () (CheckFunc , CreateFunc , HandleFunc ) {
3437 return func (key []byte ) (bool , error ) {
@@ -164,8 +167,10 @@ func Retrieve(key []byte, entity interface{}) func(storage.Reader) error {
164167 }
165168}
166169
167- // FindHighestAtOrBelow finds the highest key with the given prefix and
168- // height equal to or below the given height.
170+ // FindHighestAtOrBelow is for database entries that are indexed by block height. It is suitable to search
171+ // keys with the format prefix` + `height` (where "+" denotes concatenation of binary strings). The height
172+ // is encoded as Big-Endian (entries with numerically smaller height have lexicographically smaller key).
173+ // The function finds the *highest* key with the given prefix and height equal to or below the given height.
169174func FindHighestAtOrBelow (prefix []byte , height uint64 , entity interface {}) func (storage.Reader ) error {
170175 return func (r storage.Reader ) error {
171176 if len (prefix ) == 0 {
@@ -180,9 +185,12 @@ func FindHighestAtOrBelow(prefix []byte, height uint64, entity interface{}) func
180185 defer it .Close ()
181186
182187 var highestKey []byte
188+
183189 // find highest value below the given height
184190 for it .First (); it .Valid (); it .Next () {
185- highestKey = it .IterItem ().Key ()
191+ // copy the key to avoid the underlying slices of the key
192+ // being modified by the Next() call
193+ highestKey = it .IterItem ().KeyCopy (highestKey )
186194 }
187195
188196 if len (highestKey ) == 0 {
0 commit comments