fix/add_4th_parameter_to_parity_list (#3443)

* fix/add_4th_parameter_to_parity_list

* Change blocknum type from interface{} to rpc.BlockNumberOrHash

* Change latest block check
This commit is contained in:
primal_concrete_sledge 2022-02-08 16:02:18 +03:00 committed by GitHub
parent 98ca4bd460
commit 60f4b3a46d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 39 additions and 16 deletions

View File

@ -48,7 +48,7 @@ enode://d30d079163d7b69fcb261c0538c0c3faba4fb4429652970e60fa25deb02a789b4811e98b
Open terminal 2 and navigate to erigon/build/bin folder. Here type the following command
```bash
./rpcdaemon --datadir=dev --private.api.addr=localhost:9090 --http.api=eth,erigon,web3,net,debug,trace,txpool
./rpcdaemon --datadir=dev --private.api.addr=localhost:9090 --http.api=eth,erigon,web3,net,debug,trace,txpool,parity
```
The result will look like this:
<img width="1636" alt="rpc daemon start" src="https://user-images.githubusercontent.com/24697803/140478408-ac1be94a-4a63-42c6-8673-e24decadd658.png">

View File

@ -18,6 +18,7 @@ func init() {
listStorageKeysCmd.MarkFlagRequired("addr")
listStorageKeysCmd.Flags().StringVar(&offsetAddr, "offset", "", "Offset storage key from which the batch should start")
listStorageKeysCmd.Flags().IntVar(&quantity, "quantity", 10, "Integer number of addresses to display in a batch")
listStorageKeysCmd.Flags().StringVar(&blockNum, "block", "latest", "Integer block number, or the string 'latest', 'earliest' or 'pending'; now only 'latest' is available")
rootCmd.AddCommand(listStorageKeysCmd)
}
@ -25,12 +26,13 @@ func init() {
var listStorageKeysCmd = &cobra.Command{
Use: "parity-list",
Short: "Returns all storage keys of the given address",
Run: func(cmd *cobra.Command, args []string) {
RunE: func(cmd *cobra.Command, args []string) error {
if clearDev {
defer clearDevDB()
}
toAddress := common.HexToAddress(addr)
offset := common.Hex2Bytes(strings.TrimSuffix(offsetAddr, "0x"))
requests.ParityList(reqId, toAddress, quantity, offset)
requests.ParityList(reqId, toAddress, quantity, offset, blockNum)
return nil
},
}

View File

@ -66,13 +66,14 @@ func (req *RequestGenerator) txpoolContent() string {
return fmt.Sprintf(template, req.reqID)
}
func (req *RequestGenerator) parityStorageKeyListContent(address common.Address, quantity int, offset []byte) string {
const template = `{"jsonrpc":"2.0","method":"parity_listStorageKeys","params":["0x%x", %d, %v],"id":%d}`
func (req *RequestGenerator) parityStorageKeyListContent(address common.Address, quantity int, offset []byte, blockNum string) string {
const template = `{"jsonrpc":"2.0","method":"parity_listStorageKeys","params":["0x%x", %d, %v, "%s"],"id":%d}`
var offsetString string
if len(offset) != 0 {
offsetString = fmt.Sprintf(`"0x%x"`, offset)
} else {
offsetString = "null"
}
return fmt.Sprintf(template, address, quantity, offsetString, req.reqID)
return fmt.Sprintf(template, address, quantity, offsetString, blockNum, req.reqID)
}

View File

@ -65,11 +65,11 @@ func TxpoolContent(reqId int) {
fmt.Printf("Txpool content: %v\n", parseResponse(b))
}
func ParityList(reqId int, account common.Address, quantity int, offset []byte) {
func ParityList(reqId int, account common.Address, quantity int, offset []byte, blockNum string) {
reqGen := initialiseRequestGenerator(reqId)
var b rpctest.ParityListStorageKeysResult
res := reqGen.Erigon("parity_listStorageKeys", reqGen.parityStorageKeyListContent(account, quantity, offset), &b)
res := reqGen.Erigon("parity_listStorageKeys", reqGen.parityStorageKeyListContent(account, quantity, offset, blockNum), &b)
if res.Err != nil {
fmt.Printf("Error fetching storage keys: %v\n", res.Err)
return

View File

@ -9,11 +9,16 @@ import (
"github.com/ledgerwatch/erigon/common"
"github.com/ledgerwatch/erigon/common/hexutil"
"github.com/ledgerwatch/erigon/core/state"
"github.com/ledgerwatch/erigon/rpc"
)
var latestTag = common.BytesToHash([]byte("latest"))
var ErrWrongTag = fmt.Errorf("listStorageKeys wrong block tag or number: must be '%s' ('latest')", latestTag)
// ParityAPI the interface for the parity_ RPC commands
type ParityAPI interface {
ListStorageKeys(ctx context.Context, account common.Address, quantity int, offset *hexutil.Bytes) ([]hexutil.Bytes, error)
ListStorageKeys(ctx context.Context, account common.Address, quantity int, offset *hexutil.Bytes, blockNumber rpc.BlockNumberOrHash) ([]hexutil.Bytes, error)
}
// ParityAPIImpl data structure to store things needed for parity_ commands
@ -29,7 +34,11 @@ func NewParityAPIImpl(db kv.RoDB) *ParityAPIImpl {
}
// ListStorageKeys implements parity_listStorageKeys. Returns all storage keys of the given address
func (api *ParityAPIImpl) ListStorageKeys(ctx context.Context, account common.Address, quantity int, offset *hexutil.Bytes) ([]hexutil.Bytes, error) {
func (api *ParityAPIImpl) ListStorageKeys(ctx context.Context, account common.Address, quantity int, offset *hexutil.Bytes, blockNumberOrTag rpc.BlockNumberOrHash) ([]hexutil.Bytes, error) {
if err := api.checkBlockNumber(blockNumberOrTag); err != nil {
return nil, err
}
tx, err := api.db.BeginRo(ctx)
if err != nil {
return nil, fmt.Errorf("listStorageKeys cannot open tx: %w", err)
@ -70,3 +79,11 @@ func (api *ParityAPIImpl) ListStorageKeys(ctx context.Context, account common.Ad
}
return keys, nil
}
func (api *ParityAPIImpl) checkBlockNumber(blockNumber rpc.BlockNumberOrHash) error {
num, isNum := blockNumber.Number()
if isNum && rpc.LatestBlockNumber == num {
return nil
}
return ErrWrongTag
}

View File

@ -8,9 +8,12 @@ import (
"github.com/ledgerwatch/erigon/cmd/rpcdaemon/rpcdaemontest"
"github.com/ledgerwatch/erigon/common"
"github.com/ledgerwatch/erigon/common/hexutil"
"github.com/ledgerwatch/erigon/rpc"
"github.com/stretchr/testify/assert"
)
var latestBlock = rpc.BlockNumberOrHashWithNumber(rpc.LatestBlockNumber)
func TestParityAPIImpl_ListStorageKeys_NoOffset(t *testing.T) {
assert := assert.New(t)
db := rpcdaemontest.CreateTestKV(t)
@ -23,7 +26,7 @@ func TestParityAPIImpl_ListStorageKeys_NoOffset(t *testing.T) {
"120e23dcb7e4437386073613853db77b10011a2404eefc716b97c7767e37f8eb",
}
addr := common.HexToAddress("0x920fd5070602feaea2e251e9e7238b6c376bcae5")
result, err := api.ListStorageKeys(context.Background(), addr, 5, nil)
result, err := api.ListStorageKeys(context.Background(), addr, 5, nil, latestBlock)
if err != nil {
t.Errorf("calling ListStorageKeys: %v", err)
}
@ -47,7 +50,7 @@ func TestParityAPIImpl_ListStorageKeys_WithOffset_ExistingPrefix(t *testing.T) {
addr := common.HexToAddress("0x920fd5070602feaea2e251e9e7238b6c376bcae5")
offset := common.Hex2Bytes("29")
b := hexutil.Bytes(offset)
result, err := api.ListStorageKeys(context.Background(), addr, 5, &b)
result, err := api.ListStorageKeys(context.Background(), addr, 5, &b, latestBlock)
if err != nil {
t.Errorf("calling ListStorageKeys: %v", err)
}
@ -68,7 +71,7 @@ func TestParityAPIImpl_ListStorageKeys_WithOffset_NonExistingPrefix(t *testing.T
addr := common.HexToAddress("0x920fd5070602feaea2e251e9e7238b6c376bcae5")
offset := common.Hex2Bytes("30")
b := hexutil.Bytes(offset)
result, err := api.ListStorageKeys(context.Background(), addr, 2, &b)
result, err := api.ListStorageKeys(context.Background(), addr, 2, &b, latestBlock)
if err != nil {
t.Errorf("calling ListStorageKeys: %v", err)
}
@ -85,7 +88,7 @@ func TestParityAPIImpl_ListStorageKeys_WithOffset_EmptyResponse(t *testing.T) {
addr := common.HexToAddress("0x920fd5070602feaea2e251e9e7238b6c376bcae5")
offset := common.Hex2Bytes("ff")
b := hexutil.Bytes(offset)
result, err := api.ListStorageKeys(context.Background(), addr, 2, &b)
result, err := api.ListStorageKeys(context.Background(), addr, 2, &b, latestBlock)
if err != nil {
t.Errorf("calling ListStorageKeys: %v", err)
}
@ -97,6 +100,6 @@ func TestParityAPIImpl_ListStorageKeys_AccNotFound(t *testing.T) {
db := rpcdaemontest.CreateTestKV(t)
api := NewParityAPIImpl(db)
addr := common.HexToAddress("0x920fd5070602feaea2e251e9e7238b6c376bcaef")
_, err := api.ListStorageKeys(context.Background(), addr, 2, nil)
_, err := api.ListStorageKeys(context.Background(), addr, 2, nil, latestBlock)
assert.Error(err, fmt.Errorf("acc not found"))
}

View File

@ -256,6 +256,6 @@ type StorageResult struct {
}
type ParityListStorageKeysResult struct {
CommonResponse
Result []hexutil.Bytes `json:"result"`
Error string `json:"error"`
}