2020-07-10 05:44:01 +00:00
|
|
|
package commands
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
|
|
|
|
"github.com/holiman/uint256"
|
2023-01-13 18:12:18 +00:00
|
|
|
libcommon "github.com/ledgerwatch/erigon-lib/common"
|
2020-07-10 05:44:01 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// StorageRangeResult is the result of a debug_storageRangeAt API call.
|
|
|
|
type StorageRangeResult struct {
|
2023-01-13 18:12:18 +00:00
|
|
|
Storage storageMap `json:"storage"`
|
|
|
|
NextKey *libcommon.Hash `json:"nextKey"` // nil if Storage includes the last key in the trie.
|
2020-07-10 05:44:01 +00:00
|
|
|
}
|
|
|
|
|
2023-01-12 02:58:21 +00:00
|
|
|
// storageMap a map from storage locations to StorageEntry items
|
2023-01-13 18:12:18 +00:00
|
|
|
type storageMap map[libcommon.Hash]StorageEntry
|
2020-07-10 05:44:01 +00:00
|
|
|
|
2020-10-24 17:03:52 +00:00
|
|
|
// StorageEntry an entry in storage of the account
|
2020-07-10 05:44:01 +00:00
|
|
|
type StorageEntry struct {
|
2023-01-13 18:12:18 +00:00
|
|
|
Key *libcommon.Hash `json:"key"`
|
|
|
|
Value libcommon.Hash `json:"value"`
|
2020-07-10 05:44:01 +00:00
|
|
|
}
|
|
|
|
|
2022-11-20 03:58:20 +00:00
|
|
|
type walker interface {
|
2023-01-13 18:12:18 +00:00
|
|
|
ForEachStorage(addr libcommon.Address, startLocation libcommon.Hash, cb func(key, seckey libcommon.Hash, value uint256.Int) bool, maxResults int) error
|
2022-11-20 03:58:20 +00:00
|
|
|
}
|
|
|
|
|
2023-01-13 18:12:18 +00:00
|
|
|
func storageRangeAt(stateReader walker, contractAddress libcommon.Address, start []byte, maxResult int) (StorageRangeResult, error) {
|
2023-01-12 02:58:21 +00:00
|
|
|
result := StorageRangeResult{Storage: storageMap{}}
|
2020-07-10 05:44:01 +00:00
|
|
|
resultCount := 0
|
|
|
|
|
2023-01-13 18:12:18 +00:00
|
|
|
if err := stateReader.ForEachStorage(contractAddress, libcommon.BytesToHash(start), func(key, seckey libcommon.Hash, value uint256.Int) bool {
|
2020-07-10 05:44:01 +00:00
|
|
|
if resultCount < maxResult {
|
|
|
|
result.Storage[seckey] = StorageEntry{Key: &key, Value: value.Bytes32()}
|
|
|
|
} else {
|
|
|
|
result.NextKey = &key
|
|
|
|
}
|
|
|
|
resultCount++
|
|
|
|
return resultCount <= maxResult
|
|
|
|
}, maxResult+1); err != nil {
|
2021-10-04 15:16:52 +00:00
|
|
|
return StorageRangeResult{}, fmt.Errorf("error walking over storage: %w", err)
|
2020-07-10 05:44:01 +00:00
|
|
|
}
|
|
|
|
return result, nil
|
|
|
|
}
|