2020-07-10 05:44:01 +00:00
|
|
|
package commands
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
|
|
|
|
"github.com/holiman/uint256"
|
2021-05-20 18:25:53 +00:00
|
|
|
"github.com/ledgerwatch/erigon/common"
|
2020-07-10 05:44:01 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// StorageRangeResult is the result of a debug_storageRangeAt API call.
|
|
|
|
type StorageRangeResult struct {
|
|
|
|
Storage StorageMap `json:"storage"`
|
|
|
|
NextKey *common.Hash `json:"nextKey"` // nil if Storage includes the last key in the trie.
|
|
|
|
}
|
|
|
|
|
2020-10-24 17:03:52 +00:00
|
|
|
// StorageMap a map from storage locations to StorageEntry items
|
2020-07-10 05:44:01 +00:00
|
|
|
type StorageMap map[common.Hash]StorageEntry
|
|
|
|
|
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 {
|
|
|
|
Key *common.Hash `json:"key"`
|
|
|
|
Value common.Hash `json:"value"`
|
|
|
|
}
|
|
|
|
|
2022-11-20 03:58:20 +00:00
|
|
|
type walker interface {
|
|
|
|
ForEachStorage(addr common.Address, startLocation common.Hash, cb func(key, seckey common.Hash, value uint256.Int) bool, maxResults int) error
|
|
|
|
}
|
|
|
|
|
|
|
|
func StorageRangeAt(stateReader walker, contractAddress common.Address, start []byte, maxResult int) (StorageRangeResult, error) {
|
2020-07-10 05:44:01 +00:00
|
|
|
result := StorageRangeResult{Storage: StorageMap{}}
|
|
|
|
resultCount := 0
|
|
|
|
|
2020-11-18 14:58:52 +00:00
|
|
|
if err := stateReader.ForEachStorage(contractAddress, common.BytesToHash(start), func(key, seckey common.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
|
|
|
|
}
|