Skip to content

Commit

Permalink
move SMT scalable retainer to another place (#264)
Browse files Browse the repository at this point in the history
Co-authored-by: Igor Mandrigin <i@mandrigin.ru>
  • Loading branch information
hexoscott and mandrigin authored Apr 4, 2024
1 parent 76ca581 commit b3e9160
Showing 1 changed file with 39 additions and 17 deletions.
56 changes: 39 additions & 17 deletions core/state/trie_db.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,11 @@ import (
"sync"
"sync/atomic"

"github.com/holiman/uint256"
"github.com/gateway-fm/cdk-erigon-lib/common"
libcommon "github.com/gateway-fm/cdk-erigon-lib/common"
"github.com/gateway-fm/cdk-erigon-lib/common/length"
"github.com/gateway-fm/cdk-erigon-lib/kv"
"github.com/holiman/uint256"
eriCommon "github.com/ledgerwatch/erigon/common"
"github.com/ledgerwatch/erigon/common/dbutils"
"github.com/ledgerwatch/erigon/core/types/accounts"
Expand Down Expand Up @@ -294,21 +295,10 @@ func (tds *TrieDbState) buildStorageReads() eriCommon.StorageKeys {
for storageKey := range tds.aggregateBuffer.storageReads {
storageTouches = append(storageTouches, storageKey)
}
storageTouches = append(storageTouches, tds.scalableStorageKeys()...)
sort.Sort(storageTouches)
return storageTouches
}

func (tds *TrieDbState) scalableStorageKeys() eriCommon.StorageKeys {
keys := eriCommon.StorageKeys{}
keys = append(keys, buildStorageKey(ADDRESS_SCALABLE_L2, 1, LAST_BLOCK_STORAGE_POS))
keys = append(keys, buildStorageKey(ADDRESS_SCALABLE_L2, 1, STATE_ROOT_STORAGE_POS))
keys = append(keys, buildStorageKey(ADDRESS_SCALABLE_L2, 1, TIMESTAMP_STORAGE_POS))
keys = append(keys, buildStorageKey(ADDRESS_SCALABLE_L2, 1, BLOCK_INFO_ROOT_STORAGE_POS))
keys = append(keys, buildStorageKey(GER_MANAGER_ADDRESS, 1, GLOBAL_EXIT_ROOT_STORAGE_POS))
return keys
}

func buildStorageKey(address common.Address, incarnation uint64, slot common.Hash) eriCommon.StorageKey {
var storageKey eriCommon.StorageKey
copy(storageKey[:], address.Bytes())
Expand Down Expand Up @@ -947,24 +937,56 @@ func (tds *TrieDbState) ResolveSMTRetainList() (*trie.RetainList, error) {
keys = append(keys, codeLengthKey.GetPath())
}

getSMTPath := func(ethAddr string, key string) ([]int, error) {
a := utils.ConvertHexToBigInt(ethAddr)
addr := utils.ScalarToArrayBig(a)

storageKey, err := utils.KeyContractStorage(addr, key)

if err != nil {
return nil, err
}

return storageKey.GetPath(), nil
}

for _, storageKey := range storageTouches {
addrHash, _, keyHash := dbutils.ParseCompositeStorageKey(storageKey[:])

ethAddr := common.BytesToAddress(tds.preimageMap[addrHash]).String()
a := utils.ConvertHexToBigInt(ethAddr)
addr := utils.ScalarToArrayBig(a)

key := common.BytesToHash(tds.preimageMap[keyHash]).String()

storageKey, err := utils.KeyContractStorage(addr, key)
smtPath, err := getSMTPath(ethAddr, key)

if err != nil {
return nil, err
}

keys = append(keys, smtPath)
}

/*add 0x00...05ca1ab1e and GER manager values*/

/* 0x00...05ca1ab1e */
for _, storageSlot := range []libcommon.Hash{LAST_BLOCK_STORAGE_POS, STATE_ROOT_STORAGE_POS, TIMESTAMP_STORAGE_POS, BLOCK_INFO_ROOT_STORAGE_POS} {
smtPath, err := getSMTPath(ADDRESS_SCALABLE_L2.String(), storageSlot.String())

if err != nil {
return nil, err
}

keys = append(keys, storageKey.GetPath())
keys = append(keys, smtPath)
}

/* GER manager */
smtPath, err := getSMTPath(GER_MANAGER_ADDRESS.String(), GLOBAL_EXIT_ROOT_STORAGE_POS.String())

if err != nil {
return nil, err
}

keys = append(keys, smtPath)

rl := trie.NewRetainList(0)

for _, key := range keys {
Expand Down

0 comments on commit b3e9160

Please sign in to comment.