Skip to content

Commit

Permalink
style: hello.go
Browse files Browse the repository at this point in the history
  • Loading branch information
swift-mx committed Jun 2, 2022
1 parent 0fd74ed commit d04594a
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 55 deletions.
6 changes: 3 additions & 3 deletions cli/net.go
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,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 @@ -271,7 +271,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 @@ -290,7 +290,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
64 changes: 12 additions & 52 deletions cmd/lotus-shed/hello.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,26 +7,23 @@ import (
"os"
"time"

"github.com/filecoin-project/go-address"
cborutil "github.com/filecoin-project/go-cbor-util"
"github.com/filecoin-project/lotus/blockstore"
"github.com/filecoin-project/lotus/chain/consensus/filcns"
"github.com/filecoin-project/lotus/chain/store"
"github.com/filecoin-project/lotus/chain/types"
lcli "github.com/filecoin-project/lotus/cli"
"github.com/filecoin-project/lotus/lib/addrutil"
"github.com/filecoin-project/lotus/node/hello"
"github.com/ipfs/go-datastore"
"github.com/libp2p/go-libp2p"
"github.com/libp2p/go-libp2p-core/crypto"
"github.com/libp2p/go-libp2p-core/host"
inet "github.com/libp2p/go-libp2p-core/network"
"github.com/libp2p/go-libp2p-core/peer"
"github.com/multiformats/go-multiaddr"
"github.com/urfave/cli/v2"
"golang.org/x/xerrors"
)

var resultCh chan bool

var helloCmd = &cli.Command{
Name: "hello",
Description: "Get remote peer hello message by multiaddr",
Expand All @@ -44,6 +41,7 @@ var helloCmd = &cli.Command{

Action: func(cctx *cli.Context) error {
ctx := lcli.ReqContext(cctx)
resultCh = make(chan bool, 1)
cf := cctx.String("genesis-path")
f, err := os.OpenFile(cf, os.O_RDONLY, 0664)
if err != nil {
Expand All @@ -55,7 +53,7 @@ var helloCmd = &cli.Command{
if err != nil {
return err
}
pis, err := addrInfoFromArg(ctx, cctx)
pis, err := lcli.AddrInfoFromArg(ctx, cctx)
if err != nil {
return err
}
Expand Down Expand Up @@ -83,56 +81,17 @@ var helloCmd = &cli.Command{
if err != nil {
return err
}
time.Sleep(time.Second * 5)
ctx, done := context.WithTimeout(context.Background(), 10*time.Second)
defer done()
select {
case <-resultCh:
case <-ctx.Done():
fmt.Println("can't get hello message, please try again")
}
return nil
},
}

func addrInfoFromArg(ctx context.Context, cctx *cli.Context) ([]peer.AddrInfo, error) {
pis, err := addrutil.ParseAddresses(ctx, []string{cctx.String("multiaddr")})
if err != nil {
a, perr := address.NewFromString(cctx.String("multiaddr"))
if perr != nil {
return nil, err
}

na, fc, err := lcli.GetFullNodeAPI(cctx)
if err != nil {
return nil, err
}
defer fc()

mi, err := na.StateMinerInfo(ctx, a, types.EmptyTSK)
if err != nil {
return nil, xerrors.Errorf("getting miner info: %w", err)
}

if mi.PeerId == nil {
return nil, xerrors.Errorf("no PeerID for miner")
}
multiaddrs := make([]multiaddr.Multiaddr, 0, len(mi.Multiaddrs))
for i, a := range mi.Multiaddrs {
maddr, err := multiaddr.NewMultiaddrBytes(a)
if err != nil {
log.Warnf("parsing multiaddr %d (%x): %s", i, a, err)
continue
}
multiaddrs = append(multiaddrs, maddr)
}

pi := peer.AddrInfo{
ID: *mi.PeerId,
Addrs: multiaddrs,
}

fmt.Printf("%s -> %s\n", a, pi)

pis = append(pis, pi)
}

return pis, err
}

func HandleStream(s inet.Stream) {
var hmsg hello.HelloMessage
if err := cborutil.ReadCborRPC(s, &hmsg); err != nil {
Expand All @@ -145,6 +104,7 @@ func HandleStream(s inet.Stream) {
return
}
fmt.Println(string(data))
resultCh <- true
}

func SayHello(ctx context.Context, pid peer.ID, h host.Host, hmsg *hello.HelloMessage) error {
Expand Down

0 comments on commit d04594a

Please sign in to comment.