Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

resolve ips #1175

Merged
merged 2 commits into from
Dec 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion node/discover.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ func (n *Node) peerDiscoveryStreamHandler(s network.Stream) {
return
}

n.log.Info("sent peer list to remote peer", "num_peers", len(peers),
n.log.Debug("sent peer list to remote peer", "num_peers", len(peers),
"to_peer", s.Conn().RemotePeer())
}

Expand Down
92 changes: 0 additions & 92 deletions node/gossip.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,95 +25,3 @@ func subTopic(_ context.Context, ps *pubsub.PubSub, topic string) (*pubsub.Topic
}
return th, sub, nil
}

// WARNING: startTxGossip is an OUT OF DATE gossipsub transaction gossip system.
// Distinct mempool and confirmed tx index have since been added.

/* func (n *Node) startTxGossip(ctx context.Context, ps *pubsub.PubSub) error {
topicTx, subTx, err := subTxs(ctx, ps)
if err != nil {
return err
}

subCanceled := make(chan struct{})

n.wg.Add(1)
go func() {
defer func() {
<-subCanceled
topicTx.Close()
n.wg.Done()
}()
for {
select {
case <-ctx.Done():
return
case <-time.After(10 * time.Second):
}

txHash := randBytes(32)
txid := hex.EncodeToString(txHash)
n.mp.Store(types.Hash(txHash), randBytes(10))
fmt.Printf("announcing txid %x\n", txid)
err := topicTx.Publish(ctx, []byte(txid))
if err != nil {
fmt.Println("Publish:", err)
return
}
}
}()

me := n.host.ID()

go func() {
defer close(subCanceled)
defer subTx.Cancel()
for {
txMsg, err := subTx.Next(ctx)
if err != nil {
if !errors.Is(err, context.Canceled) {
n.log.Errorf("Stopping TX gossip!", "error", err)
}
return
}

if string(txMsg.From) == string(me) {
// n.log.Infoln("message from me ignored")
continue
}

txHash := types.Hash(txMsg.Data)
txid := txHash.String()
fromPeerID := txMsg.GetFrom()

have := n.mp.Get(txHash) != nil // danger conversion
n.log.Infof("received tx msg from %v (rcvd from %s), data = %x, already have = %v\n",
txMsg.GetFrom(), txMsg.ReceivedFrom, txMsg.Message.Data, have)
if have {
continue
}

// Now we use getTx with the ProtocolIDTransaction stream
n.log.Info("Fetching tx", "hash", txid)
if n.host.Network().Connectedness(fromPeerID) != network.Connected {
n.log.Infof("DELAY for fetch, gossip from non-peer")
time.Sleep(200 * time.Millisecond)
}
txRaw, err := n.getTxWithRetry(ctx, txHash, 500*time.Millisecond, 10)
if err != nil {
n.log.Errorf("unable to retrieve tx %v: %v", txid, err)
continue
}

n.mp.Store(types.Hash(txMsg.Data), txRaw) // danger conversion

// txMsg.ID
// txMsg.ReceivedFrom
// txMsg.ValidatorData
// txMsg.Message.Signature
}
}()

return nil
}
*/
7 changes: 6 additions & 1 deletion node/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -764,7 +764,12 @@ func newHost(ip string, port uint64, privKey crypto.PrivateKey, wl *peers.Whitel
return nil, err
}

sourceMultiAddr, _ := multiaddr.NewMultiaddr(fmt.Sprintf("/ip4/%s/tcp/%d", ip, port))
ip, ipv, err := peers.ResolveHost(ip)
if err != nil {
return nil, fmt.Errorf("unable to resolve %v: %w", ip, err)
}

sourceMultiAddr, _ := multiaddr.NewMultiaddr(fmt.Sprintf("/%s/%s/tcp/%d", ipv, ip, port))

// listenAddrs := libp2p.ListenAddrStrings("/ip4/0.0.0.0/tcp/0", "/ip4/0.0.0.0/tcp/0/ws")

Expand Down
29 changes: 28 additions & 1 deletion node/peers/convert.go
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,12 @@ func ConvertPeersToMultiAddr(peers []string) ([]string, error) {
return nil, err
}

maStr := fmt.Sprintf("/ip4/%s/tcp/%d/p2p/%s", host, port, peerID)
ip, ipv, err := ResolveHost(host)
if err != nil {
return nil, fmt.Errorf("unable to resolve %v: %w", ip, err)
}

maStr := fmt.Sprintf("/%s/%s/tcp/%d/p2p/%s", ipv, ip, port, peerID)
// ensure the multiaddress string is parsable
_, err = multiaddr.NewMultiaddr(maStr)
if err != nil {
Expand All @@ -191,6 +196,28 @@ func ConvertPeersToMultiAddr(peers []string) ([]string, error) {
return addrs, nil
}

func ResolveHost(addr string) (ip, ipv string, err error) {
// fast path with no lookup
// if netIP, err := netip.ParseAddr(addr); err != nil {
// ip = netIP.String()
// if netIP.Is4() {
// return ip, "ip4", nil
// }
// return ip, "ip4", nil
// }

ipAddr, err := net.ResolveIPAddr("ip", addr)
if err != nil {
return
}
ip = ipAddr.IP.String()
ipv = "ip4"
if ipAddr.IP.To4() == nil {
ipv = "ip6"
}
return
}

func CompressDialError(err error) error {
var dErr *swarm.DialError
if errors.Is(err, swarm.ErrAllDialsFailed) && errors.As(err, &dErr) {
Expand Down
Loading