erigon-pulse/common/dbutils/composite_keys.go
b00ris bcee4845dc
Thin history (#272)
* save state

* remove repair

* save state

* remove emptydb check

* save state

* add walkAsOf test

* add WalkAsOf and MultiWalkAsOf tests

* deployed contracts counter

* reference counter for contract code

* drop storage root&contract hash for changesets

* start incarnation is 1(save state)

* fix ReorgOverSelfDestruct test

* hack fix TestReorgOverSelfDestruct

* test benchmark

* cleanup

* remove useless debug

* remove print trie

* return remove subtrie call  to updateTrieRoot

* save state

* add mutation test

* remove useless test

* fix

* added mutation commit test

* rename experiment to thin history

* thin history mutation commit test

* fix ethdb tests

* getAsOf test

* add test&fix history index

* fix test

* make test for index search

* compute trie root incarnation fix

* tests fixes

* done job in case of panic

* fix lint

* fix&test for bad incarnation

* fix initial incarnation for genesis

* fix lint

* fix changeset test

* fix storage ranges test

* fix lint

* move set incarnation to create contract

* add comment

Co-authored-by: ledgerwatch <akhounov@gmail.com>
Co-authored-by: Evgeny Danilenko <6655321@bk.ru>
2019-12-20 15:25:40 +03:00

137 lines
4.1 KiB
Go

package dbutils
import (
"bytes"
"encoding/binary"
"github.com/ledgerwatch/turbo-geth/common"
)
// EncodeBlockNumber encodes a block number as big endian uint64
func EncodeBlockNumber(number uint64) []byte {
enc := make([]byte, 8)
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()...)
}
func IsHeaderKey(k []byte) bool {
l := common.BlockNumberLength + common.HashLength
if len(k) != l {
return false
}
return !IsHeaderHashKey(k) && !IsHeaderTDKey(k)
}
// headerTDKey = headerPrefix + num (uint64 big endian) + hash + headerTDSuffix
func HeaderTDKey(number uint64, hash common.Hash) []byte {
return append(HeaderKey(number, hash), HeaderTDSuffix...)
}
func IsHeaderTDKey(k []byte) bool {
l := common.BlockNumberLength + common.HashLength + 1
return len(k) == l && bytes.Equal(k[l-1:], HeaderTDSuffix)
}
// headerHashKey = headerPrefix + num (uint64 big endian) + headerHashSuffix
func HeaderHashKey(number uint64) []byte {
return append(EncodeBlockNumber(number), HeaderHashSuffix...)
}
func IsHeaderHashKey(k []byte) bool {
l := common.BlockNumberLength + 1
return len(k) == l && bytes.Equal(k[l-1:], HeaderHashSuffix)
}
// headerNumberKey = headerNumberPrefix + hash
func HeaderNumberKey(hash common.Hash) []byte {
return append(HeaderNumberPrefix, hash.Bytes()...)
}
// blockBodyKey = blockBodyPrefix + num (uint64 big endian) + hash
func BlockBodyKey(number uint64, hash common.Hash) []byte {
return append(EncodeBlockNumber(number), hash.Bytes()...)
}
// blockReceiptsKey = blockReceiptsPrefix + num (uint64 big endian) + hash
func BlockReceiptsKey(number uint64, hash common.Hash) []byte {
return append(EncodeBlockNumber(number), hash.Bytes()...)
}
// txLookupKey = txLookupPrefix + hash
func TxLookupKey(hash common.Hash) []byte {
return append(TxLookupPrefix, hash.Bytes()...)
}
// 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
}
// preimageKey = preimagePrefix + hash
func PreimageKey(hash common.Hash) []byte {
return append(PreimagePrefix, hash.Bytes()...)
}
// configKey = configPrefix + hash
func ConfigKey(hash common.Hash) []byte {
return append(ConfigPrefix, hash.Bytes()...)
}
// AddrHash + KeyHash
// Only for trie
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
}
// AddrHash + incarnation + KeyHash
// For contract storage
func GenerateCompositeStorageKey(addressHash common.Hash, incarnation uint64, seckey common.Hash) []byte {
compositeKey := make([]byte, 0, common.HashLength+8+common.HashLength)
compositeKey = append(compositeKey, GenerateStoragePrefix(addressHash, incarnation)...)
compositeKey = append(compositeKey, seckey[:]...)
return compositeKey
}
// address hash + incarnation prefix
func GenerateStoragePrefix(addressHash common.Hash, incarnation uint64) []byte {
prefix := make([]byte, 0, common.HashLength+8)
prefix = append(prefix, addressHash[:]...)
//todo pool
buf := make([]byte, 8)
binary.BigEndian.PutUint64(buf, incarnation^^uint64(0))
prefix = append(prefix, buf...)
return prefix
}
// Key + blockNum
func CompositeKeySuffix(key []byte, timestamp uint64) (composite, encodedTS []byte) {
encodedTS = EncodeTimestamp(timestamp)
composite = make([]byte, len(key)+len(encodedTS))
copy(composite, key)
copy(composite[len(key):], encodedTS)
return composite, encodedTS
}
// blockNum + history bucket
func CompositeChangeSetKey(encodedTS, hBucket []byte) []byte {
changeSetKey := make([]byte, len(encodedTS)+len(hBucket))
copy(changeSetKey, encodedTS)
copy(changeSetKey[len(encodedTS):], hBucket)
return changeSetKey
}