Skip to content

Commit 70208e2

Browse files
authored
Merge pull request #6433 from filecoin-project/feat/rpc-cmd
feat: add rpc command
2 parents 0e1c3cc + f97c0a9 commit 70208e2

File tree

3 files changed

+118
-1
lines changed

3 files changed

+118
-1
lines changed

cmd/main.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,7 @@ TOOL COMMANDS
140140
version - Show venus version information
141141
seed - Seal sectors for genesis miner
142142
fetch - Fetch proving parameters
143+
rpc - Interact with the jsonrpc api
143144
`,
144145
},
145146
Options: []cmds.Option{
@@ -165,6 +166,7 @@ var rootSubcmdsLocal = map[string]*cmds.Command{
165166
"version": versionCmd,
166167
"seed": seedCmd,
167168
"cid": cidCmd,
169+
"rpc": rpcCmd,
168170
}
169171

170172
// all top level commands, available on daemon. set during init() to avoid configuration loops.

cmd/rpc.go

Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
package cmd
2+
3+
import (
4+
"bytes"
5+
"encoding/json"
6+
"io"
7+
"net/http"
8+
"net/url"
9+
10+
"github.com/filecoin-project/venus/app/paths"
11+
"github.com/filecoin-project/venus/pkg/repo"
12+
"github.com/filecoin-project/venus/venus-shared/api"
13+
v1 "github.com/filecoin-project/venus/venus-shared/api/chain/v1"
14+
cmds "github.com/ipfs/go-ipfs-cmds"
15+
"golang.org/x/xerrors"
16+
)
17+
18+
var rpcCmd = &cmds.Command{
19+
Helptext: cmds.HelpText{
20+
Tagline: "Interactive JsonPRC shell",
21+
},
22+
Arguments: []cmds.Argument{
23+
cmds.StringArg("method", true, false, "method name"),
24+
cmds.StringArg("params", false, false, "method params"),
25+
},
26+
Run: func(req *cmds.Request, re cmds.ResponseEmitter, env cmds.Environment) error {
27+
repoDir, _ := req.Options[OptionRepoDir].(string)
28+
repoDir, err := paths.GetRepoPath(repoDir)
29+
if err != nil {
30+
return err
31+
}
32+
addr, err := repo.APIAddrFromRepoPath(repoDir)
33+
if err != nil {
34+
return err
35+
}
36+
token, err := repo.APITokenFromRepoPath(repoDir)
37+
if err != nil {
38+
return err
39+
}
40+
41+
ai := api.NewAPIInfo(addr, token)
42+
addr, err = ai.DialArgs(api.VerString(v1.MajorVersion))
43+
if err != nil {
44+
return err
45+
}
46+
47+
u, err := url.Parse(addr)
48+
if err != nil {
49+
return xerrors.Errorf("parsing api URL: %w", err)
50+
}
51+
52+
switch u.Scheme {
53+
case "ws":
54+
u.Scheme = "http"
55+
case "wss":
56+
u.Scheme = "https"
57+
}
58+
59+
addr = u.String()
60+
61+
if len(req.Arguments) < 1 {
62+
return re.Emit("method name is required.")
63+
}
64+
methodName := req.Arguments[0]
65+
params := "[]"
66+
if len(req.Arguments) > 1 {
67+
params = req.Arguments[1]
68+
}
69+
70+
res, err := send(addr, methodName, params, ai.AuthHeader())
71+
if err != nil {
72+
return err
73+
}
74+
75+
return printOneString(re, res)
76+
},
77+
}
78+
79+
func send(addr, method, params string, headers http.Header) (string, error) {
80+
jreq, err := json.Marshal(struct {
81+
Jsonrpc string `json:"jsonrpc"`
82+
ID int `json:"id"`
83+
Method string `json:"method"`
84+
Params json.RawMessage `json:"params"`
85+
}{
86+
Jsonrpc: "2.0",
87+
Method: "Filecoin." + method,
88+
Params: json.RawMessage(params),
89+
ID: 0,
90+
})
91+
if err != nil {
92+
return "", err
93+
}
94+
95+
req, err := http.NewRequest("POST", addr, bytes.NewReader(jreq))
96+
if err != nil {
97+
return "", err
98+
}
99+
req.Header = headers
100+
resp, err := http.DefaultClient.Do(req)
101+
if err != nil {
102+
return "", err
103+
}
104+
105+
rb, err := io.ReadAll(resp.Body)
106+
if err != nil {
107+
return "", err
108+
}
109+
110+
if err := resp.Body.Close(); err != nil {
111+
return "", err
112+
}
113+
114+
return string(rb), nil
115+
}

pkg/repo/fsrepo.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -628,7 +628,7 @@ func apiTokenFromFile(repoPath string) (string, error) {
628628
tokenFile := filepath.Join(repoPath, apiToken)
629629
token, err := os.ReadFile(tokenFile)
630630
if err != nil {
631-
return "", errors.Wrap(err, "failed to read API file")
631+
return "", errors.Wrap(err, "failed to read token file")
632632
}
633633

634634
return strings.TrimSpace(string(token)), nil

0 commit comments

Comments
 (0)