11 "github.com/bytom/api"
12 "github.com/bytom/blockchain"
13 "github.com/bytom/blockchain/rpc"
14 "github.com/bytom/env"
15 jww "github.com/spf13/jwalterweatherman"
19 // Success indicates the rpc calling is successful.
21 // ErrLocalExe indicates error occurs before the rpc calling.
23 // ErrConnect indicates error occurs connecting to the bytomd, e.g.,
24 // bytomd can't parse the received arguments.
26 // ErrLocalParse indicates error occurs locally when parsing the response.
28 // ErrRemote indicates error occurs in bytomd.
33 home = blockchain.HomeDirFromEnvironment()
34 coreURL = env.String("BYTOM_URL", "http://localhost:9888")
37 // Wraper rpc's client
38 func MustRPCClient() *rpc.Client {
39 // TODO(kr): refactor some of this cert-loading logic into bytom/blockchain
40 // and use it from cored as well.
41 // Note that this function, unlike maybeUseTLS in cored,
42 // does not load the cert and key from env vars,
43 // only from the filesystem.
44 certFile := filepath.Join(home, "tls.crt")
45 keyFile := filepath.Join(home, "tls.key")
46 config, err := blockchain.TLSConfig(certFile, keyFile, "")
47 if err == blockchain.ErrNoTLS {
48 return &rpc.Client{BaseURL: *coreURL}
49 } else if err != nil {
50 jww.ERROR.Println("loading TLS cert:", err)
54 DialContext: (&net.Dialer{
55 Timeout: 30 * time.Second,
56 KeepAlive: 30 * time.Second,
60 IdleConnTimeout: 90 * time.Second,
61 TLSClientConfig: config,
62 TLSHandshakeTimeout: 10 * time.Second,
63 ExpectContinueTimeout: 1 * time.Second,
67 if strings.HasPrefix(url, "http:") {
68 url = "https:" + url[5:]
73 Client: &http.Client{Transport: t},
77 // Wrapper rpc call api.
78 func ClientCall(path string, req ...interface{}) (interface{}, int) {
80 var response = &api.Response{}
81 var request interface{}
87 client := MustRPCClient()
88 client.Call(context.Background(), path, request, response)
90 switch response.Status {
92 jww.ERROR.Println(response.Msg)
95 jww.ERROR.Println("Unable to connect to the bytomd")
96 return nil, ErrConnect
99 return response.Data, Success