Skip to content

triedb/pathdb: rm the useless accountList and storageList #32234

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 12 additions & 27 deletions triedb/pathdb/history.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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,
}
}

Expand Down Expand Up @@ -312,16 +301,18 @@ 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])),
offset: uint32(len(accountData)),
}
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])),
Expand Down Expand Up @@ -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,
Expand All @@ -484,22 +473,18 @@ 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)
if err != nil {
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
}

Expand Down
9 changes: 6 additions & 3 deletions triedb/pathdb/history_index_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))
}
}
}
}
Expand Down
4 changes: 2 additions & 2 deletions triedb/pathdb/history_indexer.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
34 changes: 0 additions & 34 deletions triedb/pathdb/history_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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
Expand All @@ -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
}
Loading