triedb/pathdb: rm history.{account,storage} sorted list

Signed-off-by: jsvisa <delweng@gmail.com>
This commit is contained in:
jsvisa 2025-07-17 23:06:55 +08:00
parent 0dacfef8ac
commit 8b6bf1e6b3
2 changed files with 14 additions and 29 deletions

View file

@ -242,20 +242,11 @@ func (m *meta) decode(blob []byte) error {
type history struct {
meta *meta // Meta data of history
accounts map[common.Address][]byte // Account data keyed by its address hash
accountList []common.Address // Sorted account hash list
storages map[common.Address]map[common.Hash][]byte // Storage data keyed by its address hash and slot hash
storageList map[common.Address][]common.Hash // Sorted slot hash list
}
// newHistory constructs the state history object with provided state change set.
func newHistory(root common.Hash, parent common.Hash, block uint64, accounts map[common.Address][]byte, storages map[common.Address]map[common.Hash][]byte, rawStorageKey bool) *history {
var (
accountList = slices.SortedFunc(maps.Keys(accounts), common.Address.Cmp)
storageList = make(map[common.Address][]common.Hash)
)
for addr, slots := range storages {
storageList[addr] = slices.SortedFunc(maps.Keys(slots), common.Hash.Cmp)
}
version := historyVersion
if !rawStorageKey {
version = stateHistoryV0
@ -268,9 +259,7 @@ func newHistory(root common.Hash, parent common.Hash, block uint64, accounts map
block: block,
},
accounts: accounts,
accountList: accountList,
storages: storages,
storageList: storageList,
}
}
@ -312,7 +301,8 @@ func (h *history) encode() ([]byte, []byte, []byte, []byte) {
accountIndexes []byte // the buffer for concatenated account index
storageIndexes []byte // the buffer for concatenated storage index
)
for _, addr := range h.accountList {
accountList := slices.SortedFunc(maps.Keys(h.accounts), common.Address.Cmp)
for _, addr := range accountList {
accIndex := accountIndex{
address: addr,
length: uint8(len(h.accounts[addr])),
@ -320,8 +310,9 @@ func (h *history) encode() ([]byte, []byte, []byte, []byte) {
}
slots, exist := h.storages[addr]
if exist {
storageList := slices.SortedFunc(maps.Keys(slots), common.Hash.Cmp)
// Encode storage slots in order
for _, slotHash := range h.storageList[addr] {
for _, slotHash := range storageList {
sIndex := slotIndex{
id: slotHash,
length: uint8(len(slots[slotHash])),
@ -464,8 +455,6 @@ func (h *history) decode(accountData, storageData, accountIndexes, storageIndexe
count = len(accountIndexes) / accountIndexSize
accounts = make(map[common.Address][]byte, count)
storages = make(map[common.Address]map[common.Hash][]byte)
accountList = make([]common.Address, 0, count)
storageList = make(map[common.Address][]common.Hash)
r = &decoder{
accountData: accountData,
@ -484,7 +473,6 @@ func (h *history) decode(accountData, storageData, accountIndexes, storageIndexe
return err
}
accounts[accIndex.address] = accData
accountList = append(accountList, accIndex.address)
// Resolve storage slots
slotList, slotData, err := r.readStorage(accIndex)
@ -492,14 +480,11 @@ func (h *history) decode(accountData, storageData, accountIndexes, storageIndexe
return err
}
if len(slotList) > 0 {
storageList[accIndex.address] = slotList
storages[accIndex.address] = slotData
}
}
h.accounts = accounts
h.accountList = accountList
h.storages = storages
h.storageList = storageList
return nil
}

View file

@ -94,12 +94,12 @@ func newBatchIndexer(db ethdb.KeyValueStore, delete bool) *batchIndexer {
// process iterates through the accounts and their associated storage slots in the
// state history, tracking the mapping between state and history IDs.
func (b *batchIndexer) process(h *history, historyID uint64) error {
for _, address := range h.accountList {
for address := range h.accounts {
addrHash := crypto.Keccak256Hash(address.Bytes())
b.counter += 1
b.accounts[addrHash] = append(b.accounts[addrHash], historyID)
for _, slotKey := range h.storageList[address] {
for slotKey := range h.storages[address] {
b.counter += 1
if _, ok := b.storages[addrHash]; !ok {
b.storages[addrHash] = make(map[common.Hash][]uint64)