2019-05-27 13:51:49 +00:00
|
|
|
package dbutils
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/binary"
|
|
|
|
|
|
|
|
"github.com/ledgerwatch/turbo-geth/common"
|
|
|
|
)
|
|
|
|
|
|
|
|
// EncodeBlockNumber encodes a block number as big endian uint64
|
|
|
|
func EncodeBlockNumber(number uint64) []byte {
|
2021-03-05 20:34:23 +00:00
|
|
|
enc := make([]byte, 8)
|
2019-05-27 13:51:49 +00:00
|
|
|
binary.BigEndian.PutUint64(enc, number)
|
|
|
|
return enc
|
|
|
|
}
|
|
|
|
|
|
|
|
// headerKey = headerPrefix + num (uint64 big endian) + hash
|
|
|
|
func HeaderKey(number uint64, hash common.Hash) []byte {
|
|
|
|
return append(EncodeBlockNumber(number), hash.Bytes()...)
|
|
|
|
}
|
|
|
|
|
2019-12-06 09:02:43 +00:00
|
|
|
|
2019-05-27 13:51:49 +00:00
|
|
|
// blockBodyKey = blockBodyPrefix + num (uint64 big endian) + hash
|
|
|
|
func BlockBodyKey(number uint64, hash common.Hash) []byte {
|
|
|
|
return append(EncodeBlockNumber(number), hash.Bytes()...)
|
|
|
|
}
|
|
|
|
|
2020-10-25 08:38:55 +00:00
|
|
|
// ReceiptsKey = blockN (uint64 big endian)
|
|
|
|
func ReceiptsKey(blockNumber uint64) []byte {
|
|
|
|
newK := make([]byte, 8)
|
|
|
|
binary.BigEndian.PutUint64(newK, blockNumber)
|
|
|
|
return newK
|
|
|
|
}
|
|
|
|
|
|
|
|
// LogKey = blockN (uint64 big endian) + txId (uint32 big endian)
|
|
|
|
func LogKey(blockNumber uint64, txId uint32) []byte {
|
|
|
|
newK := make([]byte, 8+4)
|
|
|
|
binary.BigEndian.PutUint64(newK, blockNumber)
|
|
|
|
binary.BigEndian.PutUint32(newK[8:], txId)
|
|
|
|
return newK
|
2019-05-27 13:51:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// bloomBitsKey = bloomBitsPrefix + bit (uint16 big endian) + section (uint64 big endian) + hash
|
|
|
|
func BloomBitsKey(bit uint, section uint64, hash common.Hash) []byte {
|
|
|
|
key := append(make([]byte, 10), hash.Bytes()...)
|
|
|
|
|
|
|
|
binary.BigEndian.PutUint16(key[0:], uint16(bit))
|
|
|
|
binary.BigEndian.PutUint64(key[2:], section)
|
|
|
|
|
|
|
|
return key
|
|
|
|
}
|
|
|
|
|
2019-11-07 15:51:25 +00:00
|
|
|
// AddrHash + KeyHash
|
|
|
|
// Only for trie
|
2019-05-27 13:51:49 +00:00
|
|
|
func GenerateCompositeTrieKey(addressHash common.Hash, seckey common.Hash) []byte {
|
|
|
|
compositeKey := make([]byte, 0, common.HashLength+common.HashLength)
|
|
|
|
compositeKey = append(compositeKey, addressHash[:]...)
|
|
|
|
compositeKey = append(compositeKey, seckey[:]...)
|
|
|
|
return compositeKey
|
|
|
|
}
|
2019-11-07 15:51:25 +00:00
|
|
|
|
|
|
|
// AddrHash + incarnation + KeyHash
|
|
|
|
// For contract storage
|
2019-05-27 13:51:49 +00:00
|
|
|
func GenerateCompositeStorageKey(addressHash common.Hash, incarnation uint64, seckey common.Hash) []byte {
|
2020-07-17 14:18:31 +00:00
|
|
|
compositeKey := make([]byte, 0, common.HashLength+common.IncarnationLength+common.HashLength)
|
2020-04-23 09:35:43 +00:00
|
|
|
compositeKey = append(compositeKey, GenerateStoragePrefix(addressHash[:], incarnation)...)
|
2019-05-27 13:51:49 +00:00
|
|
|
compositeKey = append(compositeKey, seckey[:]...)
|
|
|
|
return compositeKey
|
|
|
|
}
|
2019-12-20 12:25:40 +00:00
|
|
|
|
2020-05-15 07:52:45 +00:00
|
|
|
func ParseCompositeStorageKey(compositeKey []byte) (common.Hash, uint64, common.Hash) {
|
|
|
|
prefixLen := common.HashLength + common.IncarnationLength
|
|
|
|
addrHash, inc := ParseStoragePrefix(compositeKey[:prefixLen])
|
|
|
|
var key common.Hash
|
|
|
|
copy(key[:], compositeKey[prefixLen:prefixLen+common.HashLength])
|
|
|
|
return addrHash, inc, key
|
|
|
|
}
|
|
|
|
|
|
|
|
// AddrHash + incarnation + KeyHash
|
|
|
|
// For contract storage (for plain state)
|
2020-12-08 09:44:29 +00:00
|
|
|
func PlainGenerateCompositeStorageKey(address []byte, incarnation uint64, key []byte) []byte {
|
2020-07-17 14:18:31 +00:00
|
|
|
compositeKey := make([]byte, common.AddressLength+common.IncarnationLength+common.HashLength)
|
2020-12-08 09:44:29 +00:00
|
|
|
copy(compositeKey, address)
|
2020-07-17 14:18:31 +00:00
|
|
|
binary.BigEndian.PutUint64(compositeKey[common.AddressLength:], incarnation)
|
2020-12-08 09:44:29 +00:00
|
|
|
copy(compositeKey[common.AddressLength+common.IncarnationLength:], key)
|
2020-05-15 07:52:45 +00:00
|
|
|
return compositeKey
|
|
|
|
}
|
|
|
|
|
|
|
|
func PlainParseCompositeStorageKey(compositeKey []byte) (common.Address, uint64, common.Hash) {
|
|
|
|
prefixLen := common.AddressLength + common.IncarnationLength
|
|
|
|
addr, inc := PlainParseStoragePrefix(compositeKey[:prefixLen])
|
|
|
|
var key common.Hash
|
|
|
|
copy(key[:], compositeKey[prefixLen:prefixLen+common.HashLength])
|
|
|
|
return addr, inc, key
|
|
|
|
}
|
|
|
|
|
2020-04-16 07:42:25 +00:00
|
|
|
// AddrHash + incarnation + StorageHashPrefix
|
|
|
|
func GenerateCompositeStoragePrefix(addressHash []byte, incarnation uint64, storageHashPrefix []byte) []byte {
|
2020-07-17 14:18:31 +00:00
|
|
|
key := make([]byte, common.HashLength+common.IncarnationLength+len(storageHashPrefix))
|
2020-04-16 07:42:25 +00:00
|
|
|
copy(key, addressHash)
|
2020-07-17 14:18:31 +00:00
|
|
|
binary.BigEndian.PutUint64(key[common.HashLength:], incarnation)
|
|
|
|
copy(key[common.HashLength+common.IncarnationLength:], storageHashPrefix)
|
2020-04-16 07:42:25 +00:00
|
|
|
return key
|
|
|
|
}
|
|
|
|
|
2019-12-20 12:25:40 +00:00
|
|
|
// address hash + incarnation prefix
|
2020-04-23 09:35:43 +00:00
|
|
|
func GenerateStoragePrefix(addressHash []byte, incarnation uint64) []byte {
|
2021-03-05 20:34:23 +00:00
|
|
|
prefix := make([]byte, common.HashLength+8)
|
2020-04-23 09:35:43 +00:00
|
|
|
copy(prefix, addressHash)
|
2020-07-17 14:18:31 +00:00
|
|
|
binary.BigEndian.PutUint64(prefix[common.HashLength:], incarnation)
|
2019-05-27 13:51:49 +00:00
|
|
|
return prefix
|
|
|
|
}
|
|
|
|
|
2020-05-15 07:52:45 +00:00
|
|
|
// address hash + incarnation prefix (for plain state)
|
2020-06-09 05:52:38 +00:00
|
|
|
func PlainGenerateStoragePrefix(address []byte, incarnation uint64) []byte {
|
2021-03-05 20:34:23 +00:00
|
|
|
prefix := make([]byte, common.AddressLength+8)
|
2020-06-09 05:52:38 +00:00
|
|
|
copy(prefix, address)
|
2020-07-17 14:18:31 +00:00
|
|
|
binary.BigEndian.PutUint64(prefix[common.AddressLength:], incarnation)
|
2020-05-15 07:52:45 +00:00
|
|
|
return prefix
|
|
|
|
}
|
|
|
|
|
|
|
|
func PlainParseStoragePrefix(prefix []byte) (common.Address, uint64) {
|
|
|
|
var addr common.Address
|
|
|
|
copy(addr[:], prefix[:common.AddressLength])
|
2020-07-17 14:18:31 +00:00
|
|
|
inc := binary.BigEndian.Uint64(prefix[common.AddressLength : common.AddressLength+common.IncarnationLength])
|
2020-05-15 07:52:45 +00:00
|
|
|
return addr, inc
|
|
|
|
}
|
|
|
|
|
|
|
|
func ParseStoragePrefix(prefix []byte) (common.Hash, uint64) {
|
|
|
|
var addrHash common.Hash
|
|
|
|
copy(addrHash[:], prefix[:common.HashLength])
|
2020-07-17 14:18:31 +00:00
|
|
|
inc := binary.BigEndian.Uint64(prefix[common.HashLength : common.HashLength+common.IncarnationLength])
|
2020-05-15 07:52:45 +00:00
|
|
|
return addrHash, inc
|
|
|
|
}
|
|
|
|
|
2019-11-07 15:51:25 +00:00
|
|
|
// Key + blockNum
|
|
|
|
func CompositeKeySuffix(key []byte, timestamp uint64) (composite, encodedTS []byte) {
|
|
|
|
encodedTS = EncodeTimestamp(timestamp)
|
|
|
|
composite = make([]byte, len(key)+len(encodedTS))
|
2019-05-27 13:51:49 +00:00
|
|
|
copy(composite, key)
|
2019-11-07 15:51:25 +00:00
|
|
|
copy(composite[len(key):], encodedTS)
|
|
|
|
return composite, encodedTS
|
|
|
|
}
|