-
Notifications
You must be signed in to change notification settings - Fork 1
/
fvmcmds_unix.go
205 lines (177 loc) · 4.99 KB
/
fvmcmds_unix.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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
//go:build unix
package main
import (
"bytes"
"context"
"encoding/json"
"fmt"
"io"
"log"
"net"
"net/http"
"sync"
"time"
"github.com/urfave/cli/v2"
"golang.org/x/xerrors"
"github.com/filecoin-project/go-jsonrpc"
"github.com/filecoin-project/go-state-types/abi"
lotusapi "github.com/filecoin-project/lotus/api"
lotusbs "github.com/filecoin-project/lotus/blockstore"
"github.com/filecoin-project/lotus/chain/types"
"github.com/filecoin-project/lotus/chain/types/ethtypes"
)
func cmdFevmExec(ctx *cli.Context) error {
args := ctx.Args()
eaddr, err := ethtypes.ParseEthAddress(args.Get(0))
if err != nil {
return xerrors.Errorf("unable to parse eth address: %w", err)
}
decodedBytes, err := ethtypes.DecodeHexString(args.Get(1))
if err != nil {
return xerrors.Errorf("could not decode hex bytes: %w", err)
}
bs, tsk, err := getState(ctx)
if err != nil {
return err
}
return fevmExec(ctx.Context, bs, tsk, &eaddr, decodedBytes, ctx.Path("output"))
}
func cmdFevmDaemon(ctx *cli.Context) error {
args := ctx.Args()
listenStr := "127.0.0.1:"
if args.Len() != 0 {
listenStr += args.First()
}
l, err := net.Listen("tcp", listenStr)
if err != nil {
return err
}
bs, tsk, err := getState(ctx)
if err != nil {
return err
}
ebs := NewEphemeralBlockstore(bs)
addr := "api.chain.love"
var api lotusapi.FullNodeStruct
closer, err := jsonrpc.NewMergeClient(ctx.Context, "ws://"+addr+"/rpc/v0", "Filecoin", []interface{}{&api.Internal, &api.CommonStruct.Internal}, nil)
if err != nil {
return fmt.Errorf("connecting with lotus API failed: %w", err)
}
defer closer()
erpc := ðRpcResolver{
lbs: ebs,
tsk: tsk,
lastTskCheck: time.Now(),
api: api,
}
http.HandleFunc("/rpc/v1", func(writer http.ResponseWriter, request *http.Request) {
if request.Method != http.MethodPost {
writer.WriteHeader(http.StatusMethodNotAllowed)
return
}
reqBody, err := io.ReadAll(request.Body)
if err != nil {
writer.WriteHeader(http.StatusInternalServerError)
_, _ = writer.Write([]byte(err.Error()))
return
}
type jsonRPC struct {
Jsonrpc string `json:"jsonrpc"`
ID int `json:"id,omitempty"`
Method string `json:"method"`
Params ethtypes.EthCall `json:"params"`
Meta map[string]string `json:"meta,omitempty"`
}
reqMsg, err := jsonrpc.DecodeParams[jsonRPC](reqBody)
if err != nil {
writer.WriteHeader(http.StatusInternalServerError)
_, _ = writer.Write([]byte(err.Error()))
return
}
if reqMsg.Method != "eth_call" {
writer.WriteHeader(http.StatusBadRequest)
_, _ = writer.Write([]byte("only eth_call supported"))
return
}
if reqMsg.Params.To == nil {
writer.WriteHeader(http.StatusBadRequest)
_, _ = writer.Write([]byte("must send to a valid address"))
return
}
if reqMsg.Params.From != nil {
writer.WriteHeader(http.StatusBadRequest)
_, _ = writer.Write([]byte("can only send from null address"))
return
}
eaddr := *reqMsg.Params.To
methodData := reqMsg.Params.Data
ret, err := erpc.Call(ctx.Context, eaddr, methodData)
if err != nil {
writer.WriteHeader(http.StatusInternalServerError)
_, _ = writer.Write([]byte(fmt.Errorf("could not perform call operation: %w", err).Error()))
return
}
type returnType struct {
Jsonrpc string `json:"jsonrpc"`
ID int `json:"id,omitempty"`
Result string `json:"result"`
}
sendResponse := returnType{
Jsonrpc: "2.0",
ID: reqMsg.ID,
Result: ethtypes.EthBytes(ret).String(),
}
responseBytes, err := json.Marshal(sendResponse)
if err != nil {
writer.WriteHeader(http.StatusInternalServerError)
_, _ = writer.Write([]byte(xerrors.Errorf("failed to marshal return value: %w", err).Error()))
return
}
_, _ = writer.Write(responseBytes)
})
if err := http.Serve(l, nil); err != nil {
return err
}
return nil
}
type ethRpcResolver struct {
lbs lotusbs.Blockstore
tsk types.TipSetKey
tskMx sync.RWMutex
lastTskCheck time.Time
api lotusapi.FullNodeStruct
}
func (e *ethRpcResolver) Call(ctx context.Context, eaddr ethtypes.EthAddress, methodData ethtypes.EthBytes) ([]byte, error) {
e.tskMx.RLock()
updateTsk := false
if time.Since(e.lastTskCheck) > time.Second*30 {
updateTsk = true
}
e.tskMx.RUnlock()
if updateTsk {
e.tskMx.Lock()
if time.Since(e.lastTskCheck) > time.Second*30 {
ts, err := e.api.ChainHead(ctx)
if err != nil {
log.Printf("could not update tipset %s", err.Error())
} else {
e.lastTskCheck = time.Now()
e.tsk = ts.Key()
}
}
e.tskMx.Unlock()
}
ts, sm, filMsg, err := getFevmRequest(ctx, e.lbs, e.tsk, &eaddr, methodData)
if err != nil {
return nil, err
}
res, err := sm.Call(ctx, filMsg, ts)
if err != nil {
return nil, xerrors.Errorf("unable to make a call: %w", err)
}
var ret abi.CborBytes
if err := ret.UnmarshalCBOR(bytes.NewReader(res.MsgRct.Return)); err != nil {
return nil, xerrors.Errorf("failed to unmarshal return value: %w", err)
}
return ret, nil
}