Skip to content

Commit

Permalink
Merge pull request #3337 from filecoin-project/feat/chainwatch/lotus-…
Browse files Browse the repository at this point in the history
…tokens

polish(chainwatch): accept lotus api and token
  • Loading branch information
magik6k authored Aug 27, 2020
2 parents 86c9367 + 5c4db23 commit 5aa90b0
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 4 deletions.
5 changes: 5 additions & 0 deletions cmd/lotus-chainwatch/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,11 @@ func main() {
EnvVars: []string{"LOTUS_PATH"},
Value: "~/.lotus", // TODO: Consider XDG_DATA_HOME
},
&cli.StringFlag{
Name: "api",
EnvVars: []string{"FULLNODE_API_INFO"},
Value: "",
},
&cli.StringFlag{
Name: "db",
EnvVars: []string{"LOTUS_DB"},
Expand Down
28 changes: 24 additions & 4 deletions cmd/lotus-chainwatch/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,25 @@ package main

import (
"database/sql"
"fmt"
"net/http"
_ "net/http/pprof"
"os"
"strings"

_ "github.com/lib/pq"

lcli "github.com/filecoin-project/lotus/cli"
"github.com/filecoin-project/go-jsonrpc"
logging "github.com/ipfs/go-log/v2"
"github.com/urfave/cli/v2"
"golang.org/x/xerrors"

"github.com/filecoin-project/lotus/api"
lcli "github.com/filecoin-project/lotus/cli"
"github.com/filecoin-project/lotus/cmd/lotus-chainwatch/processor"
"github.com/filecoin-project/lotus/cmd/lotus-chainwatch/scheduler"
"github.com/filecoin-project/lotus/cmd/lotus-chainwatch/syncer"
"github.com/filecoin-project/lotus/cmd/lotus-chainwatch/util"
)

var runCmd = &cli.Command{
Expand All @@ -39,9 +44,24 @@ var runCmd = &cli.Command{
return err
}

api, closer, err := lcli.GetFullNodeAPI(cctx)
if err != nil {
return err
var api api.FullNode
var closer jsonrpc.ClientCloser
var err error
if tokenMaddr := cctx.String("api"); tokenMaddr != "" {
toks := strings.Split(tokenMaddr, ":")
if len(toks) != 2 {
return fmt.Errorf("invalid api tokens, expected <token>:<maddr>, got: %s", tokenMaddr)
}

api, closer, err = util.GetFullNodeAPIUsingCredentials(cctx.Context, toks[1], toks[0])
if err != nil {
return err
}
} else {
api, closer, err = lcli.GetFullNodeAPI(cctx)
if err != nil {
return err
}
}
defer closer()
ctx := lcli.ReqContext(cctx)
Expand Down
34 changes: 34 additions & 0 deletions cmd/lotus-chainwatch/util/api.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package util

import (
"context"
"net/http"

"github.com/filecoin-project/go-jsonrpc"
"github.com/filecoin-project/lotus/api"
"github.com/filecoin-project/lotus/api/client"
ma "github.com/multiformats/go-multiaddr"
manet "github.com/multiformats/go-multiaddr/net"
)

func GetFullNodeAPIUsingCredentials(ctx context.Context, listenAddr, token string) (api.FullNode, jsonrpc.ClientCloser, error) {
parsedAddr, err := ma.NewMultiaddr(listenAddr)
if err != nil {
return nil, nil, err
}

_, addr, err := manet.DialArgs(parsedAddr)
if err != nil {
return nil, nil, err
}

return client.NewFullNodeRPC(ctx, apiURI(addr), apiHeaders(token))
}
func apiURI(addr string) string {
return "ws://" + addr + "/rpc/v0"
}
func apiHeaders(token string) http.Header {
headers := http.Header{}
headers.Add("Authorization", "Bearer "+token)
return headers
}

0 comments on commit 5aa90b0

Please sign in to comment.