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"
|
|
|
|
"github.com/ledgerwatch/erigon/core/state"
|
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"`
|
|
|
|
}
|
|
|
|
|
2021-04-26 05:37:48 +00:00
|
|
|
func StorageRangeAt(stateReader *state.PlainKVState, 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 {
|
|
|
|
return StorageRangeResult{}, fmt.Errorf("error walking over storage: %v", err)
|
|
|
|
}
|
|
|
|
return result, nil
|
|
|
|
}
|