diff --git a/triedb/pathdb/history.go b/triedb/pathdb/history.go index 47f224170d7..26e90ff2e0d 100644 --- a/triedb/pathdb/history.go +++ b/triedb/pathdb/history.go @@ -240,22 +240,13 @@ func (m *meta) decode(blob []byte) error { // (8-bytes integer), the oldest state history object can be pruned on demand // in order to control the storage size. 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 + meta *meta // Meta data of history + accounts map[common.Address][]byte // Account data keyed by its address hash + storages map[common.Address]map[common.Hash][]byte // Storage data keyed by its address hash and slot hash } // 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 @@ -267,10 +258,8 @@ func newHistory(root common.Hash, parent common.Hash, block uint64, accounts map root: root, block: block, }, - accounts: accounts, - accountList: accountList, - storages: storages, - storageList: storageList, + accounts: accounts, + storages: storages, } } @@ -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])), @@ -461,11 +452,9 @@ func (r *decoder) readStorage(accIndex accountIndex) ([]common.Hash, map[common. // decode deserializes the account and storage data from the provided byte stream. func (h *history) decode(accountData, storageData, accountIndexes, storageIndexes []byte) error { var ( - 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) + count = len(accountIndexes) / accountIndexSize + accounts = make(map[common.Address][]byte, count) + storages = make(map[common.Address]map[common.Hash][]byte) 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 } diff --git a/triedb/pathdb/history_index_test.go b/triedb/pathdb/history_index_test.go index 7b24b86fd65..663991a9527 100644 --- a/triedb/pathdb/history_index_test.go +++ b/triedb/pathdb/history_index_test.go @@ -199,15 +199,18 @@ func TestBatchIndexerWrite(t *testing.T) { storages = make(map[common.Hash]map[common.Hash][]uint64) ) for i, h := range histories { - for _, addr := range h.accountList { + for addr := range h.accounts { addrHash := crypto.Keccak256Hash(addr.Bytes()) accounts[addrHash] = append(accounts[addrHash], uint64(i+1)) if _, ok := storages[addrHash]; !ok { storages[addrHash] = make(map[common.Hash][]uint64) } - for _, slot := range h.storageList[addr] { - storages[addrHash][slot] = append(storages[addrHash][slot], uint64(i+1)) + slots, ok := h.storages[addr] + if ok { + for slot := range slots { + storages[addrHash][slot] = append(storages[addrHash][slot], uint64(i+1)) + } } } } diff --git a/triedb/pathdb/history_indexer.go b/triedb/pathdb/history_indexer.go index 42103fab32c..b7cbb5b54ed 100644 --- a/triedb/pathdb/history_indexer.go +++ b/triedb/pathdb/history_indexer.go @@ -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) diff --git a/triedb/pathdb/history_test.go b/triedb/pathdb/history_test.go index 2928d19d745..019276f6404 100644 --- a/triedb/pathdb/history_test.go +++ b/triedb/pathdb/history_test.go @@ -100,12 +100,6 @@ func testEncodeDecodeHistory(t *testing.T, rawStorageKey bool) { if !compareStorages(dec.storages, obj.storages) { t.Fatal("storage data is mismatched") } - if !compareList(dec.accountList, obj.accountList) { - t.Fatal("account list is mismatched") - } - if !compareStorageList(dec.storageList, obj.storageList) { - t.Fatal("storage list is mismatched") - } } func checkHistory(t *testing.T, db ethdb.KeyValueReader, freezer ethdb.AncientReader, id uint64, root common.Hash, exist bool) { @@ -290,18 +284,6 @@ func compareSet[k comparable](a, b map[k][]byte) bool { return true } -func compareList[k comparable](a, b []k) bool { - if len(a) != len(b) { - return false - } - for i := 0; i < len(a); i++ { - if a[i] != b[i] { - return false - } - } - return true -} - func compareStorages(a, b map[common.Address]map[common.Hash][]byte) bool { if len(a) != len(b) { return false @@ -317,19 +299,3 @@ func compareStorages(a, b map[common.Address]map[common.Hash][]byte) bool { } return true } - -func compareStorageList(a, b map[common.Address][]common.Hash) bool { - if len(a) != len(b) { - return false - } - for h, la := range a { - lb, ok := b[h] - if !ok { - return false - } - if !compareList(la, lb) { - return false - } - } - return true -}