github.com/evdatsion/aphelion-dpos-bft@v0.32.1/libs/db/util.go (about)

     1  package db
     2  
     3  import (
     4  	"bytes"
     5  )
     6  
     7  func cp(bz []byte) (ret []byte) {
     8  	ret = make([]byte, len(bz))
     9  	copy(ret, bz)
    10  	return ret
    11  }
    12  
    13  // Returns a slice of the same length (big endian)
    14  // except incremented by one.
    15  // Returns nil on overflow (e.g. if bz bytes are all 0xFF)
    16  // CONTRACT: len(bz) > 0
    17  func cpIncr(bz []byte) (ret []byte) {
    18  	if len(bz) == 0 {
    19  		panic("cpIncr expects non-zero bz length")
    20  	}
    21  	ret = cp(bz)
    22  	for i := len(bz) - 1; i >= 0; i-- {
    23  		if ret[i] < byte(0xFF) {
    24  			ret[i]++
    25  			return
    26  		}
    27  		ret[i] = byte(0x00)
    28  		if i == 0 {
    29  			// Overflow
    30  			return nil
    31  		}
    32  	}
    33  	return nil
    34  }
    35  
    36  // See DB interface documentation for more information.
    37  func IsKeyInDomain(key, start, end []byte) bool {
    38  	if bytes.Compare(key, start) < 0 {
    39  		return false
    40  	}
    41  	if end != nil && bytes.Compare(end, key) <= 0 {
    42  		return false
    43  	}
    44  	return true
    45  }