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

View File

@ -26,6 +26,12 @@ import (
// The fields below define the low level database schema prefixing. // The fields below define the low level database schema prefixing.
var ( 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 tracks the current database version.
databaseVerisionKey = []byte("DatabaseVersion") databaseVerisionKey = []byte("DatabaseVersion")
@ -42,7 +48,7 @@ var (
fastTrieProgressKey = []byte("TrieSync") fastTrieProgressKey = []byte("TrieSync")
// Data item prefixes (use single byte to avoid mixing data types, avoid `i`, used for indexes). // 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 headerTDSuffix = []byte("t") // headerPrefix + num (uint64 big endian) + hash + headerTDSuffix -> td
headerHashSuffix = []byte("n") // headerPrefix + num (uint64 big endian) + headerHashSuffix -> hash headerHashSuffix = []byte("n") // headerPrefix + num (uint64 big endian) + headerHashSuffix -> hash
headerNumberPrefix = []byte("H") // headerNumberPrefix + hash -> num (uint64 big endian) headerNumberPrefix = []byte("H") // headerNumberPrefix + hash -> num (uint64 big endian)
@ -56,7 +62,7 @@ var (
// Optmism specific // Optmism specific
txMetaPrefix = []byte("x") // txMetaPrefix + hash -> transaction metadata 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 configPrefix = []byte("ethereum-config-") // config prefix for the db
// Chain index prefixes (use `i` + single byte to avoid mixing data types). // Chain index prefixes (use `i` + single byte to avoid mixing data types).
@ -108,67 +114,67 @@ func encodeBlockNumber(number uint64) []byte {
return enc return enc
} }
// headerKeyPrefix = headerPrefix + num (uint64 big endian) // headerKeyPrefix = headerPrefix + KeyDelineation + num (uint64 big endian) + NumberDelineation
func headerKeyPrefix(number uint64) []byte { 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 { 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 { 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 { 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 { 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 { 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 { 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 { func txLookupKey(hash common.Hash) []byte {
return append(txLookupPrefix, hash.Bytes()...) return append(append(txLookupPrefix, KeyDelineation...), hash.Bytes()...)
} }
// txMetaKey = txMetaPrefix + hash // txMetaKey = txMetaPrefix + hash
func txMetaKey(hash common.Hash) []byte { 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 { 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.PutUint16(key[2:], uint16(bit))
binary.BigEndian.PutUint64(key[3:], section) binary.BigEndian.PutUint64(key[4:], section)
return key return key
} }
// preimageKey = preimagePrefix + hash // preimageKey = preimagePrefix + KeyDelineation + hash
func preimageKey(hash common.Hash) []byte { 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 { 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" "sync"
"time" "time"
"github.com/ethereum/go-ethereum/core/rawdb"
"github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/common/mclock" "github.com/ethereum/go-ethereum/common/mclock"
"github.com/ethereum/go-ethereum/common/prque" "github.com/ethereum/go-ethereum/common/prque"
@ -654,16 +656,16 @@ const (
) )
var ( var (
positiveBalancePrefix = []byte("pb:") // dbVersion(uint16 big endian) + positiveBalancePrefix + id -> balance positiveBalancePrefix = []byte("pb") // dbVersion(uint16 big endian) + positiveBalancePrefix + id -> balance
negativeBalancePrefix = []byte("nb:") // dbVersion(uint16 big endian) + negativeBalancePrefix + ip -> balance negativeBalancePrefix = []byte("nb") // dbVersion(uint16 big endian) + negativeBalancePrefix + ip -> balance
cumulativeRunningTimeKey = []byte("cumulativeTime:") // dbVersion(uint16 big endian) + cumulativeRunningTimeKey -> cumulativeTime cumulativeRunningTimeKey = []byte("cumulativeTime") // dbVersion(uint16 big endian) + cumulativeRunningTimeKey -> cumulativeTime
) )
type nodeDB struct { type nodeDB struct {
db ethdb.Database db ethdb.Database
pcache *lru.Cache pcache *lru.Cache
ncache *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 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. nbEvictCallBack func(mclock.AbsTime, negBalance) bool // Callback to determine whether the negative balance can be evicted.
clock mclock.Clock clock mclock.Clock
@ -692,9 +694,9 @@ func (db *nodeDB) close() {
} }
func (db *nodeDB) key(id []byte, neg bool) []byte { func (db *nodeDB) key(id []byte, neg bool) []byte {
prefix := positiveBalancePrefix prefix := append(positiveBalancePrefix, rawdb.KeyDelineation...)
if neg { if neg {
prefix = negativeBalancePrefix prefix = append(negativeBalancePrefix, rawdb.KeyDelineation...)
} }
if len(prefix)+len(db.verbuf)+len(id) > len(db.auxbuf) { 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))...) 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. // secureKeyPrefix is the database key prefix used to store trie node preimages.
var secureKeyPrefix = []byte("secure-key-") var secureKeyPrefix = []byte("secure-key-")
// secureKeyLength is the length of the above prefix + 32byte hash. // keyDelineation delineates a prefix and/or suffix from the rest of the key
const secureKeyLength = 11 + 32 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 // 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 // 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 // buffer. The caller must not hold onto the return value because it will become
// invalid on the next call. // invalid on the next call.
func (db *Database) secureKey(key []byte) []byte { 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...) buf = append(buf, key...)
return buf return buf
} }