forked from qtumproject/janus
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy patheth_getStorageAt.go
76 lines (64 loc) · 2.17 KB
/
eth_getStorageAt.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
package transformer
import (
"context"
"fmt"
"github.com/kaonone/eth-rpc-gate/pkg/eth"
"github.com/kaonone/eth-rpc-gate/pkg/kaon"
"github.com/kaonone/eth-rpc-gate/pkg/utils"
"github.com/labstack/echo"
)
// ProxyETHGetStorageAt implements ETHProxy
type ProxyETHGetStorageAt struct {
*kaon.Kaon
}
func (p *ProxyETHGetStorageAt) Method() string {
return "eth_getStorageAt"
}
func (p *ProxyETHGetStorageAt) Request(rawreq *eth.JSONRPCRequest, c echo.Context) (interface{}, *eth.JSONRPCError) {
var req eth.GetStorageRequest
if err := unmarshalRequest(rawreq.Params, &req); err != nil {
// TODO: Correct error code?
return nil, eth.NewInvalidParamsError(err.Error())
}
kaonAddress := utils.RemoveHexPrefix(req.Address)
blockNumber, err := getBlockNumberByParam(c.Request().Context(), p.Kaon, req.BlockNumber, false)
if err != nil {
p.GetDebugLogger().Log("msg", fmt.Sprintf("Failed to get block number by param for '%s'", req.BlockNumber), "err", err)
return nil, err
}
return p.request(
c.Request().Context(),
&kaon.GetStorageRequest{
Address: kaonAddress,
BlockNumber: blockNumber,
},
utils.RemoveHexPrefix(req.Index),
)
}
func (p *ProxyETHGetStorageAt) request(ctx context.Context, ethreq *kaon.GetStorageRequest, index string) (*eth.GetStorageResponse, *eth.JSONRPCError) {
kaonresp, err := p.Kaon.GetStorage(ctx, ethreq)
if err != nil {
return nil, eth.NewCallbackError(err.Error())
}
// kaon res -> eth res
return p.ToResponse(kaonresp, index), nil
}
func (p *ProxyETHGetStorageAt) ToResponse(kaonresp *kaon.GetStorageResponse, slot string) *eth.GetStorageResponse {
// the value for unknown anything
storageData := eth.GetStorageResponse("0x0000000000000000000000000000000000000000000000000000000000000000")
if len(slot) != 64 {
slot = leftPadStringWithZerosTo64Bytes(slot)
}
for _, outerValue := range *kaonresp {
kaonStorageData, ok := outerValue[slot]
if ok {
storageData = eth.GetStorageResponse(utils.AddHexPrefix(kaonStorageData))
return &storageData
}
}
return &storageData
}
// left pad a string with leading zeros to fit 64 bytes
func leftPadStringWithZerosTo64Bytes(hex string) string {
return fmt.Sprintf("%064v", hex)
}