style: hello.go
This commit is contained in:
parent
0fd74eda69
commit
d04594a375
@ -150,7 +150,7 @@ var NetPing = &cli.Command{
|
|||||||
|
|
||||||
ctx := ReqContext(cctx)
|
ctx := ReqContext(cctx)
|
||||||
|
|
||||||
pis, err := addrInfoFromArg(ctx, cctx)
|
pis, err := AddrInfoFromArg(ctx, cctx)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
@ -271,7 +271,7 @@ var NetConnect = &cli.Command{
|
|||||||
defer closer()
|
defer closer()
|
||||||
ctx := ReqContext(cctx)
|
ctx := ReqContext(cctx)
|
||||||
|
|
||||||
pis, err := addrInfoFromArg(ctx, cctx)
|
pis, err := AddrInfoFromArg(ctx, cctx)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
@ -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())
|
pis, err := addrutil.ParseAddresses(ctx, cctx.Args().Slice())
|
||||||
if err != nil {
|
if err != nil {
|
||||||
a, perr := address.NewFromString(cctx.Args().First())
|
a, perr := address.NewFromString(cctx.Args().First())
|
||||||
|
@ -7,26 +7,23 @@ import (
|
|||||||
"os"
|
"os"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/filecoin-project/go-address"
|
|
||||||
cborutil "github.com/filecoin-project/go-cbor-util"
|
cborutil "github.com/filecoin-project/go-cbor-util"
|
||||||
"github.com/filecoin-project/lotus/blockstore"
|
"github.com/filecoin-project/lotus/blockstore"
|
||||||
"github.com/filecoin-project/lotus/chain/consensus/filcns"
|
"github.com/filecoin-project/lotus/chain/consensus/filcns"
|
||||||
"github.com/filecoin-project/lotus/chain/store"
|
"github.com/filecoin-project/lotus/chain/store"
|
||||||
"github.com/filecoin-project/lotus/chain/types"
|
|
||||||
lcli "github.com/filecoin-project/lotus/cli"
|
lcli "github.com/filecoin-project/lotus/cli"
|
||||||
"github.com/filecoin-project/lotus/lib/addrutil"
|
|
||||||
"github.com/filecoin-project/lotus/node/hello"
|
"github.com/filecoin-project/lotus/node/hello"
|
||||||
"github.com/ipfs/go-datastore"
|
"github.com/ipfs/go-datastore"
|
||||||
"github.com/libp2p/go-libp2p"
|
"github.com/libp2p/go-libp2p"
|
||||||
"github.com/libp2p/go-libp2p-core/crypto"
|
|
||||||
"github.com/libp2p/go-libp2p-core/host"
|
"github.com/libp2p/go-libp2p-core/host"
|
||||||
inet "github.com/libp2p/go-libp2p-core/network"
|
inet "github.com/libp2p/go-libp2p-core/network"
|
||||||
"github.com/libp2p/go-libp2p-core/peer"
|
"github.com/libp2p/go-libp2p-core/peer"
|
||||||
"github.com/multiformats/go-multiaddr"
|
|
||||||
"github.com/urfave/cli/v2"
|
"github.com/urfave/cli/v2"
|
||||||
"golang.org/x/xerrors"
|
"golang.org/x/xerrors"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
var resultCh chan bool
|
||||||
|
|
||||||
var helloCmd = &cli.Command{
|
var helloCmd = &cli.Command{
|
||||||
Name: "hello",
|
Name: "hello",
|
||||||
Description: "Get remote peer hello message by multiaddr",
|
Description: "Get remote peer hello message by multiaddr",
|
||||||
@ -44,6 +41,7 @@ var helloCmd = &cli.Command{
|
|||||||
|
|
||||||
Action: func(cctx *cli.Context) error {
|
Action: func(cctx *cli.Context) error {
|
||||||
ctx := lcli.ReqContext(cctx)
|
ctx := lcli.ReqContext(cctx)
|
||||||
|
resultCh = make(chan bool, 1)
|
||||||
cf := cctx.String("genesis-path")
|
cf := cctx.String("genesis-path")
|
||||||
f, err := os.OpenFile(cf, os.O_RDONLY, 0664)
|
f, err := os.OpenFile(cf, os.O_RDONLY, 0664)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@ -55,7 +53,7 @@ var helloCmd = &cli.Command{
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
pis, err := addrInfoFromArg(ctx, cctx)
|
pis, err := lcli.AddrInfoFromArg(ctx, cctx)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
@ -83,56 +81,17 @@ var helloCmd = &cli.Command{
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
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
|
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) {
|
func HandleStream(s inet.Stream) {
|
||||||
var hmsg hello.HelloMessage
|
var hmsg hello.HelloMessage
|
||||||
if err := cborutil.ReadCborRPC(s, &hmsg); err != nil {
|
if err := cborutil.ReadCborRPC(s, &hmsg); err != nil {
|
||||||
@ -145,6 +104,7 @@ func HandleStream(s inet.Stream) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
fmt.Println(string(data))
|
fmt.Println(string(data))
|
||||||
|
resultCh <- true
|
||||||
}
|
}
|
||||||
|
|
||||||
func SayHello(ctx context.Context, pid peer.ID, h host.Host, hmsg *hello.HelloMessage) error {
|
func SayHello(ctx context.Context, pid peer.ID, h host.Host, hmsg *hello.HelloMessage) error {
|
||||||
|
Loading…
Reference in New Issue
Block a user