mirror of
https://gitlab.com/pulsechaincom/erigon-pulse.git
synced 2024-12-25 21:17:16 +00:00
aedecfc820
fmt badger changes badget puts fix shadowing fix mutation test. restore DecodeChangeSet fix pruner test
44 lines
1.1 KiB
Go
44 lines
1.1 KiB
Go
package dbutils
|
|
|
|
import "bytes"
|
|
|
|
// EncodeTimestamp has the property: if a < b, then Encoding(a) < Encoding(b) lexicographically
|
|
func EncodeTimestamp(timestamp uint64) []byte {
|
|
var suffix []byte
|
|
var limit uint64 = 32
|
|
|
|
for bytecount := 1; bytecount <= 8; bytecount++ {
|
|
if timestamp < limit {
|
|
suffix = make([]byte, bytecount)
|
|
b := timestamp
|
|
for i := bytecount - 1; i > 0; i-- {
|
|
suffix[i] = byte(b & 0xff)
|
|
b >>= 8
|
|
}
|
|
suffix[0] = byte(b) | (byte(bytecount) << 5) // 3 most significant bits of the first byte are bytecount
|
|
break
|
|
}
|
|
limit <<= 8
|
|
}
|
|
return suffix
|
|
}
|
|
|
|
func DecodeTimestamp(suffix []byte) (uint64, []byte) {
|
|
bytecount := int(suffix[0] >> 5)
|
|
timestamp := uint64(suffix[0] & 0x1f)
|
|
for i := 1; i < bytecount; i++ {
|
|
timestamp = (timestamp << 8) | uint64(suffix[i])
|
|
}
|
|
return timestamp, suffix[bytecount:]
|
|
}
|
|
|
|
func ChangeSetByIndexBucket(b []byte) []byte {
|
|
if bytes.Equal(b, AccountsHistoryBucket) {
|
|
return AccountChangeSetBucket
|
|
}
|
|
if bytes.Equal(b, StorageHistoryBucket) {
|
|
return StorageChangeSetBucket
|
|
}
|
|
panic("wrong bucket")
|
|
}
|