Skip to content

Commit

Permalink
Merge pull request #8787 from swift-mx/mx_get_hello_message
Browse files Browse the repository at this point in the history
feat: lotus-shed get remote peer hello message
  • Loading branch information
magik6k authored Jun 2, 2022
2 parents db13ada + 1fd2be1 commit 73ee595
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 3 deletions.
6 changes: 3 additions & 3 deletions cli/net.go
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ var NetPing = &cli.Command{

ctx := ReqContext(cctx)

pis, err := addrInfoFromArg(ctx, cctx)
pis, err := AddrInfoFromArg(ctx, cctx)
if err != nil {
return err
}
Expand Down Expand Up @@ -274,7 +274,7 @@ var NetConnect = &cli.Command{
defer closer()
ctx := ReqContext(cctx)

pis, err := addrInfoFromArg(ctx, cctx)
pis, err := AddrInfoFromArg(ctx, cctx)
if err != nil {
return err
}
Expand All @@ -293,7 +293,7 @@ var NetConnect = &cli.Command{
},
}

func addrInfoFromArg(ctx context.Context, cctx *cli.Context) ([]peer.AddrInfo, error) {
func AddrInfoFromArg(ctx context.Context, cctx *cli.Context) ([]peer.AddrInfo, error) {
pis, err := addrutil.ParseAddresses(ctx, cctx.Args().Slice())
if err != nil {
a, perr := address.NewFromString(cctx.Args().First())
Expand Down
63 changes: 63 additions & 0 deletions cmd/lotus-shed/hello.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
package main

import (
"context"
"encoding/json"
"fmt"
"time"

cborutil "github.com/filecoin-project/go-cbor-util"
lcli "github.com/filecoin-project/lotus/cli"
"github.com/filecoin-project/lotus/node/hello"
"github.com/libp2p/go-libp2p"
inet "github.com/libp2p/go-libp2p-core/network"
"github.com/urfave/cli/v2"
)

var resultCh chan bool

var helloCmd = &cli.Command{
Name: "hello",
Description: "Get remote peer hello message by multiaddr",
ArgsUsage: "[peerMultiaddr|minerActorAddress]",
Action: func(cctx *cli.Context) error {
ctx := lcli.ReqContext(cctx)
resultCh = make(chan bool, 1)
pis, err := lcli.AddrInfoFromArg(ctx, cctx)
if err != nil {
return err
}
h, err := libp2p.New()
if err != nil {
return err
}
h.SetStreamHandler(hello.ProtocolID, HandleStream)
err = h.Connect(ctx, pis[0])
if err != nil {
return err
}
ctx, done := context.WithTimeout(ctx, 5*time.Second)
defer done()
select {
case <-resultCh:
case <-ctx.Done():
fmt.Println("can't get hello message, please try again")
}
return nil
},
}

func HandleStream(s inet.Stream) {
var hmsg hello.HelloMessage
if err := cborutil.ReadCborRPC(s, &hmsg); err != nil {
log.Infow("failed to read hello message, disconnecting", "error", err)
_ = s.Conn().Close()
return
}
data, err := json.Marshal(hmsg)
if err != nil {
return
}
fmt.Println(string(data))
resultCh <- true
}
1 change: 1 addition & 0 deletions cmd/lotus-shed/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ func main() {
marketCmd,
miscCmd,
mpoolCmd,
helloCmd,
genesisVerifyCmd,
mathCmd,
minerCmd,
Expand Down

0 comments on commit 73ee595

Please sign in to comment.