erigon-pulse/common/dbutils/bucket.go

289 lines
8.5 KiB
Go
Raw Normal View History

package dbutils
import (
"sort"
2020-08-10 23:55:32 +00:00
"strings"
"github.com/ledgerwatch/turbo-geth/metrics"
)
// Buckets
var (
// "Plain State". The same as CurrentStateBucket, but the keys arent' hashed.
/*
Logical layout:
Contains Accounts:
key - address (unhashed)
value - account encoded for storage
Contains Storage:
key - address (unhashed) + incarnation + storage key (unhashed)
value - storage value(common.hash)
Physical layout:
PlainStateBucket and CurrentStateBucket utilises DupSort feature of LMDB (store multiple values inside 1 key).
-------------------------------------------------------------
key | value
-------------------------------------------------------------
[acc_hash] | [acc_value]
[acc_hash]+[inc] | [storage1_hash]+[storage1_value]
| [storage2_hash]+[storage2_value] // this value has no own key. it's 2nd value of [acc_hash]+[inc] key.
| [storage3_hash]+[storage3_value]
| ...
[acc_hash]+[old_inc] | [storage1_hash]+[storage1_value]
| ...
[acc2_hash] | [acc2_value]
...
*/
PlainStateBucket = "PLAIN-CST2"
PlainStateBucketOld1 = "PLAIN-CST"
// "Plain State"
//key - address+incarnation
//value - code hash
2020-08-10 23:55:32 +00:00
PlainContractCodeBucket = "PLAIN-contractCode"
// PlainAccountChangeSetBucket keeps changesets of accounts ("plain state")
// key - encoded timestamp(block number)
// value - encoded ChangeSet{k - address v - account(encoded).
2020-08-10 23:55:32 +00:00
PlainAccountChangeSetBucket = "PLAIN-ACS"
// PlainStorageChangeSetBucket keeps changesets of storage ("plain state")
// key - encoded timestamp(block number)
// value - encoded ChangeSet{k - plainCompositeKey(for storage) v - originalValue(common.Hash)}.
2020-08-10 23:55:32 +00:00
PlainStorageChangeSetBucket = "PLAIN-SCS"
// Contains Accounts:
// key - address hash
// value - account encoded for storage
// Contains Storage:
//key - address hash + incarnation + storage key hash
//value - storage value(common.hash)
2020-08-12 02:57:55 +00:00
CurrentStateBucket = "CST2"
CurrentStateBucketOld1 = "CST"
//current
//key - key + encoded timestamp(block number)
//value - account for storage(old/original value)
//layout experiment
//key - address hash
//value - list of block where it's changed
2020-08-10 23:55:32 +00:00
AccountsHistoryBucket = "hAT"
//current
//key - address hash + incarnation + storage key hash
//value - storage value(common.hash)
//layout experiment
//key - address hash
//value - list of block where it's changed
2020-08-10 23:55:32 +00:00
StorageHistoryBucket = "hST"
//key - contract code hash
//value - contract code
2020-08-10 23:55:32 +00:00
CodeBucket = "CODE"
//key - addressHash+incarnation
//value - code hash
2020-08-10 23:55:32 +00:00
ContractCodeBucket = "contractCode"
// Incarnations for deleted accounts
//key - address
//value - incarnation of account when it was last deleted
2020-08-10 23:55:32 +00:00
IncarnationMapBucket = "incarnationMap"
//AccountChangeSetBucket keeps changesets of accounts
// key - encoded timestamp(block number)
// value - encoded ChangeSet{k - addrHash v - account(encoded).
2020-08-10 23:55:32 +00:00
AccountChangeSetBucket = "ACS"
// StorageChangeSetBucket keeps changesets of storage
// key - encoded timestamp(block number)
// value - encoded ChangeSet{k - compositeKey(for storage) v - originalValue(common.Hash)}.
2020-08-10 23:55:32 +00:00
StorageChangeSetBucket = "SCS"
// some_prefix_of(hash_of_address_of_account) => hash_of_subtrie
2020-08-10 23:55:32 +00:00
IntermediateTrieHashBucket = "iTh"
// DatabaseInfoBucket is used to store information about data layout.
2020-08-10 23:55:32 +00:00
DatabaseInfoBucket = "DBINFO"
// databaseVerisionKey tracks the current database version.
2020-08-10 23:55:32 +00:00
DatabaseVerisionKey = "DatabaseVersion"
// Data item prefixes (use single byte to avoid mixing data types, avoid `i`, used for indexes).
2020-08-10 23:55:32 +00:00
HeaderPrefix = "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
2020-08-10 23:55:32 +00:00
HeaderNumberPrefix = "H" // headerNumberPrefix + hash -> num (uint64 big endian)
2020-08-10 23:55:32 +00:00
BlockBodyPrefix = "b" // blockBodyPrefix + num (uint64 big endian) + hash -> block body
BlockReceiptsPrefix = "r" // blockReceiptsPrefix + num (uint64 big endian) + hash -> block receipts
2020-08-10 23:55:32 +00:00
TxLookupPrefix = "l" // txLookupPrefix + hash -> transaction/receipt lookup metadata
BloomBitsPrefix = "B" // bloomBitsPrefix + bit (uint16 big endian) + section (uint64 big endian) + hash -> bloom bits
2020-08-10 23:55:32 +00:00
PreimagePrefix = "secure-key-" // preimagePrefix + hash -> preimage
ConfigPrefix = "ethereum-config-" // config prefix for the db
// Chain index prefixes (use `i` + single byte to avoid mixing data types).
2020-08-10 23:55:32 +00:00
BloomBitsIndexPrefix = "iB" // BloomBitsIndexPrefix is the data table of a chain indexer to track its progress
2020-08-04 09:25:28 +00:00
// Progress of sync stages: stageName -> stageData
2020-08-10 23:55:32 +00:00
SyncStageProgress = "SSP2"
SyncStageProgressOld1 = "SSP"
// Position to where to unwind sync stages: stageName -> stageData
2020-08-10 23:55:32 +00:00
SyncStageUnwind = "SSU2"
SyncStageUnwindOld1 = "SSU"
2020-08-10 23:55:32 +00:00
CliqueBucket = "clique-"
2020-08-04 09:25:28 +00:00
// this bucket stored in separated database
2020-08-10 23:55:32 +00:00
InodesBucket = "inodes"
// Transaction senders - stored separately from the block bodies
2020-08-10 23:55:32 +00:00
Senders = "txSenders"
// fastTrieProgressKey tracks the number of trie entries imported during fast sync.
2020-08-10 23:55:32 +00:00
FastTrieProgressKey = "TrieSync"
// headBlockKey tracks the latest know full block's hash.
2020-08-10 23:55:32 +00:00
HeadBlockKey = "LastBlock"
// headFastBlockKey tracks the latest known incomplete block's hash during fast sync.
2020-08-10 23:55:32 +00:00
HeadFastBlockKey = "LastFast"
// migrationName -> serialized SyncStageProgress and SyncStageUnwind buckets
// it stores stages progress to understand in which context was executed migration
// in case of bug-report developer can ask content of this bucket
2020-08-10 23:55:32 +00:00
Migrations = "migrations"
)
// Keys
var (
// last block that was pruned
// it's saved one in 5 minutes
LastPrunedBlockKey = []byte("LastPrunedBlock")
//StorageModeHistory - does node save history.
StorageModeHistory = []byte("smHistory")
//StorageModeReceipts - does node save receipts.
StorageModeReceipts = []byte("smReceipts")
//StorageModeTxIndex - does node save transactions index.
StorageModeTxIndex = []byte("smTxIndex")
2020-08-10 23:55:32 +00:00
HeadHeaderKey = "LastHeader"
)
// Metrics
var (
PreimageCounter = metrics.NewRegisteredCounter("db/preimage/total", nil)
PreimageHitCounter = metrics.NewRegisteredCounter("db/preimage/hits", nil)
)
Intermediate hash phase 3 (#377) * #remove debug prints * remove storage-mode="i" * minnet re-execute hack with checkpoints * minnet re-execute hack with checkpoints * rollback to master setup * mainnet re-exec hack * rollback some changes * v0 of "push down" functionality * move all logic to own functions * handle case when re-created account already has some storage * clear path for storage * try to rely on tree structure (but maybe need to rely on DB because can be intra-block re-creations of account) * fix some bugs with indexes, moving to tests * tests added * make linter happy * make linter happy * simplify logic * adjust comparison of keys with and without incarnation * test for keyIsBefore * test for keyIsBefore * better nibbles alignment * better nibbles alignment * cleanup * continue work on tests * simplify test * check tombstone existence before pushing it down. * put tombstone only when account deleted, not created * put tombstone only when account has storage * make linter happy * test for storage resolver * make fixedbytes work without incarnation * fix panic on short keys * use special comparison only when working with keys from cache * add blockNr for better tracing * fix: incorrect tombstone check * fix: incorrect tombstone check * trigger ci * hack for problem block * more test-cases * add test case for too long keys * speedup cached resolver by removing bucket creation transaction * remove parent type check in pruning, remove unused copy from mutation.put * dump resolving info on fail * dump resolving info on fail * set tombstone everytime for now to check if it will help * on unload: check parent type, not type of node * fix wrong order of checking node type * fix wrong order of checking node type * rebase to new master * make linter happy * rebase to new master * place tombstone only if acc has storage * rebase master * rebase master * rebase master * rebase master Co-authored-by: alex.sharov <alex.sharov@lazada.com>
2020-03-11 10:31:49 +00:00
// Buckets - list of all buckets. App will panic if some bucket is not in this list.
// This list will be sorted in `init` method.
// BucketsCfg - can be used to find index in sorted version of Buckets list by name
2020-08-10 23:55:32 +00:00
var Buckets = []string{
CurrentStateBucket,
Intermediate hash phase 3 (#377) * #remove debug prints * remove storage-mode="i" * minnet re-execute hack with checkpoints * minnet re-execute hack with checkpoints * rollback to master setup * mainnet re-exec hack * rollback some changes * v0 of "push down" functionality * move all logic to own functions * handle case when re-created account already has some storage * clear path for storage * try to rely on tree structure (but maybe need to rely on DB because can be intra-block re-creations of account) * fix some bugs with indexes, moving to tests * tests added * make linter happy * make linter happy * simplify logic * adjust comparison of keys with and without incarnation * test for keyIsBefore * test for keyIsBefore * better nibbles alignment * better nibbles alignment * cleanup * continue work on tests * simplify test * check tombstone existence before pushing it down. * put tombstone only when account deleted, not created * put tombstone only when account has storage * make linter happy * test for storage resolver * make fixedbytes work without incarnation * fix panic on short keys * use special comparison only when working with keys from cache * add blockNr for better tracing * fix: incorrect tombstone check * fix: incorrect tombstone check * trigger ci * hack for problem block * more test-cases * add test case for too long keys * speedup cached resolver by removing bucket creation transaction * remove parent type check in pruning, remove unused copy from mutation.put * dump resolving info on fail * dump resolving info on fail * set tombstone everytime for now to check if it will help * on unload: check parent type, not type of node * fix wrong order of checking node type * fix wrong order of checking node type * rebase to new master * make linter happy * rebase to new master * place tombstone only if acc has storage * rebase master * rebase master * rebase master * rebase master Co-authored-by: alex.sharov <alex.sharov@lazada.com>
2020-03-11 10:31:49 +00:00
AccountsHistoryBucket,
StorageHistoryBucket,
CodeBucket,
ContractCodeBucket,
AccountChangeSetBucket,
StorageChangeSetBucket,
IntermediateTrieHashBucket,
DatabaseVerisionKey,
HeaderPrefix,
HeaderNumberPrefix,
BlockBodyPrefix,
BlockReceiptsPrefix,
TxLookupPrefix,
BloomBitsPrefix,
PreimagePrefix,
ConfigPrefix,
BloomBitsIndexPrefix,
DatabaseInfoBucket,
IncarnationMapBucket,
CliqueBucket,
SyncStageProgress,
SyncStageUnwind,
PlainStateBucket,
PlainContractCodeBucket,
PlainAccountChangeSetBucket,
PlainStorageChangeSetBucket,
InodesBucket,
Senders,
FastTrieProgressKey,
HeadBlockKey,
HeadFastBlockKey,
HeadHeaderKey,
Migrations,
}
// DeprecatedBuckets - list of buckets which can be programmatically deleted - for example after migration
2020-08-10 23:55:32 +00:00
var DeprecatedBuckets = []string{
SyncStageProgressOld1,
SyncStageUnwindOld1,
2020-08-12 02:57:55 +00:00
CurrentStateBucketOld1,
PlainStateBucketOld1,
Intermediate hash phase 3 (#377) * #remove debug prints * remove storage-mode="i" * minnet re-execute hack with checkpoints * minnet re-execute hack with checkpoints * rollback to master setup * mainnet re-exec hack * rollback some changes * v0 of "push down" functionality * move all logic to own functions * handle case when re-created account already has some storage * clear path for storage * try to rely on tree structure (but maybe need to rely on DB because can be intra-block re-creations of account) * fix some bugs with indexes, moving to tests * tests added * make linter happy * make linter happy * simplify logic * adjust comparison of keys with and without incarnation * test for keyIsBefore * test for keyIsBefore * better nibbles alignment * better nibbles alignment * cleanup * continue work on tests * simplify test * check tombstone existence before pushing it down. * put tombstone only when account deleted, not created * put tombstone only when account has storage * make linter happy * test for storage resolver * make fixedbytes work without incarnation * fix panic on short keys * use special comparison only when working with keys from cache * add blockNr for better tracing * fix: incorrect tombstone check * fix: incorrect tombstone check * trigger ci * hack for problem block * more test-cases * add test case for too long keys * speedup cached resolver by removing bucket creation transaction * remove parent type check in pruning, remove unused copy from mutation.put * dump resolving info on fail * dump resolving info on fail * set tombstone everytime for now to check if it will help * on unload: check parent type, not type of node * fix wrong order of checking node type * fix wrong order of checking node type * rebase to new master * make linter happy * rebase to new master * place tombstone only if acc has storage * rebase master * rebase master * rebase master * rebase master Co-authored-by: alex.sharov <alex.sharov@lazada.com>
2020-03-11 10:31:49 +00:00
}
var BucketsCfg = map[string]*BucketConfigItem{}
type BucketConfigItem struct {
ID int
IsDupSort bool
DupToLen int
DupFromLen int
}
type dupSortConfigEntry struct {
2020-08-12 02:57:55 +00:00
Bucket string
IsDupSort bool
ID int
FromLen int
ToLen int
}
var dupSortConfig = []dupSortConfigEntry{
{
2020-08-12 02:57:55 +00:00
Bucket: CurrentStateBucket,
IsDupSort: true,
ToLen: 40,
FromLen: 72,
},
{
2020-08-12 02:57:55 +00:00
Bucket: PlainStateBucket,
IsDupSort: true,
2020-08-12 02:57:55 +00:00
ToLen: 28,
FromLen: 60,
},
}
func init() {
sort.SliceStable(Buckets, func(i, j int) bool {
2020-08-10 23:55:32 +00:00
return strings.Compare(Buckets[i], Buckets[j]) < 0
})
for i := range Buckets {
2020-08-12 02:57:55 +00:00
BucketsCfg[Buckets[i]] = createBucketConfig(i, Buckets[i])
}
for i := range DeprecatedBuckets {
2020-08-12 02:57:55 +00:00
BucketsCfg[DeprecatedBuckets[i]] = createBucketConfig(len(Buckets)+i, DeprecatedBuckets[i])
}
}
2020-08-10 23:55:32 +00:00
func createBucketConfig(id int, name string) *BucketConfigItem {
cfg := &BucketConfigItem{ID: id}
for _, dupCfg := range dupSortConfig {
2020-08-10 23:55:32 +00:00
if dupCfg.Bucket != name {
continue
}
cfg.DupFromLen = dupCfg.FromLen
cfg.DupToLen = dupCfg.ToLen
cfg.IsDupSort = dupCfg.IsDupSort
}
return cfg
}