adjust key prefixes to work with pg

This commit is contained in:
Ian Norden 2020-09-18 09:10:13 -05:00
parent 857214d512
commit c0b4f6fe24
4 changed files with 52 additions and 40 deletions

View File

@ -266,11 +266,11 @@ func InspectDatabase(db ethdb.Database) error {
)
total += size
switch {
case bytes.HasPrefix(key, headerPrefix) && bytes.HasSuffix(key, headerTDSuffix):
case bytes.HasPrefix(key, HeaderPrefix) && bytes.HasSuffix(key, headerTDSuffix):
tdSize += size
case bytes.HasPrefix(key, headerPrefix) && bytes.HasSuffix(key, headerHashSuffix):
case bytes.HasPrefix(key, HeaderPrefix) && bytes.HasSuffix(key, headerHashSuffix):
numHashPairing += size
case bytes.HasPrefix(key, headerPrefix) && len(key) == (len(headerPrefix)+8+common.HashLength):
case bytes.HasPrefix(key, HeaderPrefix) && len(key) == (len(HeaderPrefix)+8+common.HashLength):
headerSize += size
case bytes.HasPrefix(key, headerNumberPrefix) && len(key) == (len(headerNumberPrefix)+common.HashLength):
hashNumPairing += size
@ -280,7 +280,7 @@ func InspectDatabase(db ethdb.Database) error {
receiptSize += size
case bytes.HasPrefix(key, txLookupPrefix) && len(key) == (len(txLookupPrefix)+common.HashLength):
txlookupSize += size
case bytes.HasPrefix(key, preimagePrefix) && len(key) == (len(preimagePrefix)+common.HashLength):
case bytes.HasPrefix(key, PreimagePrefix) && len(key) == (len(PreimagePrefix)+common.HashLength):
preimageSize += size
case bytes.HasPrefix(key, bloomBitsPrefix) && len(key) == (len(bloomBitsPrefix)+10+common.HashLength):
bloomBitsSize += size

View File

@ -26,6 +26,12 @@ import (
// The fields below define the low level database schema prefixing.
var (
// KeyDelineation is used to delineate the key prefixes and suffixes
KeyDelineation = []byte("-fix-")
// NumberDelineation is used to delineate the block number encoded in a key
NumberDelineation = []byte("-nmb-")
// databaseVerisionKey tracks the current database version.
databaseVerisionKey = []byte("DatabaseVersion")
@ -42,7 +48,7 @@ var (
fastTrieProgressKey = []byte("TrieSync")
// Data item prefixes (use single byte to avoid mixing data types, avoid `i`, used for indexes).
headerPrefix = []byte("h") // headerPrefix + num (uint64 big endian) + hash -> header
HeaderPrefix = []byte("h") // headerPrefix + num (uint64 big endian) + hash -> header
headerTDSuffix = []byte("t") // headerPrefix + num (uint64 big endian) + hash + headerTDSuffix -> td
headerHashSuffix = []byte("n") // headerPrefix + num (uint64 big endian) + headerHashSuffix -> hash
headerNumberPrefix = []byte("H") // headerNumberPrefix + hash -> num (uint64 big endian)
@ -56,7 +62,7 @@ var (
// Optmism specific
txMetaPrefix = []byte("x") // txMetaPrefix + hash -> transaction metadata
preimagePrefix = []byte("secure-key-") // preimagePrefix + hash -> preimage
PreimagePrefix = []byte("secure-key-") // preimagePrefix + hash -> preimage
configPrefix = []byte("ethereum-config-") // config prefix for the db
// Chain index prefixes (use `i` + single byte to avoid mixing data types).
@ -108,67 +114,67 @@ func encodeBlockNumber(number uint64) []byte {
return enc
}
// headerKeyPrefix = headerPrefix + num (uint64 big endian)
// headerKeyPrefix = headerPrefix + KeyDelineation + num (uint64 big endian) + NumberDelineation
func headerKeyPrefix(number uint64) []byte {
return append(headerPrefix, encodeBlockNumber(number)...)
return append(append(append(HeaderPrefix, KeyDelineation...), encodeBlockNumber(number)...), NumberDelineation...)
}
// headerKey = headerPrefix + num (uint64 big endian) + hash
// headerKey = HeaderPrefix + KeyDelineation + num (uint64 big endian) + NumberDelineation + hash
func headerKey(number uint64, hash common.Hash) []byte {
return append(append(headerPrefix, encodeBlockNumber(number)...), hash.Bytes()...)
return append(append(append(append(HeaderPrefix, KeyDelineation...), encodeBlockNumber(number)...), NumberDelineation...), hash.Bytes()...)
}
// headerTDKey = headerPrefix + num (uint64 big endian) + hash + headerTDSuffix
// headerTDKey = HeaderPrefix + KeyDelineation + num (uint64 big endian) + NumberDelineation + hash + KeyDelineation + headerTDSuffix
func headerTDKey(number uint64, hash common.Hash) []byte {
return append(headerKey(number, hash), headerTDSuffix...)
return append(append(headerKey(number, hash), KeyDelineation...), headerTDSuffix...)
}
// headerHashKey = headerPrefix + num (uint64 big endian) + headerHashSuffix
// headerHashKey = HeaderPrefix + KeyDelineation + num (uint64 big endian) + NumberDelineation + KeyDelineation + headerHashSuffix
func headerHashKey(number uint64) []byte {
return append(append(headerPrefix, encodeBlockNumber(number)...), headerHashSuffix...)
return append(append(append(append(append(HeaderPrefix, KeyDelineation...), encodeBlockNumber(number)...), NumberDelineation...), KeyDelineation...), headerHashSuffix...)
}
// headerNumberKey = headerNumberPrefix + hash
// headerNumberKey = headerNumberPrefix + KeyDelineation + hash
func headerNumberKey(hash common.Hash) []byte {
return append(headerNumberPrefix, hash.Bytes()...)
return append(append(headerNumberPrefix, KeyDelineation...), hash.Bytes()...)
}
// blockBodyKey = blockBodyPrefix + num (uint64 big endian) + hash
// blockBodyKey = blockBodyPrefix + KeyDelineation + num (uint64 big endian) + NumberDelineation + hash
func blockBodyKey(number uint64, hash common.Hash) []byte {
return append(append(blockBodyPrefix, encodeBlockNumber(number)...), hash.Bytes()...)
return append(append(append(append(blockBodyPrefix, KeyDelineation...), encodeBlockNumber(number)...), NumberDelineation...), hash.Bytes()...)
}
// blockReceiptsKey = blockReceiptsPrefix + num (uint64 big endian) + hash
// blockReceiptsKey = blockReceiptsPrefix + KeyDelineation + num (uint64 big endian) + NumberDelineation + hash
func blockReceiptsKey(number uint64, hash common.Hash) []byte {
return append(append(blockReceiptsPrefix, encodeBlockNumber(number)...), hash.Bytes()...)
return append(append(append(append(blockReceiptsPrefix, KeyDelineation...), encodeBlockNumber(number)...), NumberDelineation...), hash.Bytes()...)
}
// txLookupKey = txLookupPrefix + hash
// txLookupKey = txLookupPrefix + KeyDelineation + hash
func txLookupKey(hash common.Hash) []byte {
return append(txLookupPrefix, hash.Bytes()...)
return append(append(txLookupPrefix, KeyDelineation...), hash.Bytes()...)
}
// txMetaKey = txMetaPrefix + hash
func txMetaKey(hash common.Hash) []byte {
return append(txMetaPrefix, hash.Bytes()...)
return append(append(txMetaPrefix, KeyDelineation...), hash.Bytes()...)
}
// bloomBitsKey = bloomBitsPrefix + bit (uint16 big endian) + section (uint64 big endian) + hash
// bloomBitsKey = bloomBitsPrefix + KeyDelineation + bit (uint16 big endian) + section (uint64 big endian) + hash
func bloomBitsKey(bit uint, section uint64, hash common.Hash) []byte {
key := append(append(bloomBitsPrefix, make([]byte, 10)...), hash.Bytes()...)
key := append(append(append(bloomBitsPrefix, KeyDelineation...), make([]byte, 10)...), hash.Bytes()...)
binary.BigEndian.PutUint16(key[1:], uint16(bit))
binary.BigEndian.PutUint64(key[3:], section)
binary.BigEndian.PutUint16(key[2:], uint16(bit))
binary.BigEndian.PutUint64(key[4:], section)
return key
}
// preimageKey = preimagePrefix + hash
// preimageKey = preimagePrefix + KeyDelineation + hash
func preimageKey(hash common.Hash) []byte {
return append(preimagePrefix, hash.Bytes()...)
return append(append(PreimagePrefix, KeyDelineation...), hash.Bytes()...)
}
// configKey = configPrefix + hash
// configKey = configPrefix + KeyDelineation + hash
func configKey(hash common.Hash) []byte {
return append(configPrefix, hash.Bytes()...)
return append(append(configPrefix, KeyDelineation...), hash.Bytes()...)
}

View File

@ -25,6 +25,8 @@ import (
"sync"
"time"
"github.com/ethereum/go-ethereum/core/rawdb"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/common/mclock"
"github.com/ethereum/go-ethereum/common/prque"
@ -654,16 +656,16 @@ const (
)
var (
positiveBalancePrefix = []byte("pb:") // dbVersion(uint16 big endian) + positiveBalancePrefix + id -> balance
negativeBalancePrefix = []byte("nb:") // dbVersion(uint16 big endian) + negativeBalancePrefix + ip -> balance
cumulativeRunningTimeKey = []byte("cumulativeTime:") // dbVersion(uint16 big endian) + cumulativeRunningTimeKey -> cumulativeTime
positiveBalancePrefix = []byte("pb") // dbVersion(uint16 big endian) + positiveBalancePrefix + id -> balance
negativeBalancePrefix = []byte("nb") // dbVersion(uint16 big endian) + negativeBalancePrefix + ip -> balance
cumulativeRunningTimeKey = []byte("cumulativeTime") // dbVersion(uint16 big endian) + cumulativeRunningTimeKey -> cumulativeTime
)
type nodeDB struct {
db ethdb.Database
pcache *lru.Cache
ncache *lru.Cache
auxbuf []byte // 37-byte auxiliary buffer for key encoding
auxbuf []byte // 41-byte auxiliary buffer for key encoding
verbuf [2]byte // 2-byte auxiliary buffer for db version
nbEvictCallBack func(mclock.AbsTime, negBalance) bool // Callback to determine whether the negative balance can be evicted.
clock mclock.Clock
@ -692,9 +694,9 @@ func (db *nodeDB) close() {
}
func (db *nodeDB) key(id []byte, neg bool) []byte {
prefix := positiveBalancePrefix
prefix := append(positiveBalancePrefix, rawdb.KeyDelineation...)
if neg {
prefix = negativeBalancePrefix
prefix = append(negativeBalancePrefix, rawdb.KeyDelineation...)
}
if len(prefix)+len(db.verbuf)+len(id) > len(db.auxbuf) {
db.auxbuf = append(db.auxbuf, make([]byte, len(prefix)+len(db.verbuf)+len(id)-len(db.auxbuf))...)

View File

@ -59,8 +59,11 @@ var (
// secureKeyPrefix is the database key prefix used to store trie node preimages.
var secureKeyPrefix = []byte("secure-key-")
// secureKeyLength is the length of the above prefix + 32byte hash.
const secureKeyLength = 11 + 32
// keyDelineation delineates a prefix and/or suffix from the rest of the key
var keyDelineation = []byte("-fix-")
// secureKeyLength is the length of the above prefix + KeyDelineator + 32byte hash
const secureKeyLength = 11 + 5 + 32
// Database is an intermediate write layer between the trie data structures and
// the disk database. The aim is to accumulate trie writes in-memory and only
@ -452,7 +455,8 @@ func (db *Database) preimage(hash common.Hash) ([]byte, error) {
// buffer. The caller must not hold onto the return value because it will become
// invalid on the next call.
func (db *Database) secureKey(key []byte) []byte {
buf := append(db.seckeybuf[:0], secureKeyPrefix...)
delineatedPrefix := append(secureKeyPrefix, keyDelineation...)
buf := append(db.seckeybuf[:0], delineatedPrefix...)
buf = append(buf, key...)
return buf
}